3 var noop = function() {
10 * Return absolute reverse path of given named view. (at least he have it
11 * hard-coded in one place)
13 * TODO: think of a way, not to hard-code it here ;)
17 var vname = arguments[0];
18 var base_path = "/documents";
20 if (vname == "ajax_document_text") {
21 var path = "/text/" + arguments[1] + '/';
23 if (arguments[2] !== undefined)
24 path += arguments[2] + '/';
26 return base_path + path;
29 if (vname == "ajax_document_revert") {
30 return base_path + "/revert/" + arguments[1] + '/';
34 if (vname == "ajax_document_history") {
36 return base_path + "/history/" + arguments[1] + '/';
39 if (vname == "ajax_document_gallery") {
41 return base_path + "/gallery/" + arguments[1] + '/';
44 if (vname == "ajax_document_diff")
45 return base_path + "/diff/" + arguments[1] + '/';
47 if (vname == "ajax_document_rev")
48 return base_path + "/rev/" + arguments[1] + '/';
50 if (vname == "ajax_document_addtag")
51 return base_path + "/tag/" + arguments[1] + '/';
53 if (vname == "ajax_document_pubmark")
54 return base_path + "/pubmark/" + arguments[1] + '/';
56 if (vname == "ajax_publish")
57 return base_path + "/publish/" + arguments[1] + '/';
59 console.log("Couldn't reverse match:", vname);
64 * Document Abstraction
66 function WikiDocument(element_id) {
67 var meta = $('#' + element_id);
68 this.id = meta.attr('data-book') + '/' + meta.attr('data-chunk');
70 this.revision = $("*[data-key='revision']", meta).text();
71 this.readonly = !!$("*[data-key='readonly']", meta).text();
73 this.galleryLink = $("*[data-key='gallery']", meta).text();
75 var diff = $("*[data-key='diff']", meta).text();
76 diff = diff.split(',');
77 if (diff.length == 2 && diff[0] < diff[1])
79 else if (diff.length == 1) {
80 diff = parseInt(diff);
82 this.diff = [diff - 1, diff];
85 this.galleryImages = [];
87 this.has_local_changes = false;
89 this._context_lock = -1;
93 WikiDocument.prototype.triggerDocumentChanged = function() {
94 $(document).trigger('wlapi_document_changed', this);
97 * Fetch text of this document.
99 WikiDocument.prototype.fetch = function(params) {
100 params = $.extend({}, noops, params);
104 url: reverse("ajax_document_text", self.id),
105 data: {"revision": self.revision},
107 success: function(data) {
110 if (self.text === null || self.revision !== data.revision) {
111 self.text = data.text;
112 self.revision = data.revision;
113 self.gallery = data.gallery;
115 self.triggerDocumentChanged();
118 self.has_local_changes = false;
119 params['success'](self, changed);
122 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
127 * Fetch history of this document.
129 * from - First revision to fetch (default = 0) upto - Last revision to
130 * fetch (default = tip)
133 WikiDocument.prototype.fetchHistory = function(params) {
134 /* this doesn't modify anything, so no locks */
135 params = $.extend({}, noops, params);
139 url: reverse("ajax_document_history", self.id),
142 "from": params['from'],
143 "upto": params['upto']
145 success: function(data) {
146 params['success'](self, data);
149 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
153 WikiDocument.prototype.fetchDiff = function(params) {
154 /* this doesn't modify anything, so no locks */
157 'from': self.revision,
162 url: reverse("ajax_document_diff", self.id),
165 "from": params['from'],
168 success: function(data) {
169 params['success'](self, data);
172 params['failure'](self, "Nie udało się wczytać porównania wersji.");
177 WikiDocument.prototype.checkRevision = function(params) {
178 /* this doesn't modify anything, so no locks */
182 url: reverse("ajax_document_rev", self.id),
184 success: function(data) {
189 else if (data != self.revision)
198 WikiDocument.prototype.refreshGallery = function(params) {
199 params = $.extend({}, noops, params);
203 url: reverse("ajax_document_gallery", self.galleryLink),
206 success: function(data) {
207 self.galleryImages = data;
208 params['success'](self, data);
211 self.galleryImages = [];
212 params['failure'](self, "<p>Nie udało się wczytać galerii pod nazwą: '" + self.galleryLink + "'.</p>");
218 * Set document's text
220 WikiDocument.prototype.setText = function(text) {
222 this.has_local_changes = true;
226 * Set document's gallery link
228 WikiDocument.prototype.setGalleryLink = function(gallery) {
229 this.galleryLink = gallery;
230 this.has_local_changes = true;
234 * Save text back to the server
236 WikiDocument.prototype.save = function(params) {
237 params = $.extend({}, noops, params);
240 if (!self.has_local_changes) {
241 console.log("Abort: no changes.");
242 return params['success'](self, false, "Nie ma zmian do zapisania.");
245 // Serialize form to dictionary
247 $.each(params['form'].serializeArray(), function() {
248 data[this.name] = this.value;
251 data['textsave-text'] = self.text;
254 url: reverse("ajax_document_text", self.id),
258 success: function(data) {
261 $('#header').removeClass('saving');
264 self.text = data.text;
265 self.revision = data.revision;
266 self.gallery = data.gallery;
268 self.triggerDocumentChanged();
271 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
273 error: function(xhr) {
274 if ($('#header').hasClass('saving')) {
275 $('#header').removeClass('saving');
277 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
282 params['failure'](self, $.parseJSON(xhr.responseText));
285 params['failure'](self, {
286 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
294 $('#save-hide').click(function(){
295 $('#header').addClass('saving');
297 $.wiki.blocking.unblock();
299 }; /* end of save() */
301 WikiDocument.prototype.revertToVersion = function(params) {
303 params = $.extend({}, noops, params);
305 if (params.revision >= this.revision) {
306 params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
310 // Serialize form to dictionary
312 $.each(params['form'].serializeArray(), function() {
313 data[this.name] = this.value;
317 url: reverse("ajax_document_revert", self.id),
321 success: function(data) {
323 self.text = data.text;
324 self.revision = data.revision;
325 self.gallery = data.gallery;
326 self.triggerDocumentChanged();
328 params.success(self, "Udało się przywrócić wersję :)");
331 params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
334 error: function(xhr) {
335 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
340 WikiDocument.prototype.publish = function(params) {
341 params = $.extend({}, noops, params);
344 url: reverse("ajax_publish", self.id),
347 success: function(data) {
348 params.success(self, data);
350 error: function(xhr) {
351 if (xhr.status == 403 || xhr.status == 401) {
352 params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
356 params.failure(self, xhr.responseText);
359 params.failure(self, "Nie udało się - błąd serwera.");
366 WikiDocument.prototype.setTag = function(params) {
367 params = $.extend({}, noops, params);
370 "addtag-id": self.id,
374 $.each(params.form.serializeArray(), function() {
375 data[this.name] = this.value;
379 url: reverse("ajax_document_addtag", self.id),
383 success: function(data) {
384 params.success(self, data.message);
386 error: function(xhr) {
387 if (xhr.status == 403 || xhr.status == 401) {
388 params.failure(self, {
389 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
394 params.failure(self, $.parseJSON(xhr.responseText));
397 params.failure(self, {
398 "__all__": ["Nie udało się - błąd serwera."]
406 WikiDocument.prototype.pubmark = function(params) {
407 params = $.extend({}, noops, params);
410 "pubmark-id": self.id,
414 $.each(params.form.serializeArray(), function() {
415 data[this.name] = this.value;
419 url: reverse("ajax_document_pubmark", self.id),
423 success: function(data) {
424 params.success(self, data.message);
426 error: function(xhr) {
427 if (xhr.status == 403 || xhr.status == 401) {
428 params.failure(self, {
429 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
434 params.failure(self, $.parseJSON(xhr.responseText));
437 params.failure(self, {
438 "__all__": ["Nie udało się - błąd serwera."]
446 $.wikiapi.WikiDocument = WikiDocument;