Fixed diseapearing <motto> tag.
[redakcja.git] / redakcja / static / js / wiki / view_history.js
1 (function($){
2
3     function HistoryPerspective(options) {
4                 var old_callback = options.callback || function() {};
5
6                 options.callback = function() {
7                         var self = this;
8
9                         // first time page is rendered
10                 $('#make-diff-button').click(function() {
11                                 self.makeDiff();
12                         });
13
14                         $('#tag-changeset-button').click(function() {
15                                 self.showTagForm();
16                         });
17
18                         $('#open-preview-button').click(function(event) {
19                                 var selected = $('#changes-list .entry.selected');
20
21                                 if (selected.length != 1) {
22                             window.alert("Wybierz dokładnie *jedną* wersję.");
23                         return;
24                         }
25
26                                 var version = parseInt($("*[data-stub-value='version']", selected[0]).text());
27                                 window.open($(this).attr('data-basehref') + "?revision=" + version);
28
29                                 event.preventDefault();
30                         });
31
32                 $('#changes-list .entry').live('click', function(){
33                 var $this = $(this);
34                 if ($this.hasClass('selected'))
35                         return $this.removeClass('selected');
36
37                 if ($("#changes-list .entry.selected").length < 2)
38                         return $this.addClass('selected');
39                 });
40
41             $('#changes-list span.tag').live('click', function(event){
42                     return false;
43                 });
44
45                 old_callback.call(this);
46                 }
47
48                 $.wiki.Perspective.call(this, options);
49     };
50
51     HistoryPerspective.prototype = new $.wiki.Perspective();
52
53     HistoryPerspective.prototype.freezeState = function(){
54         // must
55     };
56
57     HistoryPerspective.prototype.onEnter = function(success, failure){
58         $.wiki.Perspective.prototype.onEnter.call(this);
59
60         $.blockUI({
61             message: 'Odświeżanie historii...'
62         });
63
64         function _finalize(s){
65             $.unblockUI();
66
67             if (s) {
68                 if (success)
69                     success();
70             }
71             else {
72                 if (failure)
73                     failure();
74             }
75         }
76
77         function _failure(doc, message){
78             $('#history-view .message-box').html('Nie udało się odświeżyć historii:' + message).show();
79             _finalize(false);
80         };
81
82         function _success(doc, data){
83             $('#history-view .message-box').hide();
84             var changes_list = $('#changes-list');
85             var $stub = $('#history-view .row-stub');
86             changes_list.html('');
87
88                         var tags = $('select#id_addtag-tag option');
89
90             $.each(data, function(){
91                 $.wiki.renderStub({
92                                         container: changes_list,
93                                         stub: $stub,
94                                         data: this,
95                                         filters: {
96                                                 tag: function(value) {
97                                                         return tags.filter("*[value='"+value+"']").text();
98                                                 }
99                                         }
100                                 });
101             });
102
103             _finalize(true);
104         };
105
106         return this.doc.fetchHistory({
107             success: _success,
108             failure: _failure
109         });
110     };
111
112         HistoryPerspective.prototype.showTagForm = function(){
113                 var selected = $('#changes-list .entry.selected');
114
115                 if (selected.length != 1) {
116             window.alert("Musisz zaznaczyć dokładnie jedną wersję.");
117             return;
118         }
119
120                 var version = parseInt($("*[data-stub-value='version']", selected[0]).text());
121                 $.wiki.showDialog('#add_tag_dialog', {'revision': version});
122         };
123
124         HistoryPerspective.prototype.makeDiff = function() {
125         var changelist = $('#changes-list');
126         var selected = $('.entry.selected', changelist);
127
128         if (selected.length != 2) {
129             window.alert("Musisz zaznaczyć dokładnie dwie wersje do porównania.");
130             return;
131         }
132
133         $.blockUI({
134             message: 'Wczytywanie porównania...'
135         });
136
137                 var rev_from = $("*[data-stub-value='version']", selected[1]).text();
138                 var rev_to =  $("*[data-stub-value='version']", selected[0]).text();
139
140         return this.doc.fetchDiff({
141             from: rev_from,
142                         to: rev_to,
143             success: function(doc, data){
144                 var result = $.wiki.newTab(doc, ''+rev_from +' -> ' + rev_to, 'DiffPerspective');
145
146                                 $(result.view).html(data);
147                                 $.wiki.switchToTab(result.tab);
148                                 $.unblockUI();
149             },
150             failure: function(doc){
151                 $.unblockUI();
152             }
153         });
154     };
155
156     $.wiki.HistoryPerspective = HistoryPerspective;
157
158 })(jQuery);