Javascript refactoring. History view now displays tags
[redakcja.git] / platforma / static / js / wiki / history_view.js
1 (function($){
2
3     function fetchDiff(success, failed){
4         var changelist = $('#changes-list');
5    
6                 var selected = $('div.selected', changelist); 
7         
8         if (selected.length != 2) {
9             window.alert("Musisz zaznaczyć dokładnie dwie wersje do porównania.");
10             if(failed) failed();
11         }
12         
13         $.blockUI({
14             message: 'Wczytywanie porównania...'
15         });
16         
17         $.ajax({
18                         method: "GET",
19             url: document.location.href + '/diff/' + rev_a.val() + '/' + rev_b.val(),
20             dataType: 'html',
21             error: function(){
22                 $.unblockUI();
23                 if(failed) failed('Nie udało się wczytać porównania z serwera.');
24             },
25             success: function(data){
26                 var diffview = $('#diff-view');
27                 diffview.html(data);
28                 diffview.show();
29                 $.unblockUI();
30                 if(success) success(data);
31             }
32         });
33     }
34     
35     function HistoryPerspective(doc, callback) {
36                 this.perspective_id = 'HistoryPerspective';
37                 this.doc = doc;
38                 
39         // first time page is rendered
40         $('#make-diff-button').click(fetchDiff);
41                 
42                 $('#changes-list div').live('click', function() {
43                         var $this = $(this);
44                         if($this.hasClass('selected')) 
45                                 return $this.removeClass('selected');
46                                 
47                         if($("#changes-list div.selected").length < 2)
48                                 return $this.addClass('selected');
49                 });
50                 
51                 $('#changes-list span.tag').live('click', function(event) {                     
52                         return false;                                           
53                 });
54                 
55                 callback.call(this);
56     };
57     
58     HistoryPerspective.prototype = new $.wiki.Perspective();
59     
60     HistoryPerspective.prototype.freezeState = function(){
61         // must 
62     };
63     
64     HistoryPerspective.prototype.onEnter = function(success, failure) 
65         {               
66                 $.wiki.Perspective.prototype.onEnter.call(this);
67                 
68         $.blockUI({
69             message: 'Odświeżanie historii...'
70         });
71                 
72                 function _finalize(s) {
73                         $.unblockUI();
74                         
75                         if(s) { if(success) success(); }
76                         else { if(failure) failure(); }
77                 }
78         
79         function _failure(doc, message) {
80           $('#history-view .message-box').html('Nie udało się odświeżyć historii:' + message).show();
81                   _finalize(false);    
82         };
83           
84                 function _success(doc, data) {
85                 $('#history-view .message-box').hide();
86             var changes_list = $('#changes-list');
87             var $stub = $('#history-view .row-stub');                
88             changes_list.html('');
89                 
90             $.each(data, function(){
91                 $.wiki.renderStub(changes_list, $stub, this);                                                           
92             });
93                         
94                         $('span[data-version-tag]', changes_list).each(function() {
95                                 $(this).text($(this).attr('data-version-tag'));                         
96                         });
97                         
98                         _finalize(true);                        
99         };
100                 
101                 return this.doc.fetchHistory({success: _success, failure: _failure});
102     };    
103                 
104         $.wiki.HistoryPerspective = HistoryPerspective;
105         
106 })(jQuery);