Uproszczenie implementacji wzorca observer.
[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.element = $(element);
10     this.model = $(model).get(0);
11     this.template = template || this.template;
12     this.element.html(render_template(this.template, {}));
13     
14     this.model
15       .addObserver(this, 'xml-freeze')
16       .addObserver(this, 'xml-unfreeze');
17     
18     this.freeze('Ładowanie edytora...');
19         this.editor = new CodeMirror($('.xmlview', this.element).get(0), {
20       parserfile: 'parsexml.js',
21       path: "/static/js/lib/codemirror/",
22       stylesheet: "/static/css/xmlcolors.css",
23       parserConfig: {useHTMLKludges: false},
24       textWrapping: false,
25       tabMode: 'spaces',
26       indentUnit: 0,
27       onChange: this.changed.bind(this),
28       initCallback: this.editorDidLoad.bind(this)
29     });
30   },
31   
32   changed: function() {
33     this.model.set('text', this.editor.getCode());
34   },
35   
36   editorDidLoad: function(editor) {
37     editor.setCode('Ładowanie edytora...');
38     $(editor.frame).css({width: '100%', height: '100%'});
39     this.editor.setCode(this.model.get('text'));
40     this.model.addObserver(this, 'text-changed');
41     this.unfreeze();
42     // editor.grabKeys(
43     //   $.fbind(self, self.hotkeyPressed),
44     //   $.fbind(self, self.isHotkey)
45     // );
46   },
47   
48   handle: function(event, data) {
49     console.log('handle', this, event, data);
50     if (event == 'text-changed') {
51       this.freeze('Niezsynchronizowany');
52       // this.unfreeze();      
53     } else if (event == 'xml-freeze') {
54       this.freeze('Ładowanie XMLa...');
55     } else if (event == 'xml-unfreeze') {
56       this.editor.setCode(this.model.get('text'));
57       this.unfreeze();
58     }
59   },
60   
61   dispose: function() {
62     this.model.removeObserver(this);
63     $(this.editor.frame).remove();
64     this._super();
65   }
66 });
67
68 // Register view
69 panels.push({name: 'xml', klass: XMLView});