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 self.revision = data.revision;
154 self.gallery = data.gallery;
156 self.triggerDocumentChanged();
159 self.has_local_changes = false;
160 params['success'](self, changed);
163 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
169 * Fetch history of this document.
171 fetchHistory(params) {
172 /* this doesn't modify anything, so no locks */
173 params = $.extend({}, noops, params);
177 url: reverse("ajax_document_history", self.id) + "?before=" + params.before,
179 success: function(data) {
180 params['success'](self, data);
183 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
189 /* this doesn't modify anything, so no locks */
192 'from': self.revision,
197 url: reverse("ajax_document_diff", self.id),
200 "from": params['from'],
203 success: function(data) {
204 params['success'](self, data);
207 params['failure'](self, "Nie udało się wczytać porównania wersji.");
212 checkRevision(params) {
213 /* this doesn't modify anything, so no locks */
215 let active = self.active;
219 url: reverse("ajax_document_rev", self.id),
224 success: function(data) {
229 else if (data != self.revision)
238 refreshGallery(params) {
239 params = $.extend({}, noops, params);
241 if (!self.galleryLink) {
242 params['failure'](self, 'Brak galerii.');
247 url: reverse("ajax_document_gallery", self.galleryLink),
250 success: function(data) {
251 self.galleryImages = data;
252 params['success'](self, data);
254 error: function(xhr) {
255 switch (xhr.status) {
257 var msg = 'Galerie dostępne tylko dla zalogowanych użytkowników.';
260 var msg = "Nie znaleziono galerii o nazwie: '" + self.galleryLink + "'.";
262 var msg = "Nie udało się wczytać galerii o nazwie: '" + self.galleryLink + "'.";
264 self.galleryImages = [];
265 params['failure'](self, msg);
270 setGallery(gallery) {
271 this.galleryLink = gallery;
272 Api.setGallery(this.id, gallery);
275 setGalleryStart(start) {
276 this.galleryStart = start;
277 Api.setGalleryStart(this.id, start);
280 openGalleryEdit(start) {
281 Api.openGalleryEdit(this.bookSlug);
284 withGalleryList(callback) {
285 Api.withGalleryList(callback);
289 * Set document's text
292 if (text == this.text) return;
294 this.has_local_changes = true;
298 * Save text back to the server
301 params = $.extend({}, noops, params);
304 if (!self.has_local_changes) {
305 console.log("Abort: no changes.");
306 return params['success'](self, false, "Nie ma zmian do zapisania.");
309 // Serialize form to dictionary
311 $.each(params['form'].serializeArray(), function() {
312 data[this.name] = this.value;
315 data['textsave-text'] = self.text;
318 url: reverse("ajax_document_text", self.id),
322 success: function(data) {
325 $('#header').removeClass('saving');
328 self.text = data.text;
329 self.revision = data.revision;
330 self.gallery = data.gallery;
332 self.triggerDocumentChanged();
335 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
337 error: function(xhr) {
338 if ($('#header').hasClass('saving')) {
339 $('#header').removeClass('saving');
341 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
346 params['failure'](self, $.parseJSON(xhr.responseText));
349 params['failure'](self, {
350 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
357 $('#save-hide').click(function(){
358 $('#header').addClass('saving');
360 $.wiki.blocking.unblock();
362 } /* end of save() */
364 revertToVersion(params) {
366 params = $.extend({}, noops, params);
368 if (params.revision >= this.revision) {
369 params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
373 // Serialize form to dictionary
375 $.each(params['form'].serializeArray(), function() {
376 data[this.name] = this.value;
380 url: reverse("ajax_document_revert", self.id),
384 success: function(data) {
386 self.text = data.text;
387 self.revision = data.revision;
388 self.gallery = data.gallery;
389 self.triggerDocumentChanged();
391 params.success(self, "Udało się przywrócić wersję :)");
394 params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
397 error: function(xhr) {
398 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
404 params = $.extend({}, noops, params);
407 "pubmark-id": self.id,
411 $.each(params.form.serializeArray(), function() {
412 data[this.name] = this.value;
416 url: reverse("ajax_document_pubmark", self.id),
420 success: function(data) {
421 params.success(self, data.message);
423 error: function(xhr) {
424 if (xhr.status == 403 || xhr.status == 401) {
425 params.failure(self, {
426 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
431 params.failure(self, $.parseJSON(xhr.responseText));
434 params.failure(self, {
435 "__all__": ["Nie udało się - błąd serwera."]
443 refreshCover(params) {
446 xml: self.text // TODO: send just DC
449 url: reverse("ajax_cover_preview"),
452 success: function(data) {
453 params.success(data);
455 error: function(xhr) {
456 // params.failure("Nie udało się odświeżyć okładki - błąd serwera.");
462 params = $.extend({}, noops, params);
463 var xml = this.text.replace(/\/(\s+)/g, '<br />$1');
464 var parser = new DOMParser();
465 var doc = parser.parseFromString(xml, 'text/xml');
466 var error = $('parsererror', doc);
468 if (error.length > 0) {
469 throw "Not an XML document.";
471 $.xmlns["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
472 $('rdf|RDF', doc).remove();
473 if (params.noFootnotes) {
474 $('pa, pe, pr, pt', doc).remove();
476 if (params.noThemes) {
477 $('motyw', doc).remove();
479 var text = $(doc).text();
480 text = $.trim(text.replace(/\s{2,}/g, ' '));
484 /* Temporary workaround for relative images. */
486 return '/media/dynamic/images/' + this.galleryLink + '/';
490 $.wikiapi.WikiDocument = WikiDocument;