fixes
[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
36                         return base_path + "/history/" + arguments[1] + '/';
37                 }
38
39                 if (vname == "ajax_document_gallery") {
40
41                         return base_path + "/gallery/" + arguments[1] + '/';
42                 }
43
44                 if (vname == "ajax_document_diff")
45                         return base_path + "/diff/" + arguments[1] + '/';
46
47         if (vname == "ajax_document_rev")
48             return base_path + "/rev/" + arguments[1] + '/';
49
50                 if (vname == "ajax_document_pubmark")
51                         return base_path + "/pubmark/" + arguments[1] + '/';
52
53                 if (vname == "ajax_cover_preview")
54                         return "/cover/preview/";
55
56                 console.log("Couldn't reverse match:", vname);
57                 return "/404.html";
58         };
59
60         /*
61          * Document Abstraction
62          */
63         function WikiDocument(element_id) {
64                 var meta = $('#' + element_id);
65                 this.id = meta.attr('data-chunk-id');
66
67                 this.revision = $("*[data-key='revision']", meta).text();
68                 this.readonly = !!$("*[data-key='readonly']", meta).text();
69
70                 this.galleryLink = $("*[data-key='gallery']", meta).text();
71         this.galleryStart = parseInt($("*[data-key='gallery-start']", meta).text());
72             this.fullUri = $("*[data-key='full-uri']", meta).text();
73
74         var diff = $("*[data-key='diff']", meta).text();
75         if (diff) {
76             diff = diff.split(',');
77             if (diff.length == 2 && diff[0] < diff[1])
78                 this.diff = diff;
79             else if (diff.length == 1) {
80                 diff = parseInt(diff);
81                 if (diff != NaN)
82                     this.diff = [diff - 1, diff];
83             }
84         }
85
86                 this.galleryImages = [];
87                 this.text = null;
88                 this.has_local_changes = false;
89                 this.active = true;
90                 this._lock = -1;
91                 this._context_lock = -1;
92                 this._lock_count = 0;
93         };
94
95         WikiDocument.prototype.triggerDocumentChanged = function() {
96                 $(document).trigger('wlapi_document_changed', this);
97         };
98         /*
99          * Fetch text of this document.
100          */
101         WikiDocument.prototype.fetch = function(params) {
102                 params = $.extend({}, noops, params);
103                 var self = this;
104                 $.ajax({
105                         method: "GET",
106                         url: reverse("ajax_document_text", self.id),
107                         data: {"revision": self.revision},
108                         dataType: 'json',
109                         success: function(data) {
110                                 var changed = false;
111
112                                 if (self.text === null || self.revision !== data.revision) {
113                                         self.text = data.text;
114                                         self.revision = data.revision;
115                                         self.gallery = data.gallery;
116                                         changed = true;
117                                         self.triggerDocumentChanged();
118                                 };
119
120                                 self.has_local_changes = false;
121                                 params['success'](self, changed);
122                         },
123                         error: function() {
124                                 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
125                         }
126                 });
127         };
128     /*
129      * Fetch history of this document.
130      *
131      */
132     WikiDocument.prototype.fetchHistory = function(params) {
133         /* this doesn't modify anything, so no locks */
134         params = $.extend({}, noops, params);
135         var self = this;
136         $.ajax({
137             method: "GET",
138             url: reverse("ajax_document_history", self.id) + "?before=" + params.before,
139             dataType: 'json',
140             success: function(data) {
141                 params['success'](self, data);
142             },
143             error: function() {
144                 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
145             }
146         });
147     };
148         WikiDocument.prototype.fetchDiff = function(params) {
149                 /* this doesn't modify anything, so no locks */
150                 var self = this;
151                 params = $.extend({
152                         'from': self.revision,
153                         'to': self.revision
154                 }, noops, params);
155                 $.ajax({
156                         method: "GET",
157                         url: reverse("ajax_document_diff", self.id),
158                         dataType: 'html',
159                         data: {
160                                 "from": params['from'],
161                                 "to": params['to']
162                         },
163                         success: function(data) {
164                                 params['success'](self, data);
165                         },
166                         error: function() {
167                                 params['failure'](self, "Nie udało się wczytać porównania wersji.");
168                         }
169                 });
170         };
171
172     WikiDocument.prototype.checkRevision = function(params) {
173         /* this doesn't modify anything, so no locks */
174         var self = this;
175         let active = self.active;
176         self.active = false;
177         $.ajax({
178             method: "GET",
179             url: reverse("ajax_document_rev", self.id),
180             data: {
181                 'a': active,
182             },
183             dataType: 'text',
184             success: function(data) {
185                 if (data == '') {
186                     if (params.error)
187                         params.error();
188                 }
189                 else if (data != self.revision)
190                     params.outdated();
191             }
192         });
193     };
194
195         /*
196          * Fetch gallery
197          */
198         WikiDocument.prototype.refreshGallery = function(params) {
199                 params = $.extend({}, noops, params);
200                 var self = this;
201                 if (!self.galleryLink) {
202                         params['failure'](self, 'Brak galerii.');
203                         return;
204                 }
205                 $.ajax({
206                         method: "GET",
207                         url: reverse("ajax_document_gallery", self.galleryLink),
208                         dataType: 'json',
209                         // data: {},
210                         success: function(data) {
211                                 self.galleryImages = data;
212                                 params['success'](self, data);
213                         },
214                         error: function(xhr) {
215                 switch (xhr.status) {
216                     case 403:
217                         var msg = 'Galerie dostępne tylko dla zalogowanych użytkowników.';
218                         break;
219                     case 404:
220                         var msg = "Nie znaleziono galerii o nazwie: '" + self.galleryLink + "'.";
221                     default:
222                         var msg = "Nie udało się wczytać galerii o nazwie: '" + self.galleryLink + "'.";
223                 }
224                                 self.galleryImages = [];
225                                 params['failure'](self, msg);
226                         }
227                 });
228         };
229
230     /*
231      * Set document's text
232      */
233     WikiDocument.prototype.setText = function(text, setter) {
234         if (text == this.text) return;
235         
236         this.text = text;
237         this.has_local_changes = true;
238
239     };
240
241     
242         /*
243          * Save text back to the server
244          */
245         WikiDocument.prototype.save = function(params) {
246                 params = $.extend({}, noops, params);
247                 var self = this;
248
249                 if (!self.has_local_changes) {
250                         console.log("Abort: no changes.");
251                         return params['success'](self, false, "Nie ma zmian do zapisania.");
252                 };
253
254                 // Serialize form to dictionary
255                 var data = {};
256                 $.each(params['form'].serializeArray(), function() {
257                         data[this.name] = this.value;
258                 });
259
260                 data['textsave-text'] = self.text;
261
262                 $.ajax({
263                         url: reverse("ajax_document_text", self.id),
264                         type: "POST",
265                         dataType: "json",
266                         data: data,
267                         success: function(data) {
268                                 var changed = false;
269
270                 $('#header').removeClass('saving');
271
272                                 if (data.text) {
273                                         self.text = data.text;
274                                         self.revision = data.revision;
275                                         self.gallery = data.gallery;
276                                         changed = true;
277                                         self.triggerDocumentChanged();
278                                 };
279
280                                 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
281                         },
282                         error: function(xhr) {
283                 if ($('#header').hasClass('saving')) {
284                     $('#header').removeClass('saving');
285                     $.blockUI({
286                         message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
287                     })
288                 }
289                 else {
290                     try {
291                         params['failure'](self, $.parseJSON(xhr.responseText));
292                     }
293                     catch (e) {
294                         params['failure'](self, {
295                             "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
296                         });
297                     };
298                 }
299
300                         }
301                 });
302
303         $('#save-hide').click(function(){
304             $('#header').addClass('saving');
305             $.unblockUI();
306             $.wiki.blocking.unblock();
307         });
308         }; /* end of save() */
309
310     WikiDocument.prototype.revertToVersion = function(params) {
311         var self = this;
312         params = $.extend({}, noops, params);
313
314         if (params.revision >= this.revision) {
315             params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
316             return;
317         }
318
319         // Serialize form to dictionary
320         var data = {};
321         $.each(params['form'].serializeArray(), function() {
322             data[this.name] = this.value;
323         });
324
325         $.ajax({
326             url: reverse("ajax_document_revert", self.id),
327             type: "POST",
328             dataType: "json",
329             data: data,
330             success: function(data) {
331                 if (data.text) {
332                     self.text = data.text;
333                     self.revision = data.revision;
334                     self.gallery = data.gallery;
335                     self.triggerDocumentChanged();
336
337                     params.success(self, "Udało się przywrócić wersję :)");
338                 }
339                 else {
340                     params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
341                 }
342             },
343             error: function(xhr) {
344                 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
345             }
346         });
347     };
348
349         WikiDocument.prototype.pubmark = function(params) {
350                 params = $.extend({}, noops, params);
351                 var self = this;
352                 var data = {
353                         "pubmark-id": self.id,
354                 };
355
356                 /* unpack form */
357                 $.each(params.form.serializeArray(), function() {
358                         data[this.name] = this.value;
359                 });
360
361                 $.ajax({
362                         url: reverse("ajax_document_pubmark", self.id),
363                         type: "POST",
364                         dataType: "json",
365                         data: data,
366                         success: function(data) {
367                                 params.success(self, data.message);
368                         },
369                         error: function(xhr) {
370                                 if (xhr.status == 403 || xhr.status == 401) {
371                                         params.failure(self, {
372                                                 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
373                                         });
374                                 }
375                                 else {
376                                         try {
377                                                 params.failure(self, $.parseJSON(xhr.responseText));
378                                         }
379                                         catch (e) {
380                                                 params.failure(self, {
381                                                         "__all__": ["Nie udało się - błąd serwera."]
382                                                 });
383                                         };
384                                 };
385                         }
386                 });
387         };
388
389         WikiDocument.prototype.refreshCover = function(params) {
390         var self = this;
391                 var data = {
392                         xml: self.text // TODO: send just DC
393                 };
394         $.ajax({
395             url: reverse("ajax_cover_preview"),
396             type: "POST",
397             data: data,
398             success: function(data) {
399                 params.success(data);
400             },
401             error: function(xhr) {
402                 // params.failure("Nie udało się odświeżyć okładki - błąd serwera.");
403             }
404         });
405         };
406
407
408     WikiDocument.prototype.getLength = function(params) {
409         params = $.extend({}, noops, params);
410         var xml = this.text.replace(/\/(\s+)/g, '<br />$1');
411         var parser = new DOMParser();
412         var doc = parser.parseFromString(xml, 'text/xml');
413         var error = $('parsererror', doc);
414
415         if (error.length > 0) {
416             throw "Not an XML document.";
417         }
418         $.xmlns["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; 
419         $('rdf|RDF', doc).remove();
420         if (params.noFootnotes) {
421             $('pa, pe, pr, pt', doc).remove();
422         }
423         if (params.noThemes) {
424             $('motyw', doc).remove();
425         }
426         var text = $(doc).text();
427         text = $.trim(text.replace(/\s{2,}/g, ' '));
428         return text.length;
429     }
430
431     /* Temporary workaround for relative images. */
432     WikiDocument.prototype.getBase = function() {
433         return '/media/dynamic/images/' + this.galleryLink + '/';
434     };
435
436         $.wikiapi.WikiDocument = WikiDocument;
437 })(jQuery);