temporary attributes for bible tags
[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 = new Date();
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 = new Date() - self.active < 30 * 1000;
220             $.ajax({
221                 method: "GET",
222                 url: reverse("ajax_document_rev", self.id),
223                 data: {
224                     'a': active,
225                     'new': 1,
226                 },
227                 dataType: 'json',
228                 success: function(data) {
229                     if (data == '') {
230                         if (params.error)
231                             params.error();
232                     }
233                     else {
234                         let people = $('<div>');
235                         data.people.forEach((p) => {
236                             let item = $('<img>');
237                             item.attr('src', p.gravatar),
238                             item.attr(
239                                 'title',
240                                 p.name  + ' (' +
241                                     (p.active ? 'akt.' : 'nieakt.') +
242                                     ' od ' + p.since + ')')
243                             if (p.active) {
244                                 item.addClass('active');
245                             }
246                             people.append(item);
247                         });
248                         $("#people").html(people);
249
250                         if (data.rev != self.revision) {
251                             params.outdated();
252                         }
253                     }
254                 }
255             });
256         }
257
258         refreshImageGallery(params) {
259             if (this.galleryLink) {
260                 params = $.extend({}, params, {
261                     url: reverse("ajax_document_gallery", this.galleryLink)
262                 });
263             }
264             this.refreshGallery(params);
265         }
266
267         refreshScansGallery(params) {
268             if (this.scansLink) {
269                 params = $.extend({}, params, {
270                     url: reverse("ajax_document_scans", this.scansLink)
271                 });
272                 this.refreshGallery(params);
273             } else {
274                 // Fallback to image gallery.
275                 this.refreshImageGallery(params)
276             }
277         }
278         
279         /*
280          * Fetch gallery
281          */
282         refreshGallery(params) {
283             params = $.extend({}, noops, params);
284             var self = this;
285             if (!params.url) {
286                 params.failure('Brak galerii.');
287                 return;
288             }
289             $.ajax({
290                 method: "GET",
291                 url: params.url,
292                 dataType: 'json',
293                 success: function(data) {
294                     params.success(data);
295                 },
296                 error: function(xhr) {
297                     switch (xhr.status) {
298                     case 403:
299                         var msg = 'Galerie dostępne tylko dla zalogowanych użytkowników.';
300                         break;
301                     case 404:
302                         var msg = "Nie znaleziono galerii.";
303                     default:
304                         var msg = "Nie udało się wczytać galerii.";
305                     }
306                     params.failure(msg);
307                 }
308             });
309         }
310
311         setGallery(gallery) {
312             this.galleryLink = gallery;
313             Api.setGallery(this.id, gallery);
314         }
315
316         setGalleryStart(start) {
317             this.galleryStart = start;
318             Api.setGalleryStart(this.id, start);
319         }
320
321         openGalleryEdit(start) {
322             Api.openGalleryEdit(this.bookSlug);
323         }
324
325         withGalleryList(callback) {
326             Api.withGalleryList(callback);
327         }
328         
329         /*
330          * Set document's text
331          */
332         setText(text, silent=false) {
333             if (text == this.text) return;
334             if (!silent) {
335                 $.wiki.undo.push(text);
336             }
337             this.text = text;
338             this.has_local_changes = true;
339         }
340
341         undo() {
342             let ctx = $.wiki.exitContext();
343             this.setText(
344                 $.wiki.undo.undo(),
345                 true
346             );
347             $.wiki.enterContext(ctx);
348         }
349         redo() {
350             let ctx = $.wiki.exitContext();
351             this.setText(
352                 $.wiki.undo.redo(),
353                 true
354             );
355             $.wiki.enterContext(ctx);
356         }
357
358         /*
359          * Save text back to the server
360          */
361         save(params) {
362             params = $.extend({}, noops, params);
363             var self = this;
364
365             if (!self.has_local_changes) {
366                 console.log("Abort: no changes.");
367                 return params['success'](self, false, "Nie ma zmian do zapisania.");
368             };
369
370             // Serialize form to dictionary
371             var data = {};
372             $.each(params['form'].serializeArray(), function() {
373                 data[this.name] = this.value;
374             });
375
376             data['textsave-text'] = self.text;
377
378             $.ajax({
379                 url: reverse("ajax_document_text", self.id),
380                 type: "POST",
381                 dataType: "json",
382                 data: data,
383                 success: function(data) {
384                     var changed = false;
385
386                     $('#header').removeClass('saving');
387
388                     if (data.text) {
389                         self.text = data.text;
390                         self.revision = data.revision;
391                         self.gallery = data.gallery;
392                         changed = true;
393                         self.triggerDocumentChanged();
394                     };
395
396                     params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
397                 },
398                 error: function(xhr) {
399                     if ($('#header').hasClass('saving')) {
400                         $('#header').removeClass('saving');
401                         $.blockUI({
402                             message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
403                         })
404                     }
405                     else {
406                         try {
407                             params['failure'](self, $.parseJSON(xhr.responseText));
408                         }
409                         catch (e) {
410                             params['failure'](self, {
411                                 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
412                             });
413                         };
414                     }
415                 }
416             });
417
418             $('#save-hide').click(function(){
419                 $('#header').addClass('saving');
420                 $.unblockUI();
421                 $.wiki.blocking.unblock();
422             });
423         } /* end of save() */
424
425         revertToVersion(params) {
426             var self = this;
427             params = $.extend({}, noops, params);
428
429             if (params.revision >= this.revision) {
430                 params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
431                 return;
432             }
433
434             // Serialize form to dictionary
435             var data = {};
436             $.each(params['form'].serializeArray(), function() {
437                 data[this.name] = this.value;
438             });
439
440             $.ajax({
441                 url: reverse("ajax_document_revert", self.id),
442                 type: "POST",
443                 dataType: "json",
444                 data: data,
445                 success: function(data) {
446                     if (data.text) {
447                         self.text = data.text;
448                         self.revision = data.revision;
449                         self.gallery = data.gallery;
450                         self.triggerDocumentChanged();
451
452                         params.success(self, "Udało się przywrócić wersję :)");
453                     }
454                     else {
455                         params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
456                     }
457                 },
458                 error: function(xhr) {
459                     params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
460                 }
461             });
462         }
463
464         pubmark(params) {
465             params = $.extend({}, noops, params);
466             var self = this;
467             var data = {
468                 "pubmark-id": self.id,
469             };
470
471             /* unpack form */
472             $.each(params.form.serializeArray(), function() {
473                 data[this.name] = this.value;
474             });
475
476             $.ajax({
477                 url: reverse("ajax_document_pubmark", self.id),
478                 type: "POST",
479                 dataType: "json",
480                 data: data,
481                 success: function(data) {
482                     params.success(self, data.message);
483                 },
484                 error: function(xhr) {
485                     if (xhr.status == 403 || xhr.status == 401) {
486                         params.failure(self, {
487                             "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
488                         });
489                     }
490                     else {
491                         try {
492                             params.failure(self, $.parseJSON(xhr.responseText));
493                         }
494                         catch (e) {
495                             params.failure(self, {
496                                 "__all__": ["Nie udało się - błąd serwera."]
497                             });
498                         };
499                     };
500                 }
501             });
502         }
503
504         refreshCover(params) {
505             var self = this;
506             var data = {
507                 xml: self.text // TODO: send just DC
508             };
509             $.ajax({
510                 url: reverse("ajax_cover_preview"),
511                 type: "POST",
512                 data: data,
513                 success: function(data) {
514                     params.success(data);
515                 },
516                 error: function(xhr) {
517                     // params.failure("Nie udało się odświeżyć okładki - błąd serwera.");
518                 }
519             });
520         }
521
522         getLength(params) {
523             params = $.extend({}, noops, params);
524             var xml = this.text.replace(/\/(\s+)/g, '<br />$1');
525             var parser = new DOMParser();
526             var doc = parser.parseFromString(xml, 'text/xml');
527             var error = $('parsererror', doc);
528
529             if (error.length > 0) {
530                 throw "Not an XML document.";
531             }
532             $.xmlns["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
533             $('rdf|RDF', doc).remove();
534             if (params.noFootnotes) {
535                 $('pa, pe, pr, pt', doc).remove();
536             }
537             if (params.noThemes) {
538                 $('motyw', doc).remove();
539             }
540             var text = $(doc).text();
541             text = $.trim(text.replace(/\s{2,}/g, ' '));
542             return text.length;
543         }
544
545         /* Temporary workaround for relative images. */
546         getBase() {
547             return '/media/dynamic/images/' + this.galleryLink + '/';
548         }
549     }
550
551     $.wikiapi.WikiDocument = WikiDocument;
552 })(jQuery);