4 function Hotkey(code) {
6 this.has_alt = ((code & 0x01 << 8) !== 0);
7 this.has_ctrl = ((code & 0x01 << 9) !== 0);
8 this.has_shift = ((code & 0x01 << 10) !== 0);
9 this.character = String.fromCharCode(code & 0xff);
12 Hotkey.prototype.toString = function() {
14 if(this.has_alt) mods.push('Alt');
15 if(this.has_ctrl) mods.push('Ctrl');
16 if(this.has_shift) mods.push('Shift');
17 mods.push('"'+this.character+'"');
18 return mods.join('+');
21 function Panel(panelWrap) {
24 self.wrap = panelWrap;
25 self.contentDiv = $('.panel-content', panelWrap);
26 self.instanceId = Math.ceil(Math.random() * 1000000000);
27 // $.log('new panel - wrap: ', self.wrap);
29 $(document).bind('panel:unload.' + self.instanceId,
30 function(event, data) {
31 self.unload(event, data);
34 $(document).bind('panel:contentChanged', function(event, data) {
35 $.log(self, ' got changed event from: ', data);
37 self.otherPanelChanged(event.target);
45 Panel.prototype.callHook = function() {
46 var args = $.makeArray(arguments);
47 var hookName = args.splice(0,1)[0];
48 var noHookAction = args.splice(0,1)[0];
51 $.log('calling hook: ', hookName, 'with args: ', args);
52 if(this.hooks && this.hooks[hookName]) {
53 result = this.hooks[hookName].apply(this, args);
54 } else if (noHookAction instanceof Function) {
55 result = noHookAction(args);
60 Panel.prototype._endload = function () {
61 // this needs to be here, so we
62 this.connectToolbar();
63 this.callHook('toolbarResized');
66 Panel.prototype.load = function (url) {
67 // $.log('preparing xhr load: ', this.wrap);
68 $(document).trigger('panel:unload', this);
70 self.current_url = url;
75 success: function(data, tstat) {
77 $(self.contentDiv).html(data);
78 self.hooks = panel_hooks;
80 self.callHook('load');
82 error: function(request, textStatus, errorThrown) {
83 $.log('ajax', url, this.target, 'error:', textStatus, errorThrown);
84 $(self.contentDiv).html("<p>Wystapił błąd podczas wczytywania panelu.</p>");
89 Panel.prototype.unload = function(event, data) {
90 // $.log('got unload signal', this, ' target: ', data);
92 $(this.contentDiv).html('');
94 // disconnect the toolbar
95 $('div.panel-toolbar span.panel-toolbar-extra', this.wrap).empty();
97 this.callHook('unload');
98 this.hooks = null; // flush the hooks
103 Panel.prototype.refresh = function(event, data) {
105 var reload = function() {
106 $.log('hard reload for panel ', self.current_url);
107 self.load(self.current_url);
111 if( this.callHook('refresh', reload) ) {
112 $('.change-notification', this.wrap).fadeOut();
116 Panel.prototype.otherPanelChanged = function(other) {
117 $.log('Panel ', this, ' is aware that ', other, ' changed.');
118 if(!this.callHook('dirty')) {
119 $('.change-notification', this.wrap).fadeIn();
123 Panel.prototype.markChanged = function () {
124 this.wrap.addClass('changed');
127 Panel.prototype.changed = function () {
128 return this.wrap.hasClass('changed');
131 Panel.prototype.unmarkChanged = function () {
132 this.wrap.removeClass('changed');
135 Panel.prototype.saveInfo = function() {
137 this.callHook('saveInfo', null, saveInfo);
141 Panel.prototype.connectToolbar = function()
146 // check if there is a one
147 var toolbar = $("div.toolbar", this.contentDiv);
148 // $.log('Connecting toolbar', toolbar);
149 if(toolbar.length === 0) return;
152 var extra_buttons = $('span.panel-toolbar-extra', toolbar);
153 var placeholder = $('div.panel-toolbar span.panel-toolbar-extra', this.wrap);
154 placeholder.replaceWith(extra_buttons);
157 var action_buttons = $('button', extra_buttons);
159 // connect group-switch buttons
160 var group_buttons = $('*.toolbar-tabs-container button', toolbar);
162 // $.log('Found groups:', group_buttons);
164 group_buttons.each(function() {
166 var group_name = group.attr('ui:group');
167 // $.log('Connecting group: ' + group_name);
169 group.click(function() {
170 // change the active group
171 var active = $("*.toolbar-tabs-container button.active", toolbar);
172 if (active != group) {
173 active.removeClass('active');
174 group.addClass('active');
175 $(".toolbar-button-groups-container p", toolbar).each(function() {
176 if ( $(this).attr('ui:group') != group_name) {
182 self.callHook('toolbarResized');
187 // connect action buttons
188 var allbuttons = $.makeArray(action_buttons);
190 $.makeArray($('*.toolbar-button-groups-container button', toolbar)) );
192 $(allbuttons).each(function() {
193 var button = $(this);
194 var hk = button.attr('ui:hotkey');
195 if(hk) hk = new Hotkey( parseInt(hk) );
198 var params = $.evalJSON(button.attr('ui:action-params'));
200 $.log('JSON exception in ', button, ': ', object);
201 button.attr('disabled', 'disabled');
205 var callback = function() {
206 editor.callScriptlet(button.attr('ui:action'), self, params);
210 button.click(callback);
214 self.hotkeys[hk.code] = callback;
215 // $.log('hotkey', hk);
219 if (button.attr('ui:tooltip') )
221 var tooltip = button.attr('ui:tooltip');
222 if(hk) tooltip += ' ['+hk+']';
227 border: "1px solid #7F7D67",
229 background: "#FBFBC6",
239 Panel.prototype.hotkeyPressed = function(event)
241 var code = event.keyCode;
242 if(event.altKey) code = code | 0x100;
243 if(event.ctrlKey) code = code | 0x200;
244 if(event.shiftKey) code = code | 0x400;
246 var callback = this.hotkeys[code];
247 if(callback) callback();
250 Panel.prototype.isHotkey = function(event) {
251 var code = event.keyCode;
252 if(event.altKey) code = code | 0x100;
253 if(event.ctrlKey) code = code | 0x200;
254 if(event.shiftKey) code = code | 0x400;
256 if(this.hotkeys[code] !== null) {
262 Panel.prototype.fireEvent = function(name) {
263 $(document).trigger('panel:'+name, this);
268 this.rootDiv = $('#panels');
269 this.popupQueue = [];
270 this.autosaveTimer = null;
274 Editor.prototype.loadConfig = function() {
275 // Load options from cookie
276 var defaultOptions = {
293 var cookie = $.cookie('options');
294 this.options = $.secureEvalJSON(cookie);
296 this.options = defaultOptions;
299 this.options = defaultOptions;
302 this.fileOptions = this.options;
304 $.each(this.options.recentFiles, function(index) {
305 if (fileId == self.options.recentFiles[index].fileId) {
306 $.log('Found options for', fileId);
307 self.fileOptions = self.options.recentFiles[index];
312 $.log('fileOptions', this.fileOptions);
314 this.loadPanelOptions();
315 this.savePanelOptions();
318 Editor.prototype.loadPanelOptions = function() {
322 $('.panel-wrap', self.rootDiv).each(function(index) {
323 var panelWidth = self.fileOptions.panels[index].ratio * self.rootDiv.width();
324 if ($(this).hasClass('last-panel')) {
334 totalWidth += panelWidth;
336 $.log('panel:', this, $(this).css('left'));
337 $('.panel-toolbar option', this).each(function() {
338 if ($(this).attr('p:panel-name') == self.fileOptions.panels[index].name) {
339 $(this).parent('select').val($(this).attr('value'));
345 Editor.prototype.savePanelOptions = function() {
348 $('.panel-wrap', self.rootDiv).not('.panel-content-overlay').each(function() {
350 name: $('.panel-toolbar option:selected', this).attr('p:panel-name'),
351 ratio: $(this).width() / self.rootDiv.width()
354 self.options.panels = panels;
356 // Dodaj obecnie oglądany plik do listy recentFiles
357 var recentFiles = [{fileId: fileId, panels: panels}];
359 $.each(self.options.recentFiles, function(index) {
360 if (count < 5 && fileId != self.options.recentFiles[index].fileId) {
361 recentFiles.push(self.options.recentFiles[index]);
365 self.options.recentFiles = recentFiles;
367 self.options.lastUpdate = new Date().getTime() / 1000;
368 $.log($.toJSON(self.options));
369 $.cookie('options', $.toJSON(self.options), {
375 Editor.prototype.saveToBranch = function(msg)
377 var changed_panel = $('.panel-wrap.changed');
379 $.log('Saving to local branch - panel:', changed_panel);
381 if(!msg) msg = "Szybki zapis z edytora platformy.";
383 if( changed_panel.length === 0) {
384 $.log('Nothing to save.');
385 return true; /* no changes */
388 if( changed_panel.length > 1) {
389 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
393 var saveInfo = changed_panel.data('ctrl').saveInfo();
396 if (saveInfo.postData instanceof Object) {
397 postData = $.param(saveInfo.postData);
399 postData = saveInfo.postData;
402 postData += '&' + $.param({
403 'commit_message': msg
406 self.showPopup('save-waiting', '', -1);
411 success: function(data, textStatus) {
412 if (data.result != 'ok') {
413 self.showPopup('save-error', (data.errors && data.errors[0]) || 'Nieznany błąd X_X.');
416 self.refreshPanels();
419 if(self.autosaveTimer) {
420 clearTimeout(self.autosaveTimer);
422 if (data.warnings === null) {
423 self.showPopup('save-successful');
425 self.showPopup('save-warn', data.warnings[0]);
429 self.advancePopupQueue();
431 error: function(rq, tstat, err) {
432 self.showPopup('save-error', '- bład wewnętrzny serwera.');
433 self.advancePopupQueue();
442 Editor.prototype.autoSave = function()
444 this.autosaveTimer = null;
445 // first check if there is anything to save
447 this.saveToBranch("Automatyczny zapis z edytora platformy.");
450 Editor.prototype.onContentChanged = function(event, data) {
453 $('button.provides-save').removeAttr('disabled');
454 $('button.requires-save').attr('disabled', 'disabled');
456 if(this.autosaveTimer) return;
457 this.autosaveTimer = setTimeout( function() {
462 Editor.prototype.updateUserBranch = function() {
463 if($('.panel-wrap.changed').length !== 0) {
464 alert("There are unsaved changes - can't update.");
469 url: $('#toolbar-button-update').attr('ui:ajax-action'),
471 success: function(data, textStatus) {
472 switch(data.result) {
474 self.showPopup('generic-yes', 'Plik uaktualniony.');
475 self.refreshPanels();
477 case 'nothing-to-do':
478 self.showPopup('generic-info', 'Brak zmian do uaktualnienia.');
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);
492 Editor.prototype.sendMergeRequest = function (message) {
493 if( $('.panel-wrap.changed').length !== 0) {
494 alert("There are unsaved changes - can't commit.");
500 url: $('#commit-dialog form').attr('action'),
502 success: function(data, textStatus) {
503 switch(data.result) {
505 self.showPopup('generic-yes', 'Łączenie zmian powiodło się.');
507 if(data.localmodified) {
508 self.refreshPanels();
512 case 'nothing-to-do':
513 self.showPopup('generic-info', 'Brak zmian do połaczenia.');
516 self.showPopup('generic-error', data.errors && data.errors[0]);
519 error: function(rq, tstat, err) {
520 self.showPopup('generic-error', 'Błąd serwera: ' + err);
529 Editor.prototype.postSplitRequest = function(s, f)
532 url: $('#split-dialog form').attr('action'),
537 data: $('#split-dialog form').serialize()
542 Editor.prototype.allPanels = function() {
543 return $('#' + this.rootDiv.attr('id') +' > *.panel-wrap', this.rootDiv.parent());
546 Editor.prototype.registerScriptlet = function(scriptlet_id, scriptlet_func)
548 // I briefly assume, that it's verified not to break the world on SS
549 if (!this[scriptlet_id]) {
550 this[scriptlet_id] = scriptlet_func;
554 Editor.prototype.callScriptlet = function(scriptlet_id, panel, params) {
555 var func = this[scriptlet_id];
557 throw 'No scriptlet named "' + scriptlet_id + '" found.';
559 return func(this, panel, params);
563 $.fbind = function (self, func) {
565 return func.apply(self, arguments);
569 editor = new Editor();