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 = "/documents";
20 if (vname == "ajax_document_text") {
21 var path = "/" + arguments[1] + "/text";
23 if (arguments[2] !== undefined)
24 path += "/" + arguments[2];
26 return base_path + path;
29 if (vname == "ajax_document_revert") {
30 return base_path + "/" + arguments[1] + "/revert";
34 if (vname == "ajax_document_history") {
36 return base_path + "/" + arguments[1] + "/history";
39 if (vname == "ajax_document_gallery") {
41 return base_path + "/" + arguments[1] + "/gallery";
44 if (vname == "ajax_document_diff")
45 return base_path + "/" + arguments[1] + "/diff";
47 if (vname == "ajax_document_rev")
48 return base_path + "/" + arguments[1] + "/rev";
50 if (vname == "ajax_document_addtag")
51 return base_path + "/" + arguments[1] + "/tags";
53 if (vname == "ajax_publish")
54 return base_path + "/" + arguments[1] + "/publish";
56 console.log("Couldn't reverse match:", vname);
61 * Document Abstraction
63 function WikiDocument(element_id) {
64 var meta = $('#' + element_id);
65 this.id = meta.attr('data-document-name');
67 this.revision = $("*[data-key='revision']", meta).text();
68 this.readonly = !!$("*[data-key='readonly']", meta).text();
70 this.galleryLink = $("*[data-key='gallery']", meta).text();
71 this.galleryImages = [];
73 this.has_local_changes = false;
75 this._context_lock = -1;
79 WikiDocument.prototype.triggerDocumentChanged = function() {
80 $(document).trigger('wlapi_document_changed', this);
83 * Fetch text of this document.
85 WikiDocument.prototype.fetch = function(params) {
86 params = $.extend({}, noops, params);
90 url: reverse("ajax_document_text", self.id),
91 data: {"revision": self.revision},
93 success: function(data) {
96 if (self.text === null || self.revision !== data.revision) {
97 self.text = data.text;
98 self.revision = data.revision;
99 self.gallery = data.gallery;
101 self.triggerDocumentChanged();
104 self.has_local_changes = false;
105 params['success'](self, changed);
108 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
113 * Fetch history of this document.
115 * from - First revision to fetch (default = 0) upto - Last revision to
116 * fetch (default = tip)
119 WikiDocument.prototype.fetchHistory = function(params) {
120 /* this doesn't modify anything, so no locks */
121 params = $.extend({}, noops, params);
125 url: reverse("ajax_document_history", self.id),
128 "from": params['from'],
129 "upto": params['upto']
131 success: function(data) {
132 params['success'](self, data);
135 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
139 WikiDocument.prototype.fetchDiff = function(params) {
140 /* this doesn't modify anything, so no locks */
143 'from': self.revision,
148 url: reverse("ajax_document_diff", self.id),
151 "from": params['from'],
154 success: function(data) {
155 params['success'](self, data);
158 params['failure'](self, "Nie udało się wczytać porównania wersji.");
163 WikiDocument.prototype.checkRevision = function(params) {
164 /* this doesn't modify anything, so no locks */
168 url: reverse("ajax_document_rev", self.id),
170 success: function(data) {
175 else if (data != self.revision)
184 WikiDocument.prototype.refreshGallery = function(params) {
185 params = $.extend({}, noops, params);
189 url: reverse("ajax_document_gallery", self.galleryLink),
192 success: function(data) {
193 self.galleryImages = data;
194 params['success'](self, data);
197 self.galleryImages = [];
198 params['failure'](self, "<p>Nie udało się wczytać galerii pod nazwą: '" + self.galleryLink + "'.</p>");
204 * Set document's text
206 WikiDocument.prototype.setText = function(text) {
208 this.has_local_changes = true;
212 * Set document's gallery link
214 WikiDocument.prototype.setGalleryLink = function(gallery) {
215 this.galleryLink = gallery;
216 this.has_local_changes = true;
220 * Save text back to the server
222 WikiDocument.prototype.save = function(params) {
223 params = $.extend({}, noops, params);
226 if (!self.has_local_changes) {
227 console.log("Abort: no changes.");
228 return params['success'](self, false, "Nie ma zmian do zapisania.");
231 // Serialize form to dictionary
233 $.each(params['form'].serializeArray(), function() {
234 data[this.name] = this.value;
237 var metaComment = '<!--';
238 metaComment += '\n\tgallery:' + self.galleryLink;
239 metaComment += '\n-->\n'
241 data['textsave-text'] = metaComment + self.text;
244 url: reverse("ajax_document_text", self.id),
248 success: function(data) {
251 $('#header').removeClass('saving');
254 self.text = data.text;
255 self.revision = data.revision;
256 self.gallery = data.gallery;
258 self.triggerDocumentChanged();
261 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
263 error: function(xhr) {
264 if ($('#header').hasClass('saving')) {
265 $('#header').removeClass('saving');
267 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
272 params['failure'](self, $.parseJSON(xhr.responseText));
275 params['failure'](self, {
276 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
284 $('#save-hide').click(function(){
285 $('#header').addClass('saving');
287 $.wiki.blocking.unblock();
289 }; /* end of save() */
291 WikiDocument.prototype.revertToVersion = function(params) {
293 params = $.extend({}, noops, params);
295 if (params.revision >= this.revision) {
296 params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
300 // Serialize form to dictionary
302 $.each(params['form'].serializeArray(), function() {
303 data[this.name] = this.value;
307 url: reverse("ajax_document_revert", self.id),
311 success: function(data) {
313 self.text = data.text;
314 self.revision = data.revision;
315 self.gallery = data.gallery;
316 self.triggerDocumentChanged();
318 params.success(self, "Udało się przywrócić wersję :)");
321 params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
324 error: function(xhr) {
325 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
330 WikiDocument.prototype.publish = function(params) {
331 params = $.extend({}, noops, params);
334 url: reverse("ajax_publish", self.id),
337 success: function(data) {
338 params.success(self, data);
340 error: function(xhr) {
341 if (xhr.status == 403 || xhr.status == 401) {
342 params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
346 params.failure(self, xhr.responseText);
349 params.failure(self, "Nie udało się - błąd serwera.");
356 WikiDocument.prototype.setTag = function(params) {
357 params = $.extend({}, noops, params);
360 "addtag-id": self.id,
364 $.each(params.form.serializeArray(), function() {
365 data[this.name] = this.value;
369 url: reverse("ajax_document_addtag", self.id),
373 success: function(data) {
374 params.success(self, data.message);
376 error: function(xhr) {
377 if (xhr.status == 403 || xhr.status == 401) {
378 params.failure(self, {
379 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
384 params.failure(self, $.parseJSON(xhr.responseText));
387 params.failure(self, {
388 "__all__": ["Nie udało się - błąd serwera."]
396 WikiDocument.prototype.getLength = function(params) {
397 var xml = this.text.replace(/\/(\s+)/g, '<br />$1');
398 var parser = new DOMParser();
399 var doc = parser.parseFromString(xml, 'text/xml');
400 var error = $('parsererror', doc);
402 if (error.length > 0) {
403 throw "Not an XML document.";
405 $.xmlns["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
406 $('rdf|RDF, motyw, pa, pe, pr, pt', doc).remove();
407 var text = $(doc).text();
408 text = $.trim(text.replace(/\s{2,}/g, ' '));
413 $.wikiapi.WikiDocument = WikiDocument;