6 function AnnotationsPerspective(options){
7 var old_callback = options.callback || function() { };
9 this.vsplitbar = 'PRZYPISY';
11 options.callback = function(){
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);
20 this.$refresh.click(function() {
23 self.$refresh.removeClass('active');
24 $this.addClass('active');
25 var atype = $this.attr('data-tag');
29 self.$spinner.fadeIn(100, function() {
30 self.refresh(self, atype);
34 old_callback.call(this);
37 $.wiki.SidebarPerspective.call(this, options);
40 AnnotationsPerspective.prototype = new $.wiki.SidebarPerspective();
42 AnnotationsPerspective.prototype.updateAnnotationIds = function(self){
43 self.annotationToAnchor = {};
44 $('#html-view').find('.annotation-inline-box').each(
45 function(i, annoBox) {
46 var $annoBox = $(annoBox);
47 var $anchor = $("a[name|=anchor]", $annoBox);
48 var htmlContent = $('span', $annoBox).html();
49 // TBD: perhaps use a hash of htmlContent as key
50 self.annotationToAnchor[htmlContent] = $anchor.attr('name');
54 AnnotationsPerspective.prototype.goToAnnotation = function(self, srcNode){
55 var content = $(srcNode).html();
56 content = content.replace(/>/g, '>').replace(/</g, '<').replace(/&/g, '&');
58 xml: '<root>'+content+'</root>',
59 success: function(txt) {
60 content = $(txt).html();
62 error: function(text) {
64 self.$error.html('<div class="alert alert-danger">' + text + '</div>');
70 var anchor = self.annotationToAnchor[content];
71 if (anchor != undefined) {
72 var $htmlView = $("#html-view");
73 var top = $htmlView.offset().top +
74 $("[name=" + anchor + "]", $htmlView).offset().top -
75 $htmlView.children().eq(0).offset().top;
77 $htmlView.animate({scrollTop: top}, 250);
81 AnnotationsPerspective.prototype.refresh = function(self, atype) {
84 var persp = $.wiki.activePerspective();
85 if (persp == 'CodeMirrorPerspective') {
86 xml = $.wiki.perspectives[persp].codemirror.getValue();
88 else if (persp == 'VisualPerspective') {
90 element: $('#html-view').find('div').get(0),
91 success: function(text){
94 error: function(text){
95 self.$error.html('<div class="alert alert-danger"><p>Wystąpił błąd:</p><pre>' + text + '</pre></div>');
100 self.updateAnnotationIds(self);
106 var parser = new DOMParser();
107 var serializer = new XMLSerializer();
108 var doc = parser.parseFromString(xml, 'text/xml');
109 var error = $('parsererror', doc);
111 if (error.length > 0) {
112 self.$error.html('<div class="alert alert-danger">Błąd parsowania XML.</a>');
113 self.$spinner.hide();
117 self.$annos.html('');
119 var annos = $(atype, doc);
120 var counter = annos.length;
121 var atype_rx = atype.replace(/,/g, '|');
122 var ann_expr = new RegExp("^<("+atype_rx+")[^>]*>|</("+atype_rx+")>$", "g");
124 if (annos.length == 0)
126 self.$annos.html('<div class="alert alert-info">Nie ma żadnych przypisów</div>');
127 self.$spinner.hide();
130 annos.each(function (i, elem) {
131 var xml_text = serializer.serializeToString(elem).replace(ann_expr, "");
133 xml: "<akap>" + xml_text + "</akap>",
134 success: function(xml_text){
135 return function(elem){
136 elem.sortby = $(elem).text().trim();
137 $(elem).append("<div class='src'>"+ xml_text.replace(/&/g, "&").replace(/</g, "<") +"</div>");
138 anno_list.push(elem);
139 $(".src", elem).click(function() { self.goToAnnotation(self, this); });
143 anno_list.sort(function(a, b){return a.sortby.localeCompare(b.sortby);});
145 self.$annos.append(anno_list[i]);
146 self.$spinner.hide();
152 error: function(text) {
154 self.$error.html('<div class="alert alert-danger">' + text + '</div>');
155 self.$spinner.hide();
164 AnnotationsPerspective.prototype.onEnter = function(){
165 $.wiki.SidebarPerspective.prototype.onEnter.call(this);
167 this.$refresh.filter('.active').trigger('click');
171 AnnotationsPerspective.prototype.onExit = function(success, failure) {
175 $.wiki.AnnotationsPerspective = AnnotationsPerspective;