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_revert") {
 
  25             return base_path + "/revert/" + arguments[1] + '/';
 
  28                 if (vname == "ajax_document_history") {
 
  29                         return base_path + "/history/" + arguments[1] + '/';
 
  32                 if (vname == "ajax_document_diff")
 
  33                         return base_path + "/diff/" + arguments[1] + '/';
 
  35                 if (vname == "ajax_document_pubmark")
 
  36                         return base_path + "/pubmark/" + arguments[1] + '/';
 
  38                 console.log("Couldn't reverse match:", vname);
 
  43          * Document Abstraction
 
  45         function WikiDocument(element_id) {
 
  46                 var meta = $('#' + element_id);
 
  47                 this.id = meta.attr('data-object-id');
 
  49                 this.revision = $("*[data-key='revision']", meta).text();
 
  50                 this.readonly = !!$("*[data-key='readonly']", meta).text();
 
  53                 this.has_local_changes = false;
 
  55                 this._context_lock = -1;
 
  59         WikiDocument.prototype.triggerDocumentChanged = function() {
 
  60                 $(document).trigger('wlapi_document_changed', this);
 
  63          * Fetch text of this document.
 
  65         WikiDocument.prototype.fetch = function(params) {
 
  66                 params = $.extend({}, noops, params);
 
  70                         url: reverse("ajax_document_text", self.id),
 
  71                         data: {"commit": self.commit},
 
  73                         success: function(data) {
 
  76                                 if (self.text === null || self.commit !== data.commit) {
 
  77                                         self.text = data.text;
 
  78                                         if (self.text === '') {
 
  79                                             self.text = '<obraz></obraz>';
 
  81                                         self.revision = data.revision;
 
  82                     self.commit = data.commit;
 
  84                                         self.triggerDocumentChanged();
 
  87                                 self.has_local_changes = false;
 
  88                                 params['success'](self, changed);
 
  91                                 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
 
  96          * Fetch history of this document.
 
  98          * from - First revision to fetch (default = 0) upto - Last revision to
 
  99          * fetch (default = tip)
 
 102         WikiDocument.prototype.fetchHistory = function(params) {
 
 103                 /* this doesn't modify anything, so no locks */
 
 104                 params = $.extend({}, noops, params);
 
 108                         url: reverse("ajax_document_history", self.id),
 
 111                                 "from": params['from'],
 
 112                                 "upto": params['upto']
 
 114                         success: function(data) {
 
 115                                 params['success'](self, data);
 
 118                                 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
 
 122         WikiDocument.prototype.fetchDiff = function(params) {
 
 123                 /* this doesn't modify anything, so no locks */
 
 126                         'from': self.revision,
 
 131                         url: reverse("ajax_document_diff", self.id),
 
 134                                 "from": params['from'],
 
 137                         success: function(data) {
 
 138                                 params['success'](self, data);
 
 141                                 params['failure'](self, "Nie udało się wczytać porównania wersji.");
 
 147          * Set document's text
 
 149         WikiDocument.prototype.setText = function(text) {
 
 151                 this.has_local_changes = true;
 
 155          * Save text back to the server
 
 157         WikiDocument.prototype.save = function(params) {
 
 158                 params = $.extend({}, noops, params);
 
 161                 if (!self.has_local_changes) {
 
 162                         console.log("Abort: no changes.");
 
 163                         return params['success'](self, false, "Nie ma zmian do zapisania.");
 
 166                 // Serialize form to dictionary
 
 168                 $.each(params['form'].serializeArray(), function() {
 
 169                         data[this.name] = this.value;
 
 172                 data['textsave-text'] = self.text;
 
 175                         url: reverse("ajax_document_text", self.id),
 
 179                         success: function(data) {
 
 182                 $('#header').removeClass('saving');
 
 185                                         self.text = data.text;
 
 186                                         self.revision = data.revision;
 
 187                     self.commit = data.commit;
 
 189                                         self.triggerDocumentChanged();
 
 192                                 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
 
 194                         error: function(xhr) {
 
 195                 if ($('#header').hasClass('saving')) {
 
 196                     $('#header').removeClass('saving');
 
 198                         message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
 
 203                         params['failure'](self, $.parseJSON(xhr.responseText));
 
 206                         params['failure'](self, {
 
 207                             "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
 
 215         $('#save-hide').click(function(){
 
 216             $('#header').addClass('saving');
 
 218             $.wiki.blocking.unblock();
 
 220         }; /* end of save() */
 
 222     WikiDocument.prototype.revertToVersion = function(params) {
 
 224         params = $.extend({}, noops, params);
 
 226         if (params.revision >= this.revision) {
 
 227             params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
 
 231         // Serialize form to dictionary
 
 233         $.each(params['form'].serializeArray(), function() {
 
 234             data[this.name] = this.value;
 
 238             url: reverse("ajax_document_revert", self.id),
 
 242             success: function(data) {
 
 244                     self.text = data.text;
 
 245                     self.revision = data.revision;
 
 246                     self.gallery = data.gallery;
 
 247                     self.triggerDocumentChanged();
 
 249                     params.success(self, "Udało się przywrócić wersję :)");
 
 252                     params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
 
 255             error: function(xhr) {
 
 256                 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
 
 261         WikiDocument.prototype.pubmark = function(params) {
 
 262                 params = $.extend({}, noops, params);
 
 265                         "pubmark-id": self.id,
 
 269                 $.each(params.form.serializeArray(), function() {
 
 270                         data[this.name] = this.value;
 
 274                         url: reverse("ajax_document_pubmark", self.id),
 
 278                         success: function(data) {
 
 279                                 params.success(self, data.message);
 
 281                         error: function(xhr) {
 
 282                                 if (xhr.status == 403 || xhr.status == 401) {
 
 283                                         params.failure(self, {
 
 284                                                 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
 
 289                                                 params.failure(self, $.parseJSON(xhr.responseText));
 
 292                                                 params.failure(self, {
 
 293                                                         "__all__": ["Nie udało się - błąd serwera."]
 
 303     WikiDocument.prototype.getImageItems = function(tag) {
 
 306         var parser = new DOMParser();
 
 307         var doc = parser.parseFromString(self.text, 'text/xml');
 
 308         var error = $('parsererror', doc);
 
 310         if (error.length != 0) {
 
 315         $('sem[type="'+tag+'"]', doc).each(function(i, e) {
 
 317             var $div = $e.children().first()
 
 318             var value = $e.attr(tag);
 
 319             $e.find('div').each(function(i, div) {
 
 321                 switch ($div.attr('type')) {
 
 334                             null, null, null, null
 
 344     WikiDocument.prototype.setImageItems = function(tag, items) {
 
 347         var parser = new DOMParser();
 
 348         var doc = parser.parseFromString(self.text, 'text/xml');
 
 349         var serializer = new XMLSerializer();
 
 350         var error = $('parsererror', doc);
 
 352         if (error.length != 0) {
 
 356         $('sem[type="'+tag+'"]', doc).remove();
 
 357         $root = $(doc.firstChild);
 
 358         $.each(items, function(i, e) {
 
 359             var $sem = $(doc.createElement("sem"));
 
 360             $sem.attr('type', tag);
 
 361             $sem.attr(tag, e[0]);
 
 362             $div = $(doc.createElement("div"));
 
 364                 $div.attr('type', 'area');
 
 365                 $div.attr('x1', e[1]);
 
 366                 $div.attr('y1', e[2]);
 
 367                 $div.attr('x2', e[3]);
 
 368                 $div.attr('y2', e[4]);
 
 371                 $div.attr('type', 'whole');
 
 376         self.setText(XML(serializer.serializeToString(doc)).toXMLString());
 
 380         $.wikiapi.WikiDocument = WikiDocument;
 
 385 // Wykonuje block z załadowanymi kanonicznymi motywami
 
 386 function withThemes(code_block, onError)
 
 388     if (typeof withThemes.canon == 'undefined') {
 
 390             url: '/editor/themes',
 
 392             success: function(data) {
 
 393                 withThemes.canon = data.split('\n');
 
 394                 code_block(withThemes.canon);
 
 397                 withThemes.canon = null;
 
 398                 code_block(withThemes.canon);
 
 403         code_block(withThemes.canon);