d151f9e5434c0dd309023ba4f09e49958130eade
[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_diff")
30                         return "/" + arguments[1] + "/diff/" + arguments[2] + "/" + arguments[3] 
31                         
32                 console.log("Couldn't reverse match:", vname);
33                 return "/404.html";             
34         };
35         
36         /*
37          * Document Abstraction  
38          */
39         function WikiDocument(element_id) {
40                 var meta = $('#'+element_id);
41                 
42                 this.id = meta.attr('data-document-name');              
43                 this.revision = $("*[data-key='revision']", meta).text();
44                 this.gallery = $("*[data-key='gallery']", meta).text();
45                 
46                 this.text = null;
47                 
48                 this.has_local_changes = false;
49                 this._lock = -1;
50                 this._context_lock = -1;
51                 this._lock_count = 0; 
52         }
53         
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;
59 //                      return this._lock;
60 //              }
61 //              
62 //              // reentrant locks 
63 //              if(this._context_lock === this._lock) {
64 //                      this._lock_count += 1;
65 //                      return this._lock;
66 //              }
67 //              
68 //              throw "Document operation in progress. Try again later."
69 //      };
70 //              
71 //      WikiDocument.prototype.unlock = function(lockNumber) {
72 //              if(this.locked === lockNumber) {
73 //                      this._lock_count -= 1;
74 //                      
75 //                      if(this._lock_count === 0) {
76 //                              this._lock = -1;
77 //                              this._context_lock = -1;
78 //                      };                      
79 //                      return;
80 //              }
81 //              throw "Trying to unlock with wrong lockNumber";
82 //      };
83 //      
84 //      /*
85 //       * About to leave context of current lock.
86 //       */
87 //      WikiDocument.prototype.leaveContext = function() {
88 //              var old = this._context_lock;
89 //              this._context_lock = -1;
90 //              return old;
91 //      };
92         
93         /*
94          * Fetch text of this document.
95          */
96         WikiDocument.prototype.fetch = function(params) {               
97                 params = $.extend({}, noops, params);
98                 var self = this;
99                 
100                 $.ajax({
101                         method: "GET",
102                         url: reverse("ajax_document_text", self.id),
103                         dataType: 'json', 
104                         success: function(data) 
105                         {
106                                 var changed = false;
107                                 
108                                 if (self.text === null || self.revision !== data.revision) {
109                                         self.text = data.text;
110                                         self.revision = data.revision;
111                                         self.gallery = data.gallery;                                    
112                                         changed = true;
113                                 }
114                                 
115                                 self.has_local_changes = false;                         
116                                 params['success'](self, changed);
117                         },
118                         error: function() {             
119                                 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
120                         }
121                 });                                             
122         };
123         
124         /*
125          * Fetch history of this document.
126          * 
127          * from - First revision to fetch (default = 0)
128          * upto - Last revision to fetch (default = tip)
129          * 
130          */
131         WikiDocument.prototype.fetchHistory = function(params) {                
132                 /* this doesn't modify anything, so no locks */         
133                 params = $.extend({}, noops, params);           
134                 var self = this;
135                 
136                 $.ajax({                        
137                         method: "GET",
138                         url: reverse("ajax_document_history", self.id),
139                         dataType: 'json',
140                         data: {"from": params['from'], "upto": params['upto']},
141                         success: function(data) {
142                                 params['success'](self, data);
143                         },
144                         error: function() {
145                                 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
146                         }
147                 });                                             
148         };
149         
150         /*
151          * Set document's text
152          */
153         WikiDocument.prototype.setText = function(text) {                               
154                 this.text = text;
155                 this.has_local_changes = true;                  
156         };
157         
158         /*
159          * Set document's gallery link
160          */
161         WikiDocument.prototype.setGallery = function(gallery) {                         
162                 this.gallery = gallery;
163                 this.has_local_changes = true;                  
164         };
165         
166         /*
167          * Save text back to the server
168          */
169         WikiDocument.prototype.save = function(comment, success, failure){
170                 var self = this;
171                 
172                 if (!self.has_local_changes) {
173                         return success(self, "Nie ma zmian do zapisania.");
174                 }
175                 
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-->'
180                 
181                 var data = {
182                         name: self.id,
183                         text: self.text,
184                         parent_revision: self.revision,
185                         comment: comment,
186                 };
187                 
188                 $.ajax({
189                         url: reverse("ajax_document_text", self.id),
190                         type: "POST",
191                         dataType: "json",
192                         data: data,
193                         success: function(data){
194                                 var changed = false;
195                                 if (data.text) {
196                                         self.text = data.text;
197                                         self.revision = data.revision;
198                                         self.gallery = data.gallery;
199                                         changed = true;
200                                 }
201                                 success(self, changed);
202                         },
203                         error: function(){
204                                 failure(self, "Nie udało się zapisać.");
205                         }
206                 });             
207         }; /* end of save() */
208         
209         $.wikiapi.WikiDocument = WikiDocument;
210         
211 })(jQuery);