Poprawienienie implementacji obserwatorów.
[redakcja.git] / project / static / js / views / xml.js
1 /*global View CodeMirror render_template panels */
2 var XMLView = View.extend({
3   element: null,
4   model: null,
5   template: 'xml-view-template',
6   editor: null,
7   
8   init: function(element, model, template) {
9     this._super(element, model, template);
10     
11     this.freeze('Ładowanie edytora...');
12         this.editor = new CodeMirror($('.xmlview', this.element).get(0), {
13       parserfile: 'parsexml.js',
14       path: "/static/js/lib/codemirror/",
15       stylesheet: "/static/css/xmlcolors.css",
16       parserConfig: {useHTMLKludges: false},
17       textWrapping: false,
18       tabMode: 'spaces',
19       indentUnit: 0,
20       onChange: this.changed.bind(this),
21       initCallback: this.editorDidLoad.bind(this)
22     });
23   },
24   
25   changed: function() {
26     this.model.setData(this.editor.getCode());
27   },
28   
29   editorDidLoad: function(editor) {
30     editor.setCode('Ładowanie edytora...');
31     $(editor.frame).css({width: '100%', height: '100%'});
32     this.editor.setCode(this.model.getData());
33     this.unfreeze();
34     this.model
35       .addObserver(this, 'reloaded', function() {
36         this.editor.setCode(this.model.getData()); this.unfreeze(); }.bind(this))
37       .addObserver(this, 'needsReload', function() { 
38         this.freeze('Niezsynchronizowany'); }.bind(this))
39       .addObserver(this, 'dataChanged', this.textDidChange.bind(this));
40
41     // editor.grabKeys(
42     //   $.fbind(self, self.hotkeyPressed),
43     //   $.fbind(self, self.isHotkey)
44     // );
45   },
46   
47   textDidChange: function(event) {
48     console.log('textDidChange!');
49     if (this.editor.getCode() != this.model.getData()) {
50       this.editor.setCode(this.model.getData());
51     }
52   },
53   
54   dispose: function() {
55     this.model.removeObserver(this);
56     $(this.editor.frame).remove();
57     this._super();
58   }
59 });
60
61 // Register view
62 panels['xml'] = XMLView;