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;
98 * Fetch text of this document.
100 WikiDocument.prototype.fetch = function(params) {
101 params = $.extend({}, noops, params);
106 url: reverse("ajax_document_text", self.id),
108 success: function(data)
112 if (self.text === null || self.revision !== data.revision) {
113 self.text = data.text;
114 self.revision = data.revision;
115 self.gallery = data.gallery;
119 self.has_local_changes = false;
120 params['success'](self, changed);
123 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
129 * Fetch history of this document.
131 * from - First revision to fetch (default = 0)
132 * upto - Last revision to fetch (default = tip)
135 WikiDocument.prototype.fetchHistory = function(params) {
136 /* this doesn't modify anything, so no locks */
137 params = $.extend({}, noops, params);
142 url: reverse("ajax_document_history", self.id),
144 data: {"from": params['from'], "upto": params['upto']},
145 success: function(data) {
146 params['success'](self, data);
149 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
154 WikiDocument.prototype.fetchDiff = function(params) {
155 /* this doesn't modify anything, so no locks */
157 'from': self.revision,
165 url: reverse("ajax_document_diff", self.id),
167 data: {"from": params['from'], "to": params['to']},
168 success: function(data) {
169 params['success'](self, data);
172 params['failure'](self, "Nie udało się wczytać porównania wersji.");
180 WikiDocument.prototype.refreshGallery = function(params) {
181 params = $.extend({}, noops, params);
186 url: reverse("ajax_document_gallery", self.galleryLink),
189 success: function(data) {
190 this.galleryImages = data.images;
191 params['success'](self, data);
194 this.galleryImages = [];
195 params['failure'](self, "<p>Nie udało się wczytać gallerii pod nazwą: '"
196 + self.galleryLink + "'.</p>");
203 * Set document's text
205 WikiDocument.prototype.setText = function(text) {
206 if (this.text != text) {
208 this.has_local_changes = true;
213 * Set document's gallery link
215 WikiDocument.prototype.setGalleryLink = function(gallery) {
216 this.galleryLink = gallery;
217 this.has_local_changes = true;
221 * Save text back to the server
223 WikiDocument.prototype.save = function(params){
224 params = $.extend({'comment': 'No comment.'}, noops, params);
227 /* you can't set text while some is fetching it (or saving) */
229 if (!self.has_local_changes) {
230 console.log("Abort: no changes.");
231 return params['success'](self, false, "Nie ma zmian do zapisania.");
234 var metaComment = '<!--';
235 metaComment += '\n\tgallery:' + self.galleryLink;
236 metaComment += '\n-->\n'
240 text: metaComment + self.text,
241 parent_revision: self.revision,
242 comment: params['comment'],
246 url: reverse("ajax_document_text", self.id),
250 success: function(data){
253 self.text = data.text;
254 self.revision = data.revision;
255 self.gallery = data.gallery;
258 params['success'](self, changed, "Zapisano");
261 params['failure'](self, "Nie udało się zapisać.");
264 }; /* end of save() */
266 $.wikiapi.WikiDocument = WikiDocument;