Fix caret positioning outside editable.
[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                 old_callback.call(this);
76                 }
77
78                 $.wiki.Perspective.call(this, options);
79     };
80
81     HistoryPerspective.prototype = new $.wiki.Perspective();
82
83     HistoryPerspective.prototype.freezeState = function(){
84         // must
85     };
86
87     HistoryPerspective.prototype.onEnter = function(success, failure){
88         $.wiki.Perspective.prototype.onEnter.call(this);
89
90         $.blockUI({
91             message: 'Odświeżanie historii...'
92         });
93
94         function _finalize(s){
95             $.unblockUI();
96
97             if (s) {
98                 if (success)
99                     success();
100             }
101             else {
102                 if (failure)
103                     failure();
104             }
105         }
106
107         function _failure(doc, message){
108             $('#history-view .message-box').html('Nie udało się odświeżyć historii:' + message).show();
109             _finalize(false);
110         };
111
112         function _success(doc, data){
113             $('#history-view .message-box').hide();
114             var changes_list = $('#changes-list');
115             var $stub = $('#history-view .row-stub');
116             changes_list.html('');
117
118                         var tags = $('select#id_addtag-tag option');
119
120             $.each(data, function(){
121                 $.wiki.renderStub({
122                                         container: changes_list,
123                                         stub: $stub,
124                                         data: this,
125                                         filters: {
126 //                                              tag: function(value) {
127 //                                                      return tags.filter("*[value='"+value+"']").text();
128 //                                              }
129 //                        description: function(value) {
130 //                                                  return value.replace('\n', ');
131 //                                              }
132                                         }
133                                 });
134             });
135
136             _finalize(true);
137         };
138
139         return this.doc.fetchHistory({
140             success: _success,
141             failure: _failure
142         });
143     };
144
145         HistoryPerspective.prototype.showPubmarkForm = function(){
146                 var selected = $('#changes-list .entry.selected');
147
148                 if (selected.length != 1) {
149             window.alert("Musisz zaznaczyć dokładnie jedną wersję.");
150             return;
151         }
152
153                 var version = parseInt($("*[data-stub-value='version']", selected[0]).text());
154                 $.wiki.showDialog('#pubmark_dialog', {'revision': version});
155         };
156
157         HistoryPerspective.prototype.makeDiff = function() {
158         var changelist = $('#changes-list');
159         var selected = $('.entry.selected', changelist);
160
161         if (selected.length != 2) {
162             window.alert("Musisz zaznaczyć dokładnie dwie wersje do porównania.");
163             return;
164         }
165
166         $.blockUI({
167             message: 'Wczytywanie porównania...'
168         });
169
170                 var rev_from = $("*[data-stub-value='version']", selected[1]).text();
171                 var rev_to =  $("*[data-stub-value='version']", selected[0]).text();
172
173         return this.doc.fetchDiff({
174             from: rev_from,
175                         to: rev_to,
176             success: function(doc, data){
177                 var result = $.wiki.newTab(doc, ''+rev_from +' -> ' + rev_to, 'DiffPerspective');
178
179                                 $(result.view).html(data);
180                                 $.wiki.switchToTab(result.tab);
181                                 $.unblockUI();
182             },
183             failure: function(doc){
184                 $.unblockUI();
185             }
186         });
187     };
188
189     HistoryPerspective.prototype.revertDialog = function(){
190         var self = this;
191         var selected = $('#changes-list .entry.selected');
192
193         if (selected.length != 1) {
194             window.alert("Musisz zaznaczyć dokładnie jedną wersję.");
195             return;
196         }
197
198         var version = parseInt($("*[data-stub-value='version']", selected[0]).text());
199         $.wiki.showDialog('#revert_dialog', {revision: version});
200     };
201
202     $.wiki.HistoryPerspective = HistoryPerspective;
203
204 })(jQuery);