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 = "/editor";
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_pubmark")
51 return base_path + "/pubmark/" + arguments[1] + '/';
53 console.log("Couldn't reverse match:", vname);
58 * Document Abstraction
60 function WikiDocument(element_id) {
61 var meta = $('#' + element_id);
62 this.id = meta.attr('data-book') + '/' + meta.attr('data-chunk');
64 this.revision = $("*[data-key='revision']", meta).text();
65 this.readonly = !!$("*[data-key='readonly']", meta).text();
67 this.galleryLink = $("*[data-key='gallery']", meta).text();
69 var diff = $("*[data-key='diff']", meta).text();
70 diff = diff.split(',');
71 if (diff.length == 2 && diff[0] < diff[1])
73 else if (diff.length == 1) {
74 diff = parseInt(diff);
76 this.diff = [diff - 1, diff];
79 this.galleryImages = [];
81 this.has_local_changes = false;
83 this._context_lock = -1;
87 WikiDocument.prototype.triggerDocumentChanged = function() {
88 $(document).trigger('wlapi_document_changed', this);
91 * Fetch text of this document.
93 WikiDocument.prototype.fetch = function(params) {
94 params = $.extend({}, noops, params);
98 url: reverse("ajax_document_text", self.id),
99 data: {"revision": self.revision},
101 success: function(data) {
104 if (self.text === null || self.revision !== data.revision) {
105 self.text = data.text;
106 self.revision = data.revision;
107 self.gallery = data.gallery;
109 self.triggerDocumentChanged();
112 self.has_local_changes = false;
113 params['success'](self, changed);
116 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
121 * Fetch history of this document.
123 * from - First revision to fetch (default = 0) upto - Last revision to
124 * fetch (default = tip)
127 WikiDocument.prototype.fetchHistory = function(params) {
128 /* this doesn't modify anything, so no locks */
129 params = $.extend({}, noops, params);
133 url: reverse("ajax_document_history", self.id),
136 "from": params['from'],
137 "upto": params['upto']
139 success: function(data) {
140 params['success'](self, data);
143 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
147 WikiDocument.prototype.fetchDiff = function(params) {
148 /* this doesn't modify anything, so no locks */
151 'from': self.revision,
156 url: reverse("ajax_document_diff", self.id),
159 "from": params['from'],
162 success: function(data) {
163 params['success'](self, data);
166 params['failure'](self, "Nie udało się wczytać porównania wersji.");
171 WikiDocument.prototype.checkRevision = function(params) {
172 /* this doesn't modify anything, so no locks */
176 url: reverse("ajax_document_rev", self.id),
178 success: function(data) {
183 else if (data != self.revision)
192 WikiDocument.prototype.refreshGallery = function(params) {
193 params = $.extend({}, noops, params);
197 url: reverse("ajax_document_gallery", self.galleryLink),
200 success: function(data) {
201 self.galleryImages = data;
202 params['success'](self, data);
205 self.galleryImages = [];
206 params['failure'](self, "<p>Nie udało się wczytać galerii pod nazwą: '" + self.galleryLink + "'.</p>");
212 * Set document's text
214 WikiDocument.prototype.setText = function(text) {
216 this.has_local_changes = true;
220 * Set document's gallery link
222 WikiDocument.prototype.setGalleryLink = function(gallery) {
223 this.galleryLink = gallery;
224 this.has_local_changes = true;
228 * Save text back to the server
230 WikiDocument.prototype.save = function(params) {
231 params = $.extend({}, noops, params);
234 if (!self.has_local_changes) {
235 console.log("Abort: no changes.");
236 return params['success'](self, false, "Nie ma zmian do zapisania.");
239 // Serialize form to dictionary
241 $.each(params['form'].serializeArray(), function() {
242 data[this.name] = this.value;
245 data['textsave-text'] = self.text;
248 url: reverse("ajax_document_text", self.id),
252 success: function(data) {
255 $('#header').removeClass('saving');
258 self.text = data.text;
259 self.revision = data.revision;
260 self.gallery = data.gallery;
262 self.triggerDocumentChanged();
265 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
267 error: function(xhr) {
268 if ($('#header').hasClass('saving')) {
269 $('#header').removeClass('saving');
271 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
276 params['failure'](self, $.parseJSON(xhr.responseText));
279 params['failure'](self, {
280 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
288 $('#save-hide').click(function(){
289 $('#header').addClass('saving');
291 $.wiki.blocking.unblock();
293 }; /* end of save() */
295 WikiDocument.prototype.revertToVersion = function(params) {
297 params = $.extend({}, noops, params);
299 if (params.revision >= this.revision) {
300 params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
304 // Serialize form to dictionary
306 $.each(params['form'].serializeArray(), function() {
307 data[this.name] = this.value;
311 url: reverse("ajax_document_revert", self.id),
315 success: function(data) {
317 self.text = data.text;
318 self.revision = data.revision;
319 self.gallery = data.gallery;
320 self.triggerDocumentChanged();
322 params.success(self, "Udało się przywrócić wersję :)");
325 params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
328 error: function(xhr) {
329 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
334 WikiDocument.prototype.publish = function(params) {
335 params = $.extend({}, noops, params);
338 url: reverse("ajax_publish", self.id),
341 success: function(data) {
342 params.success(self, data);
344 error: function(xhr) {
345 if (xhr.status == 403 || xhr.status == 401) {
346 params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
350 params.failure(self, xhr.responseText);
353 params.failure(self, "Nie udało się - błąd serwera.");
361 WikiDocument.prototype.pubmark = function(params) {
362 params = $.extend({}, noops, params);
365 "pubmark-id": self.id,
369 $.each(params.form.serializeArray(), function() {
370 data[this.name] = this.value;
374 url: reverse("ajax_document_pubmark", self.id),
378 success: function(data) {
379 params.success(self, data.message);
381 error: function(xhr) {
382 if (xhr.status == 403 || xhr.status == 401) {
383 params.failure(self, {
384 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
389 params.failure(self, $.parseJSON(xhr.responseText));
392 params.failure(self, {
393 "__all__": ["Nie udało się - błąd serwera."]
401 $.wikiapi.WikiDocument = WikiDocument;