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";
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');
58 this.revision = $("*[data-key='revision']", meta).text();
59 this.galleryLink = $("*[data-key='gallery']", meta).text();
60 this.galleryImages = [];
62 this.has_local_changes = false;
64 this._context_lock = -1;
68 WikiDocument.prototype.triggerDocumentChanged = function() {
69 $(document).trigger('wlapi_document_changed', this);
72 * Fetch text of this document.
74 WikiDocument.prototype.fetch = function(params) {
75 params = $.extend({}, noops, params);
79 url: reverse("ajax_document_text", self.id),
81 success: function(data) {
84 if (self.text === null || self.revision !== data.revision) {
85 self.text = data.text;
86 self.revision = data.revision;
87 self.gallery = data.gallery;
89 self.triggerDocumentChanged();
92 self.has_local_changes = false;
93 params['success'](self, changed);
96 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
101 * Fetch history of this document.
103 * from - First revision to fetch (default = 0)
104 * upto - Last revision to fetch (default = tip)
107 WikiDocument.prototype.fetchHistory = function(params) {
108 /* this doesn't modify anything, so no locks */
109 params = $.extend({}, noops, params);
113 url: reverse("ajax_document_history", self.id),
116 "from": params['from'],
117 "upto": params['upto']
119 success: function(data) {
120 params['success'](self, data);
123 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
127 WikiDocument.prototype.fetchDiff = function(params) {
128 /* this doesn't modify anything, so no locks */
131 'from': self.revision,
136 url: reverse("ajax_document_diff", self.id),
139 "from": params['from'],
142 success: function(data) {
143 params['success'](self, data);
146 params['failure'](self, "Nie udało się wczytać porównania wersji.");
153 WikiDocument.prototype.refreshGallery = function(params) {
154 params = $.extend({}, noops, params);
158 url: reverse("ajax_document_gallery", self.galleryLink),
161 success: function(data) {
162 self.galleryImages = data;
163 params['success'](self, data);
166 self.galleryImages = [];
167 params['failure'](self, "<p>Nie udało się wczytać gallerii pod nazwą: '" + self.galleryLink + "'.</p>");
172 * Set document's text
174 WikiDocument.prototype.setText = function(text) {
176 this.has_local_changes = true;
179 * Set document's gallery link
181 WikiDocument.prototype.setGalleryLink = function(gallery) {
182 this.galleryLink = gallery;
183 this.has_local_changes = true;
186 * Save text back to the server
188 WikiDocument.prototype.save = function(params) {
189 params = $.extend({}, noops, params);
192 if (!self.has_local_changes) {
193 console.log("Abort: no changes.");
194 return params['success'](self, false, "Nie ma zmian do zapisania.");
197 // Serialize form to dictionary
199 $.each(params['form'].serializeArray(), function() {
200 data[this.name] = this.value;
202 var metaComment = '<!--';
203 metaComment += '\n\tgallery:' + self.galleryLink;
204 metaComment += '\n-->\n'
206 data['textsave-text'] = metaComment + self.text;
209 url: reverse("ajax_document_text", self.id),
213 success: function(data) {
217 self.text = data.text;
218 self.revision = data.revision;
219 self.gallery = data.gallery;
221 self.triggerDocumentChanged();
224 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
226 error: function(xhr) {
228 params['failure'](self, $.parseJSON(xhr.responseText));
231 params['failure'](self, {
232 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
238 }; /* end of save() */
239 WikiDocument.prototype.publish = function(params) {
240 params = $.extend({}, noops, params);
243 url: reverse("ajax_publish", self.id),
246 success: function(data) {
247 params.success(self, data);
249 error: function(xhr) {
250 if (xhr.status == 403 || xhr.status == 401) {
251 params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
255 params.failure(self, xhr.responseText);
258 params.failure(self, "Nie udało się - błąd serwera.");
265 WikiDocument.prototype.setTag = function(params) {
266 params = $.extend({}, noops, params);
272 $.each(params.form.serializeArray(), function() {
273 data[this.name] = this.value;
276 url: reverse("ajax_document_addtag", self.id),
280 success: function(data) {
281 params.success(self, data.message);
283 error: function(xhr) {
284 if (xhr.status == 403 || xhr.status == 401) {
285 params.failure(self, {
286 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
291 params.failure(self, $.parseJSON(xhr.responseText));
294 params.failure(self, {
295 "__all__": ["Nie udało się - błąd serwera."]
304 $.wikiapi.WikiDocument = WikiDocument;