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(hookName) {
23 if(this.hooks && this.hooks[hookName])
26 $.log('calling hook: ', hookName, 'with args: ', arguments);
27 return this.hooks[hookName].apply(this, arguments);
31 Panel.prototype.load = function (url) {
32 $.log('preparing xhr load: ', this.wrap);
33 $(document).trigger('panel:unload', this);
35 self.current_url = url;
40 success: function(data, tstat) {
42 $(self.contentDiv).html(data);
43 self.hooks = panel_hooks;
45 self.callHook('load');
47 error: function(request, textStatus, errorThrown) {
48 $.log('ajax', url, this.target, 'error:', textStatus, errorThrown);
53 Panel.prototype.unload = function(event, data) {
54 $.log('got unload signal', this, ' target: ', data);
57 $.log('unloading', this);
58 $(this.contentDiv).html('');
59 this.callHook('unload');
60 this.hooks = null; // flush the hooks
65 Panel.prototype.refresh = function(event, data) {
66 $('.change-notification', this.wrap).fadeOut();
67 $.log('refreshing view for panel ', this.current_url);
68 this.load(this.current_url);
69 // if( this.callHook('refresh') )
72 Panel.prototype.otherPanelChanged = function(other) {
73 $.log('panel ', other, ' changed.');
74 $('.change-notification', this.wrap).fadeIn();
75 this.callHook('dirty');
78 Panel.prototype.markChanged = function () {
79 if(!this.wrap.hasClass('changed') ) // TODO: is this needed ?
80 this.wrap.addClass('changed');
83 Panel.prototype.changed = function () {
84 return this.wrap.hasClass('changed');
87 Panel.prototype.unmarkChanged = function () {
88 this.wrap.removeClass('changed');
91 Panel.prototype.saveInfo = function() {
93 this.callHook('saveInfo', saveInfo);
99 this.rootDiv = $('#panels');
102 Editor.prototype.setupUI = function() {
103 // set up the UI visually and attach callbacks
106 self.rootDiv.makeHorizPanel({}); // TODO: this probably doesn't belong into jQuery
107 self.rootDiv.css('top', ($('#header').outerHeight() ) + 'px');
109 $('#panels > *.panel-wrap').each(function() {
110 var panelWrap = $(this);
111 $.log('wrap: ', panelWrap);
112 panel = new Panel(panelWrap);
113 panelWrap.data('ctrl', panel); // attach controllers to wraps
114 panel.load($('.panel-toolbar select', panelWrap).val());
116 $('.panel-toolbar select', panelWrap).change(function() {
117 var url = $(this).val();
118 panelWrap.data('ctrl').load(url);
119 self.savePanelOptions();
123 $('#toolbar-button-save').click( function (event, data) { self.saveToBranch(); } );
125 self.rootDiv.bind('stopResize', function() { self.savePanelOptions() });
128 Editor.prototype.loadConfig = function() {
129 // Load options from cookie
130 var defaultOptions = {
132 {name: 'htmleditor', ratio: 0.5},
133 {name: 'gallery', ratio: 0.5}
138 var cookie = $.cookie('options');
139 this.options = $.secureEvalJSON(cookie);
141 this.options = defaultOptions;
144 this.options = defaultOptions;
148 this.loadPanelOptions();
151 Editor.prototype.loadPanelOptions = function() {
155 $('.panel-wrap', self.rootDiv).each(function(index) {
156 var panelWidth = self.options.panels[index].ratio * self.rootDiv.width();
157 if ($(this).hasClass('last-panel')) {
167 totalWidth += panelWidth;
169 $.log('panel:', this, $(this).css('left'));
170 $('.panel-toolbar select', this).val(
171 $('.panel-toolbar option[name=' + self.options.panels[index].name + ']', this).attr('value')
176 Editor.prototype.savePanelOptions = function() {
179 $('.panel-wrap', self.rootDiv).not('.panel-content-overlay').each(function() {
181 name: $('.panel-toolbar option:selected', this).attr('name'),
182 ratio: $(this).width() / self.rootDiv.width()
185 self.options.panels = panels;
186 $.log($.toJSON(self.options));
187 $.cookie('options', $.toJSON(self.options), { expires: 7, path: '/'});
190 Editor.prototype.saveToBranch = function() {
191 var changed_panel = $('.panel-wrap.changed');
193 $.log('Saving to local branch - panel:', changed_panel);
195 if( changed_panel.length == 0) {
196 $.log('Nothing to save.');
197 return; /* no changes */
200 if( changed_panel.length > 1) {
201 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
205 saveInfo = changed_panel.data('ctrl').saveInfo();
210 success: function(data, textStatus) {
211 if (data.result != 'ok')
212 $.log('save errors: ', data.errors)
214 self.refreshPanels(changed_panel);
216 error: function(rq, tstat, err) {
217 $.log('save error', rq, tstat, err);
220 data: saveInfo.postData
224 Editor.prototype.refreshPanels = function(goodPanel) {
226 var panels = $('#' + self.rootDiv.attr('id') +' > *.panel-wrap', self.rootDiv.parent());
228 panels.each(function() {
229 var panel = $(this).data('ctrl');
231 if ( panel.changed() )
232 panel.unmarkChanged();
239 editor = new Editor();