Some modernizations.
[redakcja.git] / src / redakcja / static / js / wiki / view_annotations.js
1 (function($){
2
3     /*
4      * Perspective
5      */
6     class AnnotationsPerspective extends $.wiki.SidebarPerspective {
7         constructor(options) {
8             var old_callback = options.callback || function() { };
9
10             options.callback = function(){
11                 var self = this;
12
13                 this.vsplitbar = 'PRZYPISY';
14                 this.$element = $("#side-annotations");
15                 this.$error = $('.error-message', this.$element);
16                 this.$annos = $('.annotations-list', this.$element);
17                 this.$spinner = $('.spinner', this.$element);
18                 this.$refresh = $('.refresh', this.$element);
19
20                 this.$refresh.click(function() {
21                     var $this = $(this);
22
23                     self.$refresh.removeClass('active');
24                     $this.addClass('active');
25                     var atype = $this.attr('data-tag');
26
27                     self.$annos.hide();
28                     self.$error.hide();
29                     self.$spinner.fadeIn(100, function() {
30                         self.refresh(atype);
31                     });
32                 });
33
34                 old_callback.call(this);
35             };
36
37             super(options);
38         }
39
40         updateAnnotationIds() {
41             let selt = this;
42             self.annotationToAnchor = {};
43             $('#html-view').find('.annotation-inline-box').each(
44                 function(i, annoBox) {
45                     var $annoBox = $(annoBox);
46                     var $anchor = $("a[name|=anchor]", $annoBox);
47                     var htmlContent = $('span', $annoBox).html();
48                     // TBD: perhaps use a hash of htmlContent as key
49                     self.annotationToAnchor[htmlContent] = $anchor.attr('name');
50                 }
51             );
52         }
53
54         goToAnnotation(srcNode) {
55             let self = this;
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('<div class="alert alert-danger">' + text + '</div>');
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         refresh(atype) {
83             let self = this;
84             var xml;
85
86             var persp = $.wiki.activePerspective();
87             if (persp == 'CodeMirrorPerspective') {
88                 xml = $.wiki.perspectives[persp].codemirror.getValue();
89             }
90             else if (persp == 'VisualPerspective') {
91                 html2text({
92                     element: $('#html-view').find('div').get(0),
93                     success: function(text){
94                         xml = text;
95                     },
96                     error: function(text){
97                         self.$error.html('<div class="alert alert-danger"><p>Wystąpił błąd:</p><pre>' + text + '</pre></div>');
98                         self.$spinner.hide();
99                         self.$error.show();
100                     }
101                 });
102                 self.updateAnnotationIds();
103             }
104             else {
105                 xml = this.doc.text;
106             }
107
108             var parser = new DOMParser();
109             var serializer = new XMLSerializer();
110             var doc = parser.parseFromString(xml, 'text/xml');
111             var error = $('parsererror', doc);
112
113             if (error.length > 0) {
114                 self.$error.html('<div class="alert alert-danger">Błąd parsowania XML.</a>');
115                 self.$spinner.hide();
116                 self.$error.show();
117             }
118             else {
119                 self.$annos.html('');
120                 var anno_list = [];
121                 var annos = $(atype, doc);
122                 var counter = annos.length;
123                 var atype_rx = atype.replace(/,/g, '|');
124                 var ann_expr = new RegExp("^<("+atype_rx+")[^>]*>|</("+atype_rx+")>$", "g");
125
126                 if (annos.length == 0)
127                 {
128                     self.$annos.html('<div class="alert alert-info">Nie ma żadnych przypisów</div>');
129                     self.$spinner.hide();
130                     self.$annos.show();
131                 }
132                 annos.each(function (i, elem) {
133                     var xml_text = serializer.serializeToString(elem).replace(ann_expr, "");
134                     xml2html({
135                         xml: "<akap>" + xml_text + "</akap>",
136                         success: function(xml_text){
137                             return function(elem){
138                                 elem.sortby = $(elem).text().trim();
139                                 $(elem).append("<div class='src'>"+ xml_text.replace(/&/g, "&amp;").replace(/</g, "&lt;") +"</div>");
140                                 anno_list.push(elem);
141                                 $(".src", elem).click(function() { self.goToAnnotation(this); });
142                                 counter--;
143
144                                 if (!counter) {
145                                     anno_list.sort(function(a, b){return a.sortby.localeCompare(b.sortby);});
146                                     for (i in anno_list)
147                                         self.$annos.append(anno_list[i]);
148                                     self.$spinner.hide();
149                                     self.$annos.show();
150                                 }
151                             }
152                         }(xml_text),
153                         error: function(text) {
154                             $.unblockUI();
155                             self.$error.html('<div class="alert alert-danger">' + text + '</div>');
156                             self.$spinner.hide();
157                             self.$error.show();
158                         }
159                     });
160                 });
161             }
162         }
163
164         onEnter() {
165             super.onEnter();
166             this.$refresh.filter('.active').trigger('click');
167         };
168
169         onExit(success, failure) {};
170     }
171     $.wiki.AnnotationsPerspective = AnnotationsPerspective;
172
173 })(jQuery);