1 /*global View CodeMirror ToolbarView 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));
 
  22         // scroll to the given position (if availble)
 
  23         this.scrollCallback = this.scrollOnRequest.bind(this);
 
  24         $(document).bind('xml-scroll-request', this.scrollCallback);
 
  26         this.parent.freeze('Ładowanie edytora...');
 
  28         setTimeout((function(){
 
  30         this.editor = new CodeMirror($('.xmlview', this.element).get(0), {
 
  31             parserfile: 'parsexml.js',
 
  32             path: documentInfo.staticURL + "js/lib/codemirror/",
 
  33             stylesheet: documentInfo.staticURL + "css/xmlcolors.css",
 
  40             onChange: this.editorDataChanged.bind(this),
 
  41             initCallback: this.editorDidLoad.bind(this)
 
  47     resized: function(event) {
 
  48         var height = this.element.height() - $('.xmlview-toolbar', this.element).outerHeight();
 
  49         $('.xmlview', this.element).height(height);
 
  53         this.model.load(true);
 
  56     editorDidLoad: function(editor) {
 
  62         .addObserver(this, 'data', this.modelDataChanged.bind(this))
 
  63         .addObserver(this, 'state', this.modelStateChanged.bind(this))
 
  66         this.editor.setCode(this.model.get('data'));
 
  67         this.modelStateChanged('state', this.model.get('state'));
 
  70             this.hotkeyPressed.bind(this),
 
  71             this.isHotkey.bind(this)
 
  74         this.parent.unfreeze();
 
  77     editorDataChanged: function() {
 
  78         this.model.set('data', this.editor.getCode());
 
  81     modelDataChanged: function(property, value) {
 
  82         if (this.editor.getCode() != value) {
 
  83             this.editor.setCode(value);
 
  87     modelStateChanged: function(property, value) {
 
  88         if (value == 'synced' || value == 'dirty') {
 
  90         } else if (value == 'unsynced') {
 
  91             this.freeze('Niezsynchronizowany...');
 
  92         } else if (value == 'loading') {
 
  93             this.freeze('Ładowanie danych...');
 
  94         } else if (value == 'saving') {
 
  95             this.freeze('Zapisywanie...');
 
  96         } else if (value == 'error') {
 
  97             this.freeze(this.model.get('error'));
 
 101     dispose: function() {
 
 102         $(document).unbind('xml-scroll-request', this.scrollCallback);
 
 104         this.model.removeObserver(this);
 
 105         $(this.editor.frame).remove();
 
 109     getHotkey: function(event) {
 
 110         var code = event.keyCode;
 
 111         if(!((code >= 97 && code <= 122)
 
 112            || (code >= 65 && code <= 90)) ) return null;
 
 114         var ch = String.fromCharCode(code & 0xff).toLowerCase();
 
 115         /* # console.log(ch.charCodeAt(0), '#', buttons); */
 
 117         var buttons = $('.buttontoolbarview-button[hotkey='+ch+']', this.element);
 
 120         if(event.altKey) mod |= 0x01;
 
 121         if(event.ctrlKey) mod |= 0x02;
 
 122         if(event.shiftKey) mod |= 0x04;
 
 127             buttons.each(function() {
 
 128                 if( parseInt($(this).attr('ui:hotkey_mod')) == mod ) {
 
 141     isHotkey: function() {
 
 142         /* console.log(arguments); */
 
 143         if(this.getHotkey.apply(this, arguments))
 
 149     hotkeyPressed: function() {
 
 150         var button = this.getHotkey.apply(this, arguments);
 
 151         this.buttonToolbar.buttonPressed({
 
 156     scrollOnRequest: function(event, data) 
 
 159             var line = this.editor.nthLine(data.line);
 
 160             this.editor.selectLines(line, (data.column-1));
 
 162             console.log('Exception in scrollOnRequest:', e);
 
 168 function Hotkey(code) {
 
 170     this.has_alt = ((code & 0x01 << 8) !== 0);
 
 171     this.has_ctrl = ((code & 0x01 << 9) !== 0);
 
 172     this.has_shift = ((code & 0x01 << 10) !== 0);
 
 173     this.character = String.fromCharCode(code & 0xff);
 
 176 Hotkey.prototype.toString = function() {
 
 178     if(this.has_alt) mods.push('Alt');
 
 179     if(this.has_ctrl) mods.push('Ctrl');
 
 180     if(this.has_shift) mods.push('Shift');
 
 181     mods.push('"'+this.character+'"');
 
 182     return mods.join('+');
 
 186 panels['xml'] = XMLView;