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_addtag")
43 return base_path + "/" + arguments[1] + "/tags";
45 if (vname == "ajax_publish")
46 return base_path + "/" + arguments[1] + "/publish";
48 console.log("Couldn't reverse match:", vname);
53 * Document Abstraction
55 function WikiDocument(element_id) {
56 var meta = $('#' + element_id);
57 this.id = meta.attr('data-document-name');
59 this.revision = $("*[data-key='revision']", meta).text();
60 this.readonly = !!$("*[data-key='readonly']", meta).text();
62 this.galleryLink = $("*[data-key='gallery']", meta).text();
63 this.galleryImages = [];
65 this.has_local_changes = false;
67 this._context_lock = -1;
71 WikiDocument.prototype.triggerDocumentChanged = function() {
72 $(document).trigger('wlapi_document_changed', this);
75 * Fetch text of this document.
77 WikiDocument.prototype.fetch = function(params) {
78 params = $.extend({}, noops, params);
82 url: reverse("ajax_document_text", self.id),
83 data: {"revision": self.revision},
85 success: function(data) {
88 if (self.text === null || self.revision !== data.revision) {
89 self.text = data.text;
90 self.revision = data.revision;
91 self.gallery = data.gallery;
93 self.triggerDocumentChanged();
96 self.has_local_changes = false;
97 params['success'](self, changed);
100 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
105 * Fetch history of this document.
107 * from - First revision to fetch (default = 0) upto - Last revision to
108 * fetch (default = tip)
111 WikiDocument.prototype.fetchHistory = function(params) {
112 /* this doesn't modify anything, so no locks */
113 params = $.extend({}, noops, params);
117 url: reverse("ajax_document_history", self.id),
120 "from": params['from'],
121 "upto": params['upto']
123 success: function(data) {
124 params['success'](self, data);
127 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
131 WikiDocument.prototype.fetchDiff = function(params) {
132 /* this doesn't modify anything, so no locks */
135 'from': self.revision,
140 url: reverse("ajax_document_diff", self.id),
143 "from": params['from'],
146 success: function(data) {
147 params['success'](self, data);
150 params['failure'](self, "Nie udało się wczytać porównania wersji.");
158 WikiDocument.prototype.refreshGallery = function(params) {
159 params = $.extend({}, noops, params);
163 url: reverse("ajax_document_gallery", self.galleryLink),
166 success: function(data) {
167 self.galleryImages = data;
168 params['success'](self, data);
171 self.galleryImages = [];
172 params['failure'](self, "<p>Nie udało się wczytać gallerii pod nazwą: '" + self.galleryLink + "'.</p>");
178 * Set document's text
180 WikiDocument.prototype.setText = function(text) {
182 this.has_local_changes = true;
186 * Set document's gallery link
188 WikiDocument.prototype.setGalleryLink = function(gallery) {
189 this.galleryLink = gallery;
190 this.has_local_changes = true;
194 * Save text back to the server
196 WikiDocument.prototype.save = function(params) {
197 params = $.extend({}, noops, params);
200 if (!self.has_local_changes) {
201 console.log("Abort: no changes.");
202 return params['success'](self, false, "Nie ma zmian do zapisania.");
205 // Serialize form to dictionary
207 $.each(params['form'].serializeArray(), function() {
208 data[this.name] = this.value;
211 var metaComment = '<!--';
212 metaComment += '\n\tgallery:' + self.galleryLink;
213 metaComment += '\n-->\n'
215 data['textsave-text'] = metaComment + self.text;
218 url: reverse("ajax_document_text", self.id),
222 success: function(data) {
226 self.text = data.text;
227 self.revision = data.revision;
228 self.gallery = data.gallery;
230 self.triggerDocumentChanged();
233 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
235 error: function(xhr) {
237 params['failure'](self, $.parseJSON(xhr.responseText));
240 params['failure'](self, {
241 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
247 }; /* end of save() */
249 WikiDocument.prototype.publish = function(params) {
250 params = $.extend({}, noops, params);
253 url: reverse("ajax_publish", self.id),
256 success: function(data) {
257 params.success(self, data);
259 error: function(xhr) {
260 if (xhr.status == 403 || xhr.status == 401) {
261 params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
265 params.failure(self, xhr.responseText);
268 params.failure(self, "Nie udało się - błąd serwera.");
275 WikiDocument.prototype.setTag = function(params) {
276 params = $.extend({}, noops, params);
279 "addtag-id": self.id,
283 $.each(params.form.serializeArray(), function() {
284 data[this.name] = this.value;
288 url: reverse("ajax_document_addtag", self.id),
292 success: function(data) {
293 params.success(self, data.message);
295 error: function(xhr) {
296 if (xhr.status == 403 || xhr.status == 401) {
297 params.failure(self, {
298 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
303 params.failure(self, $.parseJSON(xhr.responseText));
306 params.failure(self, {
307 "__all__": ["Nie udało się - błąd serwera."]
315 $.wikiapi.WikiDocument = WikiDocument;