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) { self.unload(event, data); });
11 $(document).bind('panel:contentChanged', function(event, data) {
12 $.log(self, ' got changed event from: ', data);
14 self.otherPanelChanged(event.target);
22 Panel.prototype.callHook = function() {
23 args = $.makeArray(arguments)
24 var hookName = args.splice(0,1)[0]
25 var noHookAction = args.splice(0,1)[0]
28 $.log('calling hook: ', hookName, 'with args: ', args);
29 if(this.hooks && this.hooks[hookName])
30 result = this.hooks[hookName].apply(this, args);
31 else if (noHookAction instanceof Function)
32 result = noHookAction(args);
36 Panel.prototype.load = function (url) {
37 $.log('preparing xhr load: ', this.wrap);
38 $(document).trigger('panel:unload', this);
40 self.current_url = url;
45 success: function(data, tstat) {
47 $(self.contentDiv).html(data);
48 self.hooks = panel_hooks;
50 self.connectToolbar();
51 self.callHook('load');
53 error: function(request, textStatus, errorThrown) {
54 $.log('ajax', url, this.target, 'error:', textStatus, errorThrown);
55 $(self.contentDiv).html("<p>Wystapił błąd podczas wczytywania panelu.");
60 Panel.prototype.unload = function(event, data) {
61 $.log('got unload signal', this, ' target: ', data);
64 $.log('unloading', this);
65 $(this.contentDiv).html('');
66 this.callHook('unload');
67 this.hooks = null; // flush the hooks
72 Panel.prototype.refresh = function(event, data) {
75 $.log('hard reload for panel ', self.current_url);
76 self.load(self.current_url);
80 if( this.callHook('refresh', reload) )
81 $('.change-notification', this.wrap).fadeOut();
84 Panel.prototype.otherPanelChanged = function(other) {
85 $.log('panel ', other, ' changed.');
86 if(!this.callHook('dirty'))
87 $('.change-notification', this.wrap).fadeIn();
90 Panel.prototype.markChanged = function () {
91 this.wrap.addClass('changed');
94 Panel.prototype.changed = function () {
95 return this.wrap.hasClass('changed');
98 Panel.prototype.unmarkChanged = function () {
99 this.wrap.removeClass('changed');
102 Panel.prototype.saveInfo = function() {
104 this.callHook('saveInfo', null, saveInfo);
108 Panel.prototype.connectToolbar = function() {
109 // check if there is a one
110 var toolbar = $("div.toolbar", this.contentDiv);
111 $.log('Connecting toolbar', toolbar);
112 if(toolbar.length == 0) return;
114 // connect group-switch buttons
115 var group_buttons = $('*.toolbar-tabs-container button', toolbar);
117 $.log('Found groups:', group_buttons);
119 group_buttons.each(function() {
121 var group_name = group.attr('ui:group');
122 $.log('Connecting group: ' + group_name);
124 group.click(function() {
125 // change the active group
126 var active = $("*.toolbar-tabs-container button.active");
127 if (active != group) {
128 active.removeClass('active');
129 group.addClass('active');
130 $(".toolbar-button-groups-container p").each(function() {
131 if ( $(this).attr('ui:group') != group_name)
140 // connect action buttons
141 var action_buttons = $('*.toolbar-button-groups-container button');
142 action_buttons.each(function() {
143 var button = $(this);
144 var action = button.attr('ui:action').split(':');
145 var action_name = action[0];
146 var action_data = action[1];
148 button.click(function() {
149 $.log('emiting action ', action_name, ' with data: ', action_data);
150 $(document).trigger('ui:action:' + action_name, action_data);
156 this.rootDiv = $('#panels');
157 this.popupQueue = [];
158 this.autosaveTimer = null;
161 Editor.prototype.setupUI = function() {
162 // set up the UI visually and attach callbacks
165 self.rootDiv.makeHorizPanel({}); // TODO: this probably doesn't belong into jQuery
166 // self.rootDiv.css('top', ($('#header').outerHeight() ) + 'px');
168 $('#panels > *.panel-wrap').each(function() {
169 var panelWrap = $(this);
170 $.log('wrap: ', panelWrap);
171 var panel = new Panel(panelWrap);
172 panelWrap.data('ctrl', panel); // attach controllers to wraps
173 panel.load($('.panel-toolbar select', panelWrap).val());
175 $('.panel-toolbar select', panelWrap).change(function() {
176 var url = $(this).val();
177 panelWrap.data('ctrl').load(url);
178 self.savePanelOptions();
181 $('.panel-toolbar button.refresh-button', panelWrap).click(
182 function() { panel.refresh(); } );
185 $(document).bind('panel:contentChanged', function() { self.onContentChanged.apply(self, arguments) });
187 $('#toolbar-button-save').click( function (event, data) { self.saveToBranch(); } );
188 $('#toolbar-button-commit').click( function (event, data) { self.sendPullRequest(); } );
189 self.rootDiv.bind('stopResize', function() { self.savePanelOptions() });
192 Editor.prototype.loadConfig = function() {
193 // Load options from cookie
194 var defaultOptions = {
196 {name: 'htmleditor', ratio: 0.5},
197 {name: 'gallery', ratio: 0.5}
203 var cookie = $.cookie('options');
204 this.options = $.secureEvalJSON(cookie);
206 this.options = defaultOptions;
209 this.options = defaultOptions;
213 this.loadPanelOptions();
216 Editor.prototype.loadPanelOptions = function() {
220 $('.panel-wrap', self.rootDiv).each(function(index) {
221 var panelWidth = self.options.panels[index].ratio * self.rootDiv.width();
222 if ($(this).hasClass('last-panel')) {
232 totalWidth += panelWidth;
234 $.log('panel:', this, $(this).css('left'));
235 $('.panel-toolbar select', this).val(
236 $('.panel-toolbar option[name=' + self.options.panels[index].name + ']', this).attr('value')
241 Editor.prototype.savePanelOptions = function() {
244 $('.panel-wrap', self.rootDiv).not('.panel-content-overlay').each(function() {
246 name: $('.panel-toolbar option:selected', this).attr('name'),
247 ratio: $(this).width() / self.rootDiv.width()
250 self.options.panels = panels;
251 self.options.lastUpdate = (new Date()).getTime() / 1000;
252 $.log($.toJSON(self.options));
253 $.cookie('options', $.toJSON(self.options), { expires: 7, path: '/'});
256 Editor.prototype.saveToBranch = function(msg)
258 var changed_panel = $('.panel-wrap.changed');
260 $.log('Saving to local branch - panel:', changed_panel);
262 if(!msg) msg = "Zapis z edytora platformy.";
264 if( changed_panel.length == 0) {
265 $.log('Nothing to save.');
266 return true; /* no changes */
269 if( changed_panel.length > 1) {
270 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
274 saveInfo = changed_panel.data('ctrl').saveInfo();
277 if(saveInfo.postData instanceof Object)
278 postData = $.param(saveInfo.postData);
280 postData = saveInfo.postData;
282 postData += '&' + $.param({'commit_message': msg})
287 success: function(data, textStatus) {
288 if (data.result != 'ok')
289 self.showPopup('save-error', data.errors[0]);
291 self.refreshPanels(changed_panel);
292 $('#toolbar-button-save').attr('disabled', 'disabled');
293 $('#toolbar-button-commit').removeAttr('disabled');
294 if(self.autosaveTimer)
295 clearTimeout(self.autosaveTimer);
297 self.showPopup('save-successful');
300 error: function(rq, tstat, err) {
301 self.showPopup('save-error');
310 Editor.prototype.autoSave = function()
312 this.autosaveTimer = null;
313 // first check if there is anything to save
315 this.saveToBranch("Automatyczny zapis z edytora platformy.");
318 Editor.prototype.onContentChanged = function(event, data) {
321 $('#toolbar-button-save').removeAttr('disabled');
322 $('#toolbar-button-commit').attr('disabled', 'disabled');
324 if(this.autosaveTimer) return;
325 this.autosaveTimer = setTimeout( function() { self.autoSave(); }, 300000 );
328 Editor.prototype.refreshPanels = function(goodPanel) {
330 var panels = $('#' + self.rootDiv.attr('id') +' > *.panel-wrap', self.rootDiv.parent());
332 panels.each(function() {
333 var panel = $(this).data('ctrl');
334 $.log('Refreshing: ', this, panel);
335 if ( panel.changed() )
336 panel.unmarkChanged();
343 Editor.prototype.sendPullRequest = function () {
344 if( $('.panel-wrap.changed').length != 0)
345 alert("There are unsaved changes - can't make a pull request.");
347 this.showPopup('not-implemented');
350 url: '/pull-request',
352 success: function(data, textStatus) {
353 $.log('data: ' + data);
355 error: function(rq, tstat, err) {
356 $.log('commit error', rq, tstat, err);
363 Editor.prototype.showPopup = function(name, text)
366 self.popupQueue.push( [name, text] )
368 if( self.popupQueue.length > 1)
371 var box = $('#message-box > #' + name);
372 $('*.data', box).html(text);
375 self._nextPopup = function() {
376 var elem = self.popupQueue.pop()
378 var box = $('#message-box > #' + elem[0]);
380 box.fadeOut(300, function() {
381 $('*.data', box).html();
383 if( self.popupQueue.length > 0) {
384 box = $('#message-box > #' + self.popupQueue[0][0]);
385 $('*.data', box).html(self.popupQueue[0][1]);
387 setTimeout(self._nextPopup, 5000);
393 setTimeout(self._nextPopup, 5000);
398 editor = new Editor();