X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/ab76a5c2c90d386b3d5f40aee3d784b02cb92434..8132fc186eb0c5fd02c86828c3a4735754296d02:/redakcja/static/js/wiki/view_annotations.js diff --git a/redakcja/static/js/wiki/view_annotations.js b/redakcja/static/js/wiki/view_annotations.js index 071e7ee6..b55effd0 100644 --- a/redakcja/static/js/wiki/view_annotations.js +++ b/redakcja/static/js/wiki/view_annotations.js @@ -19,11 +19,11 @@ this.$refresh = $('.refresh', this.$element); this.$refresh.click(function() { - $this = $(this); + var $this = $(this); self.$refresh.removeClass('active'); $this.addClass('active'); - atype = $this.attr('data-tag'); + var atype = $this.attr('data-tag'); self.$annos.hide(); self.$error.hide(); @@ -32,24 +32,63 @@ }); }); - old_callback.call(this); + old_callback.call(this); }; $.wiki.Perspective.call(this, options); - }; + } AnnotationsPerspective.prototype = new $.wiki.Perspective(); + AnnotationsPerspective.prototype.updateAnnotationIds = function(self){ + self.annotationToAnchor = {}; + $('#html-view').find('.annotation-inline-box').each( + function(i, annoBox) { + var $annoBox = $(annoBox); + var $anchor = $("a[name|=anchor]", $annoBox); + var htmlContent = $('span', $annoBox).html(); + // TBD: perhaps use a hash of htmlContent as key + self.annotationToAnchor[htmlContent] = $anchor.attr('name'); + }); + }; + + AnnotationsPerspective.prototype.goToAnnotation = function(self, srcNode){ + var content = $(srcNode).html(); + content = content.replace(/>/g, '>').replace(/</g, '<').replace(/&/g, '&'); + xml2html({ + xml: ''+content+'', + success: function(txt) { + content = $(txt).html(); + }, + error: function(text) { + $.unblockUI(); + self.$error.html(text); + self.$spinner.hide(); + self.$error.show(); + } + }); + + var anchor = self.annotationToAnchor[content]; + if (anchor != undefined) { + var $htmlView = $("#html-view"); + var top = $htmlView.offset().top + + $("[name=" + anchor + "]", $htmlView).offset().top - + $htmlView.children().eq(0).offset().top; + + $htmlView.animate({scrollTop: top}, 250); + } + }; + AnnotationsPerspective.prototype.refresh = function(self, atype) { var xml; - persp = $.wiki.activePerspective(); + var persp = $.wiki.activePerspective(); if (persp == 'CodeMirrorPerspective') { xml = $.wiki.perspectives[persp].codemirror.getCode(); } else if (persp == 'VisualPerspective') { html2text({ - element: $('#html-view div').get(0), + element: $('#html-view').find('div').get(0), success: function(text){ xml = text; }, @@ -59,6 +98,7 @@ self.$error.show(); } }); + self.updateAnnotationIds(self); } else { xml = this.doc.text; @@ -76,11 +116,11 @@ } else { self.$annos.html(''); - var anno_list = new Array(); + var anno_list = []; var annos = $(atype, doc); var counter = annos.length; var atype_rx = atype.replace(/,/g, '|'); - var ann_expr = new RegExp("^<("+atype_rx+")[^>]*>|$", "g") + var ann_expr = new RegExp("^<("+atype_rx+")[^>]*>|$", "g"); if (annos.length == 0) { @@ -89,19 +129,21 @@ self.$annos.show(); } annos.each(function (i, elem) { - xml_text = serializer.serializeToString(elem).replace(ann_expr, ""); + var xml_text = serializer.serializeToString(elem).replace(ann_expr, ""); xml2html({ xml: "" + xml_text + "", success: function(xml_text){ return function(elem){ elem.sortby = $(elem).text().trim(); - $(elem).append("
"+ xml_text.replace("&", "&", "g").replace("<", "<", "g") +"
") + $(elem).append("
"+ xml_text.replace(/&/g, "&").replace(/"); anno_list.push(elem); + $(".src", elem).click(function() { self.goToAnnotation(self, this); }); counter--; if (!counter) { anno_list.sort(function(a, b){return a.sortby.localeCompare(b.sortby);}); - self.$annos.append(anno_list); + for (i in anno_list) + self.$annos.append(anno_list[i]); self.$spinner.hide(); self.$annos.show(); } @@ -117,12 +159,10 @@ }); }); } - } - + }; - AnnotationsPerspective.prototype.onEnter = function(success, failure){ - var self = this; + AnnotationsPerspective.prototype.onEnter = function(){ $.wiki.Perspective.prototype.onEnter.call(this); $('.vsplitbar').not('.active').trigger('click'); @@ -131,9 +171,9 @@ }; - AnnotationsPerspective.prototype.onExit = function(success, failure) { + AnnotationsPerspective.prototype.onExit = function(success, failure) { - }; + }; $.wiki.AnnotationsPerspective = AnnotationsPerspective;