Priviliged users can now add tags. Also, some minor cleanups in JS.
[redakcja.git] / platforma / 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                 $('#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                         var tags = $('select#id_tag option');
75
76             $.each(data, function(){
77                 $.wiki.renderStub({
78                                         container: changes_list,
79                                         stub: $stub,
80                                         data: this,
81                                         filters: {
82                                                 tag: function(value) {
83                                                         return tags.filter("*[value='"+value+"']").text();
84                                                 }
85                                         }
86                                 });
87             });
88
89             _finalize(true);
90         };
91
92         return this.doc.fetchHistory({
93             success: _success,
94             failure: _failure
95         });
96     };
97
98         HistoryPerspective.prototype.showTagForm = function(){
99                 var selected = $('#changes-list .entry.selected');
100
101                 if (selected.length != 1) {
102             window.alert("Musisz dokładnie jedną wersję do oznaczenia.");
103             return;
104         }
105
106                 var version = parseInt($("*[data-stub-value='version']", selected[0]).text());
107                 $.wiki.showDialog('#add_tag_dialog', {'revision': version});
108         };
109
110         HistoryPerspective.prototype.makeDiff = function() {
111         var changelist = $('#changes-list');
112         var selected = $('.entry.selected', changelist);
113
114         if (selected.length != 2) {
115             window.alert("Musisz zaznaczyć dokładnie dwie wersje do porównania.");
116             return;
117         }
118
119         $.blockUI({
120             message: 'Wczytywanie porównania...'
121         });
122
123                 var rev_from = $("*[data-stub-value='version']", selected[1]).text();
124                 var rev_to =  $("*[data-stub-value='version']", selected[0]).text();
125
126         return this.doc.fetchDiff({
127             from: rev_from,
128                         to: rev_to,
129             success: function(doc, data){
130                 var result = $.wiki.newTab(doc, ''+rev_from +' -> ' + rev_to, 'DiffPerspective');
131
132                                 $(result.view).html(data);
133                                 $.wiki.switchToTab(result.tab);
134                                 $.unblockUI();
135             },
136             failure: function(doc){
137                 $.unblockUI();
138             }
139         });
140     };
141
142     $.wiki.HistoryPerspective = HistoryPerspective;
143
144 })(jQuery);