Fix #102: Nie można zmienić domyślnych paneli.
[redakcja.git] / project / static / js / editor.js
index 3490b9a..1157437 100644 (file)
@@ -1,3 +1,21 @@
+function Hotkey(code) {
+    this.code = code
+    this.has_alt = ((code & 0x01 << 8) != 0)
+    this.has_ctrl = ((code & 0x01 << 9) != 0)
+    this.has_shift = ((code & 0x01 << 10) != 0)
+    this.character = String.fromCharCode(code & 0xff)
+}
+
+
+Hotkey.prototype.toString = function() {
+    mods = []
+    if(this.has_alt) mods.push('Alt')
+    if(this.has_ctrl) mods.push('Ctrl')
+    if(this.has_shift) mods.push('Shift')
+    mods.push('"'+this.character+'"')
+    return mods.join('+')
+}
+
 function Panel(panelWrap) {
     var self = this;
     self.hotkeys = [];
@@ -67,6 +85,10 @@ Panel.prototype.unload = function(event, data) {
     if( data == this ) {
         $.log('unloading', this);
         $(this.contentDiv).html('');
+
+        // disconnect the toolbar
+        $('div.panel-toolbar span.panel-toolbar-extra', this.wrap).empty();
+        
         this.callHook('unload');
         this.hooks = null; // flush the hooks
         return false;
@@ -119,15 +141,22 @@ Panel.prototype.connectToolbar = function()
     $.log('Connecting toolbar', toolbar);
     if(toolbar.length == 0) return;
 
+    // move the extra
+    var extra_buttons = $('span.panel-toolbar-extra', toolbar);
+    var placeholder = $('div.panel-toolbar span.panel-toolbar-extra', this.wrap);
+    placeholder.replaceWith(extra_buttons);
+
+    var action_buttons = $('button', extra_buttons);
+
     // connect group-switch buttons
     var group_buttons = $('*.toolbar-tabs-container button', toolbar);
 
-    $.log('Found groups:', group_buttons);
+    // $.log('Found groups:', group_buttons);
 
     group_buttons.each(function() {
         var group = $(this);
         var group_name = group.attr('ui:group');
-        $.log('Connecting group: ' + group_name);
+        // $.log('Connecting group: ' + group_name);
 
         group.click(function() {
             // change the active group
@@ -147,10 +176,14 @@ Panel.prototype.connectToolbar = function()
     });
 
     // connect action buttons
-    var action_buttons = $('*.toolbar-button-groups-container button', toolbar);
-    action_buttons.each(function() {
+    var allbuttons = $.makeArray(action_buttons)
+    $.merge(allbuttons,
+        $.makeArray($('*.toolbar-button-groups-container button', toolbar)) );
+        
+    $(allbuttons).each(function() {
         var button = $(this);
         var hk = button.attr('ui:hotkey');
+        if(hk) hk = new Hotkey( parseInt(hk) );
 
         try {
             var params = $.evalJSON(button.attr('ui:action-params'));
@@ -166,15 +199,18 @@ Panel.prototype.connectToolbar = function()
 
         // connect button
         button.click(callback);
-        
+       
         // connect hotkey
-        if(hk) self.hotkeys[parseInt(hk)] = callback;
-
+        if(hk) {
+            self.hotkeys[hk.code] = callback;
+             $.log('hotkey', hk);
+        }
+        
         // tooltip
         if (button.attr('ui:tooltip') )
         {
             var tooltip = button.attr('ui:tooltip');
-            if(hk) tooltip += ' [Alt+'+hk+']';
+            if(hk) tooltip += ' ['+hk+']';
 
             button.wTooltip({
                 delay: 1000,
@@ -193,13 +229,24 @@ Panel.prototype.connectToolbar = function()
 
 Panel.prototype.hotkeyPressed = function(event)
 {
-    var callback = this.hotkeys[event.keyCode];
+    code = event.keyCode;
+    if(event.altKey) code = code | 0x100;
+    if(event.ctrlKey) code = code | 0x200;
+    if(event.shiftKey) code = code | 0x400;
+
+    var callback = this.hotkeys[code];
     if(callback) callback();
 }
 
 Panel.prototype.isHotkey = function(event) {
-    if( event.altKey && (this.hotkeys[event.keyCode] != null) )
+    code = event.keyCode;
+    if(event.altKey) code = code | 0x100;
+    if(event.ctrlKey) code = code | 0x200;
+    if(event.shiftKey) code = code | 0x400;
+
+    if(this.hotkeys[code] != null)
         return true;
+        
     return false;
 }
 
@@ -221,7 +268,7 @@ Editor.prototype.setupUI = function() {
     var self = this;
    
     self.rootDiv.makeHorizPanel({}); // TODO: this probably doesn't belong into jQuery
-    // self.rootDiv.css('top', ($('#header').outerHeight() ) + 'px');
+    // self.rootcDiv.css('top', ($('#header').outerHeight() ) + 'px');
     
     $('#panels > *.panel-wrap').each(function() {
         var panelWrap = $(this);
@@ -232,8 +279,8 @@ Editor.prototype.setupUI = function() {
         
         $('.panel-toolbar select', panelWrap).change(function() {
             var url = $(this).val();
-            panelWrap.data('ctrl').load(url);
             self.savePanelOptions();
+            panelWrap.data('ctrl').load(url);
         });
 
         $('.panel-toolbar button.refresh-button', panelWrap).click(
@@ -320,9 +367,11 @@ Editor.prototype.loadPanelOptions = function() {
             totalWidth += panelWidth;               
         }
         $.log('panel:', this, $(this).css('left'));
-        $('.panel-toolbar select', this).val(
-            $('.panel-toolbar option[name=' + self.options.panels[index].name + ']', this).attr('value')
-            )
+        $('.panel-toolbar option', this).each(function() {
+            if ($(this).attr('p:panel-name') == self.options.panels[index].name) {
+                $(this).parent('select').val($(this).attr('value'));
+            }
+        });
     });   
 }
 
@@ -331,7 +380,7 @@ Editor.prototype.savePanelOptions = function() {
     var panels = [];
     $('.panel-wrap', self.rootDiv).not('.panel-content-overlay').each(function() {
         panels.push({
-            name: $('.panel-toolbar option:selected', this).attr('name'),
+            name: $('.panel-toolbar option:selected', this).attr('p:panel-name'),
             ratio: $(this).width() / self.rootDiv.width()
         })
     });
@@ -522,7 +571,7 @@ Editor.prototype.showPopup = function(name, text, timeout)
 
     var box = $('#message-box > #' + name);
     $('*.data', box).html(text || '');
-    box.fadeIn();
+    box.fadeIn(100);
  
     if(timeout > 0)
         setTimeout( $.fbind(self, self.advancePopupQueue), timeout);
@@ -534,14 +583,14 @@ Editor.prototype.advancePopupQueue = function() {
     if(elem) {
         var box = $('#message-box > #' + elem[0]);
 
-        box.fadeOut(200, function()
+        box.fadeOut(100, function()
         {
-            $('*.data', box).html();
+            $('*.data', box).html('');
 
             if( self.popupQueue.length > 0) {
                 var ibox = $('#message-box > #' + self.popupQueue[0][0]);
-                $('*.data', ibox).html(self.popupQueue[0][1]);
-                ibox.fadeIn();
+                $('*.data', ibox).html(self.popupQueue[0][1] || '');
+                ibox.fadeIn(100);
                 if(self.popupQueue[0][2] > 0)
                     setTimeout( $.fbind(self, self.advancePopupQueue), self.popupQueue[0][2]);
             }