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.load = function (url) {
57 // $.log('preparing xhr load: ', this.wrap);
58 $(document).trigger('panel:unload', this);
60 self.current_url = url;
65 success: function(data, tstat) {
67 $(self.contentDiv).html(data);
68 self.hooks = panel_hooks;
70 self.connectToolbar();
71 self.callHook('load');
72 self.callHook('toolbarResized');
74 error: function(request, textStatus, errorThrown) {
75 $.log('ajax', url, this.target, 'error:', textStatus, errorThrown);
76 $(self.contentDiv).html("<p>Wystapił błąd podczas wczytywania panelu.");
81 Panel.prototype.unload = function(event, data) {
82 // $.log('got unload signal', this, ' target: ', data);
84 $(this.contentDiv).html('');
86 // disconnect the toolbar
87 $('div.panel-toolbar span.panel-toolbar-extra', this.wrap).empty();
89 this.callHook('unload');
90 this.hooks = null; // flush the hooks
95 Panel.prototype.refresh = function(event, data) {
98 $.log('hard reload for panel ', self.current_url);
99 self.load(self.current_url);
103 if( this.callHook('refresh', reload) )
104 $('.change-notification', this.wrap).fadeOut();
107 Panel.prototype.otherPanelChanged = function(other) {
108 $.log('Panel ', this, ' is aware that ', other, ' changed.');
109 if(!this.callHook('dirty'))
110 $('.change-notification', this.wrap).fadeIn();
113 Panel.prototype.markChanged = function () {
114 this.wrap.addClass('changed');
117 Panel.prototype.changed = function () {
118 return this.wrap.hasClass('changed');
121 Panel.prototype.unmarkChanged = function () {
122 this.wrap.removeClass('changed');
125 Panel.prototype.saveInfo = function() {
127 this.callHook('saveInfo', null, saveInfo);
131 Panel.prototype.connectToolbar = function()
136 // check if there is a one
137 var toolbar = $("div.toolbar", this.contentDiv);
138 // $.log('Connecting toolbar', toolbar);
139 if(toolbar.length == 0) return;
142 var extra_buttons = $('span.panel-toolbar-extra', toolbar);
143 var placeholder = $('div.panel-toolbar span.panel-toolbar-extra', this.wrap);
144 placeholder.replaceWith(extra_buttons);
146 var action_buttons = $('button', extra_buttons);
148 // connect group-switch buttons
149 var group_buttons = $('*.toolbar-tabs-container button', toolbar);
151 // $.log('Found groups:', group_buttons);
153 group_buttons.each(function() {
155 var group_name = group.attr('ui:group');
156 // $.log('Connecting group: ' + group_name);
158 group.click(function() {
159 // change the active group
160 var active = $("*.toolbar-tabs-container button.active", toolbar);
161 if (active != group) {
162 active.removeClass('active');
163 group.addClass('active');
164 $(".toolbar-button-groups-container p", toolbar).each(function() {
165 if ( $(this).attr('ui:group') != group_name)
170 self.callHook('toolbarResized');
175 // connect action buttons
176 var allbuttons = $.makeArray(action_buttons)
178 $.makeArray($('*.toolbar-button-groups-container button', toolbar)) );
180 $(allbuttons).each(function() {
181 var button = $(this);
182 var hk = button.attr('ui:hotkey');
183 if(hk) hk = new Hotkey( parseInt(hk) );
186 var params = $.evalJSON(button.attr('ui:action-params'));
188 $.log('JSON exception in ', button, ': ', object);
189 button.attr('disabled', 'disabled');
193 var callback = function() {
194 editor.callScriptlet(button.attr('ui:action'), self, params);
198 button.click(callback);
202 self.hotkeys[hk.code] = callback;
203 // $.log('hotkey', hk);
207 if (button.attr('ui:tooltip') )
209 var tooltip = button.attr('ui:tooltip');
210 if(hk) tooltip += ' ['+hk+']';
215 border: "1px solid #7F7D67",
217 background: "#FBFBC6",
227 Panel.prototype.hotkeyPressed = function(event)
229 code = event.keyCode;
230 if(event.altKey) code = code | 0x100;
231 if(event.ctrlKey) code = code | 0x200;
232 if(event.shiftKey) code = code | 0x400;
234 var callback = this.hotkeys[code];
235 if(callback) callback();
238 Panel.prototype.isHotkey = function(event) {
239 code = event.keyCode;
240 if(event.altKey) code = code | 0x100;
241 if(event.ctrlKey) code = code | 0x200;
242 if(event.shiftKey) code = code | 0x400;
244 if(this.hotkeys[code] != null)
251 Panel.prototype.fireEvent = function(name) {
252 $(document).trigger('panel:'+name, this);
257 this.rootDiv = $('#panels');
258 this.popupQueue = [];
259 this.autosaveTimer = null;
263 Editor.prototype.loadConfig = function() {
264 // Load options from cookie
265 var defaultOptions = {
281 var cookie = $.cookie('options');
282 this.options = $.secureEvalJSON(cookie);
284 this.options = defaultOptions;
287 this.options = defaultOptions;
291 this.loadPanelOptions();
294 Editor.prototype.loadPanelOptions = function() {
298 $('.panel-wrap', self.rootDiv).each(function(index) {
299 var panelWidth = self.options.panels[index].ratio * self.rootDiv.width();
300 if ($(this).hasClass('last-panel')) {
310 totalWidth += panelWidth;
312 $.log('panel:', this, $(this).css('left'));
313 $('.panel-toolbar option', this).each(function() {
314 if ($(this).attr('p:panel-name') == self.options.panels[index].name) {
315 $(this).parent('select').val($(this).attr('value'));
321 Editor.prototype.savePanelOptions = function() {
324 $('.panel-wrap', self.rootDiv).not('.panel-content-overlay').each(function() {
326 name: $('.panel-toolbar option:selected', this).attr('p:panel-name'),
327 ratio: $(this).width() / self.rootDiv.width()
330 self.options.panels = panels;
331 self.options.lastUpdate = (new Date()).getTime() / 1000;
332 $.log($.toJSON(self.options));
333 $.cookie('options', $.toJSON(self.options), {
339 Editor.prototype.saveToBranch = function(msg)
341 var changed_panel = $('.panel-wrap.changed');
343 $.log('Saving to local branch - panel:', changed_panel);
345 if(!msg) msg = "Zapis z edytora platformy.";
347 if( changed_panel.length == 0) {
348 $.log('Nothing to save.');
349 return true; /* no changes */
352 if( changed_panel.length > 1) {
353 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
357 saveInfo = changed_panel.data('ctrl').saveInfo();
360 if(saveInfo.postData instanceof Object)
361 postData = $.param(saveInfo.postData);
363 postData = saveInfo.postData;
365 postData += '&' + $.param({
366 'commit_message': msg
369 self.showPopup('save-waiting', '', -1);
374 success: function(data, textStatus) {
375 if (data.result != 'ok') {
376 self.showPopup('save-error', (data.errors && data.errors[0]) || 'Nieznany błąd X_X.');
379 self.refreshPanels();
380 $('#toolbar-button-save').attr('disabled', 'disabled');
381 $('#toolbar-button-commit').removeAttr('disabled');
382 $('#toolbar-button-update').removeAttr('disabled');
383 if(self.autosaveTimer)
384 clearTimeout(self.autosaveTimer);
386 if (data.warnings == null)
387 self.showPopup('save-successful');
389 self.showPopup('save-warn', data.warnings[0]);
392 self.advancePopupQueue();
394 error: function(rq, tstat, err) {
395 self.showPopup('save-error', '- bład wewnętrzny serwera.');
396 self.advancePopupQueue();
405 Editor.prototype.autoSave = function()
407 this.autosaveTimer = null;
408 // first check if there is anything to save
410 this.saveToBranch("Automatyczny zapis z edytora platformy.");
413 Editor.prototype.onContentChanged = function(event, data) {
416 $('#toolbar-button-save').removeAttr('disabled');
417 $('#toolbar-button-commit').attr('disabled', 'disabled');
418 $('#toolbar-button-update').attr('disabled', 'disabled');
420 if(this.autosaveTimer) return;
421 this.autosaveTimer = setTimeout( function() {
426 Editor.prototype.updateUserBranch = function() {
427 if( $('.panel-wrap.changed').length != 0)
428 alert("There are unsaved changes - can't update.");
432 url: $('#toolbar-button-update').attr('ui:ajax-action'),
434 success: function(data, textStatus) {
435 switch(data.result) {
437 self.showPopup('generic-yes', 'Plik uaktualniony.');
440 case 'nothing-to-do':
441 self.showPopup('generic-info', 'Brak zmian do uaktualnienia.');
444 self.showPopup('generic-error', data.errors && data.errors[0]);
447 error: function(rq, tstat, err) {
448 self.showPopup('generic-error', 'Błąd serwera: ' + err);
455 Editor.prototype.sendMergeRequest = function (message) {
456 if( $('.panel-wrap.changed').length != 0)
457 alert("There are unsaved changes - can't commit.");
460 $.log('URL !: ', $('#commit-dialog form').attr('action'));
463 url: $('#commit-dialog form').attr('action'),
465 success: function(data, textStatus) {
466 switch(data.result) {
468 self.showPopup('generic-yes', 'Łączenie zmian powiodło się.');
470 if(data.localmodified)
474 case 'nothing-to-do':
475 self.showPopup('generic-info', 'Brak zmian do połaczenia.');
478 self.showPopup('generic-error', data.errors && data.errors[0]);
481 error: function(rq, tstat, err) {
482 self.showPopup('generic-error', 'Błąd serwera: ' + err);
491 Editor.prototype.postSplitRequest = function(s, f)
494 url: $('#split-dialog form').attr('action'),
499 data: $('#split-dialog form').serialize()
504 Editor.prototype.allPanels = function() {
505 return $('#' + this.rootDiv.attr('id') +' > *.panel-wrap', this.rootDiv.parent());
508 Editor.prototype.registerScriptlet = function(scriptlet_id, scriptlet_func)
510 // I briefly assume, that it's verified not to break the world on SS
511 if (!this[scriptlet_id])
512 this[scriptlet_id] = scriptlet_func;
515 Editor.prototype.callScriptlet = function(scriptlet_id, panel, params) {
516 var func = this[scriptlet_id]
518 throw 'No scriptlet named "' + scriptlet_id + '" found.';
520 return func(this, panel, params);
524 $.fbind = function (self, func) {
526 return func.apply(self, arguments);
530 editor = new Editor();