Use unmanaged keys instead of some magic.
[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
73         var diff = $("*[data-key='diff']", meta).text();
74         if (diff) {
75             diff = diff.split(',');
76             if (diff.length == 2 && diff[0] < diff[1])
77                 this.diff = diff;
78             else if (diff.length == 1) {
79                 diff = parseInt(diff);
80                 if (diff != NaN)
81                     this.diff = [diff - 1, diff];
82             }
83         }
84
85                 this.galleryImages = [];
86                 this.text = null;
87                 this.has_local_changes = false;
88                 this._lock = -1;
89                 this._context_lock = -1;
90                 this._lock_count = 0;
91         };
92
93         WikiDocument.prototype.triggerDocumentChanged = function() {
94                 $(document).trigger('wlapi_document_changed', this);
95         };
96         /*
97          * Fetch text of this document.
98          */
99         WikiDocument.prototype.fetch = function(params) {
100                 params = $.extend({}, noops, params);
101                 var self = this;
102                 $.ajax({
103                         method: "GET",
104                         url: reverse("ajax_document_text", self.id),
105                         data: {"revision": self.revision},
106                         dataType: 'json',
107                         success: function(data) {
108                                 var changed = false;
109
110                                 if (self.text === null || self.revision !== data.revision) {
111                                         self.text = data.text;
112                                         self.revision = data.revision;
113                                         self.gallery = data.gallery;
114                                         changed = true;
115                                         self.triggerDocumentChanged();
116                                 };
117
118                                 self.has_local_changes = false;
119                                 params['success'](self, changed);
120                         },
121                         error: function() {
122                                 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
123                         }
124                 });
125         };
126         /*
127          * Fetch history of this document.
128          *
129          * from - First revision to fetch (default = 0) upto - Last revision to
130          * fetch (default = tip)
131          *
132          */
133         WikiDocument.prototype.fetchHistory = function(params) {
134                 /* this doesn't modify anything, so no locks */
135                 params = $.extend({}, noops, params);
136                 var self = this;
137                 $.ajax({
138                         method: "GET",
139                         url: reverse("ajax_document_history", self.id),
140                         dataType: 'json',
141                         data: {
142                                 "from": params['from'],
143                                 "upto": params['upto']
144                         },
145                         success: function(data) {
146                                 params['success'](self, data);
147                         },
148                         error: function() {
149                                 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
150                         }
151                 });
152         };
153         WikiDocument.prototype.fetchDiff = function(params) {
154                 /* this doesn't modify anything, so no locks */
155                 var self = this;
156                 params = $.extend({
157                         'from': self.revision,
158                         'to': self.revision
159                 }, noops, params);
160                 $.ajax({
161                         method: "GET",
162                         url: reverse("ajax_document_diff", self.id),
163                         dataType: 'html',
164                         data: {
165                                 "from": params['from'],
166                                 "to": params['to']
167                         },
168                         success: function(data) {
169                                 params['success'](self, data);
170                         },
171                         error: function() {
172                                 params['failure'](self, "Nie udało się wczytać porównania wersji.");
173                         }
174                 });
175         };
176
177     WikiDocument.prototype.checkRevision = function(params) {
178         /* this doesn't modify anything, so no locks */
179         var self = this;
180         $.ajax({
181             method: "GET",
182             url: reverse("ajax_document_rev", self.id),
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) {
234                 return this.setDocumentProperty('text', text);
235         };
236
237         /*
238          * Set document's gallery link
239          */
240         WikiDocument.prototype.setGalleryLink = function(gallery) {
241                 return this.setDocumentProperty('galleryLink', gallery);
242         };
243
244         /*
245          * Set document's property
246          */
247         WikiDocument.prototype.setDocumentProperty = function(property, value) {
248                 if(this[property] !== value) {
249                         this[property] = value;
250                         this.has_local_changes = true;
251                 }
252         };
253
254         /*
255          * Save text back to the server
256          */
257         WikiDocument.prototype.save = function(params) {
258                 params = $.extend({}, noops, params);
259                 var self = this;
260
261                 if (!self.has_local_changes) {
262                         console.log("Abort: no changes.");
263                         return params['success'](self, false, "Nie ma zmian do zapisania.");
264                 };
265
266                 // Serialize form to dictionary
267                 var data = {};
268                 $.each(params['form'].serializeArray(), function() {
269                         data[this.name] = this.value;
270                 });
271
272                 data['textsave-text'] = self.text;
273
274                 $.ajax({
275                         url: reverse("ajax_document_text", self.id),
276                         type: "POST",
277                         dataType: "json",
278                         data: data,
279                         success: function(data) {
280                                 var changed = false;
281
282                 $('#header').removeClass('saving');
283
284                                 if (data.text) {
285                                         self.text = data.text;
286                                         self.revision = data.revision;
287                                         self.gallery = data.gallery;
288                                         changed = true;
289                                         self.triggerDocumentChanged();
290                                 };
291
292                                 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
293                         },
294                         error: function(xhr) {
295                 if ($('#header').hasClass('saving')) {
296                     $('#header').removeClass('saving');
297                     $.blockUI({
298                         message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
299                     })
300                 }
301                 else {
302                     try {
303                         params['failure'](self, $.parseJSON(xhr.responseText));
304                     }
305                     catch (e) {
306                         params['failure'](self, {
307                             "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
308                         });
309                     };
310                 }
311
312                         }
313                 });
314
315         $('#save-hide').click(function(){
316             $('#header').addClass('saving');
317             $.unblockUI();
318             $.wiki.blocking.unblock();
319         });
320         }; /* end of save() */
321
322     WikiDocument.prototype.revertToVersion = function(params) {
323         var self = this;
324         params = $.extend({}, noops, params);
325
326         if (params.revision >= this.revision) {
327             params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
328             return;
329         }
330
331         // Serialize form to dictionary
332         var data = {};
333         $.each(params['form'].serializeArray(), function() {
334             data[this.name] = this.value;
335         });
336
337         $.ajax({
338             url: reverse("ajax_document_revert", self.id),
339             type: "POST",
340             dataType: "json",
341             data: data,
342             success: function(data) {
343                 if (data.text) {
344                     self.text = data.text;
345                     self.revision = data.revision;
346                     self.gallery = data.gallery;
347                     self.triggerDocumentChanged();
348
349                     params.success(self, "Udało się przywrócić wersję :)");
350                 }
351                 else {
352                     params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
353                 }
354             },
355             error: function(xhr) {
356                 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
357             }
358         });
359     };
360
361         WikiDocument.prototype.pubmark = function(params) {
362                 params = $.extend({}, noops, params);
363                 var self = this;
364                 var data = {
365                         "pubmark-id": self.id,
366                 };
367
368                 /* unpack form */
369                 $.each(params.form.serializeArray(), function() {
370                         data[this.name] = this.value;
371                 });
372
373                 $.ajax({
374                         url: reverse("ajax_document_pubmark", self.id),
375                         type: "POST",
376                         dataType: "json",
377                         data: data,
378                         success: function(data) {
379                                 params.success(self, data.message);
380                         },
381                         error: function(xhr) {
382                                 if (xhr.status == 403 || xhr.status == 401) {
383                                         params.failure(self, {
384                                                 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
385                                         });
386                                 }
387                                 else {
388                                         try {
389                                                 params.failure(self, $.parseJSON(xhr.responseText));
390                                         }
391                                         catch (e) {
392                                                 params.failure(self, {
393                                                         "__all__": ["Nie udało się - błąd serwera."]
394                                                 });
395                                         };
396                                 };
397                         }
398                 });
399         };
400
401         WikiDocument.prototype.refreshCover = function(params) {
402         var self = this;
403                 var data = {
404                         xml: self.text // TODO: send just DC
405                 };
406         $.ajax({
407             url: reverse("ajax_cover_preview"),
408             type: "POST",
409             data: data,
410             success: function(data) {
411                 params.success(data);
412             },
413             error: function(xhr) {
414                 // params.failure("Nie udało się odświeżyć okładki - błąd serwera.");
415             }
416         });
417         };
418
419
420     WikiDocument.prototype.getLength = function(params) {
421         params = $.extend({}, noops, params);
422         var xml = this.text.replace(/\/(\s+)/g, '<br />$1');
423         var parser = new DOMParser();
424         var doc = parser.parseFromString(xml, 'text/xml');
425         var error = $('parsererror', doc);
426
427         if (error.length > 0) {
428             throw "Not an XML document.";
429         }
430         $.xmlns["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; 
431         $('rdf|RDF', doc).remove();
432         if (params.noFootnotes) {
433             $('pa, pe, pr, pt', doc).remove();
434         }
435         if (params.noThemes) {
436             $('motyw', doc).remove();
437         }
438         var text = $(doc).text();
439         text = $.trim(text.replace(/\s{2,}/g, ' '));
440         return text.length;
441     }
442
443
444         $.wikiapi.WikiDocument = WikiDocument;
445 })(jQuery);