Fix for covers generation.
[redakcja.git] / src / 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 a.active").parent().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, base_id) {
69         var id = (''+klass)+'_' + base_id;
70         var $tab = $('<li class="nav-item" id="'+id+'" data-ui-related="'+base_id+'" data-ui-jsclass="'+klass+'" ><a href="#" class="nav-link">'
71                      + title + ' <span class="badge badge-danger tabclose">x</span></a></li>');
72         var $view = $('<div class="editor '+klass+'" id="'+base_id+'"> </div>');
73
74         this.perspectives[id] = new $.wiki[klass]({
75             doc: doc,
76             id: id,
77             base_id: base_id,
78         });
79
80         $('#tabs').append($tab);
81         $view.hide().appendTo('#editor');
82         return {
83             tab: $tab[0],
84             view: $view[0],
85         };
86     };
87
88     $.wiki.initTab = function(options) {
89         var klass = $(options.tab).attr('data-ui-jsclass');
90
91         let perspective = new $.wiki[klass]({
92             doc: options.doc,
93             id: $(options.tab).attr('id'),
94         });
95         $.wiki.perspectives[perspective.perspective_id] = perspective;
96         return perspective;
97     };
98
99     $.wiki.perspectiveForTab = function(tab) { // element or id
100         return this.perspectives[ $(tab).attr('id')];
101     }
102
103     $.wiki.exitTab = function(tab){
104         var self = this;
105         var $tab = $(tab);
106         if (!('.active', $tab).length) return;
107         $('.active', $tab).removeClass('active');
108         self.perspectives[$tab.attr('id')].onExit();
109         $('#' + $tab.attr('data-ui-related')).hide();
110     }
111
112     $.wiki.switchToTab = function(tab){
113         var self = this;
114         var $tab = $(tab);
115
116         // Create dynamic tabs (for diffs).
117         if ($tab.length != 1) {
118             let parts = tab.split('_');
119             if (parts.length > 1) {
120                 // TODO: register perspectives for it.
121                 if (parts[0] == '#DiffPerspective') {
122                     $tab = $($.wiki.DiffPerspective.openId(parts[1]));
123                 }
124             }
125         }
126
127         if($tab.length != 1)
128             $tab = $(DEFAULT_PERSPECTIVE);
129
130         var $old_a = $tab.closest('.tabs').find('.active');
131
132         $old_a.each(function(){
133             var tab = $(this).parent()
134             $(this).removeClass('active');
135             self.perspectives[tab.attr('id')].onExit();
136             $('#' + tab.attr('data-ui-related')).hide();
137         });
138
139         /* show new */
140         $('a', tab).addClass('active');
141         $('#' + $tab.attr('data-ui-related')).show();
142
143         console.log($tab);
144         console.log($.wiki.perspectives);
145
146         $.wiki.perspectives[$tab.attr('id')].onEnter();
147     };
148
149     /*
150      * Basic perspective.
151      */
152     $.wiki.Perspective = class Perspective {
153         constructor(options) {
154             this.doc = options.doc;
155             this.perspective_id = options.id || ''
156         };
157
158         config() {
159             return $.wiki.state.perspectives[this.perspective_id];
160         }
161
162         toString() {
163             return this.perspective_id;
164         }
165
166         dirty() {
167             return true;
168         }
169
170         onEnter() {
171             // called when perspective in initialized
172             if (!this.noupdate_hash_onenter) {
173                 document.location.hash = '#' + this.perspective_id;
174             }
175         }
176
177         onExit () {
178             // called when user switches to another perspective
179             if (!this.noupdate_hash_onenter) {
180                 document.location.hash = '';
181             }
182         }
183
184         destroy() {
185             // pass
186         }
187     }
188
189     /*
190      * Stub rendering (used in generating history)
191      */
192     $.wiki.renderStub = function(params)
193     {
194         params = $.extend({ 'filters': {} }, params);
195         var $elem = params.stub.clone();
196         $elem.removeClass('row-stub');
197         params.container.append($elem);
198
199         var populate = function($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         if ($elem.attr('data-stub-value')) populate($elem);
219         $('*[data-stub-value]', $elem).each(function() {populate($(this))});
220
221         $elem.show();
222         return $elem;
223     };
224
225     /*
226      * Dialogs
227      */
228     class GenericDialog {
229         constructor(element) {
230             if(!element) return;
231
232             var self = this;
233
234             self.$elem = $(element);
235
236             if(!self.$elem.attr('data-ui-initialized')) {
237                 console.log("Initializing dialog", this);
238                 self.initialize();
239                 self.$elem.attr('data-ui-initialized', true);
240             }
241
242             self.show();
243         }
244
245         /*
246          * Steps to follow when the dialog in first loaded on page.
247          */
248         initialize(){
249             var self = this;
250
251             /* bind buttons */
252             $('button[data-ui-action]', self.$elem).click(function(event) {
253                 event.preventDefault();
254
255                 var action = $(this).attr('data-ui-action');
256                 console.log("Button pressed, action: ", action);
257
258                 try {
259                     self[action + "Action"].call(self);
260                 } catch(e) {
261                     console.log("Action failed:", e);
262                     // always hide on cancel
263                     if(action == 'cancel')
264                         self.hide();
265                 }
266             });
267         }
268
269         /*
270          * Prepare dialog for user. Clear any unnessary data.
271          */
272         show() {
273             $.blockUI({
274                 message: this.$elem,
275                 css: {
276                     'top': '25%',
277                     'left': '25%',
278                     'width': '50%',
279                     'max-height': '75%',
280                     'overflow-y': 'scroll'
281                 }
282             });
283         }
284
285         hide() {
286             $.unblockUI();
287         }
288
289         cancelAction() {
290             this.hide();
291         }
292
293         doneAction() {
294             this.hide();
295         }
296
297         clearForm() {
298             $("*[data-ui-error-for]", this.$elem).text('');
299         }
300
301         reportErrors(errors) {
302             var global = $("*[data-ui-error-for='__all__']", this.$elem);
303             var unassigned = [];
304
305             $("*[data-ui-error-for]", this.$elem).text('');
306             for (var field_name in errors)
307             {
308                 var span = $("*[data-ui-error-for='"+field_name+"']", this.$elem);
309
310                 if(!span.length) {
311                     unassigned.push(errors[field_name]);
312                     continue;
313                 }
314
315                 span.text(errors[field_name].join(' '));
316             }
317
318             if(unassigned.length > 0)
319                 global.text(
320                     global.text() + 'Wystąpił błąd: ' + unassigned.join(', '));
321         }
322     }
323
324     $.wiki.cls.GenericDialog = GenericDialog;
325
326     $.wiki.showDialog = function(selector, options) {
327         var elem = $(selector);
328
329         if(elem.length != 1) {
330             console.log("Failed to show dialog:", selector, elem);
331             return false;
332         }
333
334         try {
335             var klass = elem.attr('data-ui-jsclass');
336             return new $.wiki.cls[klass](elem, options);
337         } catch(e) {
338             console.log("Failed to show dialog", selector, klass, e);
339             return false;
340         }
341     };
342
343     window.addEventListener("message", (event) => {
344         event.source.close()
345
346         $.ajax("/editor/editor-user-area/", {
347             success: function(d) {
348                 $("#user-area")[0].innerHTML = d;
349                 $('#history-view-editor').toggleClass('can-approve', $('#user-area #pubmark_dialog').length > 0);
350             }
351         });
352     }, false);
353
354     $("#login").click(function (e) {
355         e.preventDefault();
356         let h = 600;
357         let w = 500;
358         let x = window.screenX + (window.innerWidth - w) / 2;
359         let y = window.screenY + (window.innerHeight - h) / 2;
360         window.open(
361             "/accounts/login/?next=/editor/back",
362             "login-window",
363             "width=" + w + " height=" + h + " top=" + y + " left=" + x
364         );
365     });
366
367 })(jQuery);