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());
72 this.fullUri = $("*[data-key='full-uri']", meta).text();
74 var diff = $("*[data-key='diff']", meta).text();
76 diff = diff.split(',');
77 if (diff.length == 2 && diff[0] < diff[1])
79 else if (diff.length == 1) {
80 diff = parseInt(diff);
82 this.diff = [diff - 1, diff];
86 this.galleryImages = [];
88 this.has_local_changes = false;
90 this._context_lock = -1;
94 WikiDocument.prototype.triggerDocumentChanged = function() {
95 $(document).trigger('wlapi_document_changed', this);
98 * Fetch text of this document.
100 WikiDocument.prototype.fetch = function(params) {
101 params = $.extend({}, noops, params);
105 url: reverse("ajax_document_text", self.id),
106 data: {"revision": self.revision},
108 success: function(data) {
111 if (self.text === null || self.revision !== data.revision) {
112 self.text = data.text;
113 self.revision = data.revision;
114 self.gallery = data.gallery;
116 self.triggerDocumentChanged();
119 self.has_local_changes = false;
120 params['success'](self, changed);
123 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
128 * Fetch history of this document.
130 * from - First revision to fetch (default = 0) upto - Last revision to
131 * fetch (default = tip)
134 WikiDocument.prototype.fetchHistory = function(params) {
135 /* this doesn't modify anything, so no locks */
136 params = $.extend({}, noops, params);
140 url: reverse("ajax_document_history", self.id),
143 "from": params['from'],
144 "upto": params['upto']
146 success: function(data) {
147 params['success'](self, data);
150 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
154 WikiDocument.prototype.fetchDiff = function(params) {
155 /* this doesn't modify anything, so no locks */
158 'from': self.revision,
163 url: reverse("ajax_document_diff", self.id),
166 "from": params['from'],
169 success: function(data) {
170 params['success'](self, data);
173 params['failure'](self, "Nie udało się wczytać porównania wersji.");
178 WikiDocument.prototype.checkRevision = function(params) {
179 /* this doesn't modify anything, so no locks */
183 url: reverse("ajax_document_rev", self.id),
185 success: function(data) {
190 else if (data != self.revision)
199 WikiDocument.prototype.refreshGallery = function(params) {
200 params = $.extend({}, noops, params);
202 if (!self.galleryLink) {
203 params['failure'](self, 'Brak galerii.');
208 url: reverse("ajax_document_gallery", self.galleryLink),
211 success: function(data) {
212 self.galleryImages = data;
213 params['success'](self, data);
215 error: function(xhr) {
216 switch (xhr.status) {
218 var msg = 'Galerie dostępne tylko dla zalogowanych użytkowników.';
221 var msg = "Nie znaleziono galerii o nazwie: '" + self.galleryLink + "'.";
223 var msg = "Nie udało się wczytać galerii o nazwie: '" + self.galleryLink + "'.";
225 self.galleryImages = [];
226 params['failure'](self, msg);
232 * Set document's text
234 WikiDocument.prototype.setText = function(text, setter) {
235 if (text == this.text) return;
238 this.has_local_changes = true;
244 * Save text back to the server
246 WikiDocument.prototype.save = function(params) {
247 params = $.extend({}, noops, params);
250 if (!self.has_local_changes) {
251 console.log("Abort: no changes.");
252 return params['success'](self, false, "Nie ma zmian do zapisania.");
255 // Serialize form to dictionary
257 $.each(params['form'].serializeArray(), function() {
258 data[this.name] = this.value;
261 data['textsave-text'] = self.text;
264 url: reverse("ajax_document_text", self.id),
268 success: function(data) {
271 $('#header').removeClass('saving');
274 self.text = data.text;
275 self.revision = data.revision;
276 self.gallery = data.gallery;
278 self.triggerDocumentChanged();
281 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
283 error: function(xhr) {
284 if ($('#header').hasClass('saving')) {
285 $('#header').removeClass('saving');
287 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
292 params['failure'](self, $.parseJSON(xhr.responseText));
295 params['failure'](self, {
296 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
304 $('#save-hide').click(function(){
305 $('#header').addClass('saving');
307 $.wiki.blocking.unblock();
309 }; /* end of save() */
311 WikiDocument.prototype.revertToVersion = function(params) {
313 params = $.extend({}, noops, params);
315 if (params.revision >= this.revision) {
316 params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
320 // Serialize form to dictionary
322 $.each(params['form'].serializeArray(), function() {
323 data[this.name] = this.value;
327 url: reverse("ajax_document_revert", self.id),
331 success: function(data) {
333 self.text = data.text;
334 self.revision = data.revision;
335 self.gallery = data.gallery;
336 self.triggerDocumentChanged();
338 params.success(self, "Udało się przywrócić wersję :)");
341 params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
344 error: function(xhr) {
345 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
350 WikiDocument.prototype.pubmark = function(params) {
351 params = $.extend({}, noops, params);
354 "pubmark-id": self.id,
358 $.each(params.form.serializeArray(), function() {
359 data[this.name] = this.value;
363 url: reverse("ajax_document_pubmark", self.id),
367 success: function(data) {
368 params.success(self, data.message);
370 error: function(xhr) {
371 if (xhr.status == 403 || xhr.status == 401) {
372 params.failure(self, {
373 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
378 params.failure(self, $.parseJSON(xhr.responseText));
381 params.failure(self, {
382 "__all__": ["Nie udało się - błąd serwera."]
390 WikiDocument.prototype.refreshCover = function(params) {
393 xml: self.text // TODO: send just DC
396 url: reverse("ajax_cover_preview"),
399 success: function(data) {
400 params.success(data);
402 error: function(xhr) {
403 // params.failure("Nie udało się odświeżyć okładki - błąd serwera.");
409 WikiDocument.prototype.getLength = function(params) {
410 params = $.extend({}, noops, params);
411 var xml = this.text.replace(/\/(\s+)/g, '<br />$1');
412 var parser = new DOMParser();
413 var doc = parser.parseFromString(xml, 'text/xml');
414 var error = $('parsererror', doc);
416 if (error.length > 0) {
417 throw "Not an XML document.";
419 $.xmlns["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
420 $('rdf|RDF', doc).remove();
421 if (params.noFootnotes) {
422 $('pa, pe, pr, pt', doc).remove();
424 if (params.noThemes) {
425 $('motyw', doc).remove();
427 var text = $(doc).text();
428 text = $.trim(text.replace(/\s{2,}/g, ' '));
432 /* Temporary workaround for relative images. */
433 WikiDocument.prototype.getBase = function() {
434 return '/media/dynamic/images/' + this.galleryLink + '/';
437 $.wikiapi.WikiDocument = WikiDocument;