add dependency to migration
[redakcja.git] / redakcja / static / js / wiki / base.js
1 (function($)
2 {
3         var noop = function() { };
4
5         $.wiki = {
6                 perspectives: {},
7                 cls: {},
8                 state: {
9                         "version": 1,
10                         "perspectives": {
11                                 "ScanGalleryPerspective": {
12                                         "show": true,
13                                         "page": undefined
14                                 },
15                                 "CodeMirrorPerspective": {}
16                                 /*
17                                 "VisualPerspective": {},
18                                 "HistoryPerspective": {},
19                                 "SummaryPerspective": {}
20                                 */
21                         }
22                 }
23         };
24
25         $.wiki.loadConfig = function() {
26                 if(!window.localStorage)
27                         return;
28
29                 try {
30                         var value = window.localStorage.getItem(CurrentDocument.id) || "{}";
31                         var config = JSON.parse(value);
32
33                         if (config.version == $.wiki.state.version) {
34                                 $.wiki.state.perspectives = $.extend($.wiki.state.perspectives, config.perspectives);
35                         }
36                 } catch(e) {
37                         console.log("Failed to load config, using default.");
38                 }
39
40                 console.log("Loaded:", $.wiki.state, $.wiki.state.version);
41         };
42
43         $(window).bind('unload', function() {
44                 if(window.localStorage)
45                         window.localStorage.setItem(CurrentDocument.id, JSON.stringify($.wiki.state));
46         })
47
48
49         $.wiki.activePerspective = function() {
50                 return this.perspectives[$("#tabs li.active").attr('id')];
51         };
52
53         $.wiki.exitContext = function() {
54                 var ap = this.activePerspective();
55                 if(ap) ap.onExit();
56                 return ap;
57         };
58
59         $.wiki.enterContext = function(ap) {
60                 if(ap) ap.onEnter();
61         };
62
63         $.wiki.isDirty = function() {
64                 var ap = this.activePerspective();
65                 return (!!CurrentDocument && CurrentDocument.has_local_changes) || ap.dirty();
66         };
67
68         $.wiki.newTab = function(doc, title, klass) {
69                 var base_id = 'id' + Math.floor(Math.random()* 5000000000);
70                 var id = (''+klass)+'_' + base_id;
71                 var $tab = $('<li id="'+id+'" data-ui-related="'+base_id+'" data-ui-jsclass="'+klass+'" >'
72                                 + title + '<img src="'+STATIC_URL+'icons/close.png" class="tabclose"></li>');
73                 var $view = $('<div class="editor '+klass+'" id="'+base_id+'"> </div>');
74
75                 this.perspectives[id] = new $.wiki[klass]({
76                         doc: doc,
77                         id: id,
78                         base_id: base_id,
79                 });
80
81                 $('#tabs').append($tab);
82                 $view.hide().appendTo('#editor');
83                 return {
84                         tab: $tab[0],
85                         view: $view[0],
86                 };
87         };
88
89         $.wiki.initTab = function(options) {
90                 var klass = $(options.tab).attr('data-ui-jsclass');
91
92                 return new $.wiki[klass]({
93                         doc: options.doc,
94                         id: $(options.tab).attr('id'),
95                         callback: function() {
96                                 $.wiki.perspectives[this.perspective_id] = this;
97                                 if(options.callback)
98                                         options.callback.call(this);
99                         }
100                 });
101         };
102
103         $.wiki.perspectiveForTab = function(tab) { // element or id
104                 return this.perspectives[ $(tab).attr('id')];
105         }
106
107         $.wiki.switchToTab = function(tab){
108                 var self = this;
109                 var $tab = $(tab);
110
111                 if($tab.length != 1)
112                         $tab = $(DEFAULT_PERSPECTIVE);
113
114                 var $old = $tab.closest('.tabs').find('.active');
115
116                 $old.each(function(){
117                         $(this).removeClass('active');
118                         self.perspectives[$(this).attr('id')].onExit();
119                         $('#' + $(this).attr('data-ui-related')).hide();
120                 });
121
122                 /* show new */
123                 $tab.addClass('active');
124                 $('#' + $tab.attr('data-ui-related')).show();
125
126                 console.log($tab);
127                 console.log($.wiki.perspectives);
128
129                 $.wiki.perspectives[$tab.attr('id')].onEnter();
130         };
131
132         /*
133          * Basic perspective.
134          */
135         $.wiki.Perspective = function(options) {
136                 if(!options) return;
137
138                 this.doc = options.doc;
139                 if (options.id) {
140                         this.perspective_id = options.id;
141                 }
142                 else {
143                         this.perspective_id = '';
144                 }
145
146                 if(options.callback)
147                         options.callback.call(this);
148         };
149
150         $.wiki.Perspective.prototype.config = function() {
151                 return $.wiki.state.perspectives[this.perspective_id];
152         }
153
154         $.wiki.Perspective.prototype.toString = function() {
155                 return this.perspective_id;
156         };
157
158         $.wiki.Perspective.prototype.dirty = function() {
159                 return true;
160         };
161
162         $.wiki.Perspective.prototype.onEnter = function () {
163                 // called when perspective in initialized
164                 if (!this.noupdate_hash_onenter) {
165                         document.location.hash = '#' + this.perspective_id;
166                 }
167         };
168
169         $.wiki.Perspective.prototype.onExit = function () {
170                 // called when user switches to another perspective
171                 if (!this.noupdate_hash_onenter) {
172                         document.location.hash = '';
173                 }
174         };
175
176         $.wiki.Perspective.prototype.destroy = function() {
177                 // pass
178         };
179
180         $.wiki.Perspective.prototype.freezeState = function () {
181                 // free UI state (don't store data here)
182         };
183
184         $.wiki.Perspective.prototype.unfreezeState = function (frozenState) {
185                 // restore UI state
186         };
187
188         /*
189          * Stub rendering (used in generating history)
190          */
191         $.wiki.renderStub = function(params)
192         {
193                 params = $.extend({ 'filters': {} }, params);
194                 var $elem = params.stub.clone();
195                 $elem.removeClass('row-stub');
196                 params.container.append($elem);
197
198                 $('*[data-stub-value]', $elem).each(function() {
199                         var $this = $(this);
200                         var field = $this.attr('data-stub-value');
201
202                         var value = params.data[field];
203
204                         if(params.filters[field])
205                                 value = params.filters[field](value);
206
207                         if(value === null || value === undefined) return;
208
209                         if(!$this.attr('data-stub-target')) {
210                                 $this.text(value);
211                         }
212                         else {
213                                 $this.attr($this.attr('data-stub-target'), value);
214                                 $this.removeAttr('data-stub-target');
215                                 $this.removeAttr('data-stub-value');
216                         }
217                 });
218
219                 $elem.show();
220                 return $elem;
221         };
222
223         /*
224          * Dialogs
225          */
226         function GenericDialog(element) {
227                 if(!element) return;
228
229                 var self = this;
230
231                 self.$elem = $(element);
232
233                 if(!self.$elem.attr('data-ui-initialized')) {
234                         console.log("Initializing dialog", this);
235                         self.initialize();
236                         self.$elem.attr('data-ui-initialized', true);
237                 }
238
239                 self.show();
240         };
241
242         GenericDialog.prototype = {
243
244                 /*
245                 * Steps to follow when the dialog in first loaded on page.
246                 */
247                 initialize: function(){
248                         var self = this;
249
250                         /* bind buttons */
251                         $('button[data-ui-action]', self.$elem).click(function(event) {
252                                 event.preventDefault();
253
254                                 var action = $(this).attr('data-ui-action');
255                                 console.log("Button pressed, action: ", action);
256
257                                 try {
258                                         self[action + "Action"].call(self);
259                                 } catch(e) {
260                                         console.log("Action failed:", e);
261                                         // always hide on cancel
262                                         if(action == 'cancel')
263                                                 self.hide();
264                                 }
265                         });
266                 },
267
268                 /*
269                  * Prepare dialog for user. Clear any unnessary data.
270                 */
271                 show: function() {
272                         $.blockUI({
273                                 message: this.$elem,
274                                 css: {
275                                         'top': '25%',
276                                         'left': '25%',
277                                         'width': '50%'
278                                 }
279                         });
280                 },
281
282                 hide: function(){
283                         $.unblockUI();
284                 },
285
286                 cancelAction: function() {
287                         this.hide();
288                 },
289
290                 doneAction: function() {
291                         this.hide();
292                 },
293
294                 clearForm: function() {
295                         $("*[data-ui-error-for]", this.$elem).text('');
296                 },
297
298                 reportErrors: function(errors) {
299                         var global = $("*[data-ui-error-for='__all__']", this.$elem);
300                         var unassigned = [];
301
302             $("*[data-ui-error-for]", this.$elem).text('');
303                         for (var field_name in errors)
304                         {
305                                 var span = $("*[data-ui-error-for='"+field_name+"']", this.$elem);
306
307                                 if(!span.length) {
308                                         unassigned.push(field_name);
309                                         continue;
310                                 }
311
312                                 span.text(errors[field_name].join(' '));
313                         }
314
315                         if(unassigned.length > 0)
316                                 global.text( global.text() + 'W formularzu wystąpiły błędy');
317                 }
318         };
319
320         $.wiki.cls.GenericDialog = GenericDialog;
321
322         $.wiki.showDialog = function(selector, options) {
323                 var elem = $(selector);
324
325                 if(elem.length != 1) {
326                         console.log("Failed to show dialog:", selector, elem);
327                         return false;
328                 }
329
330                 try {
331                     var klass = elem.attr('data-ui-jsclass');
332                         return new $.wiki.cls[klass](elem, options);
333                 } catch(e) {
334                         console.log("Failed to show dialog", selector, klass, e);
335                         return false;
336                 }
337         };
338
339 })(jQuery);