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) {
225 $('#header').removeClass('saving');
228 self.text = data.text;
229 self.revision = data.revision;
230 self.gallery = data.gallery;
232 self.triggerDocumentChanged();
235 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
237 error: function(xhr) {
238 if ($('#header').hasClass('saving')) {
239 $('#header').removeClass('saving');
241 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
246 params['failure'](self, $.parseJSON(xhr.responseText));
249 params['failure'](self, {
250 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
258 $('#save-hide').click(function(){
259 $('#header').addClass('saving');
261 $.wiki.blocking.unblock();
263 }; /* end of save() */
265 WikiDocument.prototype.publish = function(params) {
266 params = $.extend({}, noops, params);
269 url: reverse("ajax_publish", self.id),
272 success: function(data) {
273 params.success(self, data);
275 error: function(xhr) {
276 if (xhr.status == 403 || xhr.status == 401) {
277 params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
281 params.failure(self, xhr.responseText);
284 params.failure(self, "Nie udało się - błąd serwera.");
291 WikiDocument.prototype.setTag = function(params) {
292 params = $.extend({}, noops, params);
295 "addtag-id": self.id,
299 $.each(params.form.serializeArray(), function() {
300 data[this.name] = this.value;
304 url: reverse("ajax_document_addtag", self.id),
308 success: function(data) {
309 params.success(self, data.message);
311 error: function(xhr) {
312 if (xhr.status == 403 || xhr.status == 401) {
313 params.failure(self, {
314 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
319 params.failure(self, $.parseJSON(xhr.responseText));
322 params.failure(self, {
323 "__all__": ["Nie udało się - błąd serwera."]
331 $.wikiapi.WikiDocument = WikiDocument;