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.callHook('load');
52 error: function(request, textStatus, errorThrown) {
53 $.log('ajax', url, this.target, 'error:', textStatus, errorThrown);
54 $(self.contentDiv).html("<p>Wystapił błąd podczas wczytywania panelu.");
59 Panel.prototype.unload = function(event, data) {
60 $.log('got unload signal', this, ' target: ', data);
63 $.log('unloading', this);
64 $(this.contentDiv).html('');
65 this.callHook('unload');
66 this.hooks = null; // flush the hooks
71 Panel.prototype.refresh = function(event, data) {
74 $.log('hard reload for panel ', self.current_url);
75 self.load(self.current_url);
79 if( this.callHook('refresh', reload) )
80 $('.change-notification', this.wrap).fadeOut();
83 Panel.prototype.otherPanelChanged = function(other) {
84 $.log('panel ', other, ' changed.');
85 if(!this.callHook('dirty'))
86 $('.change-notification', this.wrap).fadeIn();
89 Panel.prototype.markChanged = function () {
90 this.wrap.addClass('changed');
93 Panel.prototype.changed = function () {
94 return this.wrap.hasClass('changed');
97 Panel.prototype.unmarkChanged = function () {
98 this.wrap.removeClass('changed');
101 Panel.prototype.saveInfo = function() {
103 this.callHook('saveInfo', null, saveInfo);
109 this.rootDiv = $('#panels');
110 this.popupQueue = [];
111 this.autosaveTimer = null;
114 Editor.prototype.setupUI = function() {
115 // set up the UI visually and attach callbacks
118 self.rootDiv.makeHorizPanel({}); // TODO: this probably doesn't belong into jQuery
119 // self.rootDiv.css('top', ($('#header').outerHeight() ) + 'px');
121 $('#panels > *.panel-wrap').each(function() {
122 var panelWrap = $(this);
123 $.log('wrap: ', panelWrap);
124 var panel = new Panel(panelWrap);
125 panelWrap.data('ctrl', panel); // attach controllers to wraps
126 panel.load($('.panel-toolbar select', panelWrap).val());
128 $('.panel-toolbar select', panelWrap).change(function() {
129 var url = $(this).val();
130 panelWrap.data('ctrl').load(url);
131 self.savePanelOptions();
134 $('.panel-toolbar button.refresh-button', panelWrap).click(
135 function() { panel.refresh(); } );
138 $(document).bind('panel:contentChanged', function() { self.onContentChanged.apply(self, arguments) });
140 $('#toolbar-button-save').click( function (event, data) { self.saveToBranch(); } );
141 $('#toolbar-button-commit').click( function (event, data) { self.sendPullRequest(); } );
142 self.rootDiv.bind('stopResize', function() { self.savePanelOptions() });
145 Editor.prototype.loadConfig = function() {
146 // Load options from cookie
147 var defaultOptions = {
149 {name: 'htmleditor', ratio: 0.5},
150 {name: 'gallery', ratio: 0.5}
156 var cookie = $.cookie('options');
157 this.options = $.secureEvalJSON(cookie);
159 this.options = defaultOptions;
162 this.options = defaultOptions;
166 this.loadPanelOptions();
169 Editor.prototype.loadPanelOptions = function() {
173 $('.panel-wrap', self.rootDiv).each(function(index) {
174 var panelWidth = self.options.panels[index].ratio * self.rootDiv.width();
175 if ($(this).hasClass('last-panel')) {
185 totalWidth += panelWidth;
187 $.log('panel:', this, $(this).css('left'));
188 $('.panel-toolbar select', this).val(
189 $('.panel-toolbar option[name=' + self.options.panels[index].name + ']', this).attr('value')
194 Editor.prototype.savePanelOptions = function() {
197 $('.panel-wrap', self.rootDiv).not('.panel-content-overlay').each(function() {
199 name: $('.panel-toolbar option:selected', this).attr('name'),
200 ratio: $(this).width() / self.rootDiv.width()
203 self.options.panels = panels;
204 self.options.lastUpdate = (new Date()).getTime() / 1000;
205 $.log($.toJSON(self.options));
206 $.cookie('options', $.toJSON(self.options), { expires: 7, path: '/'});
209 Editor.prototype.saveToBranch = function(msg)
211 var changed_panel = $('.panel-wrap.changed');
213 $.log('Saving to local branch - panel:', changed_panel);
215 if(!msg) msg = "Zapis z edytora platformy.";
217 if( changed_panel.length == 0) {
218 $.log('Nothing to save.');
219 return true; /* no changes */
222 if( changed_panel.length > 1) {
223 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
227 saveInfo = changed_panel.data('ctrl').saveInfo();
230 if(saveInfo.postData instanceof Object)
231 postData = $.param(saveInfo.postData);
233 postData = saveInfo.postData;
235 postData += '&' + $.param({'commit_message': msg})
240 success: function(data, textStatus) {
241 if (data.result != 'ok')
242 self.showPopup('save-error', data.errors[0]);
244 self.refreshPanels(changed_panel);
245 $('#toolbar-button-save').attr('disabled', 'disabled');
246 $('#toolbar-button-commit').removeAttr('disabled');
247 if(self.autosaveTimer)
248 clearTimeout(self.autosaveTimer);
250 self.showPopup('save-successful');
253 error: function(rq, tstat, err) {
254 self.showPopup('save-error');
263 Editor.prototype.autoSave = function()
265 this.autosaveTimer = null;
266 // first check if there is anything to save
268 this.saveToBranch("Automatyczny zapis z edytora platformy.");
271 Editor.prototype.onContentChanged = function(event, data) {
274 $('#toolbar-button-save').removeAttr('disabled');
275 $('#toolbar-button-commit').attr('disabled', 'disabled');
277 if(this.autosaveTimer) return;
278 this.autosaveTimer = setTimeout( function() { self.autoSave(); }, 300000 );
281 Editor.prototype.refreshPanels = function(goodPanel) {
283 var panels = $('#' + self.rootDiv.attr('id') +' > *.panel-wrap', self.rootDiv.parent());
285 panels.each(function() {
286 var panel = $(this).data('ctrl');
287 $.log('Refreshing: ', this, panel);
288 if ( panel.changed() )
289 panel.unmarkChanged();
296 Editor.prototype.sendPullRequest = function () {
297 if( $('.panel-wrap.changed').length != 0)
298 alert("There are unsaved changes - can't make a pull request.");
300 this.showPopup('not-implemented');
303 url: '/pull-request',
305 success: function(data, textStatus) {
306 $.log('data: ' + data);
308 error: function(rq, tstat, err) {
309 $.log('commit error', rq, tstat, err);
316 Editor.prototype.showPopup = function(name, text)
319 self.popupQueue.push( [name, text] )
321 if( self.popupQueue.length > 1)
324 var box = $('#message-box > #' + name);
325 $('*.data', box).html(text);
328 self._nextPopup = function() {
329 var elem = self.popupQueue.pop()
331 var box = $('#message-box > #' + elem[0]);
333 box.fadeOut(300, function() {
334 $('*.data', box).html();
336 if( self.popupQueue.length > 0) {
337 box = $('#message-box > #' + self.popupQueue[0][0]);
338 $('*.data', box).html(self.popupQueue[0][1]);
340 setTimeout(self._nextPopup, 5000);
346 setTimeout(self._nextPopup, 5000);
351 editor = new Editor();