1 function Panel(panelWrap) {
4 self.contentDiv = $('.panel-content', panelWrap);
5 self.instanceId = Math.ceil(Math.random() * 1000000000);
6 $.log('new panel - wrap: ', self.wrap);
8 $(document).bind('panel:unload.' + self.instanceId,
9 function(event, data) {
10 self.unload(event, data);
13 $(document).bind('panel:contentChanged', function(event, data) {
14 $.log(self, ' got changed event from: ', data);
16 self.otherPanelChanged(event.target);
24 Panel.prototype.callHook = function() {
25 var args = $.makeArray(arguments)
26 var hookName = args.splice(0,1)[0]
27 var noHookAction = args.splice(0,1)[0]
30 $.log('calling hook: ', hookName, 'with args: ', args);
31 if(this.hooks && this.hooks[hookName])
32 result = this.hooks[hookName].apply(this, args);
33 else if (noHookAction instanceof Function)
34 result = noHookAction(args);
38 Panel.prototype.load = function (url) {
39 $.log('preparing xhr load: ', this.wrap);
40 $(document).trigger('panel:unload', this);
42 self.current_url = url;
47 success: function(data, tstat) {
49 $(self.contentDiv).html(data);
50 self.hooks = panel_hooks;
52 self.connectToolbar();
53 self.callHook('load');
55 error: function(request, textStatus, errorThrown) {
56 $.log('ajax', url, this.target, 'error:', textStatus, errorThrown);
57 $(self.contentDiv).html("<p>Wystapił błąd podczas wczytywania panelu.");
62 Panel.prototype.unload = function(event, data) {
63 $.log('got unload signal', this, ' target: ', data);
66 $.log('unloading', this);
67 $(this.contentDiv).html('');
68 this.callHook('unload');
69 this.hooks = null; // flush the hooks
74 Panel.prototype.refresh = function(event, data) {
77 $.log('hard reload for panel ', self.current_url);
78 self.load(self.current_url);
82 if( this.callHook('refresh', reload) )
83 $('.change-notification', this.wrap).fadeOut();
86 Panel.prototype.otherPanelChanged = function(other) {
87 $.log('panel ', other, ' changed.');
88 if(!this.callHook('dirty'))
89 $('.change-notification', this.wrap).fadeIn();
92 Panel.prototype.markChanged = function () {
93 this.wrap.addClass('changed');
96 Panel.prototype.changed = function () {
97 return this.wrap.hasClass('changed');
100 Panel.prototype.unmarkChanged = function () {
101 this.wrap.removeClass('changed');
104 Panel.prototype.saveInfo = function() {
106 this.callHook('saveInfo', null, saveInfo);
110 Panel.prototype.connectToolbar = function()
114 // check if there is a one
115 var toolbar = $("div.toolbar", this.contentDiv);
116 $.log('Connecting toolbar', toolbar);
117 if(toolbar.length == 0) return;
119 // connect group-switch buttons
120 var group_buttons = $('*.toolbar-tabs-container button', toolbar);
122 $.log('Found groups:', group_buttons);
124 group_buttons.each(function() {
126 var group_name = group.attr('ui:group');
127 $.log('Connecting group: ' + group_name);
129 group.click(function() {
130 // change the active group
131 var active = $("*.toolbar-tabs-container button.active");
132 if (active != group) {
133 active.removeClass('active');
134 group.addClass('active');
135 $(".toolbar-button-groups-container p").each(function() {
136 if ( $(this).attr('ui:group') != group_name)
145 // connect action buttons
146 var action_buttons = $('*.toolbar-button-groups-container button');
147 action_buttons.each(function() {
148 var button = $(this);
150 button.click(function() {
151 editor.callScriptlet(button.attr('ui:action'),
152 self, eval(button.attr('ui:action-params')) );
159 Panel.prototype.fireEvent = function(name) {
160 $(document).trigger('panel:'+name, this);
165 this.rootDiv = $('#panels');
166 this.popupQueue = [];
167 this.autosaveTimer = null;
171 Editor.prototype.setupUI = function() {
172 // set up the UI visually and attach callbacks
175 self.rootDiv.makeHorizPanel({}); // TODO: this probably doesn't belong into jQuery
176 // self.rootDiv.css('top', ($('#header').outerHeight() ) + 'px');
178 $('#panels > *.panel-wrap').each(function() {
179 var panelWrap = $(this);
180 $.log('wrap: ', panelWrap);
181 var panel = new Panel(panelWrap);
182 panelWrap.data('ctrl', panel); // attach controllers to wraps
183 panel.load($('.panel-toolbar select', panelWrap).val());
185 $('.panel-toolbar select', panelWrap).change(function() {
186 var url = $(this).val();
187 panelWrap.data('ctrl').load(url);
188 self.savePanelOptions();
191 $('.panel-toolbar button.refresh-button', panelWrap).click(
197 $(document).bind('panel:contentChanged', function() {
198 self.onContentChanged.apply(self, arguments)
201 $('#toolbar-button-save').click( function (event, data) {
204 $('#toolbar-button-commit').click( function (event, data) {
205 self.sendPullRequest();
207 self.rootDiv.bind('stopResize', function() {
208 self.savePanelOptions()
212 Editor.prototype.loadConfig = function() {
213 // Load options from cookie
214 var defaultOptions = {
230 var cookie = $.cookie('options');
231 this.options = $.secureEvalJSON(cookie);
233 this.options = defaultOptions;
236 this.options = defaultOptions;
240 this.loadPanelOptions();
243 Editor.prototype.loadPanelOptions = function() {
247 $('.panel-wrap', self.rootDiv).each(function(index) {
248 var panelWidth = self.options.panels[index].ratio * self.rootDiv.width();
249 if ($(this).hasClass('last-panel')) {
259 totalWidth += panelWidth;
261 $.log('panel:', this, $(this).css('left'));
262 $('.panel-toolbar select', this).val(
263 $('.panel-toolbar option[name=' + self.options.panels[index].name + ']', this).attr('value')
268 Editor.prototype.savePanelOptions = function() {
271 $('.panel-wrap', self.rootDiv).not('.panel-content-overlay').each(function() {
273 name: $('.panel-toolbar option:selected', this).attr('name'),
274 ratio: $(this).width() / self.rootDiv.width()
277 self.options.panels = panels;
278 self.options.lastUpdate = (new Date()).getTime() / 1000;
279 $.log($.toJSON(self.options));
280 $.cookie('options', $.toJSON(self.options), {
286 Editor.prototype.saveToBranch = function(msg)
288 var changed_panel = $('.panel-wrap.changed');
290 $.log('Saving to local branch - panel:', changed_panel);
292 if(!msg) msg = "Zapis z edytora platformy.";
294 if( changed_panel.length == 0) {
295 $.log('Nothing to save.');
296 return true; /* no changes */
299 if( changed_panel.length > 1) {
300 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
304 saveInfo = changed_panel.data('ctrl').saveInfo();
307 if(saveInfo.postData instanceof Object)
308 postData = $.param(saveInfo.postData);
310 postData = saveInfo.postData;
312 postData += '&' + $.param({
313 'commit_message': msg
319 success: function(data, textStatus) {
320 if (data.result != 'ok')
321 self.showPopup('save-error', data.errors[0]);
323 self.refreshPanels(changed_panel);
324 $('#toolbar-button-save').attr('disabled', 'disabled');
325 $('#toolbar-button-commit').removeAttr('disabled');
326 if(self.autosaveTimer)
327 clearTimeout(self.autosaveTimer);
329 self.showPopup('save-successful');
332 error: function(rq, tstat, err) {
333 self.showPopup('save-error');
342 Editor.prototype.autoSave = function()
344 this.autosaveTimer = null;
345 // first check if there is anything to save
347 this.saveToBranch("Automatyczny zapis z edytora platformy.");
350 Editor.prototype.onContentChanged = function(event, data) {
353 $('#toolbar-button-save').removeAttr('disabled');
354 $('#toolbar-button-commit').attr('disabled', 'disabled');
356 if(this.autosaveTimer) return;
357 this.autosaveTimer = setTimeout( function() {
362 Editor.prototype.refreshPanels = function(goodPanel) {
364 var panels = $('#' + self.rootDiv.attr('id') +' > *.panel-wrap', self.rootDiv.parent());
366 panels.each(function() {
367 var panel = $(this).data('ctrl');
368 $.log('Refreshing: ', this, panel);
369 if ( panel.changed() )
370 panel.unmarkChanged();
377 Editor.prototype.sendPullRequest = function () {
378 if( $('.panel-wrap.changed').length != 0)
379 alert("There are unsaved changes - can't make a pull request.");
381 this.showPopup('not-implemented');
384 url: '/pull-request',
386 success: function(data, textStatus) {
387 $.log('data: ' + data);
389 error: function(rq, tstat, err) {
390 $.log('commit error', rq, tstat, err);
397 Editor.prototype.showPopup = function(name, text)
400 self.popupQueue.push( [name, text] )
402 if( self.popupQueue.length > 1)
405 var box = $('#message-box > #' + name);
406 $('*.data', box).html(text);
409 self._nextPopup = function() {
410 var elem = self.popupQueue.pop()
412 var box = $('#message-box > #' + elem[0]);
414 box.fadeOut(300, function() {
415 $('*.data', box).html();
417 if( self.popupQueue.length > 0) {
418 box = $('#message-box > #' + self.popupQueue[0][0]);
419 $('*.data', box).html(self.popupQueue[0][1]);
421 setTimeout(self._nextPopup, 5000);
427 setTimeout(self._nextPopup, 5000);
430 Editor.prototype.registerScriptlet = function(scriptlet_id, scriptlet_func)
432 // I briefly assume, that it's verified not to break the world on SS
433 if (!this[scriptlet_id])
434 this[scriptlet_id] = scriptlet_func;
437 Editor.prototype.callScriptlet = function(scriptlet_id, panel, params) {
438 var func = this[scriptlet_id]
440 throw 'No scriptlet named "' + scriptlet_id + '" found.';
442 return func(this, panel, params);
446 editor = new Editor();