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