Priviliged users can now add tags. Also, some minor cleanups in JS.
[redakcja.git] / platforma / static / js / wiki / wikiapi.js
1 (function($) {
2
3         $.wikiapi = {};
4         var noop = function() {};
5         var noops = {success: noop, failure: noop};
6
7         /*
8          * Return absolute reverse path of given named view.
9          * (at least he have it hard-coded in one place)
10          *
11          * TODO: think of a way, not to hard-code it here ;)
12          *
13          */
14          function reverse() {
15                 var vname = arguments[0];
16
17                 if(vname == "ajax_document_text") {
18                         var path = "/" + arguments[1] + "/text";
19                         if (arguments[2] !== undefined)
20                                 path += "/" + arguments[2];
21                         return path;
22                 }
23
24                 if (vname == "ajax_document_history") {
25                         return "/" + arguments[1] + "/history";
26                 }
27
28                 if (vname == "ajax_document_gallery") {
29                         return "/gallery/" + arguments[1];
30                 }
31
32                 if(vname == "ajax_document_diff")
33                         return "/" + arguments[1] + "/diff";
34
35                 if(vname == "ajax_document_addtag")
36                         return "/" + arguments[1] + "/tags";
37
38                 console.log("Couldn't reverse match:", vname);
39                 return "/404.html";
40         };
41
42         /*
43          * Document Abstraction
44          */
45         function WikiDocument(element_id) {
46                 var meta = $('#'+element_id);
47
48                 this.id = meta.attr('data-document-name');
49                 this.revision = $("*[data-key='revision']", meta).text();
50                 this.galleryLink = $("*[data-key='gallery']", meta).text();
51                 this.galleryImages = [];
52                 this.text = null;
53
54                 this.has_local_changes = false;
55                 this._lock = -1;
56                 this._context_lock = -1;
57                 this._lock_count = 0;
58         };
59
60
61         WikiDocument.prototype.triggerDocumentChanged = function() {
62                 $(document).trigger('wlapi_document_changed', this);
63         };
64
65         /*
66          * Fetch text of this document.
67          */
68         WikiDocument.prototype.fetch = function(params) {
69                 params = $.extend({}, noops, params);
70                 var self = this;
71
72                 $.ajax({
73                         method: "GET",
74                         url: reverse("ajax_document_text", self.id),
75                         dataType: 'json',
76                         success: function(data)
77                         {
78                                 var changed = false;
79
80                                 if (self.text === null || self.revision !== data.revision) {
81                                         self.text = data.text;
82                                         self.revision = data.revision;
83                                         self.gallery = data.gallery;
84                                         changed = true;
85                                         self.triggerDocumentChanged();
86                                 };
87
88                                 self.has_local_changes = false;
89                                 params['success'](self, changed);
90                         },
91                         error: function() {
92                                 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
93                         }
94                 });
95         };
96
97         /*
98          * Fetch history of this document.
99          *
100          * from - First revision to fetch (default = 0)
101          * upto - Last revision to fetch (default = tip)
102          *
103          */
104         WikiDocument.prototype.fetchHistory = function(params) {
105                 /* this doesn't modify anything, so no locks */
106                 params = $.extend({}, noops, params);
107                 var self = this;
108
109                 $.ajax({
110                         method: "GET",
111                         url: reverse("ajax_document_history", self.id),
112                         dataType: 'json',
113                         data: {"from": params['from'], "upto": params['upto']},
114                         success: function(data) {
115                                 params['success'](self, data);
116                         },
117                         error: function() {
118                                 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
119                         }
120                 });
121         };
122
123         WikiDocument.prototype.fetchDiff = function(params) {
124                 /* this doesn't modify anything, so no locks */
125                 var self = this;
126
127                 params = $.extend({
128                         'from': self.revision,
129                         'to': self.revision
130                 }, noops, params);
131
132                 $.ajax({
133                         method: "GET",
134                         url: reverse("ajax_document_diff", self.id),
135                         dataType: 'html',
136                         data: {"from": params['from'], "to": params['to']},
137                         success: function(data) {
138                                 params['success'](self, data);
139                         },
140                         error: function() {
141                                 params['failure'](self, "Nie udało się wczytać porównania wersji.");
142                         }
143                 });
144         };
145
146         /*
147          * Fetch gallery
148          */
149         WikiDocument.prototype.refreshGallery = function(params) {
150                 params = $.extend({}, noops, params);
151                 var self = this;
152
153                 $.ajax({
154                         method: "GET",
155                         url: reverse("ajax_document_gallery", self.galleryLink),
156                         dataType: 'json',
157                         // data: {},
158                         success: function(data) {
159                                 self.galleryImages = data;
160                                 params['success'](self, data);
161                         },
162                         error: function() {
163                                 self.galleryImages = [];
164                                 params['failure'](self, "<p>Nie udało się wczytać gallerii pod nazwą: '" + self.galleryLink + "'.</p>");
165                         }
166                 });
167         };
168
169         /*
170          * Set document's text
171          */
172         WikiDocument.prototype.setText = function(text) {
173                 this.text = text;
174                 this.has_local_changes = true;
175         };
176
177         /*
178          * Set document's gallery link
179          */
180         WikiDocument.prototype.setGalleryLink = function(gallery) {
181                 this.galleryLink = gallery;
182                 this.has_local_changes = true;
183         };
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.text = metaComment + self.text;
208                 data.comment = data.comment;
209
210                 $.ajax({
211                         url: reverse("ajax_document_text", self.id),
212                         type: "POST",
213                         dataType: "json",
214                         data: data,
215                         success: function(data){
216                                 var changed = false;
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                                 params['success'](self, changed,
225                                         ((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, {"__message": "<p>Nie udało się zapisać - błąd serwera.</p>"});
233                                 };
234                         }
235                 });
236         }; /* end of save() */
237
238         WikiDocument.prototype.setTag = function(params) {
239                 params = $.extend({}, noops, params);
240                 var self = this;
241
242                 var data = {
243                         "id": self.id,
244                 };
245
246                 /* unpack form */
247                 $.each(params.form.serializeArray(), function() {
248                         data[this.name] = this.value;
249                 });
250
251                 $.ajax({
252                         url: reverse("ajax_document_addtag", self.id),
253                         type: "POST",
254                         dataType: "json",
255                         data: data,
256                         success: function(data){
257                                 params.success(self, data.message);
258                         },
259                         error: function(xhr) {
260                                 if (xhr.status == 403 || xhr.status == 401) {
261                                         params.failure(self, {
262                                                 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
263                                         });
264                                 }
265                                 else {
266                                         try {
267                                                 params.failure(self, $.parseJSON(xhr.responseText));
268                                         }
269                                         catch (e) {
270                                                 params.failure(self, {
271                                                         "__all__": ["Nie udało się - błąd serwera."]
272                                                 });
273                                         };
274                                 };
275                         }
276                 });
277         };
278
279         $.wikiapi.WikiDocument = WikiDocument;
280
281 })(jQuery);