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 if (vname == "ajax_cover_preview")
54 return "/cover/preview/";
56 console.log("Couldn't reverse match:", vname);
61 * Document Abstraction
63 function WikiDocument(element_id) {
64 var meta = $('#' + element_id);
65 this.id = meta.attr('data-chunk-id');
67 this.revision = $("*[data-key='revision']", meta).text();
68 this.readonly = !!$("*[data-key='readonly']", meta).text();
70 this.galleryLink = $("*[data-key='gallery']", meta).text();
71 this.galleryStart = parseInt($("*[data-key='gallery-start']", meta).text());
73 var diff = $("*[data-key='diff']", meta).text();
75 diff = diff.split(',');
76 if (diff.length == 2 && diff[0] < diff[1])
78 else if (diff.length == 1) {
79 diff = parseInt(diff);
81 this.diff = [diff - 1, diff];
85 this.galleryImages = [];
87 this.has_local_changes = false;
89 this._context_lock = -1;
93 WikiDocument.prototype.triggerDocumentChanged = function() {
94 $(document).trigger('wlapi_document_changed', this);
97 * Fetch text of this document.
99 WikiDocument.prototype.fetch = function(params) {
100 params = $.extend({}, noops, params);
104 url: reverse("ajax_document_text", self.id),
105 data: {"revision": self.revision},
107 success: function(data) {
110 if (self.text === null || self.revision !== data.revision) {
111 self.text = data.text;
112 self.revision = data.revision;
113 self.gallery = data.gallery;
115 self.triggerDocumentChanged();
118 self.has_local_changes = false;
119 params['success'](self, changed);
122 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
127 * Fetch history of this document.
129 * from - First revision to fetch (default = 0) upto - Last revision to
130 * fetch (default = tip)
133 WikiDocument.prototype.fetchHistory = function(params) {
134 /* this doesn't modify anything, so no locks */
135 params = $.extend({}, noops, params);
139 url: reverse("ajax_document_history", self.id),
142 "from": params['from'],
143 "upto": params['upto']
145 success: function(data) {
146 params['success'](self, data);
149 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
153 WikiDocument.prototype.fetchDiff = function(params) {
154 /* this doesn't modify anything, so no locks */
157 'from': self.revision,
162 url: reverse("ajax_document_diff", self.id),
165 "from": params['from'],
168 success: function(data) {
169 params['success'](self, data);
172 params['failure'](self, "Nie udało się wczytać porównania wersji.");
180 WikiDocument.prototype.refreshGallery = function(params) {
181 params = $.extend({}, noops, params);
185 url: reverse("ajax_document_gallery", self.galleryLink),
188 success: function(data) {
189 self.galleryImages = data;
190 params['success'](self, data);
192 error: function(xhr) {
194 switch (xhr.status) {
196 msg = 'Galerie dostępne tylko dla zalogowanych użytkowników.';
199 msg = "Nie znaleziono galerii o nazwie: '" + self.galleryLink + "'.";
202 msg = "Nie udało się wczytać galerii o nazwie: '" + self.galleryLink + "'.";
204 self.galleryImages = [];
205 params['failure'](self, "<p>" + msg + "</p>");
211 * Set document's text
213 WikiDocument.prototype.setText = function(text) {
215 this.has_local_changes = true;
219 * Set document's gallery link
221 WikiDocument.prototype.setGalleryLink = function(gallery) {
222 this.galleryLink = gallery;
223 this.has_local_changes = true;
227 * Save text back to the server
229 WikiDocument.prototype.save = function(params) {
230 params = $.extend({}, noops, params);
233 if (!self.has_local_changes) {
234 console.log("Abort: no changes.");
235 return params['success'](self, false, "Nie ma zmian do zapisania.");
238 // Serialize form to dictionary
240 $.each(params['form'].serializeArray(), function() {
241 data[this.name] = this.value;
244 data['textsave-text'] = self.text;
247 url: reverse("ajax_document_text", self.id),
251 success: function(data) {
254 $('#header').removeClass('saving');
257 self.text = data.text;
258 self.revision = data.revision;
259 self.gallery = data.gallery;
261 self.triggerDocumentChanged();
264 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
266 error: function(xhr) {
267 if ($('#header').hasClass('saving')) {
268 $('#header').removeClass('saving');
270 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
275 params['failure'](self, $.parseJSON(xhr.responseText));
278 params['failure'](self, {
279 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
287 $('#save-hide').click(function(){
288 $('#header').addClass('saving');
290 $.wiki.blocking.unblock();
292 }; /* end of save() */
294 WikiDocument.prototype.revertToVersion = function(params) {
296 params = $.extend({}, noops, params);
298 if (params.revision >= this.revision) {
299 params.failure(self, 'Proszę wybrać wersję starszą niż aktualna.');
303 // Serialize form to dictionary
305 $.each(params['form'].serializeArray(), function() {
306 data[this.name] = this.value;
310 url: reverse("ajax_document_revert", self.id),
314 success: function(data) {
316 self.text = data.text;
317 self.revision = data.revision;
318 self.gallery = data.gallery;
319 self.triggerDocumentChanged();
321 params.success(self, "Udało się przywrócić wersję :)");
324 params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
327 error: function(xhr) {
328 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
333 WikiDocument.prototype.pubmark = function(params) {
334 params = $.extend({}, noops, params);
337 "pubmark-id": self.id
341 $.each(params.form.serializeArray(), function() {
342 data[this.name] = this.value;
346 url: reverse("ajax_document_pubmark", self.id),
350 success: function(data) {
351 params.success(self, data.message);
353 error: function(xhr) {
354 if (xhr.status == 403 || xhr.status == 401) {
355 params.failure(self, {
356 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
361 params.failure(self, $.parseJSON(xhr.responseText));
364 params.failure(self, {
365 "__all__": ["Nie udało się - błąd serwera."]
373 /* unused except view_summary.js which is apparently unused */
374 WikiDocument.prototype.refreshCover = function(params) {
377 xml: self.text // TODO: send just DC
380 url: reverse("ajax_cover_preview"),
383 success: function(data) {
384 params.success(data);
386 error: function(xhr) {
387 // params.failure("Nie udało się odświeżyć okładki - błąd serwera.");
393 WikiDocument.prototype.getLength = function(params) {
394 var xml = this.text.replace(/\/(\s+)/g, '<br />$1');
395 var parser = new DOMParser();
396 var doc = parser.parseFromString(xml, 'text/xml');
397 var error = $('parsererror', doc);
399 if (error.length > 0) {
400 throw "Not an XML document.";
402 $.xmlns["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
403 $('rdf|RDF, motyw, pa, pe, pr, pt', doc).remove();
404 var text = $(doc).text();
405 text = $.trim(text.replace(/\s{2,}/g, ' '));
410 $.wikiapi.WikiDocument = WikiDocument;