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