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);
208 self.galleryImages = [];
209 params['failure'](self, "<p>Nie udało się wczytać galerii pod nazwą: '" + self.galleryLink + "'.</p>");
215 * Set document's text
217 WikiDocument.prototype.setText = function(text) {
219 this.has_local_changes = true;
223 * Set document's gallery link
225 WikiDocument.prototype.setGalleryLink = function(gallery) {
226 this.galleryLink = gallery;
227 this.has_local_changes = true;
231 * Save text back to the server
233 WikiDocument.prototype.save = function(params) {
234 params = $.extend({}, noops, params);
237 if (!self.has_local_changes) {
238 console.log("Abort: no changes.");
239 return params['success'](self, false, "Nie ma zmian do zapisania.");
242 // Serialize form to dictionary
244 $.each(params['form'].serializeArray(), function() {
245 data[this.name] = this.value;
248 data['textsave-text'] = self.text;
251 url: reverse("ajax_document_text", self.id),
255 success: function(data) {
258 $('#header').removeClass('saving');
261 self.text = data.text;
262 self.revision = data.revision;
263 self.gallery = data.gallery;
265 self.triggerDocumentChanged();
268 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
270 error: function(xhr) {
271 if ($('#header').hasClass('saving')) {
272 $('#header').removeClass('saving');
274 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
279 params['failure'](self, $.parseJSON(xhr.responseText));
282 params['failure'](self, {
283 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
291 $('#save-hide').click(function(){
292 $('#header').addClass('saving');
294 $.wiki.blocking.unblock();
296 }; /* end of save() */
298 WikiDocument.prototype.revertToVersion = function(params) {
300 params = $.extend({}, noops, params);
302 if (params.revision >= this.revision) {
303 params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
307 // Serialize form to dictionary
309 $.each(params['form'].serializeArray(), function() {
310 data[this.name] = this.value;
314 url: reverse("ajax_document_revert", self.id),
318 success: function(data) {
320 self.text = data.text;
321 self.revision = data.revision;
322 self.gallery = data.gallery;
323 self.triggerDocumentChanged();
325 params.success(self, "Udało się przywrócić wersję :)");
328 params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
331 error: function(xhr) {
332 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
337 WikiDocument.prototype.publish = function(params) {
338 params = $.extend({}, noops, params);
341 url: reverse("ajax_publish", self.id),
344 success: function(data) {
345 params.success(self, data);
347 error: function(xhr) {
348 if (xhr.status == 403 || xhr.status == 401) {
349 params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
353 params.failure(self, xhr.responseText);
356 params.failure(self, "Nie udało się - błąd serwera.");
364 WikiDocument.prototype.pubmark = function(params) {
365 params = $.extend({}, noops, params);
368 "pubmark-id": self.id,
372 $.each(params.form.serializeArray(), function() {
373 data[this.name] = this.value;
377 url: reverse("ajax_document_pubmark", self.id),
381 success: function(data) {
382 params.success(self, data.message);
384 error: function(xhr) {
385 if (xhr.status == 403 || xhr.status == 401) {
386 params.failure(self, {
387 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
392 params.failure(self, $.parseJSON(xhr.responseText));
395 params.failure(self, {
396 "__all__": ["Nie udało się - błąd serwera."]
404 WikiDocument.prototype.getLength = function(params) {
405 var xml = this.text.replace(/\/(\s+)/g, '<br />$1');
406 var parser = new DOMParser();
407 var doc = parser.parseFromString(xml, 'text/xml');
408 var error = $('parsererror', doc);
410 if (error.length > 0) {
411 throw "Not an XML document.";
413 $.xmlns["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
414 $('rdf|RDF, motyw, pa, pe, pr, pt', doc).remove();
415 var text = $(doc).text();
416 text = $.trim(text.replace(/\s{2,}/g, ' '));
421 $.wikiapi.WikiDocument = WikiDocument;