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);
151 var action_buttons = $('button', extra_buttons);
153 // connect group-switch buttons
154 var group_buttons = $('*.toolbar-tabs-container button', toolbar);
156 // $.log('Found groups:', group_buttons);
158 group_buttons.each(function() {
160 var group_name = group.attr('ui:group');
161 // $.log('Connecting group: ' + group_name);
163 group.click(function() {
164 // change the active group
165 var active = $("*.toolbar-tabs-container button.active", toolbar);
166 if (active != group) {
167 active.removeClass('active');
168 group.addClass('active');
169 $(".toolbar-button-groups-container p", toolbar).each(function() {
170 if ( $(this).attr('ui:group') != group_name)
175 self.callHook('toolbarResized');
180 // connect action buttons
181 var allbuttons = $.makeArray(action_buttons)
183 $.makeArray($('*.toolbar-button-groups-container button', toolbar)) );
185 $(allbuttons).each(function() {
186 var button = $(this);
187 var hk = button.attr('ui:hotkey');
188 if(hk) hk = new Hotkey( parseInt(hk) );
191 var params = $.evalJSON(button.attr('ui:action-params'));
193 $.log('JSON exception in ', button, ': ', object);
194 button.attr('disabled', 'disabled');
198 var callback = function() {
199 editor.callScriptlet(button.attr('ui:action'), self, params);
203 button.click(callback);
207 self.hotkeys[hk.code] = callback;
208 // $.log('hotkey', hk);
212 if (button.attr('ui:tooltip') )
214 var tooltip = button.attr('ui:tooltip');
215 if(hk) tooltip += ' ['+hk+']';
220 border: "1px solid #7F7D67",
222 background: "#FBFBC6",
232 Panel.prototype.hotkeyPressed = function(event)
234 code = event.keyCode;
235 if(event.altKey) code = code | 0x100;
236 if(event.ctrlKey) code = code | 0x200;
237 if(event.shiftKey) code = code | 0x400;
239 var callback = this.hotkeys[code];
240 if(callback) callback();
243 Panel.prototype.isHotkey = function(event) {
244 code = event.keyCode;
245 if(event.altKey) code = code | 0x100;
246 if(event.ctrlKey) code = code | 0x200;
247 if(event.shiftKey) code = code | 0x400;
249 if(this.hotkeys[code] != null)
256 Panel.prototype.fireEvent = function(name) {
257 $(document).trigger('panel:'+name, this);
262 this.rootDiv = $('#panels');
263 this.popupQueue = [];
264 this.autosaveTimer = null;
268 Editor.prototype.loadConfig = function() {
269 // Load options from cookie
270 var defaultOptions = {
286 var cookie = $.cookie('options');
287 this.options = $.secureEvalJSON(cookie);
289 this.options = defaultOptions;
292 this.options = defaultOptions;
296 this.loadPanelOptions();
299 Editor.prototype.loadPanelOptions = function() {
303 $('.panel-wrap', self.rootDiv).each(function(index) {
304 var panelWidth = self.options.panels[index].ratio * self.rootDiv.width();
305 if ($(this).hasClass('last-panel')) {
315 totalWidth += panelWidth;
317 $.log('panel:', this, $(this).css('left'));
318 $('.panel-toolbar option', this).each(function() {
319 if ($(this).attr('p:panel-name') == self.options.panels[index].name) {
320 $(this).parent('select').val($(this).attr('value'));
326 Editor.prototype.savePanelOptions = function() {
329 $('.panel-wrap', self.rootDiv).not('.panel-content-overlay').each(function() {
331 name: $('.panel-toolbar option:selected', this).attr('p:panel-name'),
332 ratio: $(this).width() / self.rootDiv.width()
335 self.options.panels = panels;
336 self.options.lastUpdate = (new Date()).getTime() / 1000;
337 $.log($.toJSON(self.options));
338 $.cookie('options', $.toJSON(self.options), {
344 Editor.prototype.saveToBranch = function(msg)
346 var changed_panel = $('.panel-wrap.changed');
348 $.log('Saving to local branch - panel:', changed_panel);
350 if(!msg) msg = "Zapis z edytora platformy.";
352 if( changed_panel.length == 0) {
353 $.log('Nothing to save.');
354 return true; /* no changes */
357 if( changed_panel.length > 1) {
358 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
362 saveInfo = changed_panel.data('ctrl').saveInfo();
365 if(saveInfo.postData instanceof Object)
366 postData = $.param(saveInfo.postData);
368 postData = saveInfo.postData;
370 postData += '&' + $.param({
371 'commit_message': msg
374 self.showPopup('save-waiting', '', -1);
379 success: function(data, textStatus) {
380 if (data.result != 'ok') {
381 self.showPopup('save-error', (data.errors && data.errors[0]) || 'Nieznany błąd X_X.');
384 self.refreshPanels();
387 if(self.autosaveTimer)
388 clearTimeout(self.autosaveTimer);
390 if (data.warnings == null)
391 self.showPopup('save-successful');
393 self.showPopup('save-warn', data.warnings[0]);
396 self.advancePopupQueue();
398 error: function(rq, tstat, err) {
399 self.showPopup('save-error', '- bład wewnętrzny serwera.');
400 self.advancePopupQueue();
409 Editor.prototype.autoSave = function()
411 this.autosaveTimer = null;
412 // first check if there is anything to save
414 this.saveToBranch("Automatyczny zapis z edytora platformy.");
417 Editor.prototype.onContentChanged = function(event, data) {
420 $('#toolbar-button-save').removeAttr('disabled');
421 $('#toolbar-button-commit').attr('disabled', 'disabled');
422 $('#toolbar-button-update').attr('disabled', 'disabled');
424 if(this.autosaveTimer) return;
425 this.autosaveTimer = setTimeout( function() {
430 Editor.prototype.updateUserBranch = function() {
431 if( $('.panel-wrap.changed').length != 0)
432 alert("There are unsaved changes - can't update.");
436 url: $('#toolbar-button-update').attr('ui:ajax-action'),
438 success: function(data, textStatus) {
439 switch(data.result) {
441 self.showPopup('generic-yes', 'Plik uaktualniony.');
444 case 'nothing-to-do':
445 self.showPopup('generic-info', 'Brak zmian do uaktualnienia.');
448 self.showPopup('generic-error', data.errors && data.errors[0]);
451 error: function(rq, tstat, err) {
452 self.showPopup('generic-error', 'Błąd serwera: ' + err);
459 Editor.prototype.sendMergeRequest = function (message) {
460 if( $('.panel-wrap.changed').length != 0)
461 alert("There are unsaved changes - can't commit.");
465 $('#commit-dialog-related-issues input:checked').
466 each(function() { message += ' refs #' + $(this).val(); });
469 url: $('#commit-dialog form').attr('action'),
471 success: function(data, textStatus) {
472 switch(data.result) {
474 self.showPopup('generic-yes', 'Łączenie zmian powiodło się.');
476 if(data.localmodified)
480 case 'nothing-to-do':
481 self.showPopup('generic-info', 'Brak zmian do połaczenia.');
484 self.showPopup('generic-error', data.errors && data.errors[0]);
487 error: function(rq, tstat, err) {
488 self.showPopup('generic-error', 'Błąd serwera: ' + err);
497 Editor.prototype.postSplitRequest = function(s, f)
500 url: $('#split-dialog form').attr('action'),
505 data: $('#split-dialog form').serialize()
510 Editor.prototype.allPanels = function() {
511 return $('#' + this.rootDiv.attr('id') +' > *.panel-wrap', this.rootDiv.parent());
514 Editor.prototype.registerScriptlet = function(scriptlet_id, scriptlet_func)
516 // I briefly assume, that it's verified not to break the world on SS
517 if (!this[scriptlet_id])
518 this[scriptlet_id] = scriptlet_func;
521 Editor.prototype.callScriptlet = function(scriptlet_id, panel, params) {
522 var func = this[scriptlet_id]
524 throw 'No scriptlet named "' + scriptlet_id + '" found.';
526 return func(this, panel, params);
530 $.fbind = function (self, func) {
532 return func.apply(self, arguments);
536 editor = new Editor();