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-chunk-id');
64 this.revision = $("*[data-key='revision']", meta).text();
65 this.readonly = !!$("*[data-key='readonly']", meta).text();
67 this.galleryLink = $("*[data-key='gallery']", meta).text();
68 this.galleryStart = parseInt($("*[data-key='gallery-start']", meta).text());
70 var diff = $("*[data-key='diff']", meta).text();
72 diff = diff.split(',');
73 if (diff.length == 2 && diff[0] < diff[1])
75 else if (diff.length == 1) {
76 diff = parseInt(diff);
78 this.diff = [diff - 1, diff];
82 this.galleryImages = [];
84 this.has_local_changes = false;
86 this._context_lock = -1;
90 WikiDocument.prototype.triggerDocumentChanged = function() {
91 $(document).trigger('wlapi_document_changed', this);
94 * Fetch text of this document.
96 WikiDocument.prototype.fetch = function(params) {
97 params = $.extend({}, noops, params);
101 url: reverse("ajax_document_text", self.id),
102 data: {"revision": self.revision},
104 success: function(data) {
107 if (self.text === null || self.revision !== data.revision) {
108 self.text = data.text;
109 self.revision = data.revision;
110 self.gallery = data.gallery;
112 self.triggerDocumentChanged();
115 self.has_local_changes = false;
116 params['success'](self, changed);
119 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
124 * Fetch history of this document.
126 * from - First revision to fetch (default = 0) upto - Last revision to
127 * fetch (default = tip)
130 WikiDocument.prototype.fetchHistory = function(params) {
131 /* this doesn't modify anything, so no locks */
132 params = $.extend({}, noops, params);
136 url: reverse("ajax_document_history", self.id),
139 "from": params['from'],
140 "upto": params['upto']
142 success: function(data) {
143 params['success'](self, data);
146 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
150 WikiDocument.prototype.fetchDiff = function(params) {
151 /* this doesn't modify anything, so no locks */
154 'from': self.revision,
159 url: reverse("ajax_document_diff", self.id),
162 "from": params['from'],
165 success: function(data) {
166 params['success'](self, data);
169 params['failure'](self, "Nie udało się wczytać porównania wersji.");
174 WikiDocument.prototype.checkRevision = function(params) {
175 /* this doesn't modify anything, so no locks */
179 url: reverse("ajax_document_rev", self.id),
181 success: function(data) {
186 else if (data != self.revision)
195 WikiDocument.prototype.refreshGallery = function(params) {
196 params = $.extend({}, noops, params);
200 url: reverse("ajax_document_gallery", self.galleryLink),
203 success: function(data) {
204 self.galleryImages = data;
205 params['success'](self, data);
207 error: function(xhr) {
208 switch (xhr.status) {
210 var msg = 'Galerie dostępne tylko dla zalogowanych użytkowników.';
213 var msg = "Nie znaleziono galerii o nazwie: '" + self.galleryLink + "'.";
215 var msg = "Nie udało się wczytać galerii o nazwie: '" + self.galleryLink + "'.";
217 self.galleryImages = [];
218 params['failure'](self, "<p>" + msg + "</p>");
224 * Set document's text
226 WikiDocument.prototype.setText = function(text) {
228 this.has_local_changes = true;
232 * Set document's gallery link
234 WikiDocument.prototype.setGalleryLink = function(gallery) {
235 this.galleryLink = gallery;
236 this.has_local_changes = true;
240 * Save text back to the server
242 WikiDocument.prototype.save = function(params) {
243 params = $.extend({}, noops, params);
246 if (!self.has_local_changes) {
247 console.log("Abort: no changes.");
248 return params['success'](self, false, "Nie ma zmian do zapisania.");
251 // Serialize form to dictionary
253 $.each(params['form'].serializeArray(), function() {
254 data[this.name] = this.value;
257 data['textsave-text'] = self.text;
260 url: reverse("ajax_document_text", self.id),
264 success: function(data) {
267 $('#header').removeClass('saving');
270 self.text = data.text;
271 self.revision = data.revision;
272 self.gallery = data.gallery;
274 self.triggerDocumentChanged();
277 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
279 error: function(xhr) {
280 if ($('#header').hasClass('saving')) {
281 $('#header').removeClass('saving');
283 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
288 params['failure'](self, $.parseJSON(xhr.responseText));
291 params['failure'](self, {
292 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
300 $('#save-hide').click(function(){
301 $('#header').addClass('saving');
303 $.wiki.blocking.unblock();
305 }; /* end of save() */
307 WikiDocument.prototype.revertToVersion = function(params) {
309 params = $.extend({}, noops, params);
311 if (params.revision >= this.revision) {
312 params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
316 // Serialize form to dictionary
318 $.each(params['form'].serializeArray(), function() {
319 data[this.name] = this.value;
323 url: reverse("ajax_document_revert", self.id),
327 success: function(data) {
329 self.text = data.text;
330 self.revision = data.revision;
331 self.gallery = data.gallery;
332 self.triggerDocumentChanged();
334 params.success(self, "Udało się przywrócić wersję :)");
337 params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
340 error: function(xhr) {
341 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
346 WikiDocument.prototype.publish = function(params) {
347 params = $.extend({}, noops, params);
350 url: reverse("ajax_publish", self.id),
353 success: function(data) {
354 params.success(self, data);
356 error: function(xhr) {
357 if (xhr.status == 403 || xhr.status == 401) {
358 params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
362 params.failure(self, xhr.responseText);
365 params.failure(self, "Nie udało się - błąd serwera.");
373 WikiDocument.prototype.pubmark = function(params) {
374 params = $.extend({}, noops, params);
377 "pubmark-id": self.id,
381 $.each(params.form.serializeArray(), function() {
382 data[this.name] = this.value;
386 url: reverse("ajax_document_pubmark", self.id),
390 success: function(data) {
391 params.success(self, data.message);
393 error: function(xhr) {
394 if (xhr.status == 403 || xhr.status == 401) {
395 params.failure(self, {
396 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
401 params.failure(self, $.parseJSON(xhr.responseText));
404 params.failure(self, {
405 "__all__": ["Nie udało się - błąd serwera."]
413 WikiDocument.prototype.getLength = function(params) {
414 var xml = this.text.replace(/\/(\s+)/g, '<br />$1');
415 var parser = new DOMParser();
416 var doc = parser.parseFromString(xml, 'text/xml');
417 var error = $('parsererror', doc);
419 if (error.length > 0) {
420 throw "Not an XML document.";
422 $.xmlns["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
423 $('rdf|RDF, motyw, pa, pe, pr, pt', doc).remove();
424 var text = $(doc).text();
425 text = $.trim(text.replace(/\s{2,}/g, ' '));
430 $.wikiapi.WikiDocument = WikiDocument;