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('');
88 this.callHook('unload');
89 this.hooks = null; // flush the hooks
94 Panel.prototype.refresh = function(event, data) {
97 $.log('hard reload for panel ', self.current_url);
98 self.load(self.current_url);
102 if( this.callHook('refresh', reload) )
103 $('.change-notification', this.wrap).fadeOut();
106 Panel.prototype.otherPanelChanged = function(other) {
107 $.log('panel ', other, ' changed.');
108 if(!this.callHook('dirty'))
109 $('.change-notification', this.wrap).fadeIn();
112 Panel.prototype.markChanged = function () {
113 this.wrap.addClass('changed');
116 Panel.prototype.changed = function () {
117 return this.wrap.hasClass('changed');
120 Panel.prototype.unmarkChanged = function () {
121 this.wrap.removeClass('changed');
124 Panel.prototype.saveInfo = function() {
126 this.callHook('saveInfo', null, saveInfo);
130 Panel.prototype.connectToolbar = function()
135 // check if there is a one
136 var toolbar = $("div.toolbar", this.contentDiv);
137 $.log('Connecting toolbar', toolbar);
138 if(toolbar.length == 0) return;
140 // connect group-switch buttons
141 var group_buttons = $('*.toolbar-tabs-container button', toolbar);
143 // $.log('Found groups:', group_buttons);
145 group_buttons.each(function() {
147 var group_name = group.attr('ui:group');
148 // $.log('Connecting group: ' + group_name);
150 group.click(function() {
151 // change the active group
152 var active = $("*.toolbar-tabs-container button.active", toolbar);
153 if (active != group) {
154 active.removeClass('active');
155 group.addClass('active');
156 $(".toolbar-button-groups-container p", toolbar).each(function() {
157 if ( $(this).attr('ui:group') != group_name)
162 self.callHook('toolbarResized');
167 // connect action buttons
168 var action_buttons = $('*.toolbar-button-groups-container button', toolbar);
169 action_buttons.each(function() {
170 var button = $(this);
171 var hk = button.attr('ui:hotkey');
172 if(hk) hk = new Hotkey( parseInt(hk) );
175 var params = $.evalJSON(button.attr('ui:action-params'));
177 $.log('JSON exception in ', button, ': ', object);
178 button.attr('disabled', 'disabled');
182 var callback = function() {
183 editor.callScriptlet(button.attr('ui:action'), self, params);
187 button.click(callback);
191 self.hotkeys[hk.code] = callback;
196 if (button.attr('ui:tooltip') )
198 var tooltip = button.attr('ui:tooltip');
199 if(hk) tooltip += ' ['+hk+']';
204 border: "1px solid #7F7D67",
206 background: "#FBFBC6",
216 Panel.prototype.hotkeyPressed = function(event)
218 code = event.keyCode;
219 if(event.altKey) code = code | 0x100;
220 if(event.ctrlKey) code = code | 0x200;
221 if(event.shiftKey) code = code | 0x400;
223 var callback = this.hotkeys[code];
224 if(callback) callback();
227 Panel.prototype.isHotkey = function(event) {
228 code = event.keyCode;
229 if(event.altKey) code = code | 0x100;
230 if(event.ctrlKey) code = code | 0x200;
231 if(event.shiftKey) code = code | 0x400;
233 if(this.hotkeys[code] != null)
240 Panel.prototype.fireEvent = function(name) {
241 $(document).trigger('panel:'+name, this);
246 this.rootDiv = $('#panels');
247 this.popupQueue = [];
248 this.autosaveTimer = null;
252 Editor.prototype.setupUI = function() {
253 // set up the UI visually and attach callbacks
256 self.rootDiv.makeHorizPanel({}); // TODO: this probably doesn't belong into jQuery
257 // self.rootDiv.css('top', ($('#header').outerHeight() ) + 'px');
259 $('#panels > *.panel-wrap').each(function() {
260 var panelWrap = $(this);
261 $.log('wrap: ', panelWrap);
262 var panel = new Panel(panelWrap);
263 panelWrap.data('ctrl', panel); // attach controllers to wraps
264 panel.load($('.panel-toolbar select', panelWrap).val());
266 $('.panel-toolbar select', panelWrap).change(function() {
267 var url = $(this).val();
268 panelWrap.data('ctrl').load(url);
269 self.savePanelOptions();
272 $('.panel-toolbar button.refresh-button', panelWrap).click(
278 $(document).bind('panel:contentChanged', function() {
279 self.onContentChanged.apply(self, arguments)
282 $('#toolbar-button-save').click( function (event, data) {
286 $('#toolbar-button-update').click( function (event, data) {
287 if (self.updateUserBranch()) {
288 // commit/update can be called only after proper, save
289 // this means all panels are clean, and will get refreshed
290 // do this only, when there are any changes to local branch
291 self.refreshPanels();
295 $('#toolbar-button-commit').click( function (event, data) {
296 self.sendPullRequest();
297 event.preventDefault();
298 event.stopPropagation();
301 self.rootDiv.bind('stopResize', function() {
302 self.savePanelOptions()
306 Editor.prototype.loadConfig = function() {
307 // Load options from cookie
308 var defaultOptions = {
324 var cookie = $.cookie('options');
325 this.options = $.secureEvalJSON(cookie);
327 this.options = defaultOptions;
330 this.options = defaultOptions;
334 this.loadPanelOptions();
337 Editor.prototype.loadPanelOptions = function() {
341 $('.panel-wrap', self.rootDiv).each(function(index) {
342 var panelWidth = self.options.panels[index].ratio * self.rootDiv.width();
343 if ($(this).hasClass('last-panel')) {
353 totalWidth += panelWidth;
355 $.log('panel:', this, $(this).css('left'));
356 $('.panel-toolbar select', this).val(
357 $('.panel-toolbar option[name=' + self.options.panels[index].name + ']', this).attr('value')
362 Editor.prototype.savePanelOptions = function() {
365 $('.panel-wrap', self.rootDiv).not('.panel-content-overlay').each(function() {
367 name: $('.panel-toolbar option:selected', this).attr('name'),
368 ratio: $(this).width() / self.rootDiv.width()
371 self.options.panels = panels;
372 self.options.lastUpdate = (new Date()).getTime() / 1000;
373 $.log($.toJSON(self.options));
374 $.cookie('options', $.toJSON(self.options), {
380 Editor.prototype.saveToBranch = function(msg)
382 var changed_panel = $('.panel-wrap.changed');
384 $.log('Saving to local branch - panel:', changed_panel);
386 if(!msg) msg = "Zapis z edytora platformy.";
388 if( changed_panel.length == 0) {
389 $.log('Nothing to save.');
390 return true; /* no changes */
393 if( changed_panel.length > 1) {
394 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
398 saveInfo = changed_panel.data('ctrl').saveInfo();
401 if(saveInfo.postData instanceof Object)
402 postData = $.param(saveInfo.postData);
404 postData = saveInfo.postData;
406 postData += '&' + $.param({
407 'commit_message': msg
410 self.showPopup('save-waiting', '', -1);
415 success: function(data, textStatus) {
416 if (data.result != 'ok') {
417 self.showPopup('save-error', (data.errors && data.errors[0]) || 'Nieznany błąd X_X.');
420 self.refreshPanels();
421 $('#toolbar-button-save').attr('disabled', 'disabled');
422 $('#toolbar-button-commit').removeAttr('disabled');
423 $('#toolbar-button-update').removeAttr('disabled');
424 if(self.autosaveTimer)
425 clearTimeout(self.autosaveTimer);
427 if (data.warnings == null)
428 self.showPopup('save-successful');
430 self.showPopup('save-warn', data.warnings[0]);
433 self.advancePopupQueue();
435 error: function(rq, tstat, err) {
436 self.showPopup('save-error', '- bład wewnętrzny serwera.');
437 self.advancePopupQueue();
446 Editor.prototype.autoSave = function()
448 this.autosaveTimer = null;
449 // first check if there is anything to save
451 this.saveToBranch("Automatyczny zapis z edytora platformy.");
454 Editor.prototype.onContentChanged = function(event, data) {
457 $('#toolbar-button-save').removeAttr('disabled');
458 $('#toolbar-button-commit').attr('disabled', 'disabled');
459 $('#toolbar-button-update').attr('disabled', 'disabled');
461 if(this.autosaveTimer) return;
462 this.autosaveTimer = setTimeout( function() {
467 Editor.prototype.refreshPanels = function() {
470 self.allPanels().each(function() {
471 var panel = $(this).data('ctrl');
472 $.log('Refreshing: ', this, panel);
473 if ( panel.changed() )
474 panel.unmarkChanged();
481 Editor.prototype.updateUserBranch = function() {
482 if( $('.panel-wrap.changed').length != 0)
483 alert("There are unsaved changes - can't update.");
487 url: $('#toolbar-button-update').attr('ui:ajax-action'),
489 success: function(data, textStatus) {
490 switch(data.result) {
492 self.showPopup('generic-yes', 'Plik uaktualniony.');
495 case 'nothing-to-do':
496 self.showPopup('generic-info', 'Brak zmian do uaktualnienia.');
499 self.showPopup('generic-error', data.errors && data.errors[0]);
502 error: function(rq, tstat, err) {
503 self.showPopup('generic-error', 'Błąd serwera: ' + err);
510 Editor.prototype.sendPullRequest = function () {
511 if( $('.panel-wrap.changed').length != 0)
512 alert("There are unsaved changes - can't commit.");
516 /* this.showPopup('not-implemented'); */
518 $.log('URL !: ', $('#toolbar-commit-form').attr('action'));
521 url: $('#toolbar-commit-form').attr('action'),
523 success: function(data, textStatus) {
524 switch(data.result) {
526 self.showPopup('generic-yes', 'Łączenie zmian powiodło się.');
528 if(data.localmodified)
532 case 'nothing-to-do':
533 self.showPopup('generic-info', 'Brak zmian do połaczenia.');
536 self.showPopup('generic-error', data.errors && data.errors[0]);
539 error: function(rq, tstat, err) {
540 self.showPopup('generic-error', 'Błąd serwera: ' + err);
543 data: {'message': $('#toolbar-commit-message').val() }
547 Editor.prototype.showPopup = function(name, text, timeout)
549 timeout = timeout || 4000;
551 self.popupQueue.push( [name, text, timeout] )
553 if( self.popupQueue.length > 1)
556 var box = $('#message-box > #' + name);
557 $('*.data', box).html(text || '');
561 setTimeout( $.fbind(self, self.advancePopupQueue), timeout);
564 Editor.prototype.advancePopupQueue = function() {
566 var elem = this.popupQueue.shift();
568 var box = $('#message-box > #' + elem[0]);
570 box.fadeOut(100, function()
572 $('*.data', box).html('');
574 if( self.popupQueue.length > 0) {
575 var ibox = $('#message-box > #' + self.popupQueue[0][0]);
576 $('*.data', ibox).html(self.popupQueue[0][1] || '');
578 if(self.popupQueue[0][2] > 0)
579 setTimeout( $.fbind(self, self.advancePopupQueue), self.popupQueue[0][2]);
585 Editor.prototype.allPanels = function() {
586 return $('#' + this.rootDiv.attr('id') +' > *.panel-wrap', this.rootDiv.parent());
590 Editor.prototype.registerScriptlet = function(scriptlet_id, scriptlet_func)
592 // I briefly assume, that it's verified not to break the world on SS
593 if (!this[scriptlet_id])
594 this[scriptlet_id] = scriptlet_func;
597 Editor.prototype.callScriptlet = function(scriptlet_id, panel, params) {
598 var func = this[scriptlet_id]
600 throw 'No scriptlet named "' + scriptlet_id + '" found.';
602 return func(this, panel, params);
606 $.fbind = function (self, func) {
608 return func.apply(self, arguments);
612 editor = new Editor();