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');
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)
108 * upto - Last revision to 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),
223 success: function(data) {
227 self.text = data.text;
228 self.revision = data.revision;
229 self.gallery = data.gallery;
231 self.triggerDocumentChanged();
234 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
236 error: function(xhr, msg) {
237 if (msg == 'timeout') {
238 params['failure'](self, {
239 "__message": "Przekroczony czas połączenia. Zmiany nie zostały zapisane."
244 params['failure'](self, $.parseJSON(xhr.responseText));
247 params['failure'](self, {
248 "__message": "Nie udało się zapisać - błąd serwera."
254 }; /* end of save() */
256 WikiDocument.prototype.publish = function(params) {
257 params = $.extend({}, noops, params);
260 url: reverse("ajax_publish", self.id),
263 success: function(data) {
264 params.success(self, data);
266 error: function(xhr) {
267 if (xhr.status == 403 || xhr.status == 401) {
268 params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
272 params.failure(self, xhr.responseText);
275 params.failure(self, "Nie udało się - błąd serwera.");
282 WikiDocument.prototype.setTag = function(params) {
283 params = $.extend({}, noops, params);
286 "addtag-id": self.id,
290 $.each(params.form.serializeArray(), function() {
291 data[this.name] = this.value;
295 url: reverse("ajax_document_addtag", self.id),
299 success: function(data) {
300 params.success(self, data.message);
302 error: function(xhr) {
303 if (xhr.status == 403 || xhr.status == 401) {
304 params.failure(self, {
305 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
310 params.failure(self, $.parseJSON(xhr.responseText));
313 params.failure(self, {
314 "__all__": ["Nie udało się - błąd serwera."]
322 $.wikiapi.WikiDocument = WikiDocument;