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 this.galleryImages = [];
126 this.has_local_changes = false;
129 this._context_lock = -1;
130 this._lock_count = 0;
133 triggerDocumentChanged() {
134 $(document).trigger('wlapi_document_changed', this);
138 * Fetch text of this document.
141 params = $.extend({}, noops, params);
145 url: reverse("ajax_document_text", self.id),
146 data: {"revision": self.revision},
148 success: function(data) {
151 if (self.text === null || self.revision !== data.revision) {
152 self.text = data.text;
153 $.wiki.undo.push(data.text);
154 self.revision = data.revision;
155 self.gallery = data.gallery;
157 self.triggerDocumentChanged();
160 self.has_local_changes = false;
161 params['success'](self, changed);
164 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
170 * Fetch history of this document.
172 fetchHistory(params) {
173 /* this doesn't modify anything, so no locks */
174 params = $.extend({}, noops, params);
178 url: reverse("ajax_document_history", self.id) + "?before=" + params.before,
180 success: function(data) {
181 params['success'](self, data);
184 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
190 /* this doesn't modify anything, so no locks */
193 'from': self.revision,
198 url: reverse("ajax_document_diff", self.id),
201 "from": params['from'],
204 success: function(data) {
205 params['success'](self, data);
208 params['failure'](self, "Nie udało się wczytać porównania wersji.");
213 checkRevision(params) {
214 /* this doesn't modify anything, so no locks */
216 let active = self.active;
220 url: reverse("ajax_document_rev", self.id),
225 success: function(data) {
230 else if (data != self.revision)
239 refreshGallery(params) {
240 params = $.extend({}, noops, params);
242 if (!self.galleryLink) {
243 params['failure'](self, 'Brak galerii.');
248 url: reverse("ajax_document_gallery", self.galleryLink),
251 success: function(data) {
252 self.galleryImages = data;
253 params['success'](self, data);
255 error: function(xhr) {
256 switch (xhr.status) {
258 var msg = 'Galerie dostępne tylko dla zalogowanych użytkowników.';
261 var msg = "Nie znaleziono galerii o nazwie: '" + self.galleryLink + "'.";
263 var msg = "Nie udało się wczytać galerii o nazwie: '" + self.galleryLink + "'.";
265 self.galleryImages = [];
266 params['failure'](self, msg);
271 setGallery(gallery) {
272 this.galleryLink = gallery;
273 Api.setGallery(this.id, gallery);
276 setGalleryStart(start) {
277 this.galleryStart = start;
278 Api.setGalleryStart(this.id, start);
281 openGalleryEdit(start) {
282 Api.openGalleryEdit(this.bookSlug);
285 withGalleryList(callback) {
286 Api.withGalleryList(callback);
290 * Set document's text
292 setText(text, silent=false) {
293 if (text == this.text) return;
295 $.wiki.undo.push(text);
298 this.has_local_changes = true;
302 let ctx = $.wiki.exitContext();
307 $.wiki.enterContext(ctx);
310 let ctx = $.wiki.exitContext();
315 $.wiki.enterContext(ctx);
319 * Save text back to the server
322 params = $.extend({}, noops, params);
325 if (!self.has_local_changes) {
326 console.log("Abort: no changes.");
327 return params['success'](self, false, "Nie ma zmian do zapisania.");
330 // Serialize form to dictionary
332 $.each(params['form'].serializeArray(), function() {
333 data[this.name] = this.value;
336 data['textsave-text'] = self.text;
339 url: reverse("ajax_document_text", self.id),
343 success: function(data) {
346 $('#header').removeClass('saving');
349 self.text = data.text;
350 self.revision = data.revision;
351 self.gallery = data.gallery;
353 self.triggerDocumentChanged();
356 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
358 error: function(xhr) {
359 if ($('#header').hasClass('saving')) {
360 $('#header').removeClass('saving');
362 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
367 params['failure'](self, $.parseJSON(xhr.responseText));
370 params['failure'](self, {
371 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
378 $('#save-hide').click(function(){
379 $('#header').addClass('saving');
381 $.wiki.blocking.unblock();
383 } /* end of save() */
385 revertToVersion(params) {
387 params = $.extend({}, noops, params);
389 if (params.revision >= this.revision) {
390 params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
394 // Serialize form to dictionary
396 $.each(params['form'].serializeArray(), function() {
397 data[this.name] = this.value;
401 url: reverse("ajax_document_revert", self.id),
405 success: function(data) {
407 self.text = data.text;
408 self.revision = data.revision;
409 self.gallery = data.gallery;
410 self.triggerDocumentChanged();
412 params.success(self, "Udało się przywrócić wersję :)");
415 params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
418 error: function(xhr) {
419 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
425 params = $.extend({}, noops, params);
428 "pubmark-id": self.id,
432 $.each(params.form.serializeArray(), function() {
433 data[this.name] = this.value;
437 url: reverse("ajax_document_pubmark", self.id),
441 success: function(data) {
442 params.success(self, data.message);
444 error: function(xhr) {
445 if (xhr.status == 403 || xhr.status == 401) {
446 params.failure(self, {
447 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
452 params.failure(self, $.parseJSON(xhr.responseText));
455 params.failure(self, {
456 "__all__": ["Nie udało się - błąd serwera."]
464 refreshCover(params) {
467 xml: self.text // TODO: send just DC
470 url: reverse("ajax_cover_preview"),
473 success: function(data) {
474 params.success(data);
476 error: function(xhr) {
477 // params.failure("Nie udało się odświeżyć okładki - błąd serwera.");
483 params = $.extend({}, noops, params);
484 var xml = this.text.replace(/\/(\s+)/g, '<br />$1');
485 var parser = new DOMParser();
486 var doc = parser.parseFromString(xml, 'text/xml');
487 var error = $('parsererror', doc);
489 if (error.length > 0) {
490 throw "Not an XML document.";
492 $.xmlns["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
493 $('rdf|RDF', doc).remove();
494 if (params.noFootnotes) {
495 $('pa, pe, pr, pt', doc).remove();
497 if (params.noThemes) {
498 $('motyw', doc).remove();
500 var text = $(doc).text();
501 text = $.trim(text.replace(/\s{2,}/g, ' '));
505 /* Temporary workaround for relative images. */
507 return '/media/dynamic/images/' + this.galleryLink + '/';
511 $.wikiapi.WikiDocument = WikiDocument;