1 function Hotkey(code) {
3 this.has_alt = ((code & 0x01 << 8) != 0)
4 this.has_ctrl = ((code & 0x01 << 9) != 0)
5 this.has_shift = ((code & 0x01 << 10) != 0)
6 this.character = String.fromCharCode(code & 0xff)
9 Hotkey.prototype.toString = function() {
11 if(this.has_alt) mods.push('Alt')
12 if(this.has_ctrl) mods.push('Ctrl')
13 if(this.has_shift) mods.push('Shift')
14 mods.push('"'+this.character+'"')
18 function Panel(panelWrap) {
21 self.wrap = panelWrap;
22 self.contentDiv = $('.panel-content', panelWrap);
23 self.instanceId = Math.ceil(Math.random() * 1000000000);
24 // $.log('new panel - wrap: ', self.wrap);
26 $(document).bind('panel:unload.' + self.instanceId,
27 function(event, data) {
28 self.unload(event, data);
31 $(document).bind('panel:contentChanged', function(event, data) {
32 $.log(self, ' got changed event from: ', data);
34 self.otherPanelChanged(event.target);
42 Panel.prototype.callHook = function() {
43 var args = $.makeArray(arguments)
44 var hookName = args.splice(0,1)[0]
45 var noHookAction = args.splice(0,1)[0]
48 // $.log('calling hook: ', hookName, 'with args: ', args);
49 if(this.hooks && this.hooks[hookName])
50 result = this.hooks[hookName].apply(this, args);
51 else if (noHookAction instanceof Function)
52 result = noHookAction(args);
56 Panel.prototype._endload = function () {
57 // this needs to be here, so we
58 this.connectToolbar();
59 this.callHook('toolbarResized');
62 Panel.prototype.load = function (url) {
63 // $.log('preparing xhr load: ', this.wrap);
64 $(document).trigger('panel:unload', this);
66 self.current_url = url;
71 success: function(data, tstat) {
73 $(self.contentDiv).html(data);
74 self.hooks = panel_hooks;
76 self.callHook('load');
78 error: function(request, textStatus, errorThrown) {
79 $.log('ajax', url, this.target, 'error:', textStatus, errorThrown);
80 $(self.contentDiv).html("<p>Wystapił błąd podczas wczytywania panelu.</p>");
85 Panel.prototype.unload = function(event, data) {
86 // $.log('got unload signal', this, ' target: ', data);
88 $(this.contentDiv).html('');
90 // disconnect the toolbar
91 $('div.panel-toolbar span.panel-toolbar-extra', this.wrap).empty();
93 this.callHook('unload');
94 this.hooks = null; // flush the hooks
99 Panel.prototype.refresh = function(event, data) {
101 reload = function() {
102 $.log('hard reload for panel ', self.current_url);
103 self.load(self.current_url);
107 if( this.callHook('refresh', reload) )
108 $('.change-notification', this.wrap).fadeOut();
111 Panel.prototype.otherPanelChanged = function(other) {
112 $.log('Panel ', this, ' is aware that ', other, ' changed.');
113 if(!this.callHook('dirty'))
114 $('.change-notification', this.wrap).fadeIn();
117 Panel.prototype.markChanged = function () {
118 this.wrap.addClass('changed');
121 Panel.prototype.changed = function () {
122 return this.wrap.hasClass('changed');
125 Panel.prototype.unmarkChanged = function () {
126 this.wrap.removeClass('changed');
129 Panel.prototype.saveInfo = function() {
131 this.callHook('saveInfo', null, saveInfo);
135 Panel.prototype.connectToolbar = function()
140 // check if there is a one
141 var toolbar = $("div.toolbar", this.contentDiv);
142 // $.log('Connecting toolbar', toolbar);
143 if(toolbar.length == 0) return;
146 var extra_buttons = $('span.panel-toolbar-extra', toolbar);
147 var placeholder = $('div.panel-toolbar span.panel-toolbar-extra', this.wrap);
148 placeholder.replaceWith(extra_buttons);
150 var action_buttons = $('button', extra_buttons);
152 // connect group-switch buttons
153 var group_buttons = $('*.toolbar-tabs-container button', toolbar);
155 // $.log('Found groups:', group_buttons);
157 group_buttons.each(function() {
159 var group_name = group.attr('ui:group');
160 // $.log('Connecting group: ' + group_name);
162 group.click(function() {
163 // change the active group
164 var active = $("*.toolbar-tabs-container button.active", toolbar);
165 if (active != group) {
166 active.removeClass('active');
167 group.addClass('active');
168 $(".toolbar-button-groups-container p", toolbar).each(function() {
169 if ( $(this).attr('ui:group') != group_name)
174 self.callHook('toolbarResized');
179 // connect action buttons
180 var allbuttons = $.makeArray(action_buttons)
182 $.makeArray($('*.toolbar-button-groups-container button', toolbar)) );
184 $(allbuttons).each(function() {
185 var button = $(this);
186 var hk = button.attr('ui:hotkey');
187 if(hk) hk = new Hotkey( parseInt(hk) );
190 var params = $.evalJSON(button.attr('ui:action-params'));
192 $.log('JSON exception in ', button, ': ', object);
193 button.attr('disabled', 'disabled');
197 var callback = function() {
198 editor.callScriptlet(button.attr('ui:action'), self, params);
202 button.click(callback);
206 self.hotkeys[hk.code] = callback;
207 // $.log('hotkey', hk);
211 if (button.attr('ui:tooltip') )
213 var tooltip = button.attr('ui:tooltip');
214 if(hk) tooltip += ' ['+hk+']';
219 border: "1px solid #7F7D67",
221 background: "#FBFBC6",
231 Panel.prototype.hotkeyPressed = function(event)
233 code = event.keyCode;
234 if(event.altKey) code = code | 0x100;
235 if(event.ctrlKey) code = code | 0x200;
236 if(event.shiftKey) code = code | 0x400;
238 var callback = this.hotkeys[code];
239 if(callback) callback();
242 Panel.prototype.isHotkey = function(event) {
243 code = event.keyCode;
244 if(event.altKey) code = code | 0x100;
245 if(event.ctrlKey) code = code | 0x200;
246 if(event.shiftKey) code = code | 0x400;
248 if(this.hotkeys[code] != null)
255 Panel.prototype.fireEvent = function(name) {
256 $(document).trigger('panel:'+name, this);
261 this.rootDiv = $('#panels');
262 this.popupQueue = [];
263 this.autosaveTimer = null;
267 Editor.prototype.loadConfig = function() {
268 // Load options from cookie
269 var defaultOptions = {
285 var cookie = $.cookie('options');
286 this.options = $.secureEvalJSON(cookie);
288 this.options = defaultOptions;
291 this.options = defaultOptions;
295 this.loadPanelOptions();
298 Editor.prototype.loadPanelOptions = function() {
302 $('.panel-wrap', self.rootDiv).each(function(index) {
303 var panelWidth = self.options.panels[index].ratio * self.rootDiv.width();
304 if ($(this).hasClass('last-panel')) {
314 totalWidth += panelWidth;
316 $.log('panel:', this, $(this).css('left'));
317 $('.panel-toolbar option', this).each(function() {
318 if ($(this).attr('p:panel-name') == self.options.panels[index].name) {
319 $(this).parent('select').val($(this).attr('value'));
325 Editor.prototype.savePanelOptions = function() {
328 $('.panel-wrap', self.rootDiv).not('.panel-content-overlay').each(function() {
330 name: $('.panel-toolbar option:selected', this).attr('p:panel-name'),
331 ratio: $(this).width() / self.rootDiv.width()
334 self.options.panels = panels;
335 self.options.lastUpdate = (new Date()).getTime() / 1000;
336 $.log($.toJSON(self.options));
337 $.cookie('options', $.toJSON(self.options), {
343 Editor.prototype.saveToBranch = function(msg)
345 var changed_panel = $('.panel-wrap.changed');
347 $.log('Saving to local branch - panel:', changed_panel);
349 if(!msg) msg = "Zapis z edytora platformy.";
351 if( changed_panel.length == 0) {
352 $.log('Nothing to save.');
353 return true; /* no changes */
356 if( changed_panel.length > 1) {
357 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
361 saveInfo = changed_panel.data('ctrl').saveInfo();
364 if(saveInfo.postData instanceof Object)
365 postData = $.param(saveInfo.postData);
367 postData = saveInfo.postData;
369 postData += '&' + $.param({
370 'commit_message': msg
373 self.showPopup('save-waiting', '', -1);
378 success: function(data, textStatus) {
379 if (data.result != 'ok') {
380 self.showPopup('save-error', (data.errors && data.errors[0]) || 'Nieznany błąd X_X.');
383 self.refreshPanels();
386 if(self.autosaveTimer)
387 clearTimeout(self.autosaveTimer);
389 if (data.warnings == null)
390 self.showPopup('save-successful');
392 self.showPopup('save-warn', data.warnings[0]);
395 self.advancePopupQueue();
397 error: function(rq, tstat, err) {
398 self.showPopup('save-error', '- bład wewnętrzny serwera.');
399 self.advancePopupQueue();
408 Editor.prototype.autoSave = function()
410 this.autosaveTimer = null;
411 // first check if there is anything to save
413 this.saveToBranch("Automatyczny zapis z edytora platformy.");
416 Editor.prototype.onContentChanged = function(event, data) {
419 $('#toolbar-button-save').removeAttr('disabled');
420 $('#toolbar-button-commit').attr('disabled', 'disabled');
421 $('#toolbar-button-update').attr('disabled', 'disabled');
423 if(this.autosaveTimer) return;
424 this.autosaveTimer = setTimeout( function() {
429 Editor.prototype.updateUserBranch = function() {
430 if( $('.panel-wrap.changed').length != 0)
431 alert("There are unsaved changes - can't update.");
435 url: $('#toolbar-button-update').attr('ui:ajax-action'),
437 success: function(data, textStatus) {
438 switch(data.result) {
440 self.showPopup('generic-yes', 'Plik uaktualniony.');
443 case 'nothing-to-do':
444 self.showPopup('generic-info', 'Brak zmian do uaktualnienia.');
447 self.showPopup('generic-error', data.errors && data.errors[0]);
450 error: function(rq, tstat, err) {
451 self.showPopup('generic-error', 'Błąd serwera: ' + err);
458 Editor.prototype.sendMergeRequest = function (message) {
459 if( $('.panel-wrap.changed').length != 0)
460 alert("There are unsaved changes - can't commit.");
463 $.log('URL !: ', $('#commit-dialog form').attr('action'));
466 url: $('#commit-dialog form').attr('action'),
468 success: function(data, textStatus) {
469 switch(data.result) {
471 self.showPopup('generic-yes', 'Łączenie zmian powiodło się.');
473 if(data.localmodified)
477 case 'nothing-to-do':
478 self.showPopup('generic-info', 'Brak zmian do połaczenia.');
481 self.showPopup('generic-error', data.errors && data.errors[0]);
484 error: function(rq, tstat, err) {
485 self.showPopup('generic-error', 'Błąd serwera: ' + err);
494 Editor.prototype.postSplitRequest = function(s, f)
497 url: $('#split-dialog form').attr('action'),
502 data: $('#split-dialog form').serialize()
507 Editor.prototype.allPanels = function() {
508 return $('#' + this.rootDiv.attr('id') +' > *.panel-wrap', this.rootDiv.parent());
511 Editor.prototype.registerScriptlet = function(scriptlet_id, scriptlet_func)
513 // I briefly assume, that it's verified not to break the world on SS
514 if (!this[scriptlet_id])
515 this[scriptlet_id] = scriptlet_func;
518 Editor.prototype.callScriptlet = function(scriptlet_id, panel, params) {
519 var func = this[scriptlet_id]
521 throw 'No scriptlet named "' + scriptlet_id + '" found.';
523 return func(this, panel, params);
527 $.fbind = function (self, func) {
529 return func.apply(self, arguments);
533 editor = new Editor();