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 var path = "/" + arguments[1] + "/text";
23 if (arguments[2] !== undefined)
24 path += "/" + arguments[2];
26 return base_path + path;
29 /*if (vname == "ajax_document_history") {
31 return base_path + "/" + arguments[1] + "/history";
34 if (vname == "ajax_document_gallery") {
36 return base_path + "/" + arguments[1] + "/gallery";
39 if (vname == "ajax_document_diff")
40 return base_path + "/" + arguments[1] + "/diff";
42 if (vname == "ajax_document_rev")
43 return base_path + "/" + arguments[1] + "/rev";
45 if (vname == "ajax_document_addtag")
46 return base_path + "/" + arguments[1] + "/tags";
48 if (vname == "ajax_publish")
49 return base_path + "/" + arguments[1] + "/publish";*/
51 console.log("Couldn't reverse match:", vname);
56 * Document Abstraction
58 function WikiDocument(element_id) {
59 var meta = $('#' + element_id);
60 this.id = meta.attr('data-document-name');
62 this.revision = $("*[data-key='revision']", meta).text();
63 this.readonly = !!$("*[data-key='readonly']", meta).text();
65 this.galleryLink = $("*[data-key='gallery']", meta).text();
66 this.galleryImages = [];
68 this.has_local_changes = false;
70 this._context_lock = -1;
74 WikiDocument.prototype.triggerDocumentChanged = function() {
75 $(document).trigger('wlapi_document_changed', this);
78 * Fetch text of this document.
80 WikiDocument.prototype.fetch = function(params) {
81 params = $.extend({}, noops, params);
85 url: reverse("ajax_document_text", self.id),
86 data: {"revision": self.revision},
88 success: function(data) {
91 if (self.text === null || self.revision !== data.revision) {
92 self.text = data.text;
93 if (self.text === '') {
94 self.text = '<obraz></obraz>';
96 self.revision = data.revision;
98 self.triggerDocumentChanged();
101 self.has_local_changes = false;
102 params['success'](self, changed);
105 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
111 * Set document's text
113 WikiDocument.prototype.setText = function(text) {
115 this.has_local_changes = true;
119 * Save text back to the server
121 WikiDocument.prototype.save = function(params) {
122 params = $.extend({}, noops, params);
125 if (!self.has_local_changes) {
126 console.log("Abort: no changes.");
127 return params['success'](self, false, "Nie ma zmian do zapisania.");
130 // Serialize form to dictionary
132 $.each(params['form'].serializeArray(), function() {
133 data[this.name] = this.value;
136 data['textsave-text'] = self.text;
139 url: reverse("ajax_document_text", self.id),
143 success: function(data) {
146 $('#header').removeClass('saving');
149 self.text = data.text;
150 self.revision = data.revision;
152 self.triggerDocumentChanged();
155 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
157 error: function(xhr) {
158 if ($('#header').hasClass('saving')) {
159 $('#header').removeClass('saving');
161 message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
166 params['failure'](self, $.parseJSON(xhr.responseText));
169 params['failure'](self, {
170 "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
178 $('#save-hide').click(function(){
179 $('#header').addClass('saving');
181 $.wiki.blocking.unblock();
183 }; /* end of save() */
185 WikiDocument.prototype.publish = function(params) {
186 params = $.extend({}, noops, params);
189 url: reverse("ajax_publish", self.id),
192 success: function(data) {
193 params.success(self, data);
195 error: function(xhr) {
196 if (xhr.status == 403 || xhr.status == 401) {
197 params.failure(self, "Nie masz uprawnień lub nie jesteś zalogowany.");
201 params.failure(self, xhr.responseText);
204 params.failure(self, "Nie udało się - błąd serwera.");
211 WikiDocument.prototype.setTag = function(params) {
212 params = $.extend({}, noops, params);
215 "addtag-id": self.id,
219 $.each(params.form.serializeArray(), function() {
220 data[this.name] = this.value;
224 url: reverse("ajax_document_addtag", self.id),
228 success: function(data) {
229 params.success(self, data.message);
231 error: function(xhr) {
232 if (xhr.status == 403 || xhr.status == 401) {
233 params.failure(self, {
234 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
239 params.failure(self, $.parseJSON(xhr.responseText));
242 params.failure(self, {
243 "__all__": ["Nie udało się - błąd serwera."]
251 WikiDocument.prototype.getImageItems = function(tag) {
254 var parser = new DOMParser();
255 var doc = parser.parseFromString(self.text, 'text/xml');
256 var error = $('parsererror', doc);
258 if (error.length != 0) {
263 $(tag, doc).each(function(i, e) {
277 WikiDocument.prototype.setImageItems = function(tag, items) {
280 var parser = new DOMParser();
281 var doc = parser.parseFromString(self.text, 'text/xml');
282 var serializer = new XMLSerializer();
283 var error = $('parsererror', doc);
285 if (error.length != 0) {
289 $(tag, doc).remove();
290 $root = $(doc.firstChild);
291 $.each(items, function(i, e) {
292 var el = $(doc.createElement(tag));
302 self.setText(serializer.serializeToString(doc));
306 $.wikiapi.WikiDocument = WikiDocument;