better annotations dictionary
[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                 xml_text = serializer.serializeToString(annos[i]).replace(/^<pe[^>]*>|<\/pe>$/g, "");
72                 xml2html({
73                     xml: "<akap>" + xml_text + "</akap>",
74                     success: function(xml_text){
75                         return function(elem){
76                             elem.sortby = $(elem).text().trim();
77                             $(elem).append("<div class='src'>"+ xml_text.replace("&", "&amp;", "g").replace("<", "&lt;", "g") +"</div>")
78                             anno_list.push(elem);
79                             counter--;
80
81                             if (!counter) {
82                                 anno_list.sort(function(a, b){return a.sortby.localeCompare(b.sortby);});
83                                 self.$annos.append(anno_list);
84                                 self.$annos.show();
85                             }
86                         }
87                     }(xml_text),
88                     error: function(text) {
89                         $.unblockUI();
90                         self.$error.html(text);
91                         self.$error.show();
92                     }
93                 });
94             }
95         }
96     }
97
98
99     AnnotationsPerspective.prototype.onEnter = function(success, failure){
100         var self = this;
101
102         $.wiki.Perspective.prototype.onEnter.call(this);
103
104         $('.vsplitbar').not('.active').trigger('click');
105         $(".vsplitbar-title").html("&darr;&nbsp;PRZYPISY&nbsp;&darr;");        
106
107         this.refresh(this);
108
109     };
110
111         AnnotationsPerspective.prototype.onExit = function(success, failure) {
112
113         };
114
115     $.wiki.AnnotationsPerspective = AnnotationsPerspective;
116
117 })(jQuery);