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