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();
71 diff = diff.split(',');
72 if (diff.length == 2 && diff[0] < diff[1])
74 else if (diff.length == 1) {
75 diff = parseInt(diff);
77 this.diff = [diff - 1, diff];
81 this.galleryImages = [];
83 this.has_local_changes = false;
85 this._context_lock = -1;
89 WikiDocument.prototype.triggerDocumentChanged = function() {
90 $(document).trigger('wlapi_document_changed', this);
93 * Fetch text of this document.
95 WikiDocument.prototype.fetch = function(params) {
96 params = $.extend({}, noops, params);
100 url: reverse("ajax_document_text", self.id),
101 data: {"revision": self.revision},
103 success: function(data) {
106 if (self.text === null || self.revision !== data.revision) {
107 self.text = data.text;
108 self.revision = data.revision;
109 self.gallery = data.gallery;
111 self.triggerDocumentChanged();
114 self.has_local_changes = false;
115 params['success'](self, changed);
118 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
123 * Fetch history of this document.
125 * from - First revision to fetch (default = 0) upto - Last revision to
126 * fetch (default = tip)
129 WikiDocument.prototype.fetchHistory = function(params) {
130 /* this doesn't modify anything, so no locks */
131 params = $.extend({}, noops, params);
135 url: reverse("ajax_document_history", self.id),
138 "from": params['from'],
139 "upto": params['upto']
141 success: function(data) {
142 params['success'](self, data);
145 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
149 WikiDocument.prototype.fetchDiff = function(params) {
150 /* this doesn't modify anything, so no locks */
153 'from': self.revision,
158 url: reverse("ajax_document_diff", self.id),
161 "from": params['from'],
164 success: function(data) {
165 params['success'](self, data);
168 params['failure'](self, "Nie udało się wczytać porównania wersji.");
173 WikiDocument.prototype.checkRevision = function(params) {
174 /* this doesn't modify anything, so no locks */
178 url: reverse("ajax_document_rev", self.id),
180 success: function(data) {
185 else if (data != self.revision)
194 WikiDocument.prototype.refreshGallery = function(params) {
195 params = $.extend({}, noops, params);
199 url: reverse("ajax_document_gallery", self.galleryLink),
202 success: function(data) {
203 self.galleryImages = data;
204 params['success'](self, data);
207 self.galleryImages = [];
208 params['failure'](self, "<p>Nie udało się wczytać galerii pod nazwą: '" + self.galleryLink + "'.</p>");
214 * Set document's text
216 WikiDocument.prototype.setText = function(text) {
218 this.has_local_changes = true;
222 * Set document's gallery link
224 WikiDocument.prototype.setGalleryLink = function(gallery) {
225 this.galleryLink = gallery;
226 this.has_local_changes = true;
230 * Save text back to the server
232 WikiDocument.prototype.save = function(params) {
233 params = $.extend({}, noops, params);
236 if (!self.has_local_changes) {
237 console.log("Abort: no changes.");
238 return params['success'](self, false, "Nie ma zmian do zapisania.");
241 // Serialize form to dictionary
243 $.each(params['form'].serializeArray(), function() {
244 data[this.name] = this.value;
247 data['textsave-text'] = self.text;
250 url: reverse("ajax_document_text", self.id),
254 success: function(data) {
257 $('#header').removeClass('saving');
260 self.text = data.text;
261 self.revision = data.revision;
262 self.gallery = data.gallery;
264 self.triggerDocumentChanged();
267 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
269 error: function(xhr) {
270 if ($('#header').hasClass('saving')) {
271 $('#header').removeClass('saving');
273 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
278 params['failure'](self, $.parseJSON(xhr.responseText));
281 params['failure'](self, {
282 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
290 $('#save-hide').click(function(){
291 $('#header').addClass('saving');
293 $.wiki.blocking.unblock();
295 }; /* end of save() */
297 WikiDocument.prototype.revertToVersion = function(params) {
299 params = $.extend({}, noops, params);
301 if (params.revision >= this.revision) {
302 params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
306 // Serialize form to dictionary
308 $.each(params['form'].serializeArray(), function() {
309 data[this.name] = this.value;
313 url: reverse("ajax_document_revert", self.id),
317 success: function(data) {
319 self.text = data.text;
320 self.revision = data.revision;
321 self.gallery = data.gallery;
322 self.triggerDocumentChanged();
324 params.success(self, "Udało się przywrócić wersję :)");
327 params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
330 error: function(xhr) {
331 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
336 WikiDocument.prototype.publish = function(params) {
337 params = $.extend({}, noops, params);
340 url: reverse("ajax_publish", self.id),
343 success: function(data) {
344 params.success(self, data);
346 error: function(xhr) {
347 if (xhr.status == 403 || xhr.status == 401) {
348 params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
352 params.failure(self, xhr.responseText);
355 params.failure(self, "Nie udało się - błąd serwera.");
363 WikiDocument.prototype.pubmark = function(params) {
364 params = $.extend({}, noops, params);
367 "pubmark-id": self.id,
371 $.each(params.form.serializeArray(), function() {
372 data[this.name] = this.value;
376 url: reverse("ajax_document_pubmark", self.id),
380 success: function(data) {
381 params.success(self, data.message);
383 error: function(xhr) {
384 if (xhr.status == 403 || xhr.status == 401) {
385 params.failure(self, {
386 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
391 params.failure(self, $.parseJSON(xhr.responseText));
394 params.failure(self, {
395 "__all__": ["Nie udało się - błąd serwera."]
403 WikiDocument.prototype.getLength = function(params) {
404 var xml = this.text.replace(/\/(\s+)/g, '<br />$1');
405 var parser = new DOMParser();
406 var doc = parser.parseFromString(xml, 'text/xml');
407 var error = $('parsererror', doc);
409 if (error.length > 0) {
410 throw "Not an XML document.";
412 $.xmlns["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
413 $('rdf|RDF, motyw, pa, pe, pr, pt', doc).remove();
414 var text = $(doc).text();
415 text = $.trim(text.replace(/\s{2,}/g, ' '));
420 $.wikiapi.WikiDocument = WikiDocument;