3 var noop = function() {
10 * Return absolute reverse path of given named view.
11 * (at least he have it 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 + "/gallery/" + arguments[1];
39 if (vname == "ajax_document_diff")
40 return base_path + "/" + arguments[1] + "/diff";
42 if (vname == "ajax_document_addtag")
43 return base_path + "/" + arguments[1] + "/tags";
45 if (vname == "ajax_publish")
46 return base_path + "/" + arguments[1] + "/publish";
49 return base_path + '/ping/'
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)
111 * upto - Last revision to 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.");
161 WikiDocument.prototype.refreshGallery = function(params) {
162 params = $.extend({}, noops, params);
166 url: reverse("ajax_document_gallery", self.galleryLink),
169 success: function(data) {
170 self.galleryImages = data;
171 params['success'](self, data);
174 self.galleryImages = [];
175 params['failure'](self, "<p>Nie udało się wczytać gallerii pod nazwą: '" + self.galleryLink + "'.</p>");
181 * Set document's text
183 WikiDocument.prototype.setText = function(text) {
185 this.has_local_changes = true;
189 * Set document's gallery link
191 WikiDocument.prototype.setGalleryLink = function(gallery) {
192 this.galleryLink = gallery;
193 this.has_local_changes = true;
197 * Save text back to the server
199 WikiDocument.prototype.save = function(params) {
200 params = $.extend({}, noops, params);
203 if (!self.has_local_changes) {
204 console.log("Abort: no changes.");
205 return params['success'](self, false, "Nie ma zmian do zapisania.");
208 // Serialize form to dictionary
210 $.each(params['form'].serializeArray(), function() {
211 data[this.name] = this.value;
214 var metaComment = '<!--';
215 metaComment += '\n\tgallery:' + self.galleryLink;
216 metaComment += '\n-->\n'
218 data['textsave-text'] = metaComment + self.text;
221 url: reverse("ajax_document_text", self.id),
226 success: function(data) {
230 self.text = data.text;
231 self.revision = data.revision;
232 self.gallery = data.gallery;
234 self.triggerDocumentChanged();
237 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
239 error: function(xhr, msg) {
240 if (msg == 'timeout') {
241 params['failure'](self, {
242 "__message": "Przekroczony czas połączenia. Zmiany nie zostały zapisane."
247 params['failure'](self, $.parseJSON(xhr.responseText));
250 params['failure'](self, {
251 "__message": "Nie udało się zapisać - błąd serwera."
257 }; /* end of save() */
259 WikiDocument.prototype.publish = function(params) {
260 params = $.extend({}, noops, params);
263 url: reverse("ajax_publish", self.id),
266 success: function(data) {
267 params.success(self, data);
269 error: function(xhr) {
270 if (xhr.status == 403 || xhr.status == 401) {
271 params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
275 params.failure(self, xhr.responseText);
278 params.failure(self, "Nie udało się - błąd serwera.");
285 WikiDocument.prototype.setTag = function(params) {
286 params = $.extend({}, noops, params);
289 "addtag-id": self.id,
293 $.each(params.form.serializeArray(), function() {
294 data[this.name] = this.value;
298 url: reverse("ajax_document_addtag", self.id),
302 success: function(data) {
303 params.success(self, data.message);
305 error: function(xhr) {
306 if (xhr.status == 403 || xhr.status == 401) {
307 params.failure(self, {
308 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
313 params.failure(self, $.parseJSON(xhr.responseText));
316 params.failure(self, {
317 "__all__": ["Nie udało się - błąd serwera."]
325 $.wikiapi.WikiDocument = WikiDocument;
328 var ping = function () {
330 url : reverse('ping'),
332 complete : function () {
333 setTimeout(function () {