Zapisywanie i odczytywanie ustawień paneli z cookie. Potrzebne jest jeszcze ustawiani...
[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         if(!this.wrap.hasClass('changed') ) // TODO: is this needed ?
80                 this.wrap.addClass('changed');
81 }
82
83 Panel.prototype.changed = function () {
84         return this.wrap.hasClass('changed');
85 }
86
87 Panel.prototype.unmarkChanged = function () {
88         this.wrap.removeClass('changed');
89 }
90
91 Panel.prototype.saveInfo = function() {
92         var saveInfo = {};
93         this.callHook('saveInfo', saveInfo);
94         return saveInfo;
95 }
96
97
98 function Editor() {
99         // editor initialization
100         // this is probably a good place to set config
101 }
102
103 Editor.prototype.setupUI = function() {
104         // set up the UI visually and attach callbacks
105         var self = this;
106         var panelRoot = $('#panels');
107         self.rootDiv = panelRoot;
108
109     // Set panel widths from options.panels
110     if (self.options && self.options.panels) {
111         var totalWidth = 0;
112         $('.panel-wrap', panelRoot).each(function(index) {
113             var panelWidth = self.options.panels[index].ratio * panelRoot.width();
114             if ($(this).hasClass('last-panel')) {
115                 $(this).css({
116                     left: totalWidth,
117                     right: 0,
118                 });
119             } else {
120                 $(this).css({
121                     left: totalWidth,
122                     width: panelWidth,
123                 });
124                 totalWidth += panelWidth;               
125             }
126             $('.panel-toolbar select', this).val(
127                 $('.panel-toolbar option[name=' + self.options.panels[index].name + ']', this).attr('value')
128             )
129         });
130     }
131     
132         panelRoot.makeHorizPanel({}); // TODO: this probably doesn't belong into jQuery
133     panelRoot.css('top', ($('#header').outerHeight() ) + 'px');
134     
135         $('#panels > *.panel-wrap').each(function() {
136                 var panelWrap = $(this);
137                 $.log('wrap: ', panelWrap);
138                 panel = new Panel(panelWrap);
139                 panelWrap.data('ctrl', panel); // attach controllers to wraps
140         panel.load($('.panel-toolbar select', panelWrap).val());
141         
142             $('.panel-toolbar select', panelWrap).change(function() {
143                 var url = $(this).val();
144                         panelWrap.data('ctrl').load(url);
145                         var panels = [];
146                         $('.panel-wrap', panelRoot).not('.panel-content-overlay').each(function(index) {
147                 panels.push({
148                     name: $('.panel-toolbar option:selected', this).attr('name'),
149                     ratio: $(this).width() / panelRoot.width()
150                 })
151                         });
152                         self.options.panels = panels;
153             $.log($.toJSON(self.options));
154             $.cookie('options', $.toJSON(self.options), { expires: 7, path: '/'});
155             });
156         });     
157     
158         $('#toolbar-button-save').click( function (event, data) { self.saveToBranch(); } );
159     
160     panelRoot.bind('stopResize', function() {
161         var panels = [];
162         $('.panel-wrap', panelRoot).not('.panel-content-overlay').each(function() {
163             panels.push({
164                 name: $('.panel-toolbar option:selected', this).attr('name'),
165                 ratio: $(this).width() / panelRoot.width()
166             })
167         });
168         self.options.panels = panels;
169         $.log($.toJSON(self.options));
170         $.cookie('options', $.toJSON(self.options), { expires: 7, path: '/'});
171     });
172 }
173
174 Editor.prototype.loadConfig = function() {
175     // Load options from cookie
176     try {
177         var cookie = $.cookie('options');
178         this.options = $.secureEvalJSON(cookie);
179     } catch (e) {        
180         this.options = {
181             panels: [
182                 {name: 'htmleditor', ratio: 0.5},
183                 {name: 'gallery', ratio: 0.5}
184             ]
185         }
186     }
187     $.log(this.options);
188 }
189
190 Editor.prototype.saveToBranch = function() {
191         var changed_panel = $('.panel-wrap.changed');
192         var self = this;
193         $.log('Saving to local branch - panel:', changed_panel);
194
195         if( changed_panel.length == 0) {
196                 $.log('Nothing to save.');
197                 return; /* no changes */
198         }
199
200         if( changed_panel.length > 1) {
201                 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
202                 return;
203         }
204
205         saveInfo = changed_panel.data('ctrl').saveInfo();
206
207         $.ajax({
208                 url: saveInfo.url,
209                 dataType: 'json',
210                 success: function(data, textStatus) {
211                         if (data.result != 'ok')
212                                 $.log('save errors: ', data.errors)
213                         else 
214                                 self.refreshPanels(changed_panel);
215                 },
216                 error: function(rq, tstat, err) {
217                         $.log('save error', rq, tstat, err);
218                 },
219                 type: 'POST',
220                 data: saveInfo.postData
221         });
222 };
223
224 Editor.prototype.refreshPanels = function(goodPanel) {
225         var self = this;
226         var panels = $('#' + self.rootDiv.attr('id') +' > *.panel-wrap', self.rootDiv.parent());
227
228         panels.each(function() {
229                 var panel = $(this).data('ctrl');
230                 $.log(this, panel);
231                 if ( panel.changed() )
232                         panel.unmarkChanged();
233                 else 
234                         panel.refresh();
235         });
236 };              
237
238 $(function() {
239         editor = new Editor();
240
241         // do the layout
242         editor.loadConfig();
243         editor.setupUI();
244 });