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;
130 this.active = new Date();
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 = new Date() - self.active < 30 * 1000;
222 url: reverse("ajax_document_rev", self.id),
228 success: function(data) {
234 let people = $('<div>');
235 data.people.forEach((p) => {
236 let item = $('<img>');
237 item.attr('src', p.gravatar),
241 (p.active ? 'akt.' : 'nieakt.') +
242 ' od ' + p.since + ')')
244 item.addClass('active');
248 $("#people").html(people);
250 if (data.rev != self.revision) {
258 refreshImageGallery(params) {
259 if (this.galleryLink) {
260 params = $.extend({}, params, {
261 url: reverse("ajax_document_gallery", this.galleryLink)
264 this.refreshGallery(params);
267 refreshScansGallery(params) {
268 if (this.scansLink) {
269 params = $.extend({}, params, {
270 url: reverse("ajax_document_scans", this.scansLink)
272 this.refreshGallery(params);
274 // Fallback to image gallery.
275 this.refreshImageGallery(params)
282 refreshGallery(params) {
283 params = $.extend({}, noops, params);
286 params.failure('Brak galerii.');
293 success: function(data) {
294 params.success(data);
296 error: function(xhr) {
297 switch (xhr.status) {
299 var msg = 'Galerie dostępne tylko dla zalogowanych użytkowników.';
302 var msg = "Nie znaleziono galerii.";
304 var msg = "Nie udało się wczytać galerii.";
311 setGallery(gallery) {
312 this.galleryLink = gallery;
313 Api.setGallery(this.id, gallery);
316 setGalleryStart(start) {
317 this.galleryStart = start;
318 Api.setGalleryStart(this.id, start);
321 openGalleryEdit(start) {
322 Api.openGalleryEdit(this.bookSlug);
325 withGalleryList(callback) {
326 Api.withGalleryList(callback);
330 * Set document's text
332 setText(text, silent=false) {
333 if (text == this.text) return;
335 $.wiki.undo.push(text);
338 this.has_local_changes = true;
342 let ctx = $.wiki.exitContext();
347 $.wiki.enterContext(ctx);
350 let ctx = $.wiki.exitContext();
355 $.wiki.enterContext(ctx);
359 * Save text back to the server
362 params = $.extend({}, noops, params);
365 if (!self.has_local_changes) {
366 console.log("Abort: no changes.");
367 return params['success'](self, false, "Nie ma zmian do zapisania.");
370 // Serialize form to dictionary
372 $.each(params['form'].serializeArray(), function() {
373 data[this.name] = this.value;
376 data['textsave-text'] = self.text;
379 url: reverse("ajax_document_text", self.id),
383 success: function(data) {
386 $('#header').removeClass('saving');
389 self.text = data.text;
390 self.revision = data.revision;
391 self.gallery = data.gallery;
393 self.triggerDocumentChanged();
396 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
398 error: function(xhr) {
399 if ($('#header').hasClass('saving')) {
400 $('#header').removeClass('saving');
402 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
407 params['failure'](self, $.parseJSON(xhr.responseText));
410 params['failure'](self, {
411 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
418 $('#save-hide').click(function(){
419 $('#header').addClass('saving');
421 $.wiki.blocking.unblock();
423 } /* end of save() */
425 revertToVersion(params) {
427 params = $.extend({}, noops, params);
429 if (params.revision >= this.revision) {
430 params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
434 // Serialize form to dictionary
436 $.each(params['form'].serializeArray(), function() {
437 data[this.name] = this.value;
441 url: reverse("ajax_document_revert", self.id),
445 success: function(data) {
447 self.text = data.text;
448 self.revision = data.revision;
449 self.gallery = data.gallery;
450 self.triggerDocumentChanged();
452 params.success(self, "Udało się przywrócić wersję :)");
455 params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
458 error: function(xhr) {
459 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
465 params = $.extend({}, noops, params);
468 "pubmark-id": self.id,
472 $.each(params.form.serializeArray(), function() {
473 data[this.name] = this.value;
477 url: reverse("ajax_document_pubmark", self.id),
481 success: function(data) {
482 params.success(self, data.message);
484 error: function(xhr) {
485 if (xhr.status == 403 || xhr.status == 401) {
486 params.failure(self, {
487 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
492 params.failure(self, $.parseJSON(xhr.responseText));
495 params.failure(self, {
496 "__all__": ["Nie udało się - błąd serwera."]
504 refreshCover(params) {
507 xml: self.text // TODO: send just DC
510 url: reverse("ajax_cover_preview"),
513 success: function(data) {
514 params.success(data);
516 error: function(xhr) {
517 // params.failure("Nie udało się odświeżyć okładki - błąd serwera.");
523 params = $.extend({}, noops, params);
524 var xml = this.text.replace(/\/(\s+)/g, '<br />$1');
525 var parser = new DOMParser();
526 var doc = parser.parseFromString(xml, 'text/xml');
527 var error = $('parsererror', doc);
529 if (error.length > 0) {
530 throw "Not an XML document.";
532 $.xmlns["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
533 $('rdf|RDF', doc).remove();
534 if (params.noFootnotes) {
535 $('pa, pe, pr, pt', doc).remove();
537 if (params.noThemes) {
538 $('motyw', doc).remove();
540 var text = $(doc).text();
541 text = $.trim(text.replace(/\s{2,}/g, ' '));
545 /* Temporary workaround for relative images. */
547 return '/media/dynamic/images/' + this.galleryLink + '/';
551 $.wikiapi.WikiDocument = WikiDocument;