a28839793b40b0679e4a5b902c28ab992b776d30
[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                 var metaComment = '<!--';
203                 metaComment += '\n\tgallery:' + self.galleryLink;
204                 metaComment += '\n-->\n'
205                 
206                 data['textsave-text'] = metaComment + self.text;
207                 
208                 $.ajax({
209                         url: reverse("ajax_document_text", self.id),
210                         type: "POST",
211                         dataType: "json",
212                         data: data,
213                         success: function(data) {
214                                 var changed = false;
215                                 
216                                 if (data.text) {
217                                         self.text = data.text;
218                                         self.revision = data.revision;
219                                         self.gallery = data.gallery;
220                                         changed = true;
221                                         self.triggerDocumentChanged();
222                                 };
223                                 
224                                 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
225                         },
226                         error: function(xhr) {
227                                 try {
228                                         params['failure'](self, $.parseJSON(xhr.responseText));
229                                 } 
230                                 catch (e) {
231                                         params['failure'](self, {
232                                                 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
233                                         });
234                                 };
235                                 
236                         }
237                 });
238         }; /* end of save() */
239         WikiDocument.prototype.publish = function(params) {
240                 params = $.extend({}, noops, params);
241                 var self = this;
242                 $.ajax({
243                         url: reverse("ajax_publish", self.id),
244                         type: "POST",
245                         dataType: "json",
246                         success: function(data) {
247                                 params.success(self, data);
248                         },
249                         error: function(xhr) {
250                                 if (xhr.status == 403 || xhr.status == 401) {
251                                         params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
252                                 }
253                                 else {
254                                         try {
255                                                 params.failure(self, xhr.responseText);
256                                         } 
257                                         catch (e) {
258                                                 params.failure(self, "Nie udało się - błąd serwera.");                                              
259                                         };                                      
260                                 };
261                                 
262                         }
263                 });
264         };
265         WikiDocument.prototype.setTag = function(params) {
266                 params = $.extend({}, noops, params);
267                 var self = this;
268                 var data = {
269                         "id": self.id,
270                 };
271                 /* unpack form */
272                 $.each(params.form.serializeArray(), function() {
273                         data[this.name] = this.value;
274                 });
275                 $.ajax({
276                         url: reverse("ajax_document_addtag", self.id),
277                         type: "POST",
278                         dataType: "json",
279                         data: data,
280                         success: function(data) {
281                                 params.success(self, data.message);
282                         },
283                         error: function(xhr) {                          
284                                 if (xhr.status == 403 || xhr.status == 401) {
285                                         params.failure(self, {
286                                                 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
287                                         });
288                                 }
289                                 else {
290                                         try {
291                                                 params.failure(self, $.parseJSON(xhr.responseText));
292                                         } 
293                                         catch (e) {
294                                                 params.failure(self, {
295                                                         "__all__": ["Nie udało się - błąd serwera."]
296                                                 });
297                                         };
298                                         
299                                 };
300                                 
301                         }
302                 });
303         };
304         $.wikiapi.WikiDocument = WikiDocument;
305 })(jQuery);