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]
27 $.log('calling hook: ', hookName, 'with args: ', args);
28 if(this.hooks && this.hooks[hookName])
30 return this.hooks[hookName].apply(this, args);
32 else if (noHookAction instanceof Function)
33 return noHookAction(args);
37 Panel.prototype.load = function (url) {
38 $.log('preparing xhr load: ', this.wrap);
39 $(document).trigger('panel:unload', this);
41 self.current_url = url;
46 success: function(data, tstat) {
48 $(self.contentDiv).html(data);
49 self.hooks = panel_hooks;
51 self.callHook('load');
53 error: function(request, textStatus, errorThrown) {
54 $.log('ajax', url, this.target, 'error:', textStatus, errorThrown);
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) {
73 $.log('hard reload for panel ', this.current_url);
74 this.load(this.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 if(!this.wrap.hasClass('changed') ) // TODO: is this needed ?
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');
112 Editor.prototype.setupUI = function() {
113 // set up the UI visually and attach callbacks
116 self.rootDiv.makeHorizPanel({}); // TODO: this probably doesn't belong into jQuery
117 self.rootDiv.css('top', ($('#header').outerHeight() ) + 'px');
119 $('#panels > *.panel-wrap').each(function() {
120 var panelWrap = $(this);
121 $.log('wrap: ', panelWrap);
122 panel = new Panel(panelWrap);
123 panelWrap.data('ctrl', panel); // attach controllers to wraps
124 panel.load($('.panel-toolbar select', panelWrap).val());
126 $('.panel-toolbar select', panelWrap).change(function() {
127 var url = $(this).val();
128 panelWrap.data('ctrl').load(url);
129 self.savePanelOptions();
133 $(document).bind('panel:contentChanged', function(event, data) {
134 $('#toolbar-button-save').removeAttr('disabled');
137 $('#toolbar-button-save').click( function (event, data) { self.saveToBranch(); } );
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() {
204 var changed_panel = $('.panel-wrap.changed');
206 $.log('Saving to local branch - panel:', changed_panel);
208 if( changed_panel.length == 0) {
209 $.log('Nothing to save.');
210 return; /* no changes */
213 if( changed_panel.length > 1) {
214 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
218 saveInfo = changed_panel.data('ctrl').saveInfo();
223 success: function(data, textStatus) {
224 if (data.result != 'ok')
225 $.log('save errors: ', data.errors)
227 self.refreshPanels(changed_panel);
228 $('#toolbar-button-save').attr('disabled', 'disabled');
230 error: function(rq, tstat, err) {
231 $.log('save error', rq, tstat, err);
234 data: saveInfo.postData
238 Editor.prototype.refreshPanels = function(goodPanel) {
240 var panels = $('#' + self.rootDiv.attr('id') +' > *.panel-wrap', self.rootDiv.parent());
242 panels.each(function() {
243 var panel = $(this).data('ctrl');
245 if ( panel.changed() )
246 panel.unmarkChanged();
253 editor = new Editor();