add dependency to migration
[redakcja.git] / redakcja / static / js / wiki / view_annotations.js
1 (function($){
2
3     /*
4      * Perspective
5      */
6     function AnnotationsPerspective(options){
7         var old_callback = options.callback || function() { };
8
9         this.noupdate_hash_onenter = true;
10         this.vsplitbar = 'PRZYPISY';
11
12         options.callback = function(){
13             var self = this;
14
15             this.$element = $("#side-annotations");
16             this.$error = $('.error-message', this.$element);
17             this.$annos = $('.annotations-list', this.$element);
18             this.$spinner = $('.spinner', this.$element);
19             this.$refresh = $('.refresh', this.$element);
20
21             this.$refresh.click(function() {
22                 $this = $(this);
23
24                 self.$refresh.removeClass('active');
25                 $this.addClass('active');
26                 atype = $this.attr('data-tag');
27
28                 self.$annos.hide();
29                 self.$error.hide();
30                 self.$spinner.show(100, function(){
31                     self.refresh(self, atype);
32                 });
33             });
34
35                         old_callback.call(this);
36         };
37
38         $.wiki.Perspective.call(this, options);
39     };
40
41     AnnotationsPerspective.prototype = new $.wiki.Perspective();
42
43     AnnotationsPerspective.prototype.refresh = function(self, atype) {
44         var xml;
45
46         persp = $.wiki.activePerspective();
47         if (persp == 'CodeMirrorPerspective') {
48             xml = $.wiki.perspectives[persp].codemirror.getCode();
49         }
50         else if (persp == 'VisualPerspective') {
51             html2text({
52                 element: $('#html-view div').get(0),
53                 success: function(text){
54                     xml = text;
55                 },
56                 error: function(text){
57                     self.$error.html('<p>Wystąpił błąd:</p><pre>' + text + '</pre>');
58                     self.$spinner.hide();
59                     self.$error.show();
60                 }
61             });
62         }
63         else {
64             xml = this.doc.text;
65         }
66
67         var parser = new DOMParser();
68         var serializer = new XMLSerializer();
69         var doc = parser.parseFromString(xml, 'text/xml');
70         var error = $('parsererror', doc);
71
72         if (error.length > 0) {
73             self.$error.html('Błąd parsowania XML.');
74             self.$spinner.hide();
75             self.$error.show();
76         }
77         else {
78             self.$annos.html('');
79             var anno_list = new Array();
80             var annos = $(atype, doc);
81             var counter = annos.length;
82             var atype_rx = atype.replace(/,/g, '|');
83             var ann_expr = new RegExp("^<("+atype_rx+")[^>]*>|</("+atype_rx+")>$", "g")
84
85             if (annos.length == 0)
86             {
87                 self.$annos.html('Nie ma żadnych przypisów');
88                 self.$spinner.hide();
89                 self.$annos.show();
90             }
91             annos.each(function (i, elem) {
92                 xml_text = serializer.serializeToString(elem).replace(ann_expr, "");
93                 xml2html({
94                     xml: "<akap>" + xml_text + "</akap>",
95                     success: function(xml_text){
96                         return function(elem){
97                             elem.sortby = $(elem).text().trim();
98                             $(elem).append("<div class='src'>"+ xml_text.replace("&", "&amp;", "g").replace("<", "&lt;", "g") +"</div>")
99                             anno_list.push(elem);
100                             counter--;
101
102                             if (!counter) {
103                                 anno_list.sort(function(a, b){return a.sortby.localeCompare(b.sortby);});
104                                 for (i in anno_list)
105                                     self.$annos.append(anno_list[i]);
106                                 self.$spinner.hide();
107                                 self.$annos.show();
108                             }
109
110                         }
111                     }(xml_text),
112                     error: function(text) {
113                         $.unblockUI();
114                         self.$error.html(text);
115                         self.$spinner.hide();
116                         self.$error.show();
117                     }
118                 });
119             });
120         }
121     }
122
123
124     AnnotationsPerspective.prototype.onEnter = function(success, failure){
125         var self = this;
126
127         $.wiki.Perspective.prototype.onEnter.call(this);
128
129         $('.vsplitbar').not('.active').trigger('click');
130         $(".vsplitbar-title").html("&darr;&nbsp;PRZYPISY&nbsp;&darr;");
131         this.$refresh.filter('.active').trigger('click');
132
133     };
134
135         AnnotationsPerspective.prototype.onExit = function(success, failure) {
136
137         };
138
139     $.wiki.AnnotationsPerspective = AnnotationsPerspective;
140
141 })(jQuery);