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