diff --git a/core/modules/storyviews/classic.js b/core/modules/storyviews/classic.js index 0b9d554c0..3470f7be2 100644 --- a/core/modules/storyviews/classic.js +++ b/core/modules/storyviews/classic.js @@ -28,14 +28,12 @@ ClassicStoryView.prototype.navigateTo = function(historyInfo) { targetElement = listItemWidget.findFirstDomNode(); // If anchor is provided, find the element the anchor pointing to var foundAnchor = false; - if(targetElement && historyInfo.anchor) { - var anchorElement = targetElement.querySelector("[data-anchor-id='" + historyInfo.anchor + "']"); - if(anchorElement) { - targetElement = anchorElement.parentNode; - var isBefore = anchorElement.dataset.anchorPreviousSibling === "true"; - if(isBefore) { - targetElement = targetElement.previousSibling; - } + if(listItemWidget && historyInfo.anchor) { + var anchorWidget = $tw.utils.findChildNodeInTree(listItemWidget, function(widget) { + return widget.anchorId === historyInfo.anchor; + }); + if(anchorWidget) { + targetElement = anchorWidget.findAnchorTargetDomNode() foundAnchor = true; } } diff --git a/core/modules/storyviews/pop.js b/core/modules/storyviews/pop.js index e2634e3d5..3b9f9e39c 100644 --- a/core/modules/storyviews/pop.js +++ b/core/modules/storyviews/pop.js @@ -23,12 +23,23 @@ PopStoryView.prototype.navigateTo = function(historyInfo) { } var listItemWidget = this.listWidget.children[listElementIndex], targetElement = listItemWidget.findFirstDomNode(); + // If anchor is provided, find the element the anchor pointing to + var foundAnchor = false; + if(listItemWidget && historyInfo.anchor) { + var anchorWidget = $tw.utils.findChildNodeInTree(listItemWidget, function(widget) { + return widget.anchorId === historyInfo.anchor; + }); + if(anchorWidget) { + targetElement = anchorWidget.findAnchorTargetDomNode() + foundAnchor = true; + } + } // Abandon if the list entry isn't a DOM element (it might be a text node) if(!targetElement || targetElement.nodeType === Node.TEXT_NODE) { return; } // Scroll the node into view - this.listWidget.dispatchEvent({type: "tm-scroll", target: targetElement}); + this.listWidget.dispatchEvent({type: "tm-scroll", target: targetElement, highlight: foundAnchor}); }; PopStoryView.prototype.insert = function(widget) { diff --git a/core/modules/storyviews/zoomin.js b/core/modules/storyviews/zoomin.js index d02f705e7..039047372 100644 --- a/core/modules/storyviews/zoomin.js +++ b/core/modules/storyviews/zoomin.js @@ -51,6 +51,16 @@ ZoominListView.prototype.navigateTo = function(historyInfo) { } var listItemWidget = this.listWidget.children[listElementIndex], targetElement = listItemWidget.findFirstDomNode(); + // If anchor is provided, find the element the anchor pointing to + var anchorElement = null; + if(listItemWidget && historyInfo.anchor) { + var anchorWidget = $tw.utils.findChildNodeInTree(listItemWidget, function(widget) { + return widget.anchorId === historyInfo.anchor; + }); + if(anchorWidget) { + anchorElement = anchorWidget.findAnchorTargetDomNode() + } + } // Abandon if the list entry isn't a DOM element (it might be a text node) if(!targetElement) { return; @@ -119,7 +129,10 @@ ZoominListView.prototype.navigateTo = function(historyInfo) { },duration); } // Scroll the target into view -// $tw.pageScroller.scrollIntoView(targetElement); + if(anchorElement) { + this.listWidget.dispatchEvent({type: "tm-scroll", target: anchorElement, highlight: true}); + } + // $tw.pageScroller.scrollIntoView(targetElement); }; /* diff --git a/core/modules/utils/parsetree.js b/core/modules/utils/parsetree.js index a74b8f3f8..7ee2de09e 100644 --- a/core/modules/utils/parsetree.js +++ b/core/modules/utils/parsetree.js @@ -103,6 +103,21 @@ exports.findParseTreeNode = function(nodeArray,search) { return undefined; }; +exports.findChildNodeInTree = function(root,searchFn) { + if(searchFn(root)) { + return root; + } + if(root.children && root.children.length > 0) { + for(var i=0; i