temporary attributes for bible tags
[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 .entry .approved', function(){
57                 $("#changes-list .entry.selected").removeClass('selected');
58                 $(this).closest('.entry').click();
59                 self.showPubmarkForm();
60                 return false;
61             })
62             $(document).on('click', '#changes-list span.tag', function(event){
63                 return false;
64             });
65
66             $('#history-view').on('scroll', function() {
67                 if (self.finished || self.fetching) return;
68                 var elemTop = $('#history-view .message-box').offset().top;
69                 var windowH = $(window).innerHeight();
70                 if (elemTop - 20 < windowH) {
71                     self.triggerFetch();
72                 }
73             });
74         }
75
76         onEnter(success, failure) {
77             super.onEnter();
78             this.startFetching();
79             success && success();
80         }
81
82         startFetching() {
83             $('#history-view .message-box').html('Wczytywanie historii…').show();
84             $('#changes-list').html('');
85             this.finished = false;
86             this.before = '';
87             this.triggerFetch();
88         }
89         stopFetching() {
90             self.finished = true;
91             $('#history-view .message-box').hide()
92         }
93
94         triggerFetch() {
95             var self = this;
96             self.fetching = true;
97
98             function _finalize() {
99                 self.fetching = false;
100             }
101
102             function _failure(doc, message){
103                 $('#history-view .message-box').html('Nie udało się odświeżyć historii:' + message).show();
104                 _finalize();
105             };
106
107             function _success(doc, data){
108                 //$('#history-view .message-box').hide(); ONLY AFTER LAST!
109                 var changes_list = $('#changes-list');
110                 var $stub = $('#history-view .row-stub');
111
112                 if (!data.length) {
113                     self.stopFetching();
114                 }
115
116                 $.each(data, function(){
117                     $.wiki.renderStub({
118                         container: changes_list,
119                         stub: $stub,
120                         data: this,
121                     });
122                     self.before = this.version;
123                     if (this.version == 1) {
124                         self.stopFetching();
125                     }
126                 });
127
128                 _finalize();
129             };
130
131             this.doc.fetchHistory({
132                 success: _success,
133                 failure: _failure,
134                 before: this.before,
135             });
136         }
137
138         showPubmarkForm() {
139             var selected = $('#changes-list .entry.selected');
140
141             if (selected.length != 1) {
142                 window.alert("Musisz zaznaczyć dokładnie jedną wersję.");
143                 return;
144             }
145
146             var version = parseInt($("*[data-stub-value='version']", selected[0]).text());
147             var approved = selected.attr('data-approved') == 'true';
148             $.wiki.showDialog('#pubmark_dialog', {'revision': version, 'approved': !approved});
149         }
150
151         makeDiff() {
152             var changelist = $('#changes-list');
153             var selected = $('.entry.selected', changelist);
154
155             if (selected.length != 2) {
156                 window.alert("Musisz zaznaczyć dokładnie dwie wersje do porównania.");
157                 return;
158             }
159
160             var rev_from = $("*[data-stub-value='version']", selected[1]).text();
161             var rev_to =  $("*[data-stub-value='version']", selected[0]).text();
162
163             $.wiki.DiffPerspective.open(rev_from, rev_to);
164         }
165
166         revertDialog() {
167             var self = this;
168             var selected = $('#changes-list .entry.selected');
169
170             if (selected.length != 1) {
171                 window.alert("Musisz zaznaczyć dokładnie jedną wersję.");
172                 return;
173             }
174
175             var version = parseInt($("*[data-stub-value='version']", selected[0]).text());
176             $.wiki.showDialog('#revert_dialog', {revision: version});
177         }
178     }
179     $.wiki.HistoryPerspective = HistoryPerspective;
180
181 })(jQuery);