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_scans") {
39 return base_path + "/scans/" + arguments[1] + '/';
41 if (vname == "ajax_document_gallery") {
42 return base_path + "/gallery/" + arguments[1] + '/';
45 if (vname == "ajax_document_diff")
46 return base_path + "/diff/" + arguments[1] + '/';
48 if (vname == "ajax_document_rev")
49 return base_path + "/rev/" + arguments[1] + '/';
51 if (vname == "ajax_document_pubmark")
52 return base_path + "/pubmark/" + arguments[1] + '/';
54 if (vname == "ajax_cover_preview")
55 return "/cover/preview/";
57 console.log("Couldn't reverse match:", vname);
61 static csrf = $('input[name="csrfmiddlewaretoken"]').val();
63 // TODO: Add waiting marker, error reporting.
64 static post(url, data) {
65 data['csrfmiddlewaretoken'] = this.csrf;
73 static get(url, callback) {
77 success: function(data) {
83 static setGallery(id, gallery) {
85 '/editor/set-gallery/' + id + '/',
89 static setGalleryStart(id, start) {
91 '/editor/set-gallery-start/' + id + '/',
96 static openGalleryEdit(bookSlug) {
98 '/documents/book/' + bookSlug + '/gallery/',
103 static withGalleryList(callback) {
105 '/editor/galleries/',
112 * Document Abstraction
115 constructor(element_id) {
116 var meta = $('#' + element_id);
117 this.id = meta.attr('data-chunk-id');
119 this.revision = $("*[data-key='revision']", meta).text();
120 this.readonly = !!$("*[data-key='readonly']", meta).text();
122 this.bookSlug = $("*[data-key='book-slug']", meta).text();
123 this.scansLink = $("*[data-key='scans']", meta).text();
124 this.galleryLink = $("*[data-key='gallery']", meta).text();
125 this.galleryStart = parseInt($("*[data-key='gallery-start']", meta).text());
126 this.fullUri = $("*[data-key='full-uri']", meta).text();
129 this.has_local_changes = false;
132 this._context_lock = -1;
133 this._lock_count = 0;
136 triggerDocumentChanged() {
137 $(document).trigger('wlapi_document_changed', this);
141 * Fetch text of this document.
144 params = $.extend({}, noops, params);
148 url: reverse("ajax_document_text", self.id),
149 data: {"revision": self.revision},
151 success: function(data) {
154 if (self.text === null || self.revision !== data.revision) {
155 self.text = data.text;
156 $.wiki.undo.push(data.text);
157 self.revision = data.revision;
158 self.gallery = data.gallery;
160 self.triggerDocumentChanged();
163 self.has_local_changes = false;
164 params['success'](self, changed);
167 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
173 * Fetch history of this document.
175 fetchHistory(params) {
176 /* this doesn't modify anything, so no locks */
177 params = $.extend({}, noops, params);
181 url: reverse("ajax_document_history", self.id) + "?before=" + params.before,
183 success: function(data) {
184 params['success'](self, data);
187 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
193 /* this doesn't modify anything, so no locks */
196 'from': self.revision,
201 url: reverse("ajax_document_diff", self.id),
204 "from": params['from'],
207 success: function(data) {
208 params['success'](self, data);
211 params['failure'](self, "Nie udało się wczytać porównania wersji.");
216 checkRevision(params) {
217 /* this doesn't modify anything, so no locks */
219 let active = self.active;
223 url: reverse("ajax_document_rev", self.id),
228 success: function(data) {
233 else if (data != self.revision)
239 refreshImageGallery(params) {
240 if (this.galleryLink) {
241 params = $.extend({}, params, {
242 url: reverse("ajax_document_gallery", this.galleryLink)
245 this.refreshGallery(params);
248 refreshScansGallery(params) {
249 if (this.scansLink) {
250 params = $.extend({}, params, {
251 url: reverse("ajax_document_scans", this.scansLink)
253 this.refreshGallery(params);
255 // Fallback to image gallery.
256 this.refreshImageGallery(params)
263 refreshGallery(params) {
264 params = $.extend({}, noops, params);
267 params.failure('Brak galerii.');
274 success: function(data) {
275 params.success(data);
277 error: function(xhr) {
278 switch (xhr.status) {
280 var msg = 'Galerie dostępne tylko dla zalogowanych użytkowników.';
283 var msg = "Nie znaleziono galerii.";
285 var msg = "Nie udało się wczytać galerii.";
292 setGallery(gallery) {
293 this.galleryLink = gallery;
294 Api.setGallery(this.id, gallery);
297 setGalleryStart(start) {
298 this.galleryStart = start;
299 Api.setGalleryStart(this.id, start);
302 openGalleryEdit(start) {
303 Api.openGalleryEdit(this.bookSlug);
306 withGalleryList(callback) {
307 Api.withGalleryList(callback);
311 * Set document's text
313 setText(text, silent=false) {
314 if (text == this.text) return;
316 $.wiki.undo.push(text);
319 this.has_local_changes = true;
323 let ctx = $.wiki.exitContext();
328 $.wiki.enterContext(ctx);
331 let ctx = $.wiki.exitContext();
336 $.wiki.enterContext(ctx);
340 * Save text back to the server
343 params = $.extend({}, noops, params);
346 if (!self.has_local_changes) {
347 console.log("Abort: no changes.");
348 return params['success'](self, false, "Nie ma zmian do zapisania.");
351 // Serialize form to dictionary
353 $.each(params['form'].serializeArray(), function() {
354 data[this.name] = this.value;
357 data['textsave-text'] = self.text;
360 url: reverse("ajax_document_text", self.id),
364 success: function(data) {
367 $('#header').removeClass('saving');
370 self.text = data.text;
371 self.revision = data.revision;
372 self.gallery = data.gallery;
374 self.triggerDocumentChanged();
377 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
379 error: function(xhr) {
380 if ($('#header').hasClass('saving')) {
381 $('#header').removeClass('saving');
383 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
388 params['failure'](self, $.parseJSON(xhr.responseText));
391 params['failure'](self, {
392 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
399 $('#save-hide').click(function(){
400 $('#header').addClass('saving');
402 $.wiki.blocking.unblock();
404 } /* end of save() */
406 revertToVersion(params) {
408 params = $.extend({}, noops, params);
410 if (params.revision >= this.revision) {
411 params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
415 // Serialize form to dictionary
417 $.each(params['form'].serializeArray(), function() {
418 data[this.name] = this.value;
422 url: reverse("ajax_document_revert", self.id),
426 success: function(data) {
428 self.text = data.text;
429 self.revision = data.revision;
430 self.gallery = data.gallery;
431 self.triggerDocumentChanged();
433 params.success(self, "Udało się przywrócić wersję :)");
436 params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
439 error: function(xhr) {
440 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
446 params = $.extend({}, noops, params);
449 "pubmark-id": self.id,
453 $.each(params.form.serializeArray(), function() {
454 data[this.name] = this.value;
458 url: reverse("ajax_document_pubmark", self.id),
462 success: function(data) {
463 params.success(self, data.message);
465 error: function(xhr) {
466 if (xhr.status == 403 || xhr.status == 401) {
467 params.failure(self, {
468 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
473 params.failure(self, $.parseJSON(xhr.responseText));
476 params.failure(self, {
477 "__all__": ["Nie udało się - błąd serwera."]
485 refreshCover(params) {
488 xml: self.text // TODO: send just DC
491 url: reverse("ajax_cover_preview"),
494 success: function(data) {
495 params.success(data);
497 error: function(xhr) {
498 // params.failure("Nie udało się odświeżyć okładki - błąd serwera.");
504 params = $.extend({}, noops, params);
505 var xml = this.text.replace(/\/(\s+)/g, '<br />$1');
506 var parser = new DOMParser();
507 var doc = parser.parseFromString(xml, 'text/xml');
508 var error = $('parsererror', doc);
510 if (error.length > 0) {
511 throw "Not an XML document.";
513 $.xmlns["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
514 $('rdf|RDF', doc).remove();
515 if (params.noFootnotes) {
516 $('pa, pe, pr, pt', doc).remove();
518 if (params.noThemes) {
519 $('motyw', doc).remove();
521 var text = $(doc).text();
522 text = $.trim(text.replace(/\s{2,}/g, ' '));
526 /* Temporary workaround for relative images. */
528 return '/media/dynamic/images/' + this.galleryLink + '/';
532 $.wikiapi.WikiDocument = WikiDocument;