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 = "/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") {
36 return base_path + "/history/" + arguments[1] + '/';
39 if (vname == "ajax_document_gallery") {
41 return base_path + "/gallery/" + arguments[1] + '/';
44 if (vname == "ajax_document_diff")
45 return base_path + "/diff/" + arguments[1] + '/';
47 if (vname == "ajax_document_rev")
48 return base_path + "/rev/" + arguments[1] + '/';
50 if (vname == "ajax_document_addtag")
51 return base_path + "/tags/" + arguments[1] + '/';
53 if (vname == "ajax_publish")
54 return base_path + "/publish/" + arguments[1] + '/';
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-book') + '/' + meta.attr('data-chunk');
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 data['textsave-text'] = self.text;
240 url: reverse("ajax_document_text", self.id),
244 success: function(data) {
247 $('#header').removeClass('saving');
250 self.text = data.text;
251 self.revision = data.revision;
252 self.gallery = data.gallery;
254 self.triggerDocumentChanged();
257 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
259 error: function(xhr) {
260 if ($('#header').hasClass('saving')) {
261 $('#header').removeClass('saving');
263 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
268 params['failure'](self, $.parseJSON(xhr.responseText));
271 params['failure'](self, {
272 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
280 $('#save-hide').click(function(){
281 $('#header').addClass('saving');
283 $.wiki.blocking.unblock();
285 }; /* end of save() */
287 WikiDocument.prototype.revertToVersion = function(params) {
289 params = $.extend({}, noops, params);
291 if (params.revision >= this.revision) {
292 params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
296 // Serialize form to dictionary
298 $.each(params['form'].serializeArray(), function() {
299 data[this.name] = this.value;
303 url: reverse("ajax_document_revert", self.id),
307 success: function(data) {
309 self.text = data.text;
310 self.revision = data.revision;
311 self.gallery = data.gallery;
312 self.triggerDocumentChanged();
314 params.success(self, "Udało się przywrócić wersję :)");
317 params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
320 error: function(xhr) {
321 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
326 WikiDocument.prototype.publish = function(params) {
327 params = $.extend({}, noops, params);
330 url: reverse("ajax_publish", self.id),
333 success: function(data) {
334 params.success(self, data);
336 error: function(xhr) {
337 if (xhr.status == 403 || xhr.status == 401) {
338 params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
342 params.failure(self, xhr.responseText);
345 params.failure(self, "Nie udało się - błąd serwera.");
352 WikiDocument.prototype.setTag = function(params) {
353 params = $.extend({}, noops, params);
356 "addtag-id": self.id,
360 $.each(params.form.serializeArray(), function() {
361 data[this.name] = this.value;
365 url: reverse("ajax_document_addtag", self.id),
369 success: function(data) {
370 params.success(self, data.message);
372 error: function(xhr) {
373 if (xhr.status == 403 || xhr.status == 401) {
374 params.failure(self, {
375 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
380 params.failure(self, $.parseJSON(xhr.responseText));
383 params.failure(self, {
384 "__all__": ["Nie udało się - błąd serwera."]
392 $.wikiapi.WikiDocument = WikiDocument;