use proper commit id
[redakcja.git] / redakcja / static / js / wiki_img / 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 = "/images";
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_rev")
43             return base_path + "/" + arguments[1] + "/rev";
44
45                 if (vname == "ajax_document_addtag")
46                         return base_path + "/" + arguments[1] + "/tags";
47
48                 if (vname == "ajax_publish")
49                         return base_path + "/" + arguments[1] + "/publish";*/
50
51                 console.log("Couldn't reverse match:", vname);
52                 return "/404.html";
53         };
54
55         /*
56          * Document Abstraction
57          */
58         function WikiDocument(element_id) {
59                 var meta = $('#' + element_id);
60                 this.id = meta.attr('data-document-name');
61
62                 this.revision = $("*[data-key='revision']", meta).text();
63         this.commit = $("*[data-key='commit']", meta).text();
64                 this.readonly = !!$("*[data-key='readonly']", meta).text();
65
66                 this.galleryLink = $("*[data-key='gallery']", meta).text();
67                 this.galleryImages = [];
68                 this.text = null;
69                 this.has_local_changes = false;
70                 this._lock = -1;
71                 this._context_lock = -1;
72                 this._lock_count = 0;
73         };
74
75         WikiDocument.prototype.triggerDocumentChanged = function() {
76                 $(document).trigger('wlapi_document_changed', this);
77         };
78         /*
79          * Fetch text of this document.
80          */
81         WikiDocument.prototype.fetch = function(params) {
82                 params = $.extend({}, noops, params);
83                 var self = this;
84                 $.ajax({
85                         method: "GET",
86                         url: reverse("ajax_document_text", self.id),
87                         data: {"commit": self.commit},
88                         dataType: 'json',
89                         success: function(data) {
90                                 var changed = false;
91
92                                 if (self.text === null || self.commit !== data.commit) {
93                                         self.text = data.text;
94                                         if (self.text === '') {
95                                             self.text = '<obraz></obraz>';
96                                         }
97                                         self.revision = data.revision;
98                     self.commit = data.commit;
99                                         changed = true;
100                                         self.triggerDocumentChanged();
101                                 };
102
103                                 self.has_local_changes = false;
104                                 params['success'](self, changed);
105                         },
106                         error: function() {
107                                 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
108                         }
109                 });
110         };
111
112         /*
113          * Set document's text
114          */
115         WikiDocument.prototype.setText = function(text) {
116                 this.text = text;
117                 this.has_local_changes = true;
118         };
119
120         /*
121          * Save text back to the server
122          */
123         WikiDocument.prototype.save = function(params) {
124                 params = $.extend({}, noops, params);
125                 var self = this;
126
127                 if (!self.has_local_changes) {
128                         console.log("Abort: no changes.");
129                         return params['success'](self, false, "Nie ma zmian do zapisania.");
130                 };
131
132                 // Serialize form to dictionary
133                 var data = {};
134                 $.each(params['form'].serializeArray(), function() {
135                         data[this.name] = this.value;
136                 });
137
138                 data['textsave-text'] = self.text;
139
140                 $.ajax({
141                         url: reverse("ajax_document_text", self.id),
142                         type: "POST",
143                         dataType: "json",
144                         data: data,
145                         success: function(data) {
146                                 var changed = false;
147
148                 $('#header').removeClass('saving');
149
150                                 if (data.text) {
151                                         self.text = data.text;
152                                         self.revision = data.revision;
153                     self.commit = data.commit;
154                                         changed = true;
155                                         self.triggerDocumentChanged();
156                                 };
157
158                                 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
159                         },
160                         error: function(xhr) {
161                 if ($('#header').hasClass('saving')) {
162                     $('#header').removeClass('saving');
163                     $.blockUI({
164                         message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
165                     })
166                 }
167                 else {
168                     try {
169                         params['failure'](self, $.parseJSON(xhr.responseText));
170                     }
171                     catch (e) {
172                         params['failure'](self, {
173                             "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
174                         });
175                     };
176                 }
177
178                         }
179                 });
180
181         $('#save-hide').click(function(){
182             $('#header').addClass('saving');
183             $.unblockUI();
184             $.wiki.blocking.unblock();
185         });
186         }; /* end of save() */
187
188         WikiDocument.prototype.publish = function(params) {
189                 params = $.extend({}, noops, params);
190                 var self = this;
191                 $.ajax({
192                         url: reverse("ajax_publish", self.id),
193                         type: "POST",
194                         dataType: "json",
195                         success: function(data) {
196                                 params.success(self, data);
197                         },
198                         error: function(xhr) {
199                                 if (xhr.status == 403 || xhr.status == 401) {
200                                         params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
201                                 }
202                                 else {
203                                         try {
204                                                 params.failure(self, xhr.responseText);
205                                         }
206                                         catch (e) {
207                                                 params.failure(self, "Nie udało się - błąd serwera.");
208                                         };
209                                 };
210
211                         }
212                 });
213         };
214         WikiDocument.prototype.setTag = function(params) {
215                 params = $.extend({}, noops, params);
216                 var self = this;
217                 var data = {
218                         "addtag-id": self.id,
219                 };
220
221                 /* unpack form */
222                 $.each(params.form.serializeArray(), function() {
223                         data[this.name] = this.value;
224                 });
225
226                 $.ajax({
227                         url: reverse("ajax_document_addtag", self.id),
228                         type: "POST",
229                         dataType: "json",
230                         data: data,
231                         success: function(data) {
232                                 params.success(self, data.message);
233                         },
234                         error: function(xhr) {
235                                 if (xhr.status == 403 || xhr.status == 401) {
236                                         params.failure(self, {
237                                                 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
238                                         });
239                                 }
240                                 else {
241                                         try {
242                                                 params.failure(self, $.parseJSON(xhr.responseText));
243                                         }
244                                         catch (e) {
245                                                 params.failure(self, {
246                                                         "__all__": ["Nie udało się - błąd serwera."]
247                                                 });
248                                         };
249                                 };
250                         }
251                 });
252         };
253
254     WikiDocument.prototype.getImageItems = function(tag) {
255         var self = this;
256
257         var parser = new DOMParser();
258         var doc = parser.parseFromString(self.text, 'text/xml');
259         var error = $('parsererror', doc);
260
261         if (error.length != 0) {
262             return null;
263         }
264
265         var a = [];
266         $(tag, doc).each(function(i, e) {
267             var $e = $(e);
268             a.push([
269                 $e.text(),
270                 $e.attr('x1'),
271                 $e.attr('y1'),
272                 $e.attr('x2'),
273                 $e.attr('y2')
274             ]);
275         });
276
277         return a;
278     }
279
280     WikiDocument.prototype.setImageItems = function(tag, items) {
281         var self = this;
282
283         var parser = new DOMParser();
284         var doc = parser.parseFromString(self.text, 'text/xml');
285         var serializer = new XMLSerializer();
286         var error = $('parsererror', doc);
287
288         if (error.length != 0) {
289             return null;
290         }
291
292         $(tag, doc).remove();
293         $root = $(doc.firstChild);
294         $.each(items, function(i, e) {
295             var el = $(doc.createElement(tag));
296             el.text(e[0]);
297             if (e[1] !== null) {
298                 el.attr('x1', e[1]);
299                 el.attr('y1', e[2]);
300                 el.attr('x2', e[3]);
301                 el.attr('y2', e[4]);
302             }
303             $root.append(el);
304         });
305         self.setText(serializer.serializeToString(doc));
306     }
307
308
309         $.wikiapi.WikiDocument = WikiDocument;
310 })(jQuery);