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)
10 Hotkey.prototype.toString = function() {
12 if(this.has_alt) mods.push('Alt')
13 if(this.has_ctrl) mods.push('Ctrl')
14 if(this.has_shift) mods.push('Shift')
15 mods.push('"'+this.character+'"')
19 function Panel(panelWrap) {
22 self.wrap = panelWrap;
23 self.contentDiv = $('.panel-content', panelWrap);
24 self.instanceId = Math.ceil(Math.random() * 1000000000);
25 $.log('new panel - wrap: ', self.wrap);
27 $(document).bind('panel:unload.' + self.instanceId,
28 function(event, data) {
29 self.unload(event, data);
32 $(document).bind('panel:contentChanged', function(event, data) {
33 $.log(self, ' got changed event from: ', data);
35 self.otherPanelChanged(event.target);
43 Panel.prototype.callHook = function() {
44 var args = $.makeArray(arguments)
45 var hookName = args.splice(0,1)[0]
46 var noHookAction = args.splice(0,1)[0]
49 $.log('calling hook: ', hookName, 'with args: ', args);
50 if(this.hooks && this.hooks[hookName])
51 result = this.hooks[hookName].apply(this, args);
52 else if (noHookAction instanceof Function)
53 result = noHookAction(args);
57 Panel.prototype.load = function (url) {
58 $.log('preparing xhr load: ', this.wrap);
59 $(document).trigger('panel:unload', this);
61 self.current_url = url;
66 success: function(data, tstat) {
68 $(self.contentDiv).html(data);
69 self.hooks = panel_hooks;
71 self.connectToolbar();
72 self.callHook('load');
73 self.callHook('toolbarResized');
75 error: function(request, textStatus, errorThrown) {
76 $.log('ajax', url, this.target, 'error:', textStatus, errorThrown);
77 $(self.contentDiv).html("<p>Wystapił błąd podczas wczytywania panelu.");
82 Panel.prototype.unload = function(event, data) {
83 $.log('got unload signal', this, ' target: ', data);
86 $.log('unloading', this);
87 $(this.contentDiv).html('');
89 // disconnect the toolbar
90 $('div.panel-toolbar span.panel-toolbar-extra', this.wrap).empty();
92 this.callHook('unload');
93 this.hooks = null; // flush the hooks
98 Panel.prototype.refresh = function(event, data) {
100 reload = function() {
101 $.log('hard reload for panel ', self.current_url);
102 self.load(self.current_url);
106 if( this.callHook('refresh', reload) )
107 $('.change-notification', this.wrap).fadeOut();
110 Panel.prototype.otherPanelChanged = function(other) {
111 $.log('panel ', other, ' changed.');
112 if(!this.callHook('dirty'))
113 $('.change-notification', this.wrap).fadeIn();
116 Panel.prototype.markChanged = function () {
117 this.wrap.addClass('changed');
120 Panel.prototype.changed = function () {
121 return this.wrap.hasClass('changed');
124 Panel.prototype.unmarkChanged = function () {
125 this.wrap.removeClass('changed');
128 Panel.prototype.saveInfo = function() {
130 this.callHook('saveInfo', null, saveInfo);
134 Panel.prototype.connectToolbar = function()
139 // check if there is a one
140 var toolbar = $("div.toolbar", this.contentDiv);
141 $.log('Connecting toolbar', toolbar);
142 if(toolbar.length == 0) return;
145 var extra_buttons = $('span.panel-toolbar-extra', toolbar);
146 var placeholder = $('div.panel-toolbar span.panel-toolbar-extra', this.wrap);
147 placeholder.replaceWith(extra_buttons);
149 var action_buttons = $('button', extra_buttons);
151 // connect group-switch buttons
152 var group_buttons = $('*.toolbar-tabs-container button', toolbar);
154 // $.log('Found groups:', group_buttons);
156 group_buttons.each(function() {
158 var group_name = group.attr('ui:group');
159 // $.log('Connecting group: ' + group_name);
161 group.click(function() {
162 // change the active group
163 var active = $("*.toolbar-tabs-container button.active", toolbar);
164 if (active != group) {
165 active.removeClass('active');
166 group.addClass('active');
167 $(".toolbar-button-groups-container p", toolbar).each(function() {
168 if ( $(this).attr('ui:group') != group_name)
173 self.callHook('toolbarResized');
178 // connect action buttons
179 var allbuttons = $.makeArray(action_buttons)
181 $.makeArray($('*.toolbar-button-groups-container button', toolbar)) );
183 $(allbuttons).each(function() {
184 var button = $(this);
185 var hk = button.attr('ui:hotkey');
186 if(hk) hk = new Hotkey( parseInt(hk) );
189 var params = $.evalJSON(button.attr('ui:action-params'));
191 $.log('JSON exception in ', button, ': ', object);
192 button.attr('disabled', 'disabled');
196 var callback = function() {
197 editor.callScriptlet(button.attr('ui:action'), self, params);
201 button.click(callback);
205 self.hotkeys[hk.code] = callback;
210 if (button.attr('ui:tooltip') )
212 var tooltip = button.attr('ui:tooltip');
213 if(hk) tooltip += ' ['+hk+']';
218 border: "1px solid #7F7D67",
220 background: "#FBFBC6",
230 Panel.prototype.hotkeyPressed = function(event)
232 code = event.keyCode;
233 if(event.altKey) code = code | 0x100;
234 if(event.ctrlKey) code = code | 0x200;
235 if(event.shiftKey) code = code | 0x400;
237 var callback = this.hotkeys[code];
238 if(callback) callback();
241 Panel.prototype.isHotkey = function(event) {
242 code = event.keyCode;
243 if(event.altKey) code = code | 0x100;
244 if(event.ctrlKey) code = code | 0x200;
245 if(event.shiftKey) code = code | 0x400;
247 if(this.hotkeys[code] != null)
254 Panel.prototype.fireEvent = function(name) {
255 $(document).trigger('panel:'+name, this);
260 this.rootDiv = $('#panels');
261 this.popupQueue = [];
262 this.autosaveTimer = null;
266 Editor.prototype.setupUI = function() {
267 // set up the UI visually and attach callbacks
270 self.rootDiv.makeHorizPanel({}); // TODO: this probably doesn't belong into jQuery
271 // self.rootDiv.css('top', ($('#header').outerHeight() ) + 'px');
273 $('#panels > *.panel-wrap').each(function() {
274 var panelWrap = $(this);
275 $.log('wrap: ', panelWrap);
276 var panel = new Panel(panelWrap);
277 panelWrap.data('ctrl', panel); // attach controllers to wraps
278 panel.load($('.panel-toolbar select', panelWrap).val());
280 $('.panel-toolbar select', panelWrap).change(function() {
281 var url = $(this).val();
282 panelWrap.data('ctrl').load(url);
283 self.savePanelOptions();
286 $('.panel-toolbar button.refresh-button', panelWrap).click(
292 $(document).bind('panel:contentChanged', function() {
293 self.onContentChanged.apply(self, arguments)
296 $('#toolbar-button-save').click( function (event, data) {
300 $('#toolbar-button-update').click( function (event, data) {
301 if (self.updateUserBranch()) {
302 // commit/update can be called only after proper, save
303 // this means all panels are clean, and will get refreshed
304 // do this only, when there are any changes to local branch
305 self.refreshPanels();
309 $('#toolbar-button-commit').click( function (event, data) {
310 self.sendPullRequest();
311 event.preventDefault();
312 event.stopPropagation();
315 self.rootDiv.bind('stopResize', function() {
316 self.savePanelOptions()
320 Editor.prototype.loadConfig = function() {
321 // Load options from cookie
322 var defaultOptions = {
338 var cookie = $.cookie('options');
339 this.options = $.secureEvalJSON(cookie);
341 this.options = defaultOptions;
344 this.options = defaultOptions;
348 this.loadPanelOptions();
351 Editor.prototype.loadPanelOptions = function() {
355 $('.panel-wrap', self.rootDiv).each(function(index) {
356 var panelWidth = self.options.panels[index].ratio * self.rootDiv.width();
357 if ($(this).hasClass('last-panel')) {
367 totalWidth += panelWidth;
369 $.log('panel:', this, $(this).css('left'));
370 $('.panel-toolbar select', this).val(
371 $('.panel-toolbar option[name=' + self.options.panels[index].name + ']', this).attr('value')
376 Editor.prototype.savePanelOptions = function() {
379 $('.panel-wrap', self.rootDiv).not('.panel-content-overlay').each(function() {
381 name: $('.panel-toolbar option:selected', this).attr('name'),
382 ratio: $(this).width() / self.rootDiv.width()
385 self.options.panels = panels;
386 self.options.lastUpdate = (new Date()).getTime() / 1000;
387 $.log($.toJSON(self.options));
388 $.cookie('options', $.toJSON(self.options), {
394 Editor.prototype.saveToBranch = function(msg)
396 var changed_panel = $('.panel-wrap.changed');
398 $.log('Saving to local branch - panel:', changed_panel);
400 if(!msg) msg = "Zapis z edytora platformy.";
402 if( changed_panel.length == 0) {
403 $.log('Nothing to save.');
404 return true; /* no changes */
407 if( changed_panel.length > 1) {
408 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
412 saveInfo = changed_panel.data('ctrl').saveInfo();
415 if(saveInfo.postData instanceof Object)
416 postData = $.param(saveInfo.postData);
418 postData = saveInfo.postData;
420 postData += '&' + $.param({
421 'commit_message': msg
424 self.showPopup('save-waiting', '', -1);
429 success: function(data, textStatus) {
430 if (data.result != 'ok') {
431 self.showPopup('save-error', (data.errors && data.errors[0]) || 'Nieznany błąd X_X.');
434 self.refreshPanels();
435 $('#toolbar-button-save').attr('disabled', 'disabled');
436 $('#toolbar-button-commit').removeAttr('disabled');
437 $('#toolbar-button-update').removeAttr('disabled');
438 if(self.autosaveTimer)
439 clearTimeout(self.autosaveTimer);
441 if (data.warnings == null)
442 self.showPopup('save-successful');
444 self.showPopup('save-warn', data.warnings[0]);
447 self.advancePopupQueue();
449 error: function(rq, tstat, err) {
450 self.showPopup('save-error', '- bład wewnętrzny serwera.');
451 self.advancePopupQueue();
460 Editor.prototype.autoSave = function()
462 this.autosaveTimer = null;
463 // first check if there is anything to save
465 this.saveToBranch("Automatyczny zapis z edytora platformy.");
468 Editor.prototype.onContentChanged = function(event, data) {
471 $('#toolbar-button-save').removeAttr('disabled');
472 $('#toolbar-button-commit').attr('disabled', 'disabled');
473 $('#toolbar-button-update').attr('disabled', 'disabled');
475 if(this.autosaveTimer) return;
476 this.autosaveTimer = setTimeout( function() {
481 Editor.prototype.refreshPanels = function() {
484 self.allPanels().each(function() {
485 var panel = $(this).data('ctrl');
486 $.log('Refreshing: ', this, panel);
487 if ( panel.changed() )
488 panel.unmarkChanged();
495 Editor.prototype.updateUserBranch = function() {
496 if( $('.panel-wrap.changed').length != 0)
497 alert("There are unsaved changes - can't update.");
501 url: $('#toolbar-button-update').attr('ui:ajax-action'),
503 success: function(data, textStatus) {
504 switch(data.result) {
506 self.showPopup('generic-yes', 'Plik uaktualniony.');
509 case 'nothing-to-do':
510 self.showPopup('generic-info', 'Brak zmian do uaktualnienia.');
513 self.showPopup('generic-error', data.errors && data.errors[0]);
516 error: function(rq, tstat, err) {
517 self.showPopup('generic-error', 'Błąd serwera: ' + err);
524 Editor.prototype.sendPullRequest = function () {
525 if( $('.panel-wrap.changed').length != 0)
526 alert("There are unsaved changes - can't commit.");
530 /* this.showPopup('not-implemented'); */
532 $.log('URL !: ', $('#toolbar-commit-form').attr('action'));
535 url: $('#toolbar-commit-form').attr('action'),
537 success: function(data, textStatus) {
538 switch(data.result) {
540 self.showPopup('generic-yes', 'Łączenie zmian powiodło się.');
542 if(data.localmodified)
546 case 'nothing-to-do':
547 self.showPopup('generic-info', 'Brak zmian do połaczenia.');
550 self.showPopup('generic-error', data.errors && data.errors[0]);
553 error: function(rq, tstat, err) {
554 self.showPopup('generic-error', 'Błąd serwera: ' + err);
557 data: {'message': $('#toolbar-commit-message').val() }
561 Editor.prototype.showPopup = function(name, text, timeout)
563 timeout = timeout || 4000;
565 self.popupQueue.push( [name, text, timeout] )
567 if( self.popupQueue.length > 1)
570 var box = $('#message-box > #' + name);
571 $('*.data', box).html(text || '');
575 setTimeout( $.fbind(self, self.advancePopupQueue), timeout);
578 Editor.prototype.advancePopupQueue = function() {
580 var elem = this.popupQueue.shift();
582 var box = $('#message-box > #' + elem[0]);
584 box.fadeOut(100, function()
586 $('*.data', box).html('');
588 if( self.popupQueue.length > 0) {
589 var ibox = $('#message-box > #' + self.popupQueue[0][0]);
590 $('*.data', ibox).html(self.popupQueue[0][1] || '');
592 if(self.popupQueue[0][2] > 0)
593 setTimeout( $.fbind(self, self.advancePopupQueue), self.popupQueue[0][2]);
599 Editor.prototype.allPanels = function() {
600 return $('#' + this.rootDiv.attr('id') +' > *.panel-wrap', this.rootDiv.parent());
604 Editor.prototype.registerScriptlet = function(scriptlet_id, scriptlet_func)
606 // I briefly assume, that it's verified not to break the world on SS
607 if (!this[scriptlet_id])
608 this[scriptlet_id] = scriptlet_func;
611 Editor.prototype.callScriptlet = function(scriptlet_id, panel, params) {
612 var func = this[scriptlet_id]
614 throw 'No scriptlet named "' + scriptlet_id + '" found.';
616 return func(this, panel, params);
620 $.fbind = function (self, func) {
622 return func.apply(self, arguments);
626 editor = new Editor();