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") {
35 return base_path + "/history/" + arguments[1] + '/';
38 if (vname == "ajax_document_gallery") {
39 return base_path + "/gallery/" + arguments[1] + '/';
42 if (vname == "ajax_document_diff")
43 return base_path + "/diff/" + arguments[1] + '/';
45 if (vname == "ajax_document_rev")
46 return base_path + "/rev/" + arguments[1] + '/';
48 if (vname == "ajax_document_pubmark")
49 return base_path + "/pubmark/" + arguments[1] + '/';
51 if (vname == "ajax_cover_preview")
52 return "/cover/preview/";
54 console.log("Couldn't reverse match:", vname);
58 static csrf = $('input[name="csrfmiddlewaretoken"]').val();
60 // TODO: Add waiting marker, error reporting.
61 static post(url, data) {
62 data['csrfmiddlewaretoken'] = this.csrf;
70 static get(url, callback) {
74 success: function(data) {
80 static setGallery(id, gallery) {
82 '/editor/set-gallery/' + id + '/',
86 static setGalleryStart(id, start) {
88 '/editor/set-gallery-start/' + id + '/',
93 static openGalleryEdit(bookSlug) {
95 '/documents/book/' + bookSlug + '/gallery/',
100 static withGalleryList(callback) {
102 '/editor/galleries/',
109 * Document Abstraction
112 constructor(element_id) {
113 var meta = $('#' + element_id);
114 this.id = meta.attr('data-chunk-id');
116 this.revision = $("*[data-key='revision']", meta).text();
117 this.readonly = !!$("*[data-key='readonly']", meta).text();
119 this.bookSlug = $("*[data-key='book-slug']", meta).text();
120 this.galleryLink = $("*[data-key='gallery']", meta).text();
121 this.galleryStart = parseInt($("*[data-key='gallery-start']", meta).text());
122 this.fullUri = $("*[data-key='full-uri']", meta).text();
124 var diff = $("*[data-key='diff']", meta).text();
126 diff = diff.split(',');
127 if (diff.length == 2 && diff[0] < diff[1])
129 else if (diff.length == 1) {
130 diff = parseInt(diff);
132 this.diff = [diff - 1, diff];
136 this.galleryImages = [];
138 this.has_local_changes = false;
141 this._context_lock = -1;
142 this._lock_count = 0;
145 triggerDocumentChanged() {
146 $(document).trigger('wlapi_document_changed', this);
150 * Fetch text of this document.
153 params = $.extend({}, noops, params);
157 url: reverse("ajax_document_text", self.id),
158 data: {"revision": self.revision},
160 success: function(data) {
163 if (self.text === null || self.revision !== data.revision) {
164 self.text = data.text;
165 self.revision = data.revision;
166 self.gallery = data.gallery;
168 self.triggerDocumentChanged();
171 self.has_local_changes = false;
172 params['success'](self, changed);
175 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
181 * Fetch history of this document.
183 fetchHistory(params) {
184 /* this doesn't modify anything, so no locks */
185 params = $.extend({}, noops, params);
189 url: reverse("ajax_document_history", self.id) + "?before=" + params.before,
191 success: function(data) {
192 params['success'](self, data);
195 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
201 /* this doesn't modify anything, so no locks */
204 'from': self.revision,
209 url: reverse("ajax_document_diff", self.id),
212 "from": params['from'],
215 success: function(data) {
216 params['success'](self, data);
219 params['failure'](self, "Nie udało się wczytać porównania wersji.");
224 checkRevision(params) {
225 /* this doesn't modify anything, so no locks */
227 let active = self.active;
231 url: reverse("ajax_document_rev", self.id),
236 success: function(data) {
241 else if (data != self.revision)
250 refreshGallery(params) {
251 params = $.extend({}, noops, params);
253 if (!self.galleryLink) {
254 params['failure'](self, 'Brak galerii.');
259 url: reverse("ajax_document_gallery", self.galleryLink),
262 success: function(data) {
263 self.galleryImages = data;
264 params['success'](self, data);
266 error: function(xhr) {
267 switch (xhr.status) {
269 var msg = 'Galerie dostępne tylko dla zalogowanych użytkowników.';
272 var msg = "Nie znaleziono galerii o nazwie: '" + self.galleryLink + "'.";
274 var msg = "Nie udało się wczytać galerii o nazwie: '" + self.galleryLink + "'.";
276 self.galleryImages = [];
277 params['failure'](self, msg);
282 setGallery(gallery) {
283 this.galleryLink = gallery;
284 Api.setGallery(this.id, gallery);
287 setGalleryStart(start) {
288 this.galleryStart = start;
289 Api.setGalleryStart(this.id, start);
292 openGalleryEdit(start) {
293 Api.openGalleryEdit(this.bookSlug);
296 withGalleryList(callback) {
297 Api.withGalleryList(callback);
301 * Set document's text
304 if (text == this.text) return;
306 this.has_local_changes = true;
310 * Save text back to the server
313 params = $.extend({}, noops, params);
316 if (!self.has_local_changes) {
317 console.log("Abort: no changes.");
318 return params['success'](self, false, "Nie ma zmian do zapisania.");
321 // Serialize form to dictionary
323 $.each(params['form'].serializeArray(), function() {
324 data[this.name] = this.value;
327 data['textsave-text'] = self.text;
330 url: reverse("ajax_document_text", self.id),
334 success: function(data) {
337 $('#header').removeClass('saving');
340 self.text = data.text;
341 self.revision = data.revision;
342 self.gallery = data.gallery;
344 self.triggerDocumentChanged();
347 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
349 error: function(xhr) {
350 if ($('#header').hasClass('saving')) {
351 $('#header').removeClass('saving');
353 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
358 params['failure'](self, $.parseJSON(xhr.responseText));
361 params['failure'](self, {
362 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
369 $('#save-hide').click(function(){
370 $('#header').addClass('saving');
372 $.wiki.blocking.unblock();
374 } /* end of save() */
376 revertToVersion(params) {
378 params = $.extend({}, noops, params);
380 if (params.revision >= this.revision) {
381 params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
385 // Serialize form to dictionary
387 $.each(params['form'].serializeArray(), function() {
388 data[this.name] = this.value;
392 url: reverse("ajax_document_revert", self.id),
396 success: function(data) {
398 self.text = data.text;
399 self.revision = data.revision;
400 self.gallery = data.gallery;
401 self.triggerDocumentChanged();
403 params.success(self, "Udało się przywrócić wersję :)");
406 params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
409 error: function(xhr) {
410 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
416 params = $.extend({}, noops, params);
419 "pubmark-id": self.id,
423 $.each(params.form.serializeArray(), function() {
424 data[this.name] = this.value;
428 url: reverse("ajax_document_pubmark", self.id),
432 success: function(data) {
433 params.success(self, data.message);
435 error: function(xhr) {
436 if (xhr.status == 403 || xhr.status == 401) {
437 params.failure(self, {
438 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
443 params.failure(self, $.parseJSON(xhr.responseText));
446 params.failure(self, {
447 "__all__": ["Nie udało się - błąd serwera."]
455 refreshCover(params) {
458 xml: self.text // TODO: send just DC
461 url: reverse("ajax_cover_preview"),
464 success: function(data) {
465 params.success(data);
467 error: function(xhr) {
468 // params.failure("Nie udało się odświeżyć okładki - błąd serwera.");
474 params = $.extend({}, noops, params);
475 var xml = this.text.replace(/\/(\s+)/g, '<br />$1');
476 var parser = new DOMParser();
477 var doc = parser.parseFromString(xml, 'text/xml');
478 var error = $('parsererror', doc);
480 if (error.length > 0) {
481 throw "Not an XML document.";
483 $.xmlns["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
484 $('rdf|RDF', doc).remove();
485 if (params.noFootnotes) {
486 $('pa, pe, pr, pt', doc).remove();
488 if (params.noThemes) {
489 $('motyw', doc).remove();
491 var text = $(doc).text();
492 text = $.trim(text.replace(/\s{2,}/g, ' '));
496 /* Temporary workaround for relative images. */
498 return '/media/dynamic/images/' + this.galleryLink + '/';
502 $.wikiapi.WikiDocument = WikiDocument;