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_diff")
30 return "/" + arguments[1] + "/diff/" + arguments[2] + "/" + arguments[3]
32 console.log("Couldn't reverse match:", vname);
37 * Document Abstraction
39 function WikiDocument(element_id) {
40 var meta = $('#'+element_id);
42 this.id = meta.attr('data-document-name');
43 this.revision = $("*[data-key='revision']", meta).text();
44 this.gallery = $("*[data-key='gallery']", meta).text();
48 this.has_local_changes = false;
50 this._context_lock = -1;
54 // WikiDocument.prototype.lock = function() {
55 // if(this._lock < 0) {
56 // this._lock = Math.random();
57 // this._context_lock = this._lock;
58 // this._lock_count = 1;
63 // if(this._context_lock === this._lock) {
64 // this._lock_count += 1;
68 // throw "Document operation in progress. Try again later."
71 // WikiDocument.prototype.unlock = function(lockNumber) {
72 // if(this.locked === lockNumber) {
73 // this._lock_count -= 1;
75 // if(this._lock_count === 0) {
77 // this._context_lock = -1;
81 // throw "Trying to unlock with wrong lockNumber";
85 // * About to leave context of current lock.
87 // WikiDocument.prototype.leaveContext = function() {
88 // var old = this._context_lock;
89 // this._context_lock = -1;
94 * Fetch text of this document.
96 WikiDocument.prototype.fetch = function(params) {
97 params = $.extend({}, noops, params);
102 url: reverse("ajax_document_text", self.id),
104 success: function(data)
108 if (self.text === null || self.revision !== data.revision) {
109 self.text = data.text;
110 self.revision = data.revision;
111 self.gallery = data.gallery;
115 self.has_local_changes = false;
116 params['success'](self, changed);
119 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
125 * Fetch history of this document.
127 * from - First revision to fetch (default = 0)
128 * upto - Last revision to fetch (default = tip)
131 WikiDocument.prototype.fetchHistory = function(params) {
132 /* this doesn't modify anything, so no locks */
133 params = $.extend({}, noops, params);
138 url: reverse("ajax_document_history", self.id),
140 data: {"from": params['from'], "upto": params['upto']},
141 success: function(data) {
142 params['success'](self, data);
145 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
151 * Set document's text
153 WikiDocument.prototype.setText = function(text) {
155 this.has_local_changes = true;
159 * Set document's gallery link
161 WikiDocument.prototype.setGallery = function(gallery) {
162 this.gallery = gallery;
163 this.has_local_changes = true;
167 * Save text back to the server
169 WikiDocument.prototype.save = function(comment, success, failure){
172 if (!self.has_local_changes) {
173 return success(self, "Nie ma zmian do zapisania.");
176 /* you can't set text while some is fetching it (or saving) */
177 var metaComment = '<!--';
178 metaComment += '\n\tgallery:' + self.gallery;
179 metaComment += '\n-->'
184 parent_revision: self.revision,
189 url: reverse("ajax_document_text", self.id),
193 success: function(data){
196 self.text = data.text;
197 self.revision = data.revision;
198 self.gallery = data.gallery;
201 success(self, changed);
204 failure(self, "Nie udało się zapisać.");
207 }; /* end of save() */
209 $.wikiapi.WikiDocument = WikiDocument;