6 function AnnotationsPerspective(options){
7 var old_callback = options.callback || function() { };
9 this.noupdate_hash_onenter = true;
10 this.vsplitbar = 'PRZYPISY';
12 options.callback = function(){
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);
21 this.$refresh.click(function() {
24 self.$refresh.removeClass('active');
25 $this.addClass('active');
26 atype = $this.attr('data-tag');
30 self.$spinner.show(100, function(){
31 self.refresh(self, atype);
35 old_callback.call(this);
38 $.wiki.Perspective.call(this, options);
41 AnnotationsPerspective.prototype = new $.wiki.Perspective();
43 AnnotationsPerspective.prototype.updateAnnotationIds = function(self){
44 self.annotationToAnchor = {};
45 $('#html-view .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');
55 AnnotationsPerspective.prototype.goToAnnotation = function(self, srcNode){
56 var content = $(srcNode).html();
57 content = content.replace('>', '>', 'g').replace('<', '<', 'g').replace('&', '&', 'g');
59 xml: '<root>'+content+'</root>',
60 success: function(txt) {
61 content = $(txt).html();
63 error: function(text) {
65 self.$error.html(text);
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
78 $htmlView.animate({scrollTop: top}, 250);
82 AnnotationsPerspective.prototype.refresh = function(self, atype) {
85 persp = $.wiki.activePerspective();
86 if (persp == 'CodeMirrorPerspective') {
87 xml = $.wiki.perspectives[persp].codemirror.getCode();
89 else if (persp == 'VisualPerspective') {
91 element: $('#html-view div').get(0),
92 success: function(text){
95 error: function(text){
96 self.$error.html('<p>Wystąpił błąd:</p><pre>' + text + '</pre>');
101 self.updateAnnotationIds(self);
107 var parser = new DOMParser();
108 var serializer = new XMLSerializer();
109 var doc = parser.parseFromString(xml, 'text/xml');
110 var error = $('parsererror', doc);
112 if (error.length > 0) {
113 self.$error.html('Błąd parsowania XML.');
114 self.$spinner.hide();
118 self.$annos.html('');
119 var anno_list = new Array();
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")
125 if (annos.length == 0)
127 self.$annos.html('Nie ma żadnych przypisów');
128 self.$spinner.hide();
131 annos.each(function (i, elem) {
132 xml_text = serializer.serializeToString(elem).replace(ann_expr, "");
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").replace("<", "<", "g") +"</div>")
139 anno_list.push(elem);
140 $(".src", elem).click(function() { self.goToAnnotation(self, this); });
144 anno_list.sort(function(a, b){return a.sortby.localeCompare(b.sortby);});
146 self.$annos.append(anno_list[i]);
147 self.$spinner.hide();
153 error: function(text) {
155 self.$error.html(text);
156 self.$spinner.hide();
165 AnnotationsPerspective.prototype.onEnter = function(success, failure){
168 $.wiki.Perspective.prototype.onEnter.call(this);
170 $('.vsplitbar').not('.active').trigger('click');
171 $(".vsplitbar-title").html("↓ PRZYPISY ↓");
172 this.$refresh.filter('.active').trigger('click');
176 AnnotationsPerspective.prototype.onExit = function(success, failure) {
180 $.wiki.AnnotationsPerspective = AnnotationsPerspective;