1 function Panel(panelWrap) {
5 self.contentDiv = $('.panel-content', panelWrap);
6 self.instanceId = Math.ceil(Math.random() * 1000000000);
7 $.log('new panel - wrap: ', self.wrap);
9 $(document).bind('panel:unload.' + self.instanceId,
10 function(event, data) {
11 self.unload(event, data);
14 $(document).bind('panel:contentChanged', function(event, data) {
15 $.log(self, ' got changed event from: ', data);
17 self.otherPanelChanged(event.target);
25 Panel.prototype.callHook = function() {
26 var args = $.makeArray(arguments)
27 var hookName = args.splice(0,1)[0]
28 var noHookAction = args.splice(0,1)[0]
31 $.log('calling hook: ', hookName, 'with args: ', args);
32 if(this.hooks && this.hooks[hookName])
33 result = this.hooks[hookName].apply(this, args);
34 else if (noHookAction instanceof Function)
35 result = noHookAction(args);
39 Panel.prototype.load = function (url) {
40 $.log('preparing xhr load: ', this.wrap);
41 $(document).trigger('panel:unload', this);
43 self.current_url = url;
48 success: function(data, tstat) {
50 $(self.contentDiv).html(data);
51 self.hooks = panel_hooks;
53 self.connectToolbar();
54 self.callHook('load');
55 self.callHook('toolbarResized');
57 error: function(request, textStatus, errorThrown) {
58 $.log('ajax', url, this.target, 'error:', textStatus, errorThrown);
59 $(self.contentDiv).html("<p>Wystapił błąd podczas wczytywania panelu.");
64 Panel.prototype.unload = function(event, data) {
65 $.log('got unload signal', this, ' target: ', data);
68 $.log('unloading', this);
69 $(this.contentDiv).html('');
70 this.callHook('unload');
71 this.hooks = null; // flush the hooks
76 Panel.prototype.refresh = function(event, data) {
79 $.log('hard reload for panel ', self.current_url);
80 self.load(self.current_url);
84 if( this.callHook('refresh', reload) )
85 $('.change-notification', this.wrap).fadeOut();
88 Panel.prototype.otherPanelChanged = function(other) {
89 $.log('panel ', other, ' changed.');
90 if(!this.callHook('dirty'))
91 $('.change-notification', this.wrap).fadeIn();
94 Panel.prototype.markChanged = function () {
95 this.wrap.addClass('changed');
98 Panel.prototype.changed = function () {
99 return this.wrap.hasClass('changed');
102 Panel.prototype.unmarkChanged = function () {
103 this.wrap.removeClass('changed');
106 Panel.prototype.saveInfo = function() {
108 this.callHook('saveInfo', null, saveInfo);
112 Panel.prototype.connectToolbar = function()
117 // check if there is a one
118 var toolbar = $("div.toolbar", this.contentDiv);
119 $.log('Connecting toolbar', toolbar);
120 if(toolbar.length == 0) return;
122 // connect group-switch buttons
123 var group_buttons = $('*.toolbar-tabs-container button', toolbar);
125 $.log('Found groups:', group_buttons);
127 group_buttons.each(function() {
129 var group_name = group.attr('ui:group');
130 $.log('Connecting group: ' + group_name);
132 group.click(function() {
133 // change the active group
134 var active = $("*.toolbar-tabs-container button.active", toolbar);
135 if (active != group) {
136 active.removeClass('active');
137 group.addClass('active');
138 $(".toolbar-button-groups-container p", toolbar).each(function() {
139 if ( $(this).attr('ui:group') != group_name)
144 self.callHook('toolbarResized');
149 // connect action buttons
150 var action_buttons = $('*.toolbar-button-groups-container button', toolbar);
151 action_buttons.each(function() {
152 var button = $(this);
153 var hk = button.attr('ui:hotkey');
155 var callback = function() {
156 editor.callScriptlet(button.attr('ui:action'),
157 self, eval(button.attr('ui:action-params')) );
161 button.click(callback);
164 if(hk) self.hotkeys[parseInt(hk)] = callback;
167 if (button.attr('ui:tooltip') )
169 var tooltip = button.attr('ui:tooltip');
170 if(hk) tooltip += ' [Alt+'+hk+']';
175 border: "1px solid #7F7D67",
177 background: "#FBFBC6",
187 Panel.prototype.hotkeyPressed = function(event)
189 var callback = this.hotkeys[event.keyCode];
190 if(callback) callback();
193 Panel.prototype.isHotkey = function(event) {
194 if( event.altKey && (this.hotkeys[event.keyCode] != null) )
200 Panel.prototype.fireEvent = function(name) {
201 $(document).trigger('panel:'+name, this);
206 this.rootDiv = $('#panels');
207 this.popupQueue = [];
208 this.autosaveTimer = null;
212 Editor.prototype.setupUI = function() {
213 // set up the UI visually and attach callbacks
216 self.rootDiv.makeHorizPanel({}); // TODO: this probably doesn't belong into jQuery
217 // self.rootDiv.css('top', ($('#header').outerHeight() ) + 'px');
219 $('#panels > *.panel-wrap').each(function() {
220 var panelWrap = $(this);
221 $.log('wrap: ', panelWrap);
222 var panel = new Panel(panelWrap);
223 panelWrap.data('ctrl', panel); // attach controllers to wraps
224 panel.load($('.panel-toolbar select', panelWrap).val());
226 $('.panel-toolbar select', panelWrap).change(function() {
227 var url = $(this).val();
228 panelWrap.data('ctrl').load(url);
229 self.savePanelOptions();
232 $('.panel-toolbar button.refresh-button', panelWrap).click(
238 $(document).bind('panel:contentChanged', function() {
239 self.onContentChanged.apply(self, arguments)
242 $('#toolbar-button-save').click( function (event, data) {
245 $('#toolbar-button-commit').click( function (event, data) {
246 self.sendPullRequest();
248 self.rootDiv.bind('stopResize', function() {
249 self.savePanelOptions()
253 Editor.prototype.loadConfig = function() {
254 // Load options from cookie
255 var defaultOptions = {
271 var cookie = $.cookie('options');
272 this.options = $.secureEvalJSON(cookie);
274 this.options = defaultOptions;
277 this.options = defaultOptions;
281 this.loadPanelOptions();
284 Editor.prototype.loadPanelOptions = function() {
288 $('.panel-wrap', self.rootDiv).each(function(index) {
289 var panelWidth = self.options.panels[index].ratio * self.rootDiv.width();
290 if ($(this).hasClass('last-panel')) {
300 totalWidth += panelWidth;
302 $.log('panel:', this, $(this).css('left'));
303 $('.panel-toolbar select', this).val(
304 $('.panel-toolbar option[name=' + self.options.panels[index].name + ']', this).attr('value')
309 Editor.prototype.savePanelOptions = function() {
312 $('.panel-wrap', self.rootDiv).not('.panel-content-overlay').each(function() {
314 name: $('.panel-toolbar option:selected', this).attr('name'),
315 ratio: $(this).width() / self.rootDiv.width()
318 self.options.panels = panels;
319 self.options.lastUpdate = (new Date()).getTime() / 1000;
320 $.log($.toJSON(self.options));
321 $.cookie('options', $.toJSON(self.options), {
327 Editor.prototype.saveToBranch = function(msg)
329 var changed_panel = $('.panel-wrap.changed');
331 $.log('Saving to local branch - panel:', changed_panel);
333 if(!msg) msg = "Zapis z edytora platformy.";
335 if( changed_panel.length == 0) {
336 $.log('Nothing to save.');
337 return true; /* no changes */
340 if( changed_panel.length > 1) {
341 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
345 saveInfo = changed_panel.data('ctrl').saveInfo();
348 if(saveInfo.postData instanceof Object)
349 postData = $.param(saveInfo.postData);
351 postData = saveInfo.postData;
353 postData += '&' + $.param({
354 'commit_message': msg
360 success: function(data, textStatus) {
361 if (data.result != 'ok')
362 self.showPopup('save-error', data.errors[0]);
364 self.refreshPanels(changed_panel);
365 $('#toolbar-button-save').attr('disabled', 'disabled');
366 $('#toolbar-button-commit').removeAttr('disabled');
367 if(self.autosaveTimer)
368 clearTimeout(self.autosaveTimer);
370 self.showPopup('save-successful');
373 error: function(rq, tstat, err) {
374 self.showPopup('save-error');
383 Editor.prototype.autoSave = function()
385 this.autosaveTimer = null;
386 // first check if there is anything to save
388 this.saveToBranch("Automatyczny zapis z edytora platformy.");
391 Editor.prototype.onContentChanged = function(event, data) {
394 $('#toolbar-button-save').removeAttr('disabled');
395 $('#toolbar-button-commit').attr('disabled', 'disabled');
397 if(this.autosaveTimer) return;
398 this.autosaveTimer = setTimeout( function() {
403 Editor.prototype.refreshPanels = function(goodPanel) {
405 var panels = $('#' + self.rootDiv.attr('id') +' > *.panel-wrap', self.rootDiv.parent());
407 panels.each(function() {
408 var panel = $(this).data('ctrl');
409 $.log('Refreshing: ', this, panel);
410 if ( panel.changed() )
411 panel.unmarkChanged();
418 Editor.prototype.sendPullRequest = function () {
419 if( $('.panel-wrap.changed').length != 0)
420 alert("There are unsaved changes - can't make a pull request.");
422 this.showPopup('not-implemented');
425 url: '/pull-request',
427 success: function(data, textStatus) {
428 $.log('data: ' + data);
430 error: function(rq, tstat, err) {
431 $.log('commit error', rq, tstat, err);
438 Editor.prototype.showPopup = function(name, text)
441 self.popupQueue.push( [name, text] )
443 if( self.popupQueue.length > 1)
446 var box = $('#message-box > #' + name);
447 $('*.data', box).html(text);
450 self._nextPopup = function() {
451 var elem = self.popupQueue.pop()
453 var box = $('#message-box > #' + elem[0]);
455 box.fadeOut(300, function() {
456 $('*.data', box).html();
458 if( self.popupQueue.length > 0) {
459 box = $('#message-box > #' + self.popupQueue[0][0]);
460 $('*.data', box).html(self.popupQueue[0][1]);
462 setTimeout(self._nextPopup, 5000);
468 setTimeout(self._nextPopup, 5000);
471 Editor.prototype.registerScriptlet = function(scriptlet_id, scriptlet_func)
473 // I briefly assume, that it's verified not to break the world on SS
474 if (!this[scriptlet_id])
475 this[scriptlet_id] = scriptlet_func;
478 Editor.prototype.callScriptlet = function(scriptlet_id, panel, params) {
479 var func = this[scriptlet_id]
481 throw 'No scriptlet named "' + scriptlet_id + '" found.';
483 return func(this, panel, params);
487 $.fbind = function (self, func) {
488 return function() { return func.apply(self, arguments); };
491 editor = new Editor();