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