From 983055c269f91ed13283984950acfcd0d9b842c8 Mon Sep 17 00:00:00 2001 From: Dean O'Connor Date: Thu, 25 Jun 2015 18:05:11 +1000 Subject: [PATCH] Pass a page base url to the pageitems directive to allow menu link to be compatible with routing. --- dist/ngScrollSpy.debug.js | 11 ++++++++++- dist/ngScrollSpy.js | 11 ++++++++++- dist/ngScrollSpy.min.js | 2 +- src/pageitems.js | 11 ++++++++++- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/dist/ngScrollSpy.debug.js b/dist/ngScrollSpy.debug.js index f9f5a02..a89c182 100755 --- a/dist/ngScrollSpy.debug.js +++ b/dist/ngScrollSpy.debug.js @@ -353,6 +353,14 @@ mod.directive('pageitems', function(ScrollSpy) { return; } scope.spyElems = elem[0].getElementsByClassName(scope.selector); // dom items + + // Prefix the page URL base URL to the id + if (angular.isDefined(scope.pageurl)) { + for (var i = 0; i < scope.spyElems.length; i++) { + scope.spyElems[i].id = scope.pageurl + '#' + scope.spyElems[i].id; + } + } + scope.spies = {}; // menu items // this function will be called once dom is parsed and menu is created @@ -425,7 +433,8 @@ mod.directive('pageitems', function(ScrollSpy) { restrict: 'A', scope: { selector: '@', - topmargin: '@' + topmargin: '@', + pageurl: '@' }, link: linkfn }; diff --git a/dist/ngScrollSpy.js b/dist/ngScrollSpy.js index c200ac9..770b887 100755 --- a/dist/ngScrollSpy.js +++ b/dist/ngScrollSpy.js @@ -353,6 +353,14 @@ mod.directive('pageitems', function(ScrollSpy) { return; } scope.spyElems = elem[0].getElementsByClassName(scope.selector); // dom items + + // Prefix the page URL base URL to the id + if (angular.isDefined(scope.pageurl)) { + for (var i = 0; i < scope.spyElems.length; i++) { + scope.spyElems[i].id = scope.pageurl + '#' + scope.spyElems[i].id; + } + } + scope.spies = {}; // menu items // this function will be called once dom is parsed and menu is created @@ -425,7 +433,8 @@ mod.directive('pageitems', function(ScrollSpy) { restrict: 'A', scope: { selector: '@', - topmargin: '@' + topmargin: '@', + pageurl: '@' }, link: linkfn }; diff --git a/dist/ngScrollSpy.min.js b/dist/ngScrollSpy.min.js index d52d011..85bacae 100755 --- a/dist/ngScrollSpy.min.js +++ b/dist/ngScrollSpy.min.js @@ -2,4 +2,4 @@ Copyright (c) 2014 Magnús Örn Gylfason Licence: MIT */ -!function(t){"use strict";function n(){return o}var e=t.module("ngScrollSpy",[]);e.service("ScrollSpy",["$window",function(n){var e,o=function(t){var n=(t.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,t.innerHeight||document.documentElement.clientHeight||document.body.clientHeight,void 0!==t.pageXOffset),e="CSS1Compat"===(document.compatMode||""),o=(n?t.pageXOffset:e?document.documentElement.scrollLeft:document.body.scrollLeft,n?t.pageYOffset:e?document.documentElement.scrollTop:document.body.scrollTop,{width:t.innerWidth,height:t.innerHeight,maxWidth:t.document.body.scrollWidth,maxHeight:t.document.body.scrollHeight,posX:t.scrollX||t.pageXOffset||t.document.documentElement.scrollLeft,posY:t.scrollY||t.pageYOffset||t.document.documentElement.scrollTop});return o.posX<0?(o.posX=0,o.overscrollLeft=!0):o.posX+o.width>o.maxWidth&&(o.posX=o.maxWidth-o.width,o.overscrollRight=!0),o.posY<0?(o.posY=0,o.overscrollTop=!0):o.posY+o.height>o.maxHeight&&(o.posY=o.maxHeight-o.height,o.overscrollBottom=!0),o.hasOverscroll=o.overscrollTop||o.overscrollBottom||o.overscrollLeft||o.overscrollRight,o},i=function(n,e){if(!n||!e)return t.extend({isEqual:!1,velocityX:0,velocityY:0},e);var o={posX:e.posX-n.posX,posY:e.posY-n.posY,width:e.width-n.width,height:e.height-n.height,maxWidth:e.maxWidth-n.maxWidth,maxHeight:e.maxHeight-n.maxHeight};return e.width>0&&(o.velocityX=o.posX/e.width),e.height>0&&(o.velocityY=o.posY/e.height),o.isEqual=!(0!==o.posX||0!==o.posY||0!==o.width||0!==o.height||0!==o.maxWidth||0!==o.maxHeight),o},r={},l=function(t){var l=o(n),s=i(e,l);if(!s.isEqual||l.hasOverscroll||t){for(var c in r){var u=r[c].cond;(u(l,s)||t)&&r[c].handler(l,s)}e=l}};t.element(n).on("scroll",l);var s=this,c=0;this.trigger=function(){this.isForced=!0,l(!0),this.isForced=!1},this.addHandler=function(t,n){return r[c]={cond:t,handler:n},c++,c-1},this.removeHandler=function(t){delete r[t]},this.onScroll=function(t){return s.addHandler(function(){return!0},function(n,e){t(n,e)})},this.onXScroll=function(t){return s.addHandler(function(t,n){return 0!==n.posX},function(n,e){t(n.posX,e.posX,n,e)})},this.onYScroll=function(t){return s.addHandler(function(t,n){return 0!==n.posY},function(n,e){t(n.posY,e.posY,n,e)})},this.onOverscrollHorz=function(t){return s.addHandler(function(t,n){return t.overscrollLeft||t.overscrollRight},t)},this.onOverscrollLeft=function(t){return s.addHandler(function(t,n){return t.overscrollLeft},t)},this.onOverscrollRight=function(t){return s.addHandler(function(t,n){return t.overscrollRight},t)},this.onOverscrollVert=function(t){return s.addHandler(function(t,n){return t.overscrollTop||t.overscrollBottom},t)},this.onOverscrollTop=function(t){return s.addHandler(function(t,n){return t.overscrollTop},t)},this.onOverscrollBottom=function(t){return s.addHandler(function(t,n){return t.overscrollBottom},t)}}]),e.directive("affix",["ScrollSpy",function(n){var e,o=function(t,n,e,o){var i=t(o[0].getBoundingClientRect());i!==n&&(i?o.addClass(e):o.removeClass(e))},i=function(i,r,l,s){var c,u=!1,a=!1,f=!1;l=t.extend({offset:0},l),"top"===i?e=n.onYScroll(function(t){a=u,o(function(n){return u?u=c<=t+l.offset:n.top<=l.offset?(c=n.top=t:e.bottom>=i.height?(c=n.isForced&&0===t?t+e.bottom-i.height-e.height:t+e.bottom-i.height,u=!0):!1},a,r,s)})):"left"===i?e=n.onXScroll(function(t){a=u,o(function(n){return u?u=t>=c:n.left<=0?(c=n.left<0?t+n.left:t,u=!0):!1},a,r,s)}):"right"===i&&(f=!0,e=n.onXScroll(function(t,e,i){a=u,o(function(e){return u?u=c>=t:e.right>=i.width?(c=n.isForced&&0===t?t+e.right-i.width-e.width:t+e.right-i.width,u=!0):!1},a,r,s)})),f&&n.trigger()};return{restrict:"A",scope:{affix:"@",affixClass:"@",affixOptions:"@"},link:function(t,o,r,l){t.affix=t.affix||"top",t.affixClass=t.affixClass||"affix",t.affixOptions=t.affixOptions?t.$eval(t.affixOptions):{},i(t.affix,t.affixClass,t.affixOptions,o),t.$on("destroy",function(){n.removeHandler(e)})}}}]);var o={onRun:null,state:null,store:function(t){for(var n in t)this[n]=t[n];this.state=!0,this.run()},builder:null,setBuilder:function(t){this.builder=t,this.run()},run:function(){this.builder&&this.state&&(this.builder(),this.builder=null,this.state=null,this.onRun&&(this.onRun(),this.onRun=null))}};e.directive("pageitems",["ScrollSpy",function(e){var o=function(o,i,r){if(t.isDefined(o.selector)){o.spyElems=i[0].getElementsByClassName(o.selector),o.spies={},n().onRun=function(){o.spies[o.spyElems[0].id].set()},n().store({topMargin:function(){return 0|o.topmargin},addSpy:function(t){o.spies[t.id]=t},getSpy:function(t){return o.spies[t]},items:function(){return o.spyElems}});var l=o.spyElems,s=0|o.topmargin,c=e.onYScroll(function(t,n,i){for(var r=null,u=o.spies,a=0;a=h?(d.pos=h,null===r&&(r=d),r.pos=i.maxHeight&&(r=u[l[l.length-1].id]),r.set(),o.$on("destroy",function(){e.removeHandler(c)})})}};return{restrict:"A",scope:{selector:"@",topmargin:"@"},link:o}}]),e.directive("pagemenu",["$compile","$location","$anchorScroll",function(t,e,o){var i=function(i,r){for(var l,s=[],c=[],u=function(t){for(var n={link:t.id,text:t.textContent||t.innerText,parent:""},e=t.tagName,o=0;o=0&&e!=s[r];r--);if(0>r)s.push(e),n.push=!0,c.push(l);else for(n.pop=i-1-r;s.length>r+1;)s.pop(),c.pop()}return c.length>0&&(n.parent=c[c.length-1]),l=n.link,n},a=n().items(),f="",d=0;d';else if(h.pop)for(var p=0;p";else 0!==d&&(f+="");f+='
  • ',f+='',f+=h.text,f+=""}f+="
  • ",r.append(t(f)(i)),r.on("click",function(t){var i=t.target.hash.substring(1);e.hash(i),o(),0!==n().topMargin()&&setTimeout(function(){window.scrollTo(window.pageXOffset,window.pageYOffset-n().topMargin())},0)})};return{restrict:"E",replace:!0,template:'',link:function(t,e){n().setBuilder(function(){i(t,e)})}}}]),e.directive("pagemenuspy",["$location","$anchorScroll",function(t,e){return{restrict:"A",link:function(t,e,o){n().addSpy({id:o.pagemenuspy,parent:o.parent,set:function(){e.addClass("active");var t=n().getSpy(this.parent);t&&t.set()},clear:function(){e.removeClass("active")}})}}}])}(angular); \ No newline at end of file +!function(t){"use strict";function e(){return o}var n=t.module("ngScrollSpy",[]);n.service("ScrollSpy",["$window",function(e){var n,o=function(t){var e=(t.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,t.innerHeight||document.documentElement.clientHeight||document.body.clientHeight,void 0!==t.pageXOffset),n="CSS1Compat"===(document.compatMode||""),o=(e?t.pageXOffset:n?document.documentElement.scrollLeft:document.body.scrollLeft,e?t.pageYOffset:n?document.documentElement.scrollTop:document.body.scrollTop,{width:t.innerWidth,height:t.innerHeight,maxWidth:t.document.body.scrollWidth,maxHeight:t.document.body.scrollHeight,posX:t.scrollX||t.pageXOffset||t.document.documentElement.scrollLeft,posY:t.scrollY||t.pageYOffset||t.document.documentElement.scrollTop});return o.posX<0?(o.posX=0,o.overscrollLeft=!0):o.posX+o.width>o.maxWidth&&(o.posX=o.maxWidth-o.width,o.overscrollRight=!0),o.posY<0?(o.posY=0,o.overscrollTop=!0):o.posY+o.height>o.maxHeight&&(o.posY=o.maxHeight-o.height,o.overscrollBottom=!0),o.hasOverscroll=o.overscrollTop||o.overscrollBottom||o.overscrollLeft||o.overscrollRight,o},i=function(e,n){if(!e||!n)return t.extend({isEqual:!1,velocityX:0,velocityY:0},n);var o={posX:n.posX-e.posX,posY:n.posY-e.posY,width:n.width-e.width,height:n.height-e.height,maxWidth:n.maxWidth-e.maxWidth,maxHeight:n.maxHeight-e.maxHeight};return n.width>0&&(o.velocityX=o.posX/n.width),n.height>0&&(o.velocityY=o.posY/n.height),o.isEqual=!(0!==o.posX||0!==o.posY||0!==o.width||0!==o.height||0!==o.maxWidth||0!==o.maxHeight),o},r={},l=function(t){var l=o(e),s=i(n,l);if(!s.isEqual||l.hasOverscroll||t){for(var c in r){var u=r[c].cond;(u(l,s)||t)&&r[c].handler(l,s)}n=l}};t.element(e).on("scroll",l);var s=this,c=0;this.trigger=function(){this.isForced=!0,l(!0),this.isForced=!1},this.addHandler=function(t,e){return r[c]={cond:t,handler:e},c++,c-1},this.removeHandler=function(t){delete r[t]},this.onScroll=function(t){return s.addHandler(function(){return!0},function(e,n){t(e,n)})},this.onXScroll=function(t){return s.addHandler(function(t,e){return 0!==e.posX},function(e,n){t(e.posX,n.posX,e,n)})},this.onYScroll=function(t){return s.addHandler(function(t,e){return 0!==e.posY},function(e,n){t(e.posY,n.posY,e,n)})},this.onOverscrollHorz=function(t){return s.addHandler(function(t,e){return t.overscrollLeft||t.overscrollRight},t)},this.onOverscrollLeft=function(t){return s.addHandler(function(t,e){return t.overscrollLeft},t)},this.onOverscrollRight=function(t){return s.addHandler(function(t,e){return t.overscrollRight},t)},this.onOverscrollVert=function(t){return s.addHandler(function(t,e){return t.overscrollTop||t.overscrollBottom},t)},this.onOverscrollTop=function(t){return s.addHandler(function(t,e){return t.overscrollTop},t)},this.onOverscrollBottom=function(t){return s.addHandler(function(t,e){return t.overscrollBottom},t)}}]),n.directive("affix",["ScrollSpy",function(e){var n,o=function(t,e,n,o){var i=t(o[0].getBoundingClientRect());i!==e&&(i?o.addClass(n):o.removeClass(n))},i=function(i,r,l,s){var c,u=!1,a=!1,f=!1;l=t.extend({offset:0},l),"top"===i?n=e.onYScroll(function(t){a=u,o(function(e){return u?u=c<=t+l.offset:e.top<=l.offset?(c=e.top=t:n.bottom>=i.height?(c=e.isForced&&0===t?t+n.bottom-i.height-n.height:t+n.bottom-i.height,u=!0):!1},a,r,s)})):"left"===i?n=e.onXScroll(function(t){a=u,o(function(e){return u?u=t>=c:e.left<=0?(c=e.left<0?t+e.left:t,u=!0):!1},a,r,s)}):"right"===i&&(f=!0,n=e.onXScroll(function(t,n,i){a=u,o(function(n){return u?u=c>=t:n.right>=i.width?(c=e.isForced&&0===t?t+n.right-i.width-n.width:t+n.right-i.width,u=!0):!1},a,r,s)})),f&&e.trigger()};return{restrict:"A",scope:{affix:"@",affixClass:"@",affixOptions:"@"},link:function(t,o,r,l){t.affix=t.affix||"top",t.affixClass=t.affixClass||"affix",t.affixOptions=t.affixOptions?t.$eval(t.affixOptions):{},i(t.affix,t.affixClass,t.affixOptions,o),t.$on("destroy",function(){e.removeHandler(n)})}}}]);var o={onRun:null,state:null,store:function(t){for(var e in t)this[e]=t[e];this.state=!0,this.run()},builder:null,setBuilder:function(t){this.builder=t,this.run()},run:function(){this.builder&&this.state&&(this.builder(),this.builder=null,this.state=null,this.onRun&&(this.onRun(),this.onRun=null))}};n.directive("pageitems",["ScrollSpy",function(n){var o=function(o,i,r){if(t.isDefined(o.selector)){if(o.spyElems=i[0].getElementsByClassName(o.selector),t.isDefined(o.pageurl))for(var l=0;l=h?(d.pos=h,null===r&&(r=d),r.pos=i.maxHeight&&(r=l[s[s.length-1].id]),r.set(),o.$on("destroy",function(){n.removeHandler(u)})})}};return{restrict:"A",scope:{selector:"@",topmargin:"@",pageurl:"@"},link:o}}]),n.directive("pagemenu",["$compile","$location","$anchorScroll",function(t,n,o){var i=function(i,r){for(var l,s=[],c=[],u=function(t){for(var e={link:t.id,text:t.textContent||t.innerText,parent:""},n=t.tagName,o=0;o=0&&n!=s[r];r--);if(0>r)s.push(n),e.push=!0,c.push(l);else for(e.pop=i-1-r;s.length>r+1;)s.pop(),c.pop()}return c.length>0&&(e.parent=c[c.length-1]),l=e.link,e},a=e().items(),f="",d=0;d';else if(h.pop)for(var p=0;p";else 0!==d&&(f+="");f+='
  • ',f+='',f+=h.text,f+=""}f+="
  • ",r.append(t(f)(i)),r.on("click",function(t){var i=t.target.hash.substring(1);n.hash(i),o(),0!==e().topMargin()&&setTimeout(function(){window.scrollTo(window.pageXOffset,window.pageYOffset-e().topMargin())},0)})};return{restrict:"E",replace:!0,template:'',link:function(t,n){e().setBuilder(function(){i(t,n)})}}}]),n.directive("pagemenuspy",["$location","$anchorScroll",function(t,n){return{restrict:"A",link:function(t,n,o){e().addSpy({id:o.pagemenuspy,parent:o.parent,set:function(){n.addClass("active");var t=e().getSpy(this.parent);t&&t.set()},clear:function(){n.removeClass("active")}})}}}])}(angular); \ No newline at end of file diff --git a/src/pageitems.js b/src/pageitems.js index 41f4d8a..c840cec 100644 --- a/src/pageitems.js +++ b/src/pageitems.js @@ -5,6 +5,14 @@ mod.directive('pageitems', function(ScrollSpy) { return; } scope.spyElems = elem[0].getElementsByClassName(scope.selector); // dom items + + // Prefix the page URL base URL to the id + if (angular.isDefined(scope.pageurl)) { + for (var i = 0; i < scope.spyElems.length; i++) { + scope.spyElems[i].id = scope.pageurl + '#' + scope.spyElems[i].id; + } + } + scope.spies = {}; // menu items // this function will be called once dom is parsed and menu is created @@ -77,7 +85,8 @@ mod.directive('pageitems', function(ScrollSpy) { restrict: 'A', scope: { selector: '@', - topmargin: '@' + topmargin: '@', + pageurl: '@' }, link: linkfn };