fix demo retries
[redakcja.git] / src / redakcja / static / js / wiki / wikiapi.js
1 (function($) {
2     $.wikiapi = {};
3     var noop = function() {
4     };
5     var noops = {
6         success: noop,
7         failure: noop
8     };
9     /*
10      * Return absolute reverse path of given named view. (at least he have it
11      * hard-coded in one place)
12      *
13      * TODO: think of a way, not to hard-code it here ;)
14      *
15      */
16     function reverse() {
17         var vname = arguments[0];
18         var base_path = "/editor";
19
20         if (vname == "ajax_document_text") {
21             var path = "/text/" + arguments[1] + '/';
22
23             if (arguments[2] !== undefined)
24                 path += arguments[2] + '/';
25
26             return base_path + path;
27         }
28
29         if (vname == "ajax_document_revert") {
30             return base_path + "/revert/" + arguments[1] + '/';
31         }
32
33
34         if (vname == "ajax_document_history") {
35             return base_path + "/history/" + arguments[1] + '/';
36         }
37
38         if (vname == "ajax_document_scans") {
39             return base_path + "/scans/" + arguments[1] + '/';
40         }
41         if (vname == "ajax_document_gallery") {
42             return base_path + "/gallery/" + arguments[1] + '/';
43         }
44
45         if (vname == "ajax_document_diff")
46             return base_path + "/diff/" + arguments[1] + '/';
47
48         if (vname == "ajax_document_rev")
49             return base_path + "/rev/" + arguments[1] + '/';
50
51         if (vname == "ajax_document_pubmark")
52             return base_path + "/pubmark/" + arguments[1] + '/';
53
54         if (vname == "ajax_cover_preview")
55             return "/cover/preview/";
56
57         console.log("Couldn't reverse match:", vname);
58         return "/404.html";
59     };
60     class Api {
61         static csrf = $('input[name="csrfmiddlewaretoken"]').val();
62
63         // TODO: Add waiting marker, error reporting.
64         static post(url, data) {
65             data['csrfmiddlewaretoken'] = this.csrf;
66             $.ajax({
67                 url: url,
68                 type: "POST",
69                 data: data
70             });
71         }
72
73         static get(url, callback) {
74             $.ajax({
75                 url: url,
76                 type: "GET",
77                 success: function(data) {
78                     callback(data);
79                 },
80             });
81         }
82
83         static setGallery(id, gallery) {
84             this.post(
85                 '/editor/set-gallery/' + id + '/',
86                 {gallery: gallery}
87             )
88         }
89         static setGalleryStart(id, start) {
90             this.post(
91                 '/editor/set-gallery-start/' + id + '/',
92                 {start: start}
93             )
94         }
95
96         static openGalleryEdit(bookSlug) {
97             window.open(
98                 '/documents/book/' + bookSlug + '/gallery/',
99                 '_blank'
100             ).focus();
101         }
102
103         static withGalleryList(callback) {
104             this.get(
105                 '/editor/galleries/',
106                 callback
107             );
108         }
109     }
110
111     /*
112      * Document Abstraction
113      */
114     class WikiDocument {
115         constructor(element_id) {
116             var meta = $('#' + element_id);
117             this.id = meta.attr('data-chunk-id');
118
119             this.revision = $("*[data-key='revision']", meta).text();
120             this.readonly = !!$("*[data-key='readonly']", meta).text();
121
122             this.bookSlug = $("*[data-key='book-slug']", meta).text();
123             this.scansLink = $("*[data-key='scans']", meta).text();
124             this.galleryLink = $("*[data-key='gallery']", meta).text();
125             this.galleryStart = parseInt($("*[data-key='gallery-start']", meta).text());
126             this.fullUri = $("*[data-key='full-uri']", meta).text();
127
128             this.text = null;
129             this.has_local_changes = false;
130             this.active = true;
131             this._lock = -1;
132             this._context_lock = -1;
133             this._lock_count = 0;
134         };
135
136         triggerDocumentChanged() {
137             $(document).trigger('wlapi_document_changed', this);
138         }
139
140         /*
141          * Fetch text of this document.
142          */
143         fetch(params) {
144             params = $.extend({}, noops, params);
145             var self = this;
146             $.ajax({
147                 method: "GET",
148                 url: reverse("ajax_document_text", self.id),
149                 data: {"revision": self.revision},
150                 dataType: 'json',
151                 success: function(data) {
152                     var changed = false;
153
154                     if (self.text === null || self.revision !== data.revision) {
155                         self.text = data.text;
156                         $.wiki.undo.push(data.text);
157                         self.revision = data.revision;
158                         self.gallery = data.gallery;
159                         changed = true;
160                         self.triggerDocumentChanged();
161                     };
162
163                     self.has_local_changes = false;
164                     params['success'](self, changed);
165                 },
166                 error: function() {
167                     params['failure'](self, "Nie udało się wczytać treści dokumentu.");
168                 }
169             });
170         }
171
172         /*
173          * Fetch history of this document.
174          */
175         fetchHistory(params) {
176             /* this doesn't modify anything, so no locks */
177             params = $.extend({}, noops, params);
178             var self = this;
179             $.ajax({
180                 method: "GET",
181                 url: reverse("ajax_document_history", self.id) + "?before=" + params.before,
182                 dataType: 'json',
183                 success: function(data) {
184                     params['success'](self, data);
185                 },
186                 error: function() {
187                     params['failure'](self, "Nie udało się wczytać historii dokumentu.");
188                 }
189             });
190         }
191
192         fetchDiff(params) {
193             /* this doesn't modify anything, so no locks */
194             var self = this;
195             params = $.extend({
196                 'from': self.revision,
197                 'to': self.revision
198             }, noops, params);
199             $.ajax({
200                 method: "GET",
201                 url: reverse("ajax_document_diff", self.id),
202                 dataType: 'html',
203                 data: {
204                     "from": params['from'],
205                     "to": params['to']
206                 },
207                 success: function(data) {
208                     params['success'](self, data);
209                 },
210                 error: function() {
211                     params['failure'](self, "Nie udało się wczytać porównania wersji.");
212                 }
213             });
214         }
215
216         checkRevision(params) {
217             /* this doesn't modify anything, so no locks */
218             var self = this;
219             let active = self.active;
220             self.active = false;
221             $.ajax({
222                 method: "GET",
223                 url: reverse("ajax_document_rev", self.id),
224                 data: {
225                     'a': active,
226                 },
227                 dataType: 'text',
228                 success: function(data) {
229                     if (data == '') {
230                         if (params.error)
231                             params.error();
232                     }
233                     else if (data != self.revision)
234                         params.outdated();
235                 }
236             });
237         }
238
239         refreshImageGallery(params) {
240             if (this.galleryLink) {
241                 params = $.extend({}, params, {
242                     url: reverse("ajax_document_gallery", this.galleryLink)
243                 });
244             }
245             this.refreshGallery(params);
246         }
247
248         refreshScansGallery(params) {
249             if (this.scansLink) {
250                 params = $.extend({}, params, {
251                     url: reverse("ajax_document_scans", this.scansLink)
252                 });
253                 this.refreshGallery(params);
254             } else {
255                 // Fallback to image gallery.
256                 this.refreshImageGallery(params)
257             }
258         }
259         
260         /*
261          * Fetch gallery
262          */
263         refreshGallery(params) {
264             params = $.extend({}, noops, params);
265             var self = this;
266             if (!params.url) {
267                 params.failure('Brak galerii.');
268                 return;
269             }
270             $.ajax({
271                 method: "GET",
272                 url: params.url,
273                 dataType: 'json',
274                 success: function(data) {
275                     params.success(data);
276                 },
277                 error: function(xhr) {
278                     switch (xhr.status) {
279                     case 403:
280                         var msg = 'Galerie dostępne tylko dla zalogowanych użytkowników.';
281                         break;
282                     case 404:
283                         var msg = "Nie znaleziono galerii.";
284                     default:
285                         var msg = "Nie udało się wczytać galerii.";
286                     }
287                     params.failure(msg);
288                 }
289             });
290         }
291
292         setGallery(gallery) {
293             this.galleryLink = gallery;
294             Api.setGallery(this.id, gallery);
295         }
296
297         setGalleryStart(start) {
298             this.galleryStart = start;
299             Api.setGalleryStart(this.id, start);
300         }
301
302         openGalleryEdit(start) {
303             Api.openGalleryEdit(this.bookSlug);
304         }
305
306         withGalleryList(callback) {
307             Api.withGalleryList(callback);
308         }
309         
310         /*
311          * Set document's text
312          */
313         setText(text, silent=false) {
314             if (text == this.text) return;
315             if (!silent) {
316                 $.wiki.undo.push(text);
317             }
318             this.text = text;
319             this.has_local_changes = true;
320         }
321
322         undo() {
323             let ctx = $.wiki.exitContext();
324             this.setText(
325                 $.wiki.undo.undo(),
326                 true
327             );
328             $.wiki.enterContext(ctx);
329         }
330         redo() {
331             let ctx = $.wiki.exitContext();
332             this.setText(
333                 $.wiki.undo.redo(),
334                 true
335             );
336             $.wiki.enterContext(ctx);
337         }
338
339         /*
340          * Save text back to the server
341          */
342         save(params) {
343             params = $.extend({}, noops, params);
344             var self = this;
345
346             if (!self.has_local_changes) {
347                 console.log("Abort: no changes.");
348                 return params['success'](self, false, "Nie ma zmian do zapisania.");
349             };
350
351             // Serialize form to dictionary
352             var data = {};
353             $.each(params['form'].serializeArray(), function() {
354                 data[this.name] = this.value;
355             });
356
357             data['textsave-text'] = self.text;
358
359             $.ajax({
360                 url: reverse("ajax_document_text", self.id),
361                 type: "POST",
362                 dataType: "json",
363                 data: data,
364                 success: function(data) {
365                     var changed = false;
366
367                     $('#header').removeClass('saving');
368
369                     if (data.text) {
370                         self.text = data.text;
371                         self.revision = data.revision;
372                         self.gallery = data.gallery;
373                         changed = true;
374                         self.triggerDocumentChanged();
375                     };
376
377                     params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
378                 },
379                 error: function(xhr) {
380                     if ($('#header').hasClass('saving')) {
381                         $('#header').removeClass('saving');
382                         $.blockUI({
383                             message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
384                         })
385                     }
386                     else {
387                         try {
388                             params['failure'](self, $.parseJSON(xhr.responseText));
389                         }
390                         catch (e) {
391                             params['failure'](self, {
392                                 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
393                             });
394                         };
395                     }
396                 }
397             });
398
399             $('#save-hide').click(function(){
400                 $('#header').addClass('saving');
401                 $.unblockUI();
402                 $.wiki.blocking.unblock();
403             });
404         } /* end of save() */
405
406         revertToVersion(params) {
407             var self = this;
408             params = $.extend({}, noops, params);
409
410             if (params.revision >= this.revision) {
411                 params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
412                 return;
413             }
414
415             // Serialize form to dictionary
416             var data = {};
417             $.each(params['form'].serializeArray(), function() {
418                 data[this.name] = this.value;
419             });
420
421             $.ajax({
422                 url: reverse("ajax_document_revert", self.id),
423                 type: "POST",
424                 dataType: "json",
425                 data: data,
426                 success: function(data) {
427                     if (data.text) {
428                         self.text = data.text;
429                         self.revision = data.revision;
430                         self.gallery = data.gallery;
431                         self.triggerDocumentChanged();
432
433                         params.success(self, "Udało się przywrócić wersję :)");
434                     }
435                     else {
436                         params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
437                     }
438                 },
439                 error: function(xhr) {
440                     params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
441                 }
442             });
443         }
444
445         pubmark(params) {
446             params = $.extend({}, noops, params);
447             var self = this;
448             var data = {
449                 "pubmark-id": self.id,
450             };
451
452             /* unpack form */
453             $.each(params.form.serializeArray(), function() {
454                 data[this.name] = this.value;
455             });
456
457             $.ajax({
458                 url: reverse("ajax_document_pubmark", self.id),
459                 type: "POST",
460                 dataType: "json",
461                 data: data,
462                 success: function(data) {
463                     params.success(self, data.message);
464                 },
465                 error: function(xhr) {
466                     if (xhr.status == 403 || xhr.status == 401) {
467                         params.failure(self, {
468                             "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
469                         });
470                     }
471                     else {
472                         try {
473                             params.failure(self, $.parseJSON(xhr.responseText));
474                         }
475                         catch (e) {
476                             params.failure(self, {
477                                 "__all__": ["Nie udało się - błąd serwera."]
478                             });
479                         };
480                     };
481                 }
482             });
483         }
484
485         refreshCover(params) {
486             var self = this;
487             var data = {
488                 xml: self.text // TODO: send just DC
489             };
490             $.ajax({
491                 url: reverse("ajax_cover_preview"),
492                 type: "POST",
493                 data: data,
494                 success: function(data) {
495                     params.success(data);
496                 },
497                 error: function(xhr) {
498                     // params.failure("Nie udało się odświeżyć okładki - błąd serwera.");
499                 }
500             });
501         }
502
503         getLength(params) {
504             params = $.extend({}, noops, params);
505             var xml = this.text.replace(/\/(\s+)/g, '<br />$1');
506             var parser = new DOMParser();
507             var doc = parser.parseFromString(xml, 'text/xml');
508             var error = $('parsererror', doc);
509
510             if (error.length > 0) {
511                 throw "Not an XML document.";
512             }
513             $.xmlns["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
514             $('rdf|RDF', doc).remove();
515             if (params.noFootnotes) {
516                 $('pa, pe, pr, pt', doc).remove();
517             }
518             if (params.noThemes) {
519                 $('motyw', doc).remove();
520             }
521             var text = $(doc).text();
522             text = $.trim(text.replace(/\s{2,}/g, ' '));
523             return text.length;
524         }
525
526         /* Temporary workaround for relative images. */
527         getBase() {
528             return '/media/dynamic/images/' + this.galleryLink + '/';
529         }
530     }
531
532     $.wikiapi.WikiDocument = WikiDocument;
533 })(jQuery);