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