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);
58 Panel.prototype.unload = function(event, data) {
59 $.log('got unload signal', this, ' target: ', data);
62 $.log('unloading', this);
63 $(this.contentDiv).html('');
64 this.callHook('unload');
65 this.hooks = null; // flush the hooks
70 Panel.prototype.refresh = function(event, data) {
73 $.log('hard reload for panel ', self.current_url);
74 self.load(self.current_url);
78 if( this.callHook('refresh', reload) )
79 $('.change-notification', this.wrap).fadeOut();
82 Panel.prototype.otherPanelChanged = function(other) {
83 $.log('panel ', other, ' changed.');
84 if(!this.callHook('dirty'))
85 $('.change-notification', this.wrap).fadeIn();
88 Panel.prototype.markChanged = function () {
89 this.wrap.addClass('changed');
92 Panel.prototype.changed = function () {
93 return this.wrap.hasClass('changed');
96 Panel.prototype.unmarkChanged = function () {
97 this.wrap.removeClass('changed');
100 Panel.prototype.saveInfo = function() {
102 this.callHook('saveInfo', null, saveInfo);
108 this.rootDiv = $('#panels');
109 this.popupQueue = [];
110 this.autosaveTimer = null;
113 Editor.prototype.setupUI = function() {
114 // set up the UI visually and attach callbacks
117 self.rootDiv.makeHorizPanel({}); // TODO: this probably doesn't belong into jQuery
118 // self.rootDiv.css('top', ($('#header').outerHeight() ) + 'px');
120 $('#panels > *.panel-wrap').each(function() {
121 var panelWrap = $(this);
122 $.log('wrap: ', panelWrap);
123 panel = new Panel(panelWrap);
124 panelWrap.data('ctrl', panel); // attach controllers to wraps
125 panel.load($('.panel-toolbar select', panelWrap).val());
127 $('.panel-toolbar select', panelWrap).change(function() {
128 var url = $(this).val();
129 panelWrap.data('ctrl').load(url);
130 self.savePanelOptions();
134 $(document).bind('panel:contentChanged', function() { self.onContentChanged.apply(self, arguments) });
136 $('#toolbar-button-save').click( function (event, data) { self.saveToBranch(); } );
137 $('#toolbar-button-commit').click( function (event, data) { self.sendPullRequest(); } );
138 self.rootDiv.bind('stopResize', function() { self.savePanelOptions() });
141 Editor.prototype.loadConfig = function() {
142 // Load options from cookie
143 var defaultOptions = {
145 {name: 'htmleditor', ratio: 0.5},
146 {name: 'gallery', ratio: 0.5}
151 var cookie = $.cookie('options');
152 this.options = $.secureEvalJSON(cookie);
154 this.options = defaultOptions;
157 this.options = defaultOptions;
161 this.loadPanelOptions();
164 Editor.prototype.loadPanelOptions = function() {
168 $('.panel-wrap', self.rootDiv).each(function(index) {
169 var panelWidth = self.options.panels[index].ratio * self.rootDiv.width();
170 if ($(this).hasClass('last-panel')) {
180 totalWidth += panelWidth;
182 $.log('panel:', this, $(this).css('left'));
183 $('.panel-toolbar select', this).val(
184 $('.panel-toolbar option[name=' + self.options.panels[index].name + ']', this).attr('value')
189 Editor.prototype.savePanelOptions = function() {
192 $('.panel-wrap', self.rootDiv).not('.panel-content-overlay').each(function() {
194 name: $('.panel-toolbar option:selected', this).attr('name'),
195 ratio: $(this).width() / self.rootDiv.width()
198 self.options.panels = panels;
199 $.log($.toJSON(self.options));
200 $.cookie('options', $.toJSON(self.options), { expires: 7, path: '/'});
203 Editor.prototype.saveToBranch = function(msg)
205 var changed_panel = $('.panel-wrap.changed');
207 $.log('Saving to local branch - panel:', changed_panel);
209 if(!msg) msg = "Zapis z edytora platformy.";
211 if( changed_panel.length == 0) {
212 $.log('Nothing to save.');
213 return true; /* no changes */
216 if( changed_panel.length > 1) {
217 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
221 saveInfo = changed_panel.data('ctrl').saveInfo();
222 $.extend(saveInfo.postData, {'commit_message': msg});
227 success: function(data, textStatus) {
228 if (data.result != 'ok')
229 $.log('save errors: ', data.errors)
231 self.refreshPanels(changed_panel);
232 $('#toolbar-button-save').attr('disabled', 'disabled');
233 $('#toolbar-button-commit').removeAttr('disabled');
234 if(self.autosaveTimer)
235 clearTimeout(self.autosaveTimer);
237 self.showPopup('save-successful');
240 error: function(rq, tstat, err) {
241 self.showPopup('save-error');
244 data: saveInfo.postData
250 Editor.prototype.autoSave = function()
252 this.autosaveTimer = null;
253 // first check if there is anything to save
255 this.saveToBranch("Automatyczny zapis z edytora platformy.");
258 Editor.prototype.onContentChanged = function(event, data) {
261 $('#toolbar-button-save').removeAttr('disabled');
262 $('#toolbar-button-commit').attr('disabled', 'disabled');
264 if(this.autosaveTimer) return;
265 this.autosaveTimer = setTimeout( function() { self.autoSave(); }, 300000 );
268 Editor.prototype.refreshPanels = function(goodPanel) {
270 var panels = $('#' + self.rootDiv.attr('id') +' > *.panel-wrap', self.rootDiv.parent());
272 panels.each(function() {
273 var panel = $(this).data('ctrl');
274 $.log('Refreshing: ', this, panel);
275 if ( panel.changed() )
276 panel.unmarkChanged();
283 Editor.prototype.sendPullRequest = function () {
284 if( $('.panel-wrap.changed').length != 0)
285 alert("There are unsaved changes - can't make a pull request.");
287 this.showPopup('not-implemented');
290 url: '/pull-request',
292 success: function(data, textStatus) {
293 $.log('data: ' + data);
295 error: function(rq, tstat, err) {
296 $.log('commit error', rq, tstat, err);
303 Editor.prototype.showPopup = function(name)
306 self.popupQueue.push(name)
308 if( self.popupQueue.length > 1)
311 $('#message-box > #' + name).fadeIn();
313 self._nextPopup = function() {
314 var elem = self.popupQueue.pop()
316 elem = $('#message-box > #' + elem);
317 elem.fadeOut(300, function() {
318 if( self.popupQueue.length > 0)
319 $('#message-box > #' + self.popupQueue[0]).fadeIn();
320 setTimeout(self._nextPopup, 1700);
326 setTimeout(self._nextPopup, 2000);
331 editor = new Editor();