No, this isn't needed :-)
[redakcja.git] / project / static / js / editor.js
1 function Panel(panelWrap) {
2         var self = this;
3         self.wrap = panelWrap;
4         self.contentDiv = $('.panel-content', panelWrap);
5         self.instanceId = Math.ceil(Math.random() * 1000000000);
6         $.log('new panel - wrap: ', self.wrap);
7         
8         $(document).bind('panel:unload.' + self.instanceId, 
9                         function(event, data) { self.unload(event, data); });   
10
11         $(document).bind('panel:contentChanged', function(event, data) {
12                 $.log(self, ' got changed event from: ', data);
13                 if(self != data) 
14                         self.otherPanelChanged(event.target);
15                 else 
16                         self.markChanged();
17
18                 return false;           
19         });
20 }
21
22 Panel.prototype.callHook = function(hookName) {
23         if(this.hooks && this.hooks[hookName])
24         {       
25 //              arguments.shift();
26                 $.log('calling hook: ', hookName, 'with args: ', arguments);
27                 return this.hooks[hookName].apply(this, arguments);
28         }
29 }
30
31 Panel.prototype.load = function (url) {
32     $.log('preparing xhr load: ', this.wrap);
33     $(document).trigger('panel:unload', this);
34         var self = this;
35         self.current_url = url;
36
37     $.ajax({
38         url: url,
39         dataType: 'html',
40                 success: function(data, tstat) {
41                         panel_hooks = null;
42                         $(self.contentDiv).html(data);
43                         self.hooks = panel_hooks;                       
44                         panel_hooks = null;
45                         self.callHook('load');
46                 },
47         error: function(request, textStatus, errorThrown) {
48             $.log('ajax', url, this.target, 'error:', textStatus, errorThrown);
49         }
50     });
51 }
52
53 Panel.prototype.unload = function(event, data) {
54         $.log('got unload signal', this, ' target: ', data);
55
56         if( data == this ) {
57                 $.log('unloading', this);
58         $(this.contentDiv).html('');
59                 this.callHook('unload');
60                 this.hooks = null; // flush the hooks
61                 return false;
62     };
63 }
64
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') )
70
71
72 Panel.prototype.otherPanelChanged = function(other) {
73         $.log('panel ', other, ' changed.');
74         $('.change-notification', this.wrap).fadeIn();
75         this.callHook('dirty');
76 }       
77
78 Panel.prototype.markChanged = function () {
79         this.wrap.addClass('changed');
80 }
81
82 Panel.prototype.changed = function () {
83         return this.wrap.hasClass('changed');
84 }
85
86 Panel.prototype.unmarkChanged = function () {
87         this.wrap.removeClass('changed');
88 }
89
90 Panel.prototype.saveInfo = function() {
91         var saveInfo = {};
92         this.callHook('saveInfo', saveInfo);
93         return saveInfo;
94 }
95
96
97 function Editor() {
98         this.rootDiv = $('#panels');
99 }
100
101 Editor.prototype.setupUI = function() {
102         // set up the UI visually and attach callbacks
103         var self = this;
104     
105         self.rootDiv.makeHorizPanel({}); // TODO: this probably doesn't belong into jQuery
106     self.rootDiv.css('top', ($('#header').outerHeight() ) + 'px');
107     
108         $('#panels > *.panel-wrap').each(function() {
109                 var panelWrap = $(this);
110                 $.log('wrap: ', panelWrap);
111                 panel = new Panel(panelWrap);
112                 panelWrap.data('ctrl', panel); // attach controllers to wraps
113         panel.load($('.panel-toolbar select', panelWrap).val());
114         
115         $('.panel-toolbar select', panelWrap).change(function() {
116             var url = $(this).val();
117             panelWrap.data('ctrl').load(url);
118             self.savePanelOptions();
119         });
120     }); 
121     
122     $('#toolbar-button-save').click( function (event, data) { self.saveToBranch(); } );
123     
124     self.rootDiv.bind('stopResize', function() { self.savePanelOptions() });
125 }
126
127 Editor.prototype.loadConfig = function() {
128     // Load options from cookie
129     var defaultOptions = {
130         panels: [
131             {name: 'htmleditor', ratio: 0.5},
132             {name: 'gallery', ratio: 0.5}
133         ]
134     }
135     
136     try {
137         var cookie = $.cookie('options');
138         this.options = $.secureEvalJSON(cookie);
139         if (!this.options) {
140             this.options = defaultOptions;
141         }
142     } catch (e) {    
143         this.options = defaultOptions;
144     }
145     $.log(this.options);
146     
147     this.loadPanelOptions();
148 }
149
150 Editor.prototype.loadPanelOptions = function() {
151     var self = this;
152     var totalWidth = 0;
153     
154     $('.panel-wrap', self.rootDiv).each(function(index) {
155         var panelWidth = self.options.panels[index].ratio * self.rootDiv.width();
156         if ($(this).hasClass('last-panel')) {
157             $(this).css({
158                 left: totalWidth,
159                 right: 0,
160             });
161         } else {
162             $(this).css({
163                 left: totalWidth,
164                 width: panelWidth,
165             });
166             totalWidth += panelWidth;               
167         }
168         $.log('panel:', this, $(this).css('left'));
169         $('.panel-toolbar select', this).val(
170             $('.panel-toolbar option[name=' + self.options.panels[index].name + ']', this).attr('value')
171         )
172     });   
173 }
174
175 Editor.prototype.savePanelOptions = function() {
176     var self = this;
177     var panels = [];
178     $('.panel-wrap', self.rootDiv).not('.panel-content-overlay').each(function() {
179         panels.push({
180             name: $('.panel-toolbar option:selected', this).attr('name'),
181             ratio: $(this).width() / self.rootDiv.width()
182         })
183     });
184     self.options.panels = panels;
185     $.log($.toJSON(self.options));
186     $.cookie('options', $.toJSON(self.options), { expires: 7, path: '/'});
187 }
188
189 Editor.prototype.saveToBranch = function() {
190         var changed_panel = $('.panel-wrap.changed');
191         var self = this;
192         $.log('Saving to local branch - panel:', changed_panel);
193
194         if( changed_panel.length == 0) {
195                 $.log('Nothing to save.');
196                 return; /* no changes */
197         }
198
199         if( changed_panel.length > 1) {
200                 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
201                 return;
202         }
203
204         saveInfo = changed_panel.data('ctrl').saveInfo();
205
206         $.ajax({
207                 url: saveInfo.url,
208                 dataType: 'json',
209                 success: function(data, textStatus) {
210                         if (data.result != 'ok')
211                                 $.log('save errors: ', data.errors)
212                         else 
213                                 self.refreshPanels(changed_panel);
214                 },
215                 error: function(rq, tstat, err) {
216                         $.log('save error', rq, tstat, err);
217                 },
218                 type: 'POST',
219                 data: saveInfo.postData
220         });
221 };
222
223 Editor.prototype.refreshPanels = function(goodPanel) {
224         var self = this;
225         var panels = $('#' + self.rootDiv.attr('id') +' > *.panel-wrap', self.rootDiv.parent());
226
227         panels.each(function() {
228                 var panel = $(this).data('ctrl');
229                 $.log(this, panel);
230                 if ( panel.changed() )
231                         panel.unmarkChanged();
232                 else 
233                         panel.refresh();
234         });
235 };              
236
237 $(function() {
238         editor = new Editor();
239
240         // do the layout
241         editor.loadConfig();
242         editor.setupUI();
243 });