e9693b71f1cc4744d6255c08bb4fd986b5461f28
[redakcja.git] / src / redakcja / static / js / wiki / view_history.js
1 (function($){
2
3     class HistoryPerspective extends $.wiki.Perspective {
4         constructor(options) {
5             var old_callback = options.callback || function() {};
6
7             options.callback = function() {
8                 var self = this;
9                 if (CurrentDocument.diff) {
10                     rev_from = CurrentDocument.diff[0];
11                     rev_to = CurrentDocument.diff[1];
12                     this.doc.fetchDiff({
13                         from: rev_from,
14                         to: rev_to,
15                         success: function(doc, data){
16                             var result = $.wiki.newTab(doc, ''+rev_from +' -> ' + rev_to, 'DiffPerspective');
17
18                             $(result.view).html(data);
19                             $.wiki.switchToTab(result.tab);
20                         }
21                     });
22                 }
23
24                 // first time page is rendered
25                 $('#make-diff-button').click(function() {
26                     self.makeDiff();
27                 });
28
29                 $('#pubmark-changeset-button').click(function() {
30                     self.showPubmarkForm();
31                 });
32
33                 $('#doc-revert-button').click(function() {
34                     self.revertDialog();
35                 });
36
37                 $('#open-preview-button').click(function(event) {
38                     var selected = $('#changes-list .entry.selected');
39
40                     if (selected.length != 1) {
41                         window.alert("Wybierz dokładnie *jedną* wersję.");
42                         return;
43                     }
44
45                     var version = parseInt($("*[data-stub-value='version']", selected[0]).text());
46                     window.open($(this).attr('data-basehref') + "?revision=" + version);
47
48                     event.preventDefault();
49                 });
50
51                 $(document).on('click', '#changes-list .entry', function(){
52                     var $this = $(this);
53
54                     var selected_count = $("#changes-list .entry.selected").length;
55
56                     if ($this.hasClass('selected')) {
57                         $this.removeClass('selected');
58                         selected_count -= 1;
59                     }
60                     else {
61                         if (selected_count  < 2) {
62                             $this.addClass('selected');
63                             selected_count += 1;
64                         };
65                     };
66
67                     $('#history-view-editor .toolbar button').attr('disabled', 'disabled').
68                         filter('*[data-enabled-when~="' + selected_count + '"]').
69                         attr('disabled', null);
70                 });
71
72                 $(document).on('click', '#changes-list span.tag', function(event){
73                     return false;
74                 });
75
76                 $('#history-view').on('scroll', function() {
77                     if (self.finished || self.fetching) return;
78                     var elemTop = $('#history-view .message-box').offset().top;
79                     var windowH = $(window).innerHeight();
80                     if (elemTop - 20 < windowH) {
81                         self.triggerFetch();
82                     }
83                 });
84
85                 old_callback.call(this);
86             }
87
88             super(options);
89         }
90
91         freezeState() {
92             // must
93         }
94
95         onEnter(success, failure) {
96             super.onEnter();
97             this.startFetching();
98             success && success();
99         }
100
101         startFetching() {
102             $('#history-view .message-box').html('Wczytywanie historii…').show();
103             $('#changes-list').html('');
104             this.finished = false;
105             this.before = '';
106             this.triggerFetch();
107         }
108         stopFetching() {
109             self.finished = true;
110             $('#history-view .message-box').hide()
111         }
112
113         triggerFetch() {
114             var self = this;
115             self.fetching = true;
116
117             function _finalize() {
118                 self.fetching = false;
119             }
120
121             function _failure(doc, message){
122                 $('#history-view .message-box').html('Nie udało się odświeżyć historii:' + message).show();
123                 _finalize();
124             };
125
126             function _success(doc, data){
127                 //$('#history-view .message-box').hide(); ONLY AFTER LAST!
128                 var changes_list = $('#changes-list');
129                 var $stub = $('#history-view .row-stub');
130
131                 if (!data.length) {
132                     self.stopFetching();
133                 }
134
135                 $.each(data, function(){
136                     $.wiki.renderStub({
137                         container: changes_list,
138                         stub: $stub,
139                         data: this,
140                     });
141                     self.before = this.version;
142                     if (this.version == 1) {
143                         self.stopFetching();
144                     }
145                 });
146
147                 _finalize();
148             };
149
150             this.doc.fetchHistory({
151                 success: _success,
152                 failure: _failure,
153                 before: this.before,
154             });
155         }
156
157         showPubmarkForm() {
158             var selected = $('#changes-list .entry.selected');
159
160             if (selected.length != 1) {
161                 window.alert("Musisz zaznaczyć dokładnie jedną wersję.");
162                 return;
163             }
164
165             var version = parseInt($("*[data-stub-value='version']", selected[0]).text());
166             $.wiki.showDialog('#pubmark_dialog', {'revision': version});
167         }
168
169         makeDiff() {
170             var changelist = $('#changes-list');
171             var selected = $('.entry.selected', changelist);
172
173             if (selected.length != 2) {
174                 window.alert("Musisz zaznaczyć dokładnie dwie wersje do porównania.");
175                 return;
176             }
177
178             $.blockUI({
179                 message: 'Wczytywanie porównania...'
180             });
181
182             var rev_from = $("*[data-stub-value='version']", selected[1]).text();
183             var rev_to =  $("*[data-stub-value='version']", selected[0]).text();
184
185             return this.doc.fetchDiff({
186                 from: rev_from,
187                 to: rev_to,
188                 success: function(doc, data){
189                     var result = $.wiki.newTab(doc, ''+rev_from +' -> ' + rev_to, 'DiffPerspective');
190                     $(result.view).html(data);
191                     $.wiki.switchToTab(result.tab);
192                     $.unblockUI();
193                 },
194                 failure: function(doc){
195                     $.unblockUI();
196                 }
197             });
198         }
199
200         revertDialog() {
201             var self = this;
202             var selected = $('#changes-list .entry.selected');
203
204             if (selected.length != 1) {
205                 window.alert("Musisz zaznaczyć dokładnie jedną wersję.");
206                 return;
207             }
208
209             var version = parseInt($("*[data-stub-value='version']", selected[0]).text());
210             $.wiki.showDialog('#revert_dialog', {revision: version});
211         }
212     }
213     $.wiki.HistoryPerspective = HistoryPerspective;
214
215 })(jQuery);