latex dir for debugging
[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                 var $this = $(this);
23
24                 self.$refresh.removeClass('active');
25                 $this.addClass('active');
26                 var 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.updateAnnotationIds = function(self){
44         self.annotationToAnchor = {};
45         $('#html-view').find('.annotation-inline-box').each(
46             function(i, annoBox) {
47                 var $annoBox = $(annoBox);
48                 var $anchor = $("a[name|=anchor]", $annoBox);
49                 var htmlContent = $('span', $annoBox).html();
50                 // TBD: perhaps use a hash of htmlContent as key
51                 self.annotationToAnchor[htmlContent] = $anchor.attr('name');
52                 });
53     };
54
55     AnnotationsPerspective.prototype.goToAnnotation = function(self, srcNode){
56         var content = $(srcNode).html();
57         content = content.replace(/&gt;/g, '>').replace(/&lt;/g, '<').replace(/&amp;/g, '&');
58         xml2html({
59             xml: '<root>'+content+'</root>',
60             success: function(txt) {
61                 content = $(txt).html();
62                 },
63             error: function(text) {
64                 $.unblockUI();
65                 self.$error.html(text);
66                 self.$spinner.hide();
67                 self.$error.show();
68             }
69         });
70
71         var anchor = self.annotationToAnchor[content];
72         if (anchor != undefined) {
73             var $htmlView = $("#html-view");
74             var top = $htmlView.offset().top + 
75                 $("[name=" + anchor + "]", $htmlView).offset().top - 
76                 $htmlView.children().eq(0).offset().top;
77
78             $htmlView.animate({scrollTop: top}, 250);
79         }
80     };
81
82     AnnotationsPerspective.prototype.refresh = function(self, atype) {
83         var xml;
84
85         var persp = $.wiki.activePerspective();
86         if (persp == 'CodeMirrorPerspective') {
87             xml = $.wiki.perspectives[persp].codemirror.getCode();
88         }
89         else if (persp == 'VisualPerspective') {
90             html2text({
91                 element: $('#html-view').find('div').get(0),
92                 success: function(text){
93                     xml = text;
94                 },
95                 error: function(text){
96                     self.$error.html('<p>Wystąpił błąd:</p><pre>' + text + '</pre>');
97                     self.$spinner.hide();
98                     self.$error.show();
99                 }
100             });
101             self.updateAnnotationIds(self);
102         }
103         else {
104             xml = this.doc.text;
105         }
106
107         var parser = new DOMParser();
108         var serializer = new XMLSerializer();
109         var doc = parser.parseFromString(xml, 'text/xml');
110         var error = $('parsererror', doc);
111
112         if (error.length > 0) {
113             self.$error.html('Błąd parsowania XML.');
114             self.$spinner.hide();
115             self.$error.show();
116         }
117         else {
118             self.$annos.html('');
119             var anno_list = [];
120             var annos = $(atype, doc);
121             var counter = annos.length;
122             var atype_rx = atype.replace(/,/g, '|');
123             var ann_expr = new RegExp("^<("+atype_rx+")[^>]*>|</("+atype_rx+")>$", "g");
124
125             if (annos.length == 0)
126             {
127                 self.$annos.html('Nie ma żadnych przypisów');
128                 self.$spinner.hide();
129                 self.$annos.show();
130             }
131             annos.each(function (i, elem) {
132                 var xml_text = serializer.serializeToString(elem).replace(ann_expr, "");
133                 xml2html({
134                     xml: "<akap>" + xml_text + "</akap>",
135                     success: function(xml_text){
136                         return function(elem){
137                             elem.sortby = $(elem).text().trim();
138                             $(elem).append("<div class='src'>"+ xml_text.replace(/&/g, "&amp;").replace(/</g, "&lt;") +"</div>");
139                             anno_list.push(elem);
140                             $(".src", elem).click(function() { self.goToAnnotation(self, this); });
141                             counter--;
142
143                             if (!counter) {
144                                 anno_list.sort(function(a, b){return a.sortby.localeCompare(b.sortby);});
145                                 for (i in anno_list)
146                                     self.$annos.append(anno_list[i]);
147                                 self.$spinner.hide();
148                                 self.$annos.show();
149                             }
150
151                         }
152                     }(xml_text),
153                     error: function(text) {
154                         $.unblockUI();
155                         self.$error.html(text);
156                         self.$spinner.hide();
157                         self.$error.show();
158                     }
159                 });
160             });
161         }
162     };
163
164
165     AnnotationsPerspective.prototype.onEnter = function(){
166         $.wiki.Perspective.prototype.onEnter.call(this);
167
168         $('.vsplitbar').not('.active').trigger('click');
169         $(".vsplitbar-title").html("&darr;&nbsp;PRZYPISY&nbsp;&darr;");
170         this.$refresh.filter('.active').trigger('click');
171
172     };
173
174         AnnotationsPerspective.prototype.onExit = function(success, failure) {
175
176         };
177
178     $.wiki.AnnotationsPerspective = AnnotationsPerspective;
179
180 })(jQuery);