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