ee444ca1553b53b1176516f55363c934a5ce84f5
[redakcja.git] / platforma / static / js / wiki / wikiapi.js
1 (function($) {
2         
3         $.wikiapi = {};
4         
5         var noop = function() {};
6         var noops = {'success': noop, 'failure': noop};
7         
8         /*
9          * Return absolute reverse path of given named view.
10          * (at least he have it hard-coded in one place)
11          * 
12          * TODO: think of a way, not to hard-code it here ;) 
13          * 
14          */
15         function reverse() {
16                 var vname = arguments[0];
17                         
18                 if(vname == "ajax_document_text") {
19                         var path = "/" + arguments[1] + "/text";
20                         if (arguments[2] !== undefined) 
21                                 path += "/" + arguments[2];
22                         return path;
23                 }
24                         
25                 if (vname == "ajax_document_history") {
26                         return "/" + arguments[1] + "/history";
27                 }
28                 
29                 if (vname == "ajax_document_gallery") {
30                         return "/gallery/" + arguments[1];
31                 }                                               
32                                         
33                 if(vname == "ajax_document_diff")
34                         return "/" + arguments[1] + "/diff"; 
35                         
36                 console.log("Couldn't reverse match:", vname);
37                 return "/404.html";             
38         };
39         
40         /*
41          * Document Abstraction  
42          */
43         function WikiDocument(element_id) {
44                 var meta = $('#'+element_id);
45                 
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 = [];
50                 this.text = null;
51                 
52                 this.has_local_changes = false;
53                 this._lock = -1;
54                 this._context_lock = -1;
55                 this._lock_count = 0; 
56         }
57         
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;
63 //                      return this._lock;
64 //              }
65 //              
66 //              // reentrant locks 
67 //              if(this._context_lock === this._lock) {
68 //                      this._lock_count += 1;
69 //                      return this._lock;
70 //              }
71 //              
72 //              throw "Document operation in progress. Try again later."
73 //      };
74 //              
75 //      WikiDocument.prototype.unlock = function(lockNumber) {
76 //              if(this.locked === lockNumber) {
77 //                      this._lock_count -= 1;
78 //                      
79 //                      if(this._lock_count === 0) {
80 //                              this._lock = -1;
81 //                              this._context_lock = -1;
82 //                      };                      
83 //                      return;
84 //              }
85 //              throw "Trying to unlock with wrong lockNumber";
86 //      };
87 //      
88 //      /*
89 //       * About to leave context of current lock.
90 //       */
91 //      WikiDocument.prototype.leaveContext = function() {
92 //              var old = this._context_lock;
93 //              this._context_lock = -1;
94 //              return old;
95 //      };
96         
97         /*
98          * Fetch text of this document.
99          */
100         WikiDocument.prototype.fetch = function(params) {               
101                 params = $.extend({}, noops, params);
102                 var self = this;
103                 
104                 $.ajax({
105                         method: "GET",
106                         url: reverse("ajax_document_text", self.id),
107                         dataType: 'json', 
108                         success: function(data) 
109                         {
110                                 var changed = false;
111                                 
112                                 if (self.text === null || self.revision !== data.revision) {
113                                         self.text = data.text;
114                                         self.revision = data.revision;
115                                         self.gallery = data.gallery;                                    
116                                         changed = true;
117                                 }
118                                 
119                                 self.has_local_changes = false;                         
120                                 params['success'](self, changed);
121                         },
122                         error: function() {             
123                                 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
124                         }
125                 });                                             
126         };
127         
128         /*
129          * Fetch history of this document.
130          * 
131          * from - First revision to fetch (default = 0)
132          * upto - Last revision to fetch (default = tip)
133          * 
134          */
135         WikiDocument.prototype.fetchHistory = function(params) {                
136                 /* this doesn't modify anything, so no locks */         
137                 params = $.extend({}, noops, params);           
138                 var self = this;
139                 
140                 $.ajax({                        
141                         method: "GET",
142                         url: reverse("ajax_document_history", self.id),
143                         dataType: 'json',
144                         data: {"from": params['from'], "upto": params['upto']},
145                         success: function(data) {
146                                 params['success'](self, data);
147                         },
148                         error: function() {
149                                 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
150                         }
151                 });                                             
152         };
153         
154         WikiDocument.prototype.fetchDiff = function(params) {           
155                 /* this doesn't modify anything, so no locks */         
156                 params = $.extend({
157                         'from': self.revision, 
158                         'to': self.revision
159                 }, noops, params);
160                                         
161                 var self = this;        
162                                         
163                 $.ajax({                        
164                         method: "GET",
165                         url: reverse("ajax_document_diff", self.id),
166                         dataType: 'json',
167                         data: {"from": params['from'], "to": params['to']},
168                         success: function(data) {
169                                 params['success'](self, data);
170                         },
171                         error: function() {
172                                 params['failure'](self, "Nie udało się wczytać porównania wersji.");
173                         }
174                 });                                             
175         };
176         
177         /* 
178          * Fetch gallery
179          */
180         WikiDocument.prototype.refreshGallery = function(params) {
181                 params = $.extend({}, noops, params);           
182                 var self = this;
183                 
184                 $.ajax({                        
185                         method: "GET",
186                         url: reverse("ajax_document_gallery", self.galleryLink),
187                         dataType: 'json',
188                         // data: {},
189                         success: function(data) {
190                                 this.galleryImages = data.images;
191                                 params['success'](self, data);
192                         },
193                         error: function() {
194                                 this.galleryImages = [];        
195                                 params['failure'](self, "<p>Nie udało się wczytać gallerii pod nazwą: '"
196                                         + self.galleryLink + "'.</p>");
197                                                         
198                         }
199                 });                             
200         };      
201         
202         /*
203          * Set document's text
204          */
205         WikiDocument.prototype.setText = function(text) {
206                 if (this.text != text) {
207                         this.text = text;
208                         this.has_local_changes = true;
209                 }                       
210         };
211         
212         /*
213          * Set document's gallery link
214          */
215         WikiDocument.prototype.setGalleryLink = function(gallery) {                             
216                 this.galleryLink = gallery;
217                 this.has_local_changes = true;                  
218         };
219         
220         /*
221          * Save text back to the server
222          */
223         WikiDocument.prototype.save = function(params){
224                 params = $.extend({'comment': 'No comment.'}, noops, params);
225                 var self = this;
226                 
227                 /* you can't set text while some is fetching it (or saving) */
228                 
229                 if (!self.has_local_changes) {
230                         console.log("Abort: no changes.");
231                         return params['success'](self, false, "Nie ma zmian do zapisania.");
232                 };              
233                 
234                 var metaComment = '<!--';
235                 metaComment += '\n\tgallery:' + self.galleryLink;
236                 metaComment += '\n-->\n'
237                 
238                 var data = {
239                         name: self.id,
240                         text: metaComment + self.text,
241                         parent_revision: self.revision,
242                         comment: params['comment'],
243                 };
244                 
245                 $.ajax({
246                         url: reverse("ajax_document_text", self.id),
247                         type: "POST",
248                         dataType: "json",
249                         data: data,
250                         success: function(data){
251                                 var changed = false;
252                                 if (data.text) {
253                                         self.text = data.text;
254                                         self.revision = data.revision;
255                                         self.gallery = data.gallery;
256                                         changed = true;
257                                 }
258                                 params['success'](self, changed, "Zapisano");
259                         },
260                         error: function() {
261                                 params['failure'](self, "Nie udało się zapisać.");
262                         }
263                 });             
264         }; /* end of save() */
265         
266         $.wikiapi.WikiDocument = WikiDocument;
267         
268 })(jQuery);