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_history") {
31 return base_path + "/" + arguments[1] + "/history";
34 if (vname == "ajax_document_gallery") {
36 return base_path + "/" + arguments[1] + "/gallery";
39 if (vname == "ajax_document_diff")
40 return base_path + "/" + arguments[1] + "/diff";
42 if (vname == "ajax_document_rev")
43 return base_path + "/" + arguments[1] + "/rev";
45 if (vname == "ajax_document_addtag")
46 return base_path + "/" + arguments[1] + "/tags";
48 if (vname == "ajax_publish")
49 return base_path + "/" + arguments[1] + "/publish";
51 console.log("Couldn't reverse match:", vname);
56 * Document Abstraction
58 function WikiDocument(element_id) {
59 var meta = $('#' + element_id);
60 this.id = meta.attr('data-document-name');
62 this.revision = $("*[data-key='revision']", meta).text();
63 this.readonly = !!$("*[data-key='readonly']", meta).text();
65 this.galleryLink = $("*[data-key='gallery']", meta).text();
66 this.galleryImages = [];
68 this.has_local_changes = false;
70 this._context_lock = -1;
74 WikiDocument.prototype.triggerDocumentChanged = function() {
75 $(document).trigger('wlapi_document_changed', this);
78 * Fetch text of this document.
80 WikiDocument.prototype.fetch = function(params) {
81 params = $.extend({}, noops, params);
85 url: reverse("ajax_document_text", self.id),
86 data: {"revision": self.revision},
88 success: function(data) {
91 if (self.text === null || self.revision !== data.revision) {
92 self.text = data.text;
93 self.revision = data.revision;
94 self.gallery = data.gallery;
96 self.triggerDocumentChanged();
99 self.has_local_changes = false;
100 params['success'](self, changed);
103 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
108 * Fetch history of this document.
110 * from - First revision to fetch (default = 0) upto - Last revision to
111 * fetch (default = tip)
114 WikiDocument.prototype.fetchHistory = function(params) {
115 /* this doesn't modify anything, so no locks */
116 params = $.extend({}, noops, params);
120 url: reverse("ajax_document_history", self.id),
123 "from": params['from'],
124 "upto": params['upto']
126 success: function(data) {
127 params['success'](self, data);
130 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
134 WikiDocument.prototype.fetchDiff = function(params) {
135 /* this doesn't modify anything, so no locks */
138 'from': self.revision,
143 url: reverse("ajax_document_diff", self.id),
146 "from": params['from'],
149 success: function(data) {
150 params['success'](self, data);
153 params['failure'](self, "Nie udało się wczytać porównania wersji.");
158 WikiDocument.prototype.checkRevision = function(params) {
159 /* this doesn't modify anything, so no locks */
163 url: reverse("ajax_document_rev", self.id),
165 success: function(data) {
166 if (data == '' && params.error)
168 else if (data != self.revision)
177 WikiDocument.prototype.refreshGallery = function(params) {
178 params = $.extend({}, noops, params);
182 url: reverse("ajax_document_gallery", self.galleryLink),
185 success: function(data) {
186 self.galleryImages = data;
187 params['success'](self, data);
190 self.galleryImages = [];
191 params['failure'](self, "<p>Nie udało się wczytać galerii pod nazwą: '" + self.galleryLink + "'.</p>");
197 * Set document's text
199 WikiDocument.prototype.setText = function(text) {
201 this.has_local_changes = true;
205 * Set document's gallery link
207 WikiDocument.prototype.setGalleryLink = function(gallery) {
208 this.galleryLink = gallery;
209 this.has_local_changes = true;
213 * Save text back to the server
215 WikiDocument.prototype.save = function(params) {
216 params = $.extend({}, noops, params);
219 if (!self.has_local_changes) {
220 console.log("Abort: no changes.");
221 return params['success'](self, false, "Nie ma zmian do zapisania.");
224 // Serialize form to dictionary
226 $.each(params['form'].serializeArray(), function() {
227 data[this.name] = this.value;
230 var metaComment = '<!--';
231 metaComment += '\n\tgallery:' + self.galleryLink;
232 metaComment += '\n-->\n'
234 data['textsave-text'] = metaComment + self.text;
237 url: reverse("ajax_document_text", self.id),
241 success: function(data) {
244 $('#header').removeClass('saving');
247 self.text = data.text;
248 self.revision = data.revision;
249 self.gallery = data.gallery;
251 self.triggerDocumentChanged();
254 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
256 error: function(xhr) {
257 if ($('#header').hasClass('saving')) {
258 $('#header').removeClass('saving');
260 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
265 params['failure'](self, $.parseJSON(xhr.responseText));
268 params['failure'](self, {
269 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
277 $('#save-hide').click(function(){
278 $('#header').addClass('saving');
280 $.wiki.blocking.unblock();
282 }; /* end of save() */
284 WikiDocument.prototype.publish = function(params) {
285 params = $.extend({}, noops, params);
288 url: reverse("ajax_publish", self.id),
291 success: function(data) {
292 params.success(self, data);
294 error: function(xhr) {
295 if (xhr.status == 403 || xhr.status == 401) {
296 params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
300 params.failure(self, xhr.responseText);
303 params.failure(self, "Nie udało się - błąd serwera.");
310 WikiDocument.prototype.setTag = function(params) {
311 params = $.extend({}, noops, params);
314 "addtag-id": self.id,
318 $.each(params.form.serializeArray(), function() {
319 data[this.name] = this.value;
323 url: reverse("ajax_document_addtag", self.id),
327 success: function(data) {
328 params.success(self, data.message);
330 error: function(xhr) {
331 if (xhr.status == 403 || xhr.status == 401) {
332 params.failure(self, {
333 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
338 params.failure(self, $.parseJSON(xhr.responseText));
341 params.failure(self, {
342 "__all__": ["Nie udało się - błąd serwera."]
350 $.wikiapi.WikiDocument = WikiDocument;