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...');
 
  27         this.editor = new CodeMirror($('.xmlview', this.element).get(0), {
 
  28             parserfile: 'parsexml.js',
 
  29             path: "/static/js/lib/codemirror/",
 
  30             stylesheet: "/static/css/xmlcolors.css",
 
  37             onChange: this.editorDataChanged.bind(this),
 
  38             initCallback: this.editorDidLoad.bind(this)
 
  42     resized: function(event) {
 
  43         var height = this.element.height() - $('.xmlview-toolbar', this.element).outerHeight();
 
  44         $('.xmlview', this.element).height(height);
 
  48         this.model.load(true);
 
  51     editorDidLoad: function(editor) {
 
  57         .addObserver(this, 'data', this.modelDataChanged.bind(this))
 
  58         .addObserver(this, 'state', this.modelStateChanged.bind(this))
 
  61         this.parent.unfreeze();
 
  63         this.editor.setCode(this.model.get('data'));
 
  64         this.modelStateChanged('state', this.model.get('state'));
 
  67             this.hotkeyPressed.bind(this),
 
  68             this.isHotkey.bind(this)
 
  72     editorDataChanged: function() {
 
  73         this.model.set('data', this.editor.getCode());
 
  76     modelDataChanged: function(property, value) {
 
  77         if (this.editor.getCode() != value) {
 
  78             this.editor.setCode(value);
 
  82     modelStateChanged: function(property, value) {
 
  83         if (value == 'synced' || value == 'dirty') {
 
  85         } else if (value == 'unsynced') {
 
  86             this.freeze('Niezsynchronizowany...');
 
  87         } else if (value == 'loading') {
 
  88             this.freeze('Ładowanie...');
 
  89         } else if (value == 'saving') {
 
  90             this.freeze('Zapisywanie...');
 
  91         } else if (value == 'error') {
 
  92             this.freeze(this.model.get('error'));
 
  97         $(document).unbind('xml-scroll-request', this.scrollCallback);
 
  99         this.model.removeObserver(this);
 
 100         $(this.editor.frame).remove();
 
 104     getHotkey: function(event) {
 
 105         var code = event.keyCode;
 
 106         if(!((code >= 97 && code <= 122)
 
 107            || (code >= 65 && code <= 90)) ) return null;
 
 109         var ch = String.fromCharCode(code & 0xff).toLowerCase();
 
 110         /* # console.log(ch.charCodeAt(0), '#', buttons); */
 
 112         var buttons = $('.buttontoolbarview-button[hotkey='+ch+']', this.element);
 
 115         if(event.altKey) mod |= 0x01;
 
 116         if(event.ctrlKey) mod |= 0x02;
 
 117         if(event.shiftKey) mod |= 0x04;
 
 122             buttons.each(function() {
 
 123                 if( parseInt($(this).attr('ui:hotkey_mod')) == mod ) {
 
 136     isHotkey: function() {
 
 137         /* console.log(arguments); */
 
 138         if(this.getHotkey.apply(this, arguments))
 
 144     hotkeyPressed: function() {
 
 145         var button = this.getHotkey.apply(this, arguments);
 
 146         this.buttonToolbar.buttonPressed({
 
 151     scrollOnRequest: function(event, data) 
 
 154             var line = this.editor.nthLine(data.line);
 
 155             this.editor.selectLines(line, (data.column-1));
 
 157             console.log('Exception in scrollOnRequest:', e);
 
 163 function Hotkey(code) {
 
 165     this.has_alt = ((code & 0x01 << 8) !== 0);
 
 166     this.has_ctrl = ((code & 0x01 << 9) !== 0);
 
 167     this.has_shift = ((code & 0x01 << 10) !== 0);
 
 168     this.character = String.fromCharCode(code & 0xff);
 
 171 Hotkey.prototype.toString = function() {
 
 173     if(this.has_alt) mods.push('Alt');
 
 174     if(this.has_ctrl) mods.push('Ctrl');
 
 175     if(this.has_shift) mods.push('Shift');
 
 176     mods.push('"'+this.character+'"');
 
 177     return mods.join('+');
 
 181 panels['xml'] = XMLView;