Dużo poprawek :-)
[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     if (!this.model.get('synced')) {
38       this.parent.freeze('Niezsynchronizowany...');
39       this.model.load();
40     } else {
41       this.editor.setCode(this.model.get('data'));
42     }
43   
44     // editor.grabKeys(
45     //   $.fbind(self, self.hotkeyPressed),
46     //   $.fbind(self, self.isHotkey)
47     // );
48   },
49   
50   editorDataChanged: function() {
51     this.model.set('data', this.editor.getCode());
52   },
53   
54   modelDataChanged: function(property, value) {
55     if (this.editor.getCode() != value) {
56       this.editor.setCode(value);
57     }
58   },
59   
60   modelSyncChanged: function(property, value) {
61     if (value) {
62       this.parent.unfreeze();
63     } else {
64       this.parent.freeze('Niezsynchronizowany...');
65     }
66   },
67     
68   dispose: function() {
69     this.model.removeObserver(this);
70     $(this.editor.frame).remove();
71     this._super();
72   }
73 });
74
75 // Register view
76 panels['xml'] = XMLView;