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