5 var noop = function() {};
6 var noops = {'success': noop, 'failure': noop};
9 * Return absolute reverse path of given named view.
10 * (at least he have it hard-coded in one place)
12 * TODO: think of a way, not to hard-code it here ;)
16 var vname = arguments[0];
18 if(vname == "ajax_document_text") {
19 var path = "/" + arguments[1] + "/text";
20 if (arguments[2] !== undefined)
21 path += "/" + arguments[2];
25 if (vname == "ajax_document_history") {
26 return "/" + arguments[1] + "/history";
29 if (vname == "ajax_document_gallery") {
30 return "/gallery/" + arguments[1];
33 if(vname == "ajax_document_diff")
34 return "/" + arguments[1] + "/diff";
36 console.log("Couldn't reverse match:", vname);
41 * Document Abstraction
43 function WikiDocument(element_id) {
44 var meta = $('#'+element_id);
46 this.id = meta.attr('data-document-name');
47 this.revision = $("*[data-key='revision']", meta).text();
48 this.galleryLink = $("*[data-key='gallery']", meta).text();
49 this.galleryImages = [];
52 this.has_local_changes = false;
54 this._context_lock = -1;
58 // WikiDocument.prototype.lock = function() {
59 // if(this._lock < 0) {
60 // this._lock = Math.random();
61 // this._context_lock = this._lock;
62 // this._lock_count = 1;
67 // if(this._context_lock === this._lock) {
68 // this._lock_count += 1;
72 // throw "Document operation in progress. Try again later."
75 // WikiDocument.prototype.unlock = function(lockNumber) {
76 // if(this.locked === lockNumber) {
77 // this._lock_count -= 1;
79 // if(this._lock_count === 0) {
81 // this._context_lock = -1;
85 // throw "Trying to unlock with wrong lockNumber";
89 // * About to leave context of current lock.
91 // WikiDocument.prototype.leaveContext = function() {
92 // var old = this._context_lock;
93 // this._context_lock = -1;
97 WikiDocument.prototype.triggerDocumentChanged = function() {
98 $(document).trigger('wlapi_document_changed', this);
102 * Fetch text of this document.
104 WikiDocument.prototype.fetch = function(params) {
105 params = $.extend({}, noops, params);
110 url: reverse("ajax_document_text", self.id),
112 success: function(data)
116 if (self.text === null || self.revision !== data.revision) {
117 self.text = data.text;
118 self.revision = data.revision;
119 self.gallery = data.gallery;
121 self.triggerDocumentChanged();
124 self.has_local_changes = false;
125 params['success'](self, changed);
128 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
134 * Fetch history of this document.
136 * from - First revision to fetch (default = 0)
137 * upto - Last revision to fetch (default = tip)
140 WikiDocument.prototype.fetchHistory = function(params) {
141 /* this doesn't modify anything, so no locks */
142 params = $.extend({}, noops, params);
147 url: reverse("ajax_document_history", self.id),
149 data: {"from": params['from'], "upto": params['upto']},
150 success: function(data) {
151 params['success'](self, data);
154 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
159 WikiDocument.prototype.fetchDiff = function(params) {
160 /* this doesn't modify anything, so no locks */
164 'from': self.revision,
170 url: reverse("ajax_document_diff", self.id),
172 data: {"from": params['from'], "to": params['to']},
173 success: function(data) {
174 params['success'](self, data);
177 params['failure'](self, "Nie udało się wczytać porównania wersji.");
185 WikiDocument.prototype.refreshGallery = function(params) {
186 params = $.extend({}, noops, params);
191 url: reverse("ajax_document_gallery", self.galleryLink),
194 success: function(data) {
195 self.galleryImages = data;
196 params['success'](self, data);
199 self.galleryImages = [];
200 params['failure'](self, "<p>Nie udało się wczytać gallerii pod nazwą: '"
201 + self.galleryLink + "'.</p>");
208 * Set document's text
210 WikiDocument.prototype.setText = function(text) {
212 this.has_local_changes = true;
216 * Set document's gallery link
218 WikiDocument.prototype.setGalleryLink = function(gallery) {
219 this.galleryLink = gallery;
220 this.has_local_changes = true;
224 * Save text back to the server
226 WikiDocument.prototype.save = function(params){
227 params = $.extend({}, noops, params);
230 if (!self.has_local_changes) {
231 console.log("Abort: no changes.");
232 return params['success'](self, false, "Nie ma zmian do zapisania.");
235 // Serialize form to dictionary
237 $.each(params['form'].serializeArray(), function() {
238 data[this.name] = this.value;
241 var metaComment = '<!--';
242 metaComment += '\n\tgallery:' + self.galleryLink;
243 metaComment += '\n-->\n'
245 data.text = metaComment + self.text;
246 data.comment = data.comment;
249 url: reverse("ajax_document_text", self.id),
253 success: function(data){
256 self.text = data.text;
257 self.revision = data.revision;
258 self.gallery = data.gallery;
260 self.triggerDocumentChanged();
262 params['success'](self, changed,
263 ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna") );
265 error: function(xhr) {
267 params['failure'](self, $.parseJSON(xhr.responseText));
270 params['failure'](self, {"__message": "<p>Nie udało się zapisać - błąd serwera.</p>"});
274 }; /* end of save() */
276 WikiDocument.prototype.setTag = function(params) {
281 $.wikiapi.WikiDocument = WikiDocument;