1 /*global View CodeMirror ToolbarView render_template panels */
2 var XMLView = View.extend({
6 template: 'xml-view-template',
10 init: function(element, model, parent, template) {
11 var submodel = model.contentModels['xml'];
12 this._super(element, submodel, template);
16 this.buttonToolbar = new ButtonToolbarView(
17 $('.xmlview-toolbar', this.element),
18 this.model.toolbarButtonsModel, parent);
23 $('.xmlview-toolbar', this.element).bind('resize.xmlview', this.resized.bind(this));
25 // scroll to the given position (if availble)
26 this.scrollCallback = this.scrollOnRequest.bind(this);
27 $(document).bind('xml-scroll-request', this.scrollCallback);
29 this.parent.freeze('Ładowanie edytora...');
31 setTimeout((function(){
33 this.editor = new CodeMirror($('.xmlview', this.element).get(0), {
34 parserfile: 'parsexml.js',
35 path: documentInfo.staticURL + "js/lib/codemirror/",
36 stylesheet: documentInfo.staticURL + "css/xmlcolors.css",
43 onChange: this.editorDataChanged.bind(this),
44 initCallback: this.editorDidLoad.bind(this)
50 resized: function(event) {
51 var height = this.element.height() - $('.xmlview-toolbar', this.element).outerHeight();
52 $('.xmlview', this.element).height(height);
56 this.model.load(true);
59 editorDidLoad: function(editor) {
65 .addObserver(this, 'data', this.modelDataChanged.bind(this))
66 .addObserver(this, 'state', this.modelStateChanged.bind(this))
69 this.editor.setCode(this.model.get('data'));
70 this.modelStateChanged('state', this.model.get('state'));
73 this.hotkeyPressed.bind(this),
74 this.isHotkey.bind(this)
77 this.parent.unfreeze();
80 editorDataChanged: function() {
81 this.model.set('data', this.editor.getCode());
84 modelDataChanged: function(property, value) {
85 if (this.editor.getCode() != value) {
86 this.editor.setCode(value);
90 modelStateChanged: function(property, value) {
91 if (value == 'synced' || value == 'dirty') {
93 } else if (value == 'unsynced') {
94 this.freeze('Niezsynchronizowany...');
95 } else if (value == 'loading') {
96 this.freeze('Ładowanie danych...');
97 } else if (value == 'saving') {
98 this.freeze('Zapisywanie...');
99 } else if (value == 'error') {
100 this.freeze(this.model.get('error'));
104 dispose: function() {
105 $(document).unbind('xml-scroll-request', this.scrollCallback);
107 this.model.removeObserver(this);
108 $(this.editor.frame).remove();
112 getHotkey: function(event) {
113 var code = event.keyCode;
114 if(!((code >= 97 && code <= 122)
115 || (code >= 65 && code <= 90)) ) return null;
117 var ch = String.fromCharCode(code & 0xff).toLowerCase();
118 /* # console.log(ch.charCodeAt(0), '#', buttons); */
120 var buttons = $('.buttontoolbarview-button[hotkey='+ch+']', this.element);
123 if(event.altKey) mod |= 0x01;
124 if(event.ctrlKey) mod |= 0x02;
125 if(event.shiftKey) mod |= 0x04;
130 buttons.each(function() {
131 if( parseInt($(this).attr('ui:hotkey_mod')) == mod ) {
144 isHotkey: function() {
145 /* console.log(arguments); */
146 if(this.getHotkey.apply(this, arguments))
152 hotkeyPressed: function() {
153 var button = this.getHotkey.apply(this, arguments);
154 this.buttonToolbar.buttonPressed({
159 scrollOnRequest: function(event, data)
162 var line = this.editor.nthLine(data.line);
163 this.editor.selectLines(line, (data.column-1));
165 console.log('Exception in scrollOnRequest:', e);
171 function Hotkey(code) {
173 this.has_alt = ((code & 0x01 << 8) !== 0);
174 this.has_ctrl = ((code & 0x01 << 9) !== 0);
175 this.has_shift = ((code & 0x01 << 10) !== 0);
176 this.character = String.fromCharCode(code & 0xff);
179 Hotkey.prototype.toString = function() {
181 if(this.has_alt) mods.push('Alt');
182 if(this.has_ctrl) mods.push('Ctrl');
183 if(this.has_shift) mods.push('Shift');
184 mods.push('"'+this.character+'"');
185 return mods.join('+');
189 panels['xml'] = XMLView;