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();
 
  52         var diff = $("*[data-key='diff']", meta).text();
 
  54             diff = diff.split(',');
 
  55             if (diff.length == 2 && diff[0] < diff[1])
 
  57             else if (diff.length == 1) {
 
  58                 diff = parseInt(diff);
 
  60                     this.diff = [diff - 1, diff];
 
  65                 this.has_local_changes = false;
 
  67                 this._context_lock = -1;
 
  71         WikiDocument.prototype.triggerDocumentChanged = function() {
 
  72                 $(document).trigger('wlapi_document_changed', this);
 
  75          * Fetch text of this document.
 
  77         WikiDocument.prototype.fetch = function(params) {
 
  78                 params = $.extend({}, noops, params);
 
  82                         url: reverse("ajax_document_text", self.id),
 
  83                         data: {"revision": self.revision},
 
  85                         success: function(data) {
 
  88                                 if (self.text === null || self.revision !== data.revision) {
 
  89                                         self.text = data.text;
 
  90                                         if (self.text === '') {
 
  91                                             self.text = '<picture></picture>';
 
  93                                         self.revision = data.revision;
 
  94 //                    self.commit = data.commit;
 
  96                                         self.triggerDocumentChanged();
 
  99                                 self.has_local_changes = false;
 
 100                                 params['success'](self, changed);
 
 103                                 params['failure'](self, "Nie udało się wczytać treści dokumentu.");
 
 108          * Fetch history of this document.
 
 110          * from - First revision to fetch (default = 0) upto - Last revision to
 
 111          * fetch (default = tip)
 
 114         WikiDocument.prototype.fetchHistory = function(params) {
 
 115                 /* this doesn't modify anything, so no locks */
 
 116                 params = $.extend({}, noops, params);
 
 120                         url: reverse("ajax_document_history", self.id),
 
 123                                 "from": params['from'],
 
 124                                 "upto": params['upto']
 
 126                         success: function(data) {
 
 127                                 params['success'](self, data);
 
 130                                 params['failure'](self, "Nie udało się wczytać historii dokumentu.");
 
 134         WikiDocument.prototype.fetchDiff = function(params) {
 
 135                 /* this doesn't modify anything, so no locks */
 
 138                         'from': self.revision,
 
 143                         url: reverse("ajax_document_diff", self.id),
 
 146                                 "from": params['from'],
 
 149                         success: function(data) {
 
 150                                 params['success'](self, data);
 
 153                                 params['failure'](self, "Nie udało się wczytać porównania wersji.");
 
 159          * Set document's text
 
 161         WikiDocument.prototype.setText = function(text) {
 
 163                 this.has_local_changes = true;
 
 167          * Save text back to the server
 
 169         WikiDocument.prototype.save = function(params) {
 
 170                 params = $.extend({}, noops, params);
 
 173                 if (!self.has_local_changes) {
 
 174                         console.log("Abort: no changes.");
 
 175                         return params['success'](self, false, "Nie ma zmian do zapisania.");
 
 178                 // Serialize form to dictionary
 
 180                 $.each(params['form'].serializeArray(), function() {
 
 181                         data[this.name] = this.value;
 
 184                 data['textsave-text'] = self.text;
 
 187                         url: reverse("ajax_document_text", self.id),
 
 191                         success: function(data) {
 
 194                 $('#header').removeClass('saving');
 
 197                                         self.text = data.text;
 
 198                                         self.revision = data.revision;
 
 199 //                    self.commit = data.commit;
 
 201                                         self.triggerDocumentChanged();
 
 204                                 params['success'](self, changed, ((changed && "Udało się zapisać :)") || "Twoja wersja i serwera jest identyczna"));
 
 206                         error: function(xhr) {
 
 207                 if ($('#header').hasClass('saving')) {
 
 208                     $('#header').removeClass('saving');
 
 210                         message: "<p>Nie udało się zapisać zmian. <br/><button onclick='$.unblockUI()'>OK</button></p>"
 
 215                         params['failure'](self, $.parseJSON(xhr.responseText));
 
 218                         params['failure'](self, {
 
 219                             "__message": "<p>Nie udało się zapisać - błąd serwera.</p>"
 
 227         $('#save-hide').click(function(){
 
 228             $('#header').addClass('saving');
 
 230             $.wiki.blocking.unblock();
 
 232         }; /* end of save() */
 
 234     WikiDocument.prototype.revertToVersion = function(params) {
 
 236         params = $.extend({}, noops, params);
 
 238         if (params.revision >= this.revision) {
 
 239             params.failure(self, 'Proszę wybrać rewizję starszą niż aktualna.');
 
 243         // Serialize form to dictionary
 
 245         $.each(params['form'].serializeArray(), function() {
 
 246             data[this.name] = this.value;
 
 250             url: reverse("ajax_document_revert", self.id),
 
 254             success: function(data) {
 
 256                     self.text = data.text;
 
 257                     self.revision = data.revision;
 
 258                     self.gallery = data.gallery;
 
 259                     self.triggerDocumentChanged();
 
 261                     params.success(self, "Udało się przywrócić wersję :)");
 
 264                     params.failure(self, "Przywracana wersja identyczna z aktualną. Anulowano przywracanie.");
 
 267             error: function(xhr) {
 
 268                 params.failure(self, "Nie udało się przywrócić wersji - błąd serwera.");
 
 273         WikiDocument.prototype.pubmark = function(params) {
 
 274                 params = $.extend({}, noops, params);
 
 277                         "pubmark-id": self.id,
 
 281                 $.each(params.form.serializeArray(), function() {
 
 282                         data[this.name] = this.value;
 
 286                         url: reverse("ajax_document_pubmark", self.id),
 
 290                         success: function(data) {
 
 291                                 params.success(self, data.message);
 
 293                         error: function(xhr) {
 
 294                                 if (xhr.status == 403 || xhr.status == 401) {
 
 295                                         params.failure(self, {
 
 296                                                 "__all__": ["Nie masz uprawnień lub nie jesteś zalogowany."]
 
 301                                                 params.failure(self, $.parseJSON(xhr.responseText));
 
 304                                                 params.failure(self, {
 
 305                                                         "__all__": ["Nie udało się - błąd serwera."]
 
 315     WikiDocument.prototype.getImageItems = function(tag) {
 
 318         var parser = new DOMParser();
 
 319         var doc = parser.parseFromString(self.text, 'text/xml');
 
 320         var error = $('parsererror', doc);
 
 322         if (error.length != 0) {
 
 327         $('sem[type="'+tag+'"]', doc).each(function(i, e) {
 
 329             var $div = $e.children().first()
 
 330             var value = $e.attr(tag);
 
 331             $e.find('div').each(function(i, div) {
 
 333                 switch ($div.attr('type')) {
 
 346                             null, null, null, null
 
 356     WikiDocument.prototype.setImageItems = function(tag, items) {
 
 359         var parser = new DOMParser();
 
 360         var doc = parser.parseFromString(self.text, 'text/xml');
 
 361         var serializer = new XMLSerializer();
 
 362         var error = $('parsererror', doc);
 
 364         if (error.length != 0) {
 
 368         $('sem[type="'+tag+'"]', doc).remove();
 
 369         $root = $(doc.firstChild);
 
 370         $.each(items, function(i, e) {
 
 371             var $sem = $(doc.createElement("sem"));
 
 372             $sem.attr('type', tag);
 
 373             $sem.attr(tag, e[0]);
 
 374             $div = $(doc.createElement("div"));
 
 376                 $div.attr('type', 'rect');
 
 377                 $div.attr('x1', e[1]);
 
 378                 $div.attr('y1', e[2]);
 
 379                 $div.attr('x2', e[3]);
 
 380                 $div.attr('y2', e[4]);
 
 383                 $div.attr('type', 'whole');
 
 388         self.setText(serializer.serializeToString(doc));
 
 392         $.wikiapi.WikiDocument = WikiDocument;