3 var noop = function() {
10 * Return absolute reverse path of given named view. (at least he have it
11 * hard-coded in one place)
13 * TODO: think of a way, not to hard-code it here ;)
17 var vname = arguments[0];
18 var base_path = "/images";
20 if (vname == "ajax_document_text") {
21 return base_path + "/text/" + arguments[1] + "/";
24 if (vname == "ajax_document_history") {
26 return base_path + "/history/" + arguments[1] + "/";
30 if (vname == "ajax_document_diff")
31 return base_path + "/" + arguments[1] + "/diff";
33 if (vname == "ajax_document_rev")
34 return base_path + "/" + arguments[1] + "/rev";
36 if (vname == "ajax_document_addtag")
37 return base_path + "/" + arguments[1] + "/tags";
39 if (vname == "ajax_publish")
40 return base_path + "/" + arguments[1] + "/publish";*/
42 console.log("Couldn't reverse match:", vname);
47 * Document Abstraction
49 function WikiDocument(element_id) {
50 var meta = $('#' + element_id);
51 this.id = meta.attr('data-object-id');
53 this.revision = $("*[data-key='revision']", meta).text();
54 this.readonly = !!$("*[data-key='readonly']", meta).text();
57 this.has_local_changes = false;
59 this._context_lock = -1;
63 WikiDocument.prototype.triggerDocumentChanged = function() {
64 $(document).trigger('wlapi_document_changed', this);
67 * Fetch text of this document.
69 WikiDocument.prototype.fetch = function(params) {
70 params = $.extend({}, noops, params);
74 url: reverse("ajax_document_text", self.id),
75 data: {"commit": self.commit},
77 success: function(data) {
80 if (self.text === null || self.commit !== data.commit) {
81 self.text = data.text;
82 if (self.text === '') {
83 self.text = '<obraz></obraz>';
85 self.revision = data.revision;
86 self.commit = data.commit;
88 self.triggerDocumentChanged();
91 self.has_local_changes = false;
92 params['success'](self, changed);
95 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
100 * Fetch history of this document.
102 * from - First revision to fetch (default = 0) upto - Last revision to
103 * fetch (default = tip)
106 WikiDocument.prototype.fetchHistory = function(params) {
107 /* this doesn't modify anything, so no locks */
108 params = $.extend({}, noops, params);
112 url: reverse("ajax_document_history", self.id),
115 "from": params['from'],
116 "upto": params['upto']
118 success: function(data) {
119 params['success'](self, data);
122 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
128 * Set document's text
130 WikiDocument.prototype.setText = function(text) {
132 this.has_local_changes = true;
136 * Save text back to the server
138 WikiDocument.prototype.save = function(params) {
139 params = $.extend({}, noops, params);
142 if (!self.has_local_changes) {
143 console.log("Abort: no changes.");
144 return params['success'](self, false, "Nie ma zmian do zapisania.");
147 // Serialize form to dictionary
149 $.each(params['form'].serializeArray(), function() {
150 data[this.name] = this.value;
153 data['textsave-text'] = self.text;
156 url: reverse("ajax_document_text", self.id),
160 success: function(data) {
163 $('#header').removeClass('saving');
166 self.text = data.text;
167 self.revision = data.revision;
168 self.commit = data.commit;
170 self.triggerDocumentChanged();
173 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
175 error: function(xhr) {
176 if ($('#header').hasClass('saving')) {
177 $('#header').removeClass('saving');
179 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
184 params['failure'](self, $.parseJSON(xhr.responseText));
187 params['failure'](self, {
188 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
196 $('#save-hide').click(function(){
197 $('#header').addClass('saving');
199 $.wiki.blocking.unblock();
201 }; /* end of save() */
203 WikiDocument.prototype.publish = function(params) {
204 params = $.extend({}, noops, params);
207 url: reverse("ajax_publish", self.id),
210 success: function(data) {
211 params.success(self, data);
213 error: function(xhr) {
214 if (xhr.status == 403 || xhr.status == 401) {
215 params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
219 params.failure(self, xhr.responseText);
222 params.failure(self, "Nie udało się - błąd serwera.");
229 WikiDocument.prototype.setTag = function(params) {
230 params = $.extend({}, noops, params);
233 "addtag-id": self.id,
237 $.each(params.form.serializeArray(), function() {
238 data[this.name] = this.value;
242 url: reverse("ajax_document_addtag", self.id),
246 success: function(data) {
247 params.success(self, data.message);
249 error: function(xhr) {
250 if (xhr.status == 403 || xhr.status == 401) {
251 params.failure(self, {
252 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
257 params.failure(self, $.parseJSON(xhr.responseText));
260 params.failure(self, {
261 "__all__": ["Nie udało się - błąd serwera."]
269 WikiDocument.prototype.getImageItems = function(tag) {
272 var parser = new DOMParser();
273 var doc = parser.parseFromString(self.text, 'text/xml');
274 var error = $('parsererror', doc);
276 if (error.length != 0) {
281 $(tag, doc).each(function(i, e) {
295 WikiDocument.prototype.setImageItems = function(tag, items) {
298 var parser = new DOMParser();
299 var doc = parser.parseFromString(self.text, 'text/xml');
300 var serializer = new XMLSerializer();
301 var error = $('parsererror', doc);
303 if (error.length != 0) {
307 $(tag, doc).remove();
308 $root = $(doc.firstChild);
309 $.each(items, function(i, e) {
310 var el = $(doc.createElement(tag));
320 self.setText(serializer.serializeToString(doc));
324 $.wikiapi.WikiDocument = WikiDocument;
329 // Wykonuje block z załadowanymi kanonicznymi motywami
330 function withThemes(code_block, onError)
332 if (typeof withThemes.canon == 'undefined') {
334 url: '/editor/themes',
336 success: function(data) {
337 withThemes.canon = data.split('\n');
338 code_block(withThemes.canon);
341 withThemes.canon = null;
342 code_block(withThemes.canon);
347 code_block(withThemes.canon);