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 $.log(event.character, this.hotkeys[code]);
258 if(this.hotkeys[code]) {
264 Panel.prototype.fireEvent = function(name) {
265 $(document).trigger('panel:'+name, this);
270 this.rootDiv = $('#panels');
271 this.popupQueue = [];
272 this.autosaveTimer = null;
276 Editor.prototype.loadConfig = function() {
277 // Load options from cookie
278 var defaultOptions = {
295 var cookie = $.cookie('options');
296 this.options = $.secureEvalJSON(cookie);
298 this.options = defaultOptions;
301 this.options = defaultOptions;
304 this.fileOptions = this.options;
307 if(!this.options.recentFiles)
308 this.options.recentFiles = [];
310 $.each(this.options.recentFiles, function(index) {
311 if (fileId == self.options.recentFiles[index].fileId) {
312 $.log('Found options for', fileId);
313 self.fileOptions = self.options.recentFiles[index];
318 $.log('fileOptions', this.fileOptions);
320 this.loadPanelOptions();
321 this.savePanelOptions();
324 Editor.prototype.loadPanelOptions = function() {
328 $('.panel-wrap', self.rootDiv).each(function(index) {
329 var panelWidth = self.fileOptions.panels[index].ratio * self.rootDiv.width();
330 if ($(this).hasClass('last-panel')) {
340 totalWidth += panelWidth;
342 $.log('panel:', this, $(this).css('left'));
343 $('.panel-toolbar option', this).each(function() {
344 if ($(this).attr('p:panel-name') == self.fileOptions.panels[index].name) {
345 $(this).parent('select').val($(this).attr('value'));
351 Editor.prototype.savePanelOptions = function() {
354 $('.panel-wrap', self.rootDiv).not('.panel-content-overlay').each(function() {
356 name: $('.panel-toolbar option:selected', this).attr('p:panel-name'),
357 ratio: $(this).width() / self.rootDiv.width()
360 self.options.panels = panels;
362 // Dodaj obecnie oglądany plik do listy recentFiles
363 var recentFiles = [{fileId: fileId, panels: panels}];
365 $.each(self.options.recentFiles, function(index) {
366 if (count < 5 && fileId != self.options.recentFiles[index].fileId) {
367 recentFiles.push(self.options.recentFiles[index]);
371 self.options.recentFiles = recentFiles;
373 self.options.lastUpdate = new Date().getTime() / 1000;
374 $.log($.toJSON(self.options));
375 $.cookie('options', $.toJSON(self.options), {
381 Editor.prototype.saveToBranch = function(msg)
383 var changed_panel = $('.panel-wrap.changed');
385 $.log('Saving to local branch - panel:', changed_panel);
387 if(!msg) msg = "Szybki zapis z edytora platformy.";
389 if( changed_panel.length === 0) {
390 $.log('Nothing to save.');
391 return true; /* no changes */
394 if( changed_panel.length > 1) {
395 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
399 var saveInfo = changed_panel.data('ctrl').saveInfo();
402 if (saveInfo.postData instanceof Object) {
403 postData = $.param(saveInfo.postData);
405 postData = saveInfo.postData;
408 postData += '&' + $.param({
409 'commit_message': msg
412 self.showPopup('save-waiting', '', -1);
417 success: function(data, textStatus) {
418 if (data.result != 'ok') {
419 self.showPopup('save-error', (data.errors && data.errors[0]) || 'Nieznany błąd X_X.');
422 self.refreshPanels();
425 if(self.autosaveTimer) {
426 clearTimeout(self.autosaveTimer);
428 if (data.warnings === null || data.warning === undefined) {
429 self.showPopup('save-successful');
431 self.showPopup('save-warn', data.warnings[0]);
435 self.advancePopupQueue();
437 error: function(rq, tstat, err) {
438 self.showPopup('save-error', '- bład wewnętrzny serwera.');
439 self.advancePopupQueue();
448 Editor.prototype.autoSave = function()
450 this.autosaveTimer = null;
451 // first check if there is anything to save
453 this.saveToBranch("Automatyczny zapis z edytora platformy.");
456 Editor.prototype.onContentChanged = function(event, data) {
459 $('button.provides-save').removeAttr('disabled');
460 $('button.requires-save').attr('disabled', 'disabled');
462 if(this.autosaveTimer) return;
463 this.autosaveTimer = setTimeout( function() {
468 Editor.prototype.updateUserBranch = function() {
469 if($('.panel-wrap.changed').length !== 0) {
470 alert("There are unsaved changes - can't update.");
475 url: $('#toolbar-button-update').attr('ui:ajax-action'),
477 success: function(data, textStatus) {
478 switch(data.result) {
480 self.showPopup('generic-yes', 'Plik uaktualniony.');
481 self.refreshPanels();
483 case 'nothing-to-do':
484 self.showPopup('generic-info', 'Brak zmian do uaktualnienia.');
487 self.showPopup('generic-error', data.errors && data.errors[0]);
490 error: function(rq, tstat, err) {
491 self.showPopup('generic-error', 'Błąd serwera: ' + err);
498 Editor.prototype.sendMergeRequest = function (message) {
499 if( $('.panel-wrap.changed').length !== 0) {
500 alert("There are unsaved changes - can't commit.");
506 url: $('#commit-dialog form').attr('action'),
508 success: function(data, textStatus) {
509 switch(data.result) {
511 self.showPopup('generic-yes', 'Łączenie zmian powiodło się.');
513 if(data.localmodified) {
514 self.refreshPanels();
518 case 'nothing-to-do':
519 self.showPopup('generic-info', 'Brak zmian do połaczenia.');
522 self.showPopup('generic-error', data.errors && data.errors[0]);
525 error: function(rq, tstat, err) {
526 self.showPopup('generic-error', 'Błąd serwera: ' + err);
535 Editor.prototype.postSplitRequest = function(s, f)
538 url: $('#split-dialog form').attr('action'),
543 data: $('#split-dialog form').serialize()
548 Editor.prototype.allPanels = function() {
549 return $('#' + this.rootDiv.attr('id') +' > *.panel-wrap', this.rootDiv.parent());
552 Editor.prototype.registerScriptlet = function(scriptlet_id, scriptlet_func)
554 // I briefly assume, that it's verified not to break the world on SS
555 if (!this[scriptlet_id]) {
556 this[scriptlet_id] = scriptlet_func;
560 Editor.prototype.callScriptlet = function(scriptlet_id, panel, params) {
561 var func = this[scriptlet_id];
563 throw 'No scriptlet named "' + scriptlet_id + '" found.';
565 return func(this, panel, params);
569 $.fbind = function (self, func) {
571 return func.apply(self, arguments);
575 editor = new Editor();