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