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');
56 error: function(request, textStatus, errorThrown) {
57 $.log('ajax', url, this.target, 'error:', textStatus, errorThrown);
58 $(self.contentDiv).html("<p>Wystapił błąd podczas wczytywania panelu.");
63 Panel.prototype.unload = function(event, data) {
64 $.log('got unload signal', this, ' target: ', data);
67 $.log('unloading', this);
68 $(this.contentDiv).html('');
69 this.callHook('unload');
70 this.hooks = null; // flush the hooks
75 Panel.prototype.refresh = function(event, data) {
78 $.log('hard reload for panel ', self.current_url);
79 self.load(self.current_url);
83 if( this.callHook('refresh', reload) )
84 $('.change-notification', this.wrap).fadeOut();
87 Panel.prototype.otherPanelChanged = function(other) {
88 $.log('panel ', other, ' changed.');
89 if(!this.callHook('dirty'))
90 $('.change-notification', this.wrap).fadeIn();
93 Panel.prototype.markChanged = function () {
94 this.wrap.addClass('changed');
97 Panel.prototype.changed = function () {
98 return this.wrap.hasClass('changed');
101 Panel.prototype.unmarkChanged = function () {
102 this.wrap.removeClass('changed');
105 Panel.prototype.saveInfo = function() {
107 this.callHook('saveInfo', null, saveInfo);
111 Panel.prototype.connectToolbar = function()
116 // check if there is a one
117 var toolbar = $("div.toolbar", this.contentDiv);
118 $.log('Connecting toolbar', toolbar);
119 if(toolbar.length == 0) return;
121 // connect group-switch buttons
122 var group_buttons = $('*.toolbar-tabs-container button', toolbar);
124 $.log('Found groups:', group_buttons);
126 group_buttons.each(function() {
128 var group_name = group.attr('ui:group');
129 $.log('Connecting group: ' + group_name);
131 group.click(function() {
132 // change the active group
133 var active = $("*.toolbar-tabs-container button.active", toolbar);
134 if (active != group) {
135 active.removeClass('active');
136 group.addClass('active');
137 $(".toolbar-button-groups-container p", toolbar).each(function() {
138 if ( $(this).attr('ui:group') != group_name)
147 // connect action buttons
148 var action_buttons = $('*.toolbar-button-groups-container button', toolbar);
149 action_buttons.each(function() {
150 var button = $(this);
151 var hk = button.attr('ui:hotkey');
153 var callback = function() {
154 editor.callScriptlet(button.attr('ui:action'),
155 self, eval(button.attr('ui:action-params')) );
159 button.click(callback);
162 if(hk) self.hotkeys[parseInt(hk)] = callback;
165 if (button.attr('ui:tooltip') )
167 var tooltip = button.attr('ui:tooltip');
168 if(hk) tooltip += ' [Alt+'+hk+']';
173 border: "1px solid #7F7D67",
175 background: "#FBFBC6",
185 Panel.prototype.hotkeyPressed = function(event)
187 var callback = this.hotkeys[event.keyCode];
188 if(callback) callback();
191 Panel.prototype.isHotkey = function(event) {
192 if( event.altKey && (this.hotkeys[event.keyCode] != null) )
198 Panel.prototype.fireEvent = function(name) {
199 $(document).trigger('panel:'+name, this);
204 this.rootDiv = $('#panels');
205 this.popupQueue = [];
206 this.autosaveTimer = null;
210 Editor.prototype.setupUI = function() {
211 // set up the UI visually and attach callbacks
214 self.rootDiv.makeHorizPanel({}); // TODO: this probably doesn't belong into jQuery
215 // self.rootDiv.css('top', ($('#header').outerHeight() ) + 'px');
217 $('#panels > *.panel-wrap').each(function() {
218 var panelWrap = $(this);
219 $.log('wrap: ', panelWrap);
220 var panel = new Panel(panelWrap);
221 panelWrap.data('ctrl', panel); // attach controllers to wraps
222 panel.load($('.panel-toolbar select', panelWrap).val());
224 $('.panel-toolbar select', panelWrap).change(function() {
225 var url = $(this).val();
226 panelWrap.data('ctrl').load(url);
227 self.savePanelOptions();
230 $('.panel-toolbar button.refresh-button', panelWrap).click(
236 $(document).bind('panel:contentChanged', function() {
237 self.onContentChanged.apply(self, arguments)
240 $('#toolbar-button-save').click( function (event, data) {
243 $('#toolbar-button-commit').click( function (event, data) {
244 self.sendPullRequest();
246 self.rootDiv.bind('stopResize', function() {
247 self.savePanelOptions()
251 Editor.prototype.loadConfig = function() {
252 // Load options from cookie
253 var defaultOptions = {
269 var cookie = $.cookie('options');
270 this.options = $.secureEvalJSON(cookie);
272 this.options = defaultOptions;
275 this.options = defaultOptions;
279 this.loadPanelOptions();
282 Editor.prototype.loadPanelOptions = function() {
286 $('.panel-wrap', self.rootDiv).each(function(index) {
287 var panelWidth = self.options.panels[index].ratio * self.rootDiv.width();
288 if ($(this).hasClass('last-panel')) {
298 totalWidth += panelWidth;
300 $.log('panel:', this, $(this).css('left'));
301 $('.panel-toolbar select', this).val(
302 $('.panel-toolbar option[name=' + self.options.panels[index].name + ']', this).attr('value')
307 Editor.prototype.savePanelOptions = function() {
310 $('.panel-wrap', self.rootDiv).not('.panel-content-overlay').each(function() {
312 name: $('.panel-toolbar option:selected', this).attr('name'),
313 ratio: $(this).width() / self.rootDiv.width()
316 self.options.panels = panels;
317 self.options.lastUpdate = (new Date()).getTime() / 1000;
318 $.log($.toJSON(self.options));
319 $.cookie('options', $.toJSON(self.options), {
325 Editor.prototype.saveToBranch = function(msg)
327 var changed_panel = $('.panel-wrap.changed');
329 $.log('Saving to local branch - panel:', changed_panel);
331 if(!msg) msg = "Zapis z edytora platformy.";
333 if( changed_panel.length == 0) {
334 $.log('Nothing to save.');
335 return true; /* no changes */
338 if( changed_panel.length > 1) {
339 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
343 saveInfo = changed_panel.data('ctrl').saveInfo();
346 if(saveInfo.postData instanceof Object)
347 postData = $.param(saveInfo.postData);
349 postData = saveInfo.postData;
351 postData += '&' + $.param({
352 'commit_message': msg
358 success: function(data, textStatus) {
359 if (data.result != 'ok')
360 self.showPopup('save-error', data.errors[0]);
362 self.refreshPanels(changed_panel);
363 $('#toolbar-button-save').attr('disabled', 'disabled');
364 $('#toolbar-button-commit').removeAttr('disabled');
365 if(self.autosaveTimer)
366 clearTimeout(self.autosaveTimer);
368 self.showPopup('save-successful');
371 error: function(rq, tstat, err) {
372 self.showPopup('save-error');
381 Editor.prototype.autoSave = function()
383 this.autosaveTimer = null;
384 // first check if there is anything to save
386 this.saveToBranch("Automatyczny zapis z edytora platformy.");
389 Editor.prototype.onContentChanged = function(event, data) {
392 $('#toolbar-button-save').removeAttr('disabled');
393 $('#toolbar-button-commit').attr('disabled', 'disabled');
395 if(this.autosaveTimer) return;
396 this.autosaveTimer = setTimeout( function() {
401 Editor.prototype.refreshPanels = function(goodPanel) {
403 var panels = $('#' + self.rootDiv.attr('id') +' > *.panel-wrap', self.rootDiv.parent());
405 panels.each(function() {
406 var panel = $(this).data('ctrl');
407 $.log('Refreshing: ', this, panel);
408 if ( panel.changed() )
409 panel.unmarkChanged();
416 Editor.prototype.sendPullRequest = function () {
417 if( $('.panel-wrap.changed').length != 0)
418 alert("There are unsaved changes - can't make a pull request.");
420 this.showPopup('not-implemented');
423 url: '/pull-request',
425 success: function(data, textStatus) {
426 $.log('data: ' + data);
428 error: function(rq, tstat, err) {
429 $.log('commit error', rq, tstat, err);
436 Editor.prototype.showPopup = function(name, text)
439 self.popupQueue.push( [name, text] )
441 if( self.popupQueue.length > 1)
444 var box = $('#message-box > #' + name);
445 $('*.data', box).html(text);
448 self._nextPopup = function() {
449 var elem = self.popupQueue.pop()
451 var box = $('#message-box > #' + elem[0]);
453 box.fadeOut(300, function() {
454 $('*.data', box).html();
456 if( self.popupQueue.length > 0) {
457 box = $('#message-box > #' + self.popupQueue[0][0]);
458 $('*.data', box).html(self.popupQueue[0][1]);
460 setTimeout(self._nextPopup, 5000);
466 setTimeout(self._nextPopup, 5000);
469 Editor.prototype.registerScriptlet = function(scriptlet_id, scriptlet_func)
471 // I briefly assume, that it's verified not to break the world on SS
472 if (!this[scriptlet_id])
473 this[scriptlet_id] = scriptlet_func;
476 Editor.prototype.callScriptlet = function(scriptlet_id, panel, params) {
477 var func = this[scriptlet_id]
479 throw 'No scriptlet named "' + scriptlet_id + '" found.';
481 return func(this, panel, params);
485 $.fbind = function (self, func) {
486 return function() { return func.apply(self, arguments); };
489 editor = new Editor();