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