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).html(
98 this.callHook('unload');
99 this.hooks = null; // flush the hooks
104 Panel.prototype.refresh = function(event, data) {
106 var reload = function() {
107 $.log('hard reload for panel ', self.current_url);
108 self.load(self.current_url);
112 if( this.callHook('refresh', reload) ) {
113 $('.change-notification', this.wrap).fadeOut();
117 Panel.prototype.otherPanelChanged = function(other) {
118 $.log('Panel ', this, ' is aware that ', other, ' changed.');
119 if(!this.callHook('dirty')) {
120 $('.change-notification', this.wrap).fadeIn();
124 Panel.prototype.markChanged = function () {
125 this.wrap.addClass('changed');
128 Panel.prototype.changed = function () {
129 return this.wrap.hasClass('changed');
132 Panel.prototype.unmarkChanged = function () {
133 this.wrap.removeClass('changed');
136 Panel.prototype.saveInfo = function() {
138 this.callHook('saveInfo', null, saveInfo);
142 Panel.prototype.connectToolbar = function()
147 // check if there is a one
148 var toolbar = $("div.toolbar", this.contentDiv);
149 // $.log('Connecting toolbar', toolbar);
150 if(toolbar.length === 0) return;
153 var extra_buttons = $('span.panel-toolbar-extra button', toolbar);
154 var placeholder = $('div.panel-toolbar span.panel-toolbar-extra > span', this.wrap);
155 placeholder.replaceWith(extra_buttons);
157 // connect group-switch buttons
158 var group_buttons = $('*.toolbar-tabs-container button', toolbar);
160 // $.log('Found groups:', group_buttons);
162 group_buttons.each(function() {
164 var group_name = group.attr('ui:group');
165 // $.log('Connecting group: ' + group_name);
167 group.click(function() {
168 // change the active group
169 var active = $("*.toolbar-tabs-container button.active", toolbar);
170 if (active != group) {
171 active.removeClass('active');
172 group.addClass('active');
173 $(".toolbar-button-groups-container p", toolbar).each(function() {
174 if ( $(this).attr('ui:group') != group_name) {
180 self.callHook('toolbarResized');
185 // connect action buttons
186 var allbuttons = $.makeArray(extra_buttons);
188 $.makeArray($('*.toolbar-button-groups-container button', toolbar)) );
190 $(allbuttons).each(function() {
191 var button = $(this);
192 var hk = button.attr('ui:hotkey');
193 if(hk) hk = new Hotkey( parseInt(hk) );
196 var params = $.evalJSON(button.attr('ui:action-params'));
198 $.log('JSON exception in ', button, ': ', object);
199 button.attr('disabled', 'disabled');
203 var callback = function() {
204 editor.callScriptlet(button.attr('ui:action'), self, params);
208 button.click(callback);
212 self.hotkeys[hk.code] = callback;
213 // $.log('hotkey', hk);
217 if (button.attr('ui:tooltip') )
219 var tooltip = button.attr('ui:tooltip');
220 if(hk) tooltip += ' ['+hk+']';
225 border: "1px solid #7F7D67",
227 background: "#FBFBC6",
237 Panel.prototype.hotkeyPressed = function(event)
239 var 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 var callback = this.hotkeys[code];
245 if(callback) callback();
248 Panel.prototype.isHotkey = function(event) {
249 var code = event.keyCode;
250 if(event.altKey) code = code | 0x100;
251 if(event.ctrlKey) code = code | 0x200;
252 if(event.shiftKey) code = code | 0x400;
254 $.log(event.character, this.hotkeys[code]);
256 if(this.hotkeys[code]) {
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;
305 if(!this.options.recentFiles)
306 this.options.recentFiles = [];
308 $.each(this.options.recentFiles, function(index) {
309 if (fileId == self.options.recentFiles[index].fileId) {
310 $.log('Found options for', fileId);
311 self.fileOptions = self.options.recentFiles[index];
316 $.log('fileOptions', this.fileOptions);
318 this.loadPanelOptions();
319 this.savePanelOptions();
322 Editor.prototype.loadPanelOptions = function() {
326 $('.panel-wrap', self.rootDiv).each(function(index) {
327 var panelWidth = self.fileOptions.panels[index].ratio * self.rootDiv.width();
328 if ($(this).hasClass('last-panel')) {
338 totalWidth += panelWidth;
340 $.log('panel:', this, $(this).css('left'));
341 $('.panel-toolbar option', this).each(function() {
342 if ($(this).attr('p:panel-name') == self.fileOptions.panels[index].name) {
343 $(this).parent('select').val($(this).attr('value'));
349 Editor.prototype.savePanelOptions = function() {
352 $('.panel-wrap', self.rootDiv).not('.panel-content-overlay').each(function() {
354 name: $('.panel-toolbar option:selected', this).attr('p:panel-name'),
355 ratio: $(this).width() / self.rootDiv.width()
358 self.options.panels = panels;
360 // Dodaj obecnie oglądany plik do listy recentFiles
361 var recentFiles = [{fileId: fileId, panels: panels}];
363 $.each(self.options.recentFiles, function(index) {
364 if (count < 5 && fileId != self.options.recentFiles[index].fileId) {
365 recentFiles.push(self.options.recentFiles[index]);
369 self.options.recentFiles = recentFiles;
371 self.options.lastUpdate = new Date().getTime() / 1000;
372 $.log($.toJSON(self.options));
373 $.cookie('options', $.toJSON(self.options), {
379 Editor.prototype.saveToBranch = function(msg)
381 var changed_panel = $('.panel-wrap.changed');
383 $.log('Saving to local branch - panel:', changed_panel);
385 if(!msg) msg = "Szybki zapis z edytora platformy.";
387 if( changed_panel.length === 0) {
388 $.log('Nothing to save.');
389 return true; /* no changes */
392 if( changed_panel.length > 1) {
393 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
397 var saveInfo = changed_panel.data('ctrl').saveInfo();
400 if (saveInfo.postData instanceof Object) {
401 postData = $.param(saveInfo.postData);
403 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();
423 if(self.autosaveTimer) {
424 clearTimeout(self.autosaveTimer);
426 if (data.warnings === null || data.warning === undefined) {
427 self.showPopup('save-successful');
429 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 $('button.provides-save').removeAttr('disabled');
458 $('button.requires-save').attr('disabled', 'disabled');
460 if(this.autosaveTimer) return;
461 this.autosaveTimer = setTimeout( function() {
466 Editor.prototype.updateUserBranch = function() {
467 if($('.panel-wrap.changed').length !== 0) {
468 alert("There are unsaved changes - can't update.");
473 url: $('#toolbar-button-update').attr('ui:ajax-action'),
475 success: function(data, textStatus) {
476 switch(data.result) {
478 self.showPopup('generic-yes', 'Plik uaktualniony.');
479 self.refreshPanels();
481 case 'nothing-to-do':
482 self.showPopup('generic-info', 'Brak zmian do uaktualnienia.');
485 self.showPopup('generic-error', data.errors && data.errors[0]);
488 error: function(rq, tstat, err) {
489 self.showPopup('generic-error', 'Błąd serwera: ' + err);
496 Editor.prototype.sendMergeRequest = function (message) {
497 if( $('.panel-wrap.changed').length !== 0) {
498 alert("There are unsaved changes - can't commit.");
504 url: $('#commit-dialog form').attr('action'),
506 success: function(data, textStatus) {
507 switch(data.result) {
509 self.showPopup('generic-yes', 'Łączenie zmian powiodło się.');
511 if(data.localmodified) {
512 self.refreshPanels();
516 case 'nothing-to-do':
517 self.showPopup('generic-info', 'Brak zmian do połaczenia.');
520 self.showPopup('generic-error', data.errors && data.errors[0]);
523 error: function(rq, tstat, err) {
524 self.showPopup('generic-error', 'Błąd serwera: ' + err);
533 Editor.prototype.postSplitRequest = function(s, f)
536 url: $('#split-dialog form').attr('action'),
541 data: $('#split-dialog form').serialize()
546 Editor.prototype.allPanels = function() {
547 return $('#' + this.rootDiv.attr('id') +' > *.panel-wrap', this.rootDiv.parent());
550 Editor.prototype.registerScriptlet = function(scriptlet_id, scriptlet_func)
552 // I briefly assume, that it's verified not to break the world on SS
553 if (!this[scriptlet_id]) {
554 this[scriptlet_id] = scriptlet_func;
558 Editor.prototype.callScriptlet = function(scriptlet_id, panel, params) {
559 var func = this[scriptlet_id];
561 throw 'No scriptlet named "' + scriptlet_id + '" found.';
563 return func(this, panel, params);
567 $.fbind = function (self, func) {
569 return func.apply(self, arguments);
573 editor = new Editor();