d51ce9c84b477a116dac1da2b3953b0332009cb6
[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
11         options.callback = function(){
12             var self = this;
13
14             this.$element = $("#side-annotations");
15             this.$error = $('.error-message', this.$element);
16             this.$annos = $('.annotations-list', this.$element);
17             $('.refresh', this.$element).click(function() {
18                 self.refresh(self);
19             });
20
21                         old_callback.call(this);
22         };
23
24         $.wiki.Perspective.call(this, options);
25     };
26
27     AnnotationsPerspective.prototype = new $.wiki.Perspective();
28
29     AnnotationsPerspective.prototype.refresh = function(self) {
30         var xml;
31
32         persp = $.wiki.activePerspective();
33         if (persp == 'CodeMirrorPerspective') {
34             xml = $.wiki.perspectives[persp].codemirror.getCode();
35         }
36         else if (persp == 'VisualPerspective') {
37             html2text({
38                 element: $('#html-view div').get(0),
39                 success: function(text){
40                     xml = text;
41                 },
42                 error: function(text){
43                     self.$error.html('<p>Wystąpił błąd:</p><pre>' + text + '</pre>');
44                 }
45             });
46         }
47         else {
48             xml = this.doc.text;
49         }
50         
51         var parser = new DOMParser();
52         var serializer = new XMLSerializer();
53         var doc = parser.parseFromString(xml, 'text/xml');
54         var error = $('parsererror', doc);
55
56         if (error.length > 0) {
57             self.$error.html('Błąd parsowania XML.');
58             self.$error.show();
59             self.$annos.hide();
60         }
61         else {
62             self.$error.hide();
63             self.$annos.hide();
64             self.$annos.html('');
65             var anno_list = new Array();
66             var annos = doc.getElementsByTagName('pe');
67             var counter = annos.length;
68
69             for (var i=0; i<annos.length; i++)
70             {
71                 text = serializer.serializeToString(annos[i]).replace(/<(\/?)pe[^>]*>/g, "<$1akap>");
72                 xml2html({
73                     xml: text,
74                     success: function(elem){
75                         elem.txt = $(elem).text().trim();
76                         anno_list.push(elem);
77                         counter--;
78
79                         if (!counter) {
80                             anno_list.sort(function(a, b){return (a.txt < b.txt) ? -1 : (a.txt > b.txt ? 1 : 0)});
81                             self.$annos.append(anno_list);
82                             self.$annos.show();
83                         }
84                     },
85                     error: function(text) {
86                         $.unblockUI();
87                         self.$error.html(text);
88                         self.$error.show();
89                     }
90                 });
91             }
92         }
93     }
94
95
96     AnnotationsPerspective.prototype.onEnter = function(success, failure){
97         var self = this;
98
99         $.wiki.Perspective.prototype.onEnter.call(this);
100
101         $('.vsplitbar').not('.active').trigger('click');
102         $(".vsplitbar-title").html("&darr;&nbsp;PRZYPISY&nbsp;&darr;");        
103
104         this.refresh(this);
105
106     };
107
108         AnnotationsPerspective.prototype.onExit = function(success, failure) {
109
110         };
111
112     $.wiki.AnnotationsPerspective = AnnotationsPerspective;
113
114 })(jQuery);