Merge branch 'master' into view-refactor
[redakcja.git] / project / static / js / views / xml.js
1 /*global View CodeMirror render_template panels */
2 var XMLView = View.extend({
3   _className: 'XMLView',
4   element: null,
5   model: null,
6   template: 'xml-view-template',
7   editor: null,
8   buttonToolbar: null,
9   
10   init: function(element, model, parent, template) {
11     this._super(element, model, template);
12     this.parent = parent;
13     
14     this.parent.freeze('Ɓadowanie edytora...');
15         this.editor = new CodeMirror($('.xmlview', this.element).get(0), {
16       parserfile: 'parsexml.js',
17       path: "/static/js/lib/codemirror/",
18       stylesheet: "/static/css/xmlcolors.css",
19       parserConfig: {useHTMLKludges: false},
20       textWrapping: false,
21       tabMode: 'spaces',
22       indentUnit: 0,
23       onChange: this.editorDataChanged.bind(this),
24       initCallback: this.editorDidLoad.bind(this)
25     });
26   },
27   
28   editorDidLoad: function(editor) {
29     $(editor.frame).css({width: '100%', height: '100%'});
30     
31     this.model
32       .addObserver(this, 'data', this.modelDataChanged.bind(this))
33       .addObserver(this, 'synced', this.modelSyncChanged.bind(this));
34     
35     this.parent.unfreeze();
36       
37     this.editor.setCode(this.model.get('data'));
38     if (!this.model.get('synced')) {
39       this.parent.freeze('Niezsynchronizowany...');
40       this.model.load();
41     }
42     
43     // editor.grabKeys(
44     //   $.fbind(self, self.hotkeyPressed),
45     //   $.fbind(self, self.isHotkey)
46     // );
47   },
48   
49   editorDataChanged: function() {
50     this.model.set('data', this.editor.getCode());
51   },
52   
53   modelDataChanged: function(property, value) {
54     if (this.editor.getCode() != value) {
55       this.editor.setCode(value);
56     }
57   },
58   
59   modelSyncChanged: function(property, value) {
60     if (value) {
61       this.parent.unfreeze();
62     } else {
63       this.parent.freeze('Niezsynchronizowany...');
64     }
65   },
66     
67   dispose: function() {
68     this.model.removeObserver(this);
69     $(this.editor.frame).remove();
70     this._super();
71   }
72 });
73
74 // Register view
75 panels['xml'] = XMLView;