Fixed #623 (annotation and emphasis css)
[redakcja.git] / 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 = "/documents";
19
20                 if (vname == "ajax_document_text") {
21                         var path = "/" + arguments[1] + "/text";
22
23                 if (arguments[2] !== undefined)
24                                 path += "/" + arguments[2];
25
26                         return base_path + path;
27                 }
28
29                 if (vname == "ajax_document_history") {
30
31                         return base_path + "/" + arguments[1] + "/history";
32                 }
33
34                 if (vname == "ajax_document_gallery") {
35
36                         return base_path + "/" + arguments[1] + "/gallery";
37                 }
38
39                 if (vname == "ajax_document_diff")
40                         return base_path + "/" + arguments[1] + "/diff";
41
42                 if (vname == "ajax_document_addtag")
43                         return base_path + "/" + arguments[1] + "/tags";
44
45                 if (vname == "ajax_publish")
46                         return base_path + "/" + arguments[1] + "/publish";
47
48                 console.log("Couldn't reverse match:", vname);
49                 return "/404.html";
50         };
51
52         /*
53          * Document Abstraction
54          */
55         function WikiDocument(element_id) {
56                 var meta = $('#' + element_id);
57                 this.id = meta.attr('data-document-name');
58
59                 this.revision = $("*[data-key='revision']", meta).text();
60                 this.readonly = !!$("*[data-key='readonly']", meta).text();
61
62                 this.galleryLink = $("*[data-key='gallery']", meta).text();
63                 this.galleryImages = [];
64                 this.text = null;
65                 this.has_local_changes = false;
66                 this._lock = -1;
67                 this._context_lock = -1;
68                 this._lock_count = 0;
69         };
70
71         WikiDocument.prototype.triggerDocumentChanged = function() {
72                 $(document).trigger('wlapi_document_changed', this);
73         };
74         /*
75          * Fetch text of this document.
76          */
77         WikiDocument.prototype.fetch = function(params) {
78                 params = $.extend({}, noops, params);
79                 var self = this;
80                 $.ajax({
81                         method: "GET",
82                         url: reverse("ajax_document_text", self.id),
83                         data: {"revision": self.revision},
84                         dataType: 'json',
85                         success: function(data) {
86                                 var changed = false;
87
88                                 if (self.text === null || self.revision !== data.revision) {
89                                         self.text = data.text;
90                                         self.revision = data.revision;
91                                         self.gallery = data.gallery;
92                                         changed = true;
93                                         self.triggerDocumentChanged();
94                                 };
95
96                                 self.has_local_changes = false;
97                                 params['success'](self, changed);
98                         },
99                         error: function() {
100                                 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
101                         }
102                 });
103         };
104         /*
105          * Fetch history of this document.
106          *
107          * from - First revision to fetch (default = 0) upto - Last revision to
108          * fetch (default = tip)
109          *
110          */
111         WikiDocument.prototype.fetchHistory = function(params) {
112                 /* this doesn't modify anything, so no locks */
113                 params = $.extend({}, noops, params);
114                 var self = this;
115                 $.ajax({
116                         method: "GET",
117                         url: reverse("ajax_document_history", self.id),
118                         dataType: 'json',
119                         data: {
120                                 "from": params['from'],
121                                 "upto": params['upto']
122                         },
123                         success: function(data) {
124                                 params['success'](self, data);
125                         },
126                         error: function() {
127                                 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
128                         }
129                 });
130         };
131         WikiDocument.prototype.fetchDiff = function(params) {
132                 /* this doesn't modify anything, so no locks */
133                 var self = this;
134                 params = $.extend({
135                         'from': self.revision,
136                         'to': self.revision
137                 }, noops, params);
138                 $.ajax({
139                         method: "GET",
140                         url: reverse("ajax_document_diff", self.id),
141                         dataType: 'html',
142                         data: {
143                                 "from": params['from'],
144                                 "to": params['to']
145                         },
146                         success: function(data) {
147                                 params['success'](self, data);
148                         },
149                         error: function() {
150                                 params['failure'](self, "Nie udało się wczytać porównania wersji.");
151                         }
152                 });
153         };
154
155         /*
156          * Fetch gallery
157          */
158         WikiDocument.prototype.refreshGallery = function(params) {
159                 params = $.extend({}, noops, params);
160                 var self = this;
161                 $.ajax({
162                         method: "GET",
163                         url: reverse("ajax_document_gallery", self.galleryLink),
164                         dataType: 'json',
165                         // data: {},
166                         success: function(data) {
167                                 self.galleryImages = data;
168                                 params['success'](self, data);
169                         },
170                         error: function() {
171                                 self.galleryImages = [];
172                                 params['failure'](self, "<p>Nie udało się wczytać gallerii pod nazwą: '" + self.galleryLink + "'.</p>");
173                         }
174                 });
175         };
176
177         /*
178          * Set document's text
179          */
180         WikiDocument.prototype.setText = function(text) {
181                 this.text = text;
182                 this.has_local_changes = true;
183         };
184
185         /*
186          * Set document's gallery link
187          */
188         WikiDocument.prototype.setGalleryLink = function(gallery) {
189                 this.galleryLink = gallery;
190                 this.has_local_changes = true;
191         };
192
193         /*
194          * Save text back to the server
195          */
196         WikiDocument.prototype.save = function(params) {
197                 params = $.extend({}, noops, params);
198                 var self = this;
199
200                 if (!self.has_local_changes) {
201                         console.log("Abort: no changes.");
202                         return params['success'](self, false, "Nie ma zmian do zapisania.");
203                 };
204
205                 // Serialize form to dictionary
206                 var data = {};
207                 $.each(params['form'].serializeArray(), function() {
208                         data[this.name] = this.value;
209                 });
210
211                 var metaComment = '<!--';
212                 metaComment += '\n\tgallery:' + self.galleryLink;
213                 metaComment += '\n-->\n'
214
215                 data['textsave-text'] = metaComment + self.text;
216
217                 $.ajax({
218                         url: reverse("ajax_document_text", self.id),
219                         type: "POST",
220                         dataType: "json",
221                         data: data,
222                         success: function(data) {
223                                 var changed = false;
224
225                                 if (data.text) {
226                                         self.text = data.text;
227                                         self.revision = data.revision;
228                                         self.gallery = data.gallery;
229                                         changed = true;
230                                         self.triggerDocumentChanged();
231                                 };
232
233                                 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
234                         },
235                         error: function(xhr) {
236                                 try {
237                                         params['failure'](self, $.parseJSON(xhr.responseText));
238                                 }
239                                 catch (e) {
240                                         params['failure'](self, {
241                                                 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
242                                         });
243                                 };
244
245                         }
246                 });
247         }; /* end of save() */
248
249         WikiDocument.prototype.publish = function(params) {
250                 params = $.extend({}, noops, params);
251                 var self = this;
252                 $.ajax({
253                         url: reverse("ajax_publish", self.id),
254                         type: "POST",
255                         dataType: "json",
256                         success: function(data) {
257                                 params.success(self, data);
258                         },
259                         error: function(xhr) {
260                                 if (xhr.status == 403 || xhr.status == 401) {
261                                         params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
262                                 }
263                                 else {
264                                         try {
265                                                 params.failure(self, xhr.responseText);
266                                         }
267                                         catch (e) {
268                                                 params.failure(self, "Nie udało się - błąd serwera.");
269                                         };
270                                 };
271
272                         }
273                 });
274         };
275         WikiDocument.prototype.setTag = function(params) {
276                 params = $.extend({}, noops, params);
277                 var self = this;
278                 var data = {
279                         "addtag-id": self.id,
280                 };
281
282                 /* unpack form */
283                 $.each(params.form.serializeArray(), function() {
284                         data[this.name] = this.value;
285                 });
286
287                 $.ajax({
288                         url: reverse("ajax_document_addtag", self.id),
289                         type: "POST",
290                         dataType: "json",
291                         data: data,
292                         success: function(data) {
293                                 params.success(self, data.message);
294                         },
295                         error: function(xhr) {
296                                 if (xhr.status == 403 || xhr.status == 401) {
297                                         params.failure(self, {
298                                                 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
299                                         });
300                                 }
301                                 else {
302                                         try {
303                                                 params.failure(self, $.parseJSON(xhr.responseText));
304                                         }
305                                         catch (e) {
306                                                 params.failure(self, {
307                                                         "__all__": ["Nie udało się - błąd serwera."]
308                                                 });
309                                         };
310                                 };
311                         }
312                 });
313         };
314
315         $.wikiapi.WikiDocument = WikiDocument;
316 })(jQuery);