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();
74 this.galleryImages = [];
76 this.has_local_changes = false;
78 this._context_lock = -1;
82 WikiDocument.prototype.triggerDocumentChanged = function() {
83 $(document).trigger('wlapi_document_changed', this);
86 * Fetch text of this document.
88 WikiDocument.prototype.fetch = function(params) {
89 params = $.extend({}, noops, params);
93 url: reverse("ajax_document_text", self.id),
94 data: {"revision": self.revision},
96 success: function(data) {
99 if (self.text === null || self.revision !== data.revision) {
100 self.text = data.text;
101 self.revision = data.revision;
102 self.gallery = data.gallery;
104 self.triggerDocumentChanged();
107 self.has_local_changes = false;
108 params['success'](self, changed);
111 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
116 * Fetch history of this document.
118 * from - First revision to fetch (default = 0) upto - Last revision to
119 * fetch (default = tip)
122 WikiDocument.prototype.fetchHistory = function(params) {
123 /* this doesn't modify anything, so no locks */
124 params = $.extend({}, noops, params);
128 url: reverse("ajax_document_history", self.id),
131 "from": params['from'],
132 "upto": params['upto']
134 success: function(data) {
135 params['success'](self, data);
138 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
142 WikiDocument.prototype.fetchDiff = function(params) {
143 /* this doesn't modify anything, so no locks */
146 'from': self.revision,
151 url: reverse("ajax_document_diff", self.id),
154 "from": params['from'],
157 success: function(data) {
158 params['success'](self, data);
161 params['failure'](self, "Nie udało się wczytać porównania wersji.");
166 WikiDocument.prototype.checkRevision = function(params) {
167 /* this doesn't modify anything, so no locks */
171 url: reverse("ajax_document_rev", self.id),
173 success: function(data) {
178 else if (data != self.revision)
187 WikiDocument.prototype.refreshGallery = function(params) {
188 params = $.extend({}, noops, params);
192 url: reverse("ajax_document_gallery", self.galleryLink),
195 success: function(data) {
196 self.galleryImages = data;
197 params['success'](self, data);
200 self.galleryImages = [];
201 params['failure'](self, "<p>Nie udało się wczytać galerii pod nazwą: '" + self.galleryLink + "'.</p>");
207 * Set document's text
209 WikiDocument.prototype.setText = function(text) {
211 this.has_local_changes = true;
215 * Set document's gallery link
217 WikiDocument.prototype.setGalleryLink = function(gallery) {
218 this.galleryLink = gallery;
219 this.has_local_changes = true;
223 * Save text back to the server
225 WikiDocument.prototype.save = function(params) {
226 params = $.extend({}, noops, params);
229 if (!self.has_local_changes) {
230 console.log("Abort: no changes.");
231 return params['success'](self, false, "Nie ma zmian do zapisania.");
234 // Serialize form to dictionary
236 $.each(params['form'].serializeArray(), function() {
237 data[this.name] = this.value;
240 data['textsave-text'] = self.text;
243 url: reverse("ajax_document_text", self.id),
247 success: function(data) {
250 $('#header').removeClass('saving');
253 self.text = data.text;
254 self.revision = data.revision;
255 self.gallery = data.gallery;
257 self.triggerDocumentChanged();
260 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
262 error: function(xhr) {
263 if ($('#header').hasClass('saving')) {
264 $('#header').removeClass('saving');
266 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
271 params['failure'](self, $.parseJSON(xhr.responseText));
274 params['failure'](self, {
275 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
283 $('#save-hide').click(function(){
284 $('#header').addClass('saving');
286 $.wiki.blocking.unblock();
288 }; /* end of save() */
290 WikiDocument.prototype.revertToVersion = function(params) {
292 params = $.extend({}, noops, params);
294 if (params.revision >= this.revision) {
295 params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
299 // Serialize form to dictionary
301 $.each(params['form'].serializeArray(), function() {
302 data[this.name] = this.value;
306 url: reverse("ajax_document_revert", self.id),
310 success: function(data) {
312 self.text = data.text;
313 self.revision = data.revision;
314 self.gallery = data.gallery;
315 self.triggerDocumentChanged();
317 params.success(self, "Udało się przywrócić wersję :)");
320 params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
323 error: function(xhr) {
324 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
329 WikiDocument.prototype.publish = function(params) {
330 params = $.extend({}, noops, params);
333 url: reverse("ajax_publish", self.id),
336 success: function(data) {
337 params.success(self, data);
339 error: function(xhr) {
340 if (xhr.status == 403 || xhr.status == 401) {
341 params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
345 params.failure(self, xhr.responseText);
348 params.failure(self, "Nie udało się - błąd serwera.");
355 WikiDocument.prototype.setTag = function(params) {
356 params = $.extend({}, noops, params);
359 "addtag-id": self.id,
363 $.each(params.form.serializeArray(), function() {
364 data[this.name] = this.value;
368 url: reverse("ajax_document_addtag", self.id),
372 success: function(data) {
373 params.success(self, data.message);
375 error: function(xhr) {
376 if (xhr.status == 403 || xhr.status == 401) {
377 params.failure(self, {
378 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
383 params.failure(self, $.parseJSON(xhr.responseText));
386 params.failure(self, {
387 "__all__": ["Nie udało się - błąd serwera."]
395 WikiDocument.prototype.pubmark = function(params) {
396 params = $.extend({}, noops, params);
399 "pubmark-id": self.id,
403 $.each(params.form.serializeArray(), function() {
404 data[this.name] = this.value;
408 url: reverse("ajax_document_pubmark", self.id),
412 success: function(data) {
413 params.success(self, data.message);
415 error: function(xhr) {
416 if (xhr.status == 403 || xhr.status == 401) {
417 params.failure(self, {
418 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
423 params.failure(self, $.parseJSON(xhr.responseText));
426 params.failure(self, {
427 "__all__": ["Nie udało się - błąd serwera."]
435 $.wikiapi.WikiDocument = WikiDocument;