1 /*global View CodeMirror ButtonToolbarView render_template panels */
2 var XMLView = View.extend({
6 template: 'xml-view-template',
10 init: function(element, model, parent, template) {
11 this._super(element, model, template);
13 this.buttonToolbar = new ButtonToolbarView(
14 $('.xmlview-toolbar', this.element),
15 this.model.toolbarButtonsModel, parent);
20 $('.xmlview-toolbar', this.element).bind('resize.xmlview', this.resized.bind(this));
23 this.parent.freeze('Ładowanie edytora...');
24 this.editor = new CodeMirror($('.xmlview', this.element).get(0), {
25 parserfile: 'parsexml.js',
26 path: "/static/js/lib/codemirror/",
27 stylesheet: "/static/css/xmlcolors.css",
34 onChange: this.editorDataChanged.bind(this),
35 initCallback: this.editorDidLoad.bind(this)
39 resized: function(event) {
40 var height = this.element.height() - $('.xmlview-toolbar', this.element).outerHeight();
41 $('.xmlview', this.element).height(height);
45 this.model.load(true);
48 editorDidLoad: function(editor) {
54 .addObserver(this, 'data', this.modelDataChanged.bind(this))
55 .addObserver(this, 'state', this.modelStateChanged.bind(this))
58 this.parent.unfreeze();
60 this.editor.setCode(this.model.get('data'));
61 this.modelStateChanged('state', this.model.get('state'));
64 this.hotkeyPressed.bind(this),
65 this.isHotkey.bind(this)
69 editorDataChanged: function() {
70 this.model.set('data', this.editor.getCode());
73 modelDataChanged: function(property, value) {
74 if (this.editor.getCode() != value) {
75 this.editor.setCode(value);
79 modelStateChanged: function(property, value) {
80 if (value == 'synced' || value == 'dirty') {
82 } else if (value == 'unsynced') {
83 this.freeze('Niezsynchronizowany...');
84 } else if (value == 'loading') {
85 this.freeze('Ładowanie...');
86 } else if (value == 'saving') {
87 this.freeze('Zapisywanie...');
88 } else if (value == 'error') {
89 this.freeze(this.model.get('error'));
94 this.model.removeObserver(this);
95 $(this.editor.frame).remove();
99 getHotkey: function(event) {
100 var code = event.keyCode;
101 if(!((code >= 97 && code <= 122)
102 || (code >= 65 && code <= 90)) ) return null;
104 var ch = String.fromCharCode(code & 0xff).toLowerCase();
105 /* # console.log(ch.charCodeAt(0), '#', buttons); */
107 var buttons = $('.buttontoolbarview-button[title='+ch+']', this.element);
110 if(event.altKey) mod |= 0x01;
111 if(event.ctrlKey) mod |= 0x02;
112 if(event.shiftKey) mod |= 0x04;
117 buttons.each(function() {
118 if( parseInt($(this).attr('ui:hotkey_mod')) == mod ) {
131 isHotkey: function() {
132 /* console.log(arguments); */
133 if(this.getHotkey.apply(this, arguments))
139 hotkeyPressed: function() {
140 var button = this.getHotkey.apply(this, arguments);
141 this.buttonToolbar.buttonPressed({
148 function Hotkey(code) {
150 this.has_alt = ((code & 0x01 << 8) !== 0);
151 this.has_ctrl = ((code & 0x01 << 9) !== 0);
152 this.has_shift = ((code & 0x01 << 10) !== 0);
153 this.character = String.fromCharCode(code & 0xff);
156 Hotkey.prototype.toString = function() {
158 if(this.has_alt) mods.push('Alt');
159 if(this.has_ctrl) mods.push('Ctrl');
160 if(this.has_shift) mods.push('Shift');
161 mods.push('"'+this.character+'"');
162 return mods.join('+');
166 panels['xml'] = XMLView;