From 6862afa4b0f595671c4c07a1dde364647955037f Mon Sep 17 00:00:00 2001 From: Christian Stoller Date: Thu, 14 Apr 2016 14:31:49 +0200 Subject: [PATCH 1/2] Added method to refresh the placeholder of an affix --- dist/ngScrollSpy.debug.js | 18 ++++++++++++++---- dist/ngScrollSpy.js | 18 ++++++++++++++---- dist/ngScrollSpy.min.js | 2 +- src/affix.js | 18 ++++++++++++++---- 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/dist/ngScrollSpy.debug.js b/dist/ngScrollSpy.debug.js index 8eacde3..8ab71a7 100755 --- a/dist/ngScrollSpy.debug.js +++ b/dist/ngScrollSpy.debug.js @@ -198,12 +198,22 @@ mod.service('ScrollSpy', function($window) { }); mod.directive('affix', function(ScrollSpy) { - var affixCloneFn= function(elem) { - if (!elem.data('$ngScrollSpy.placeholder')) { + var affixCloneFn= function(elem) { + if (!elem.prop('ngScrollSpy')) { var placeholder = elem.clone(); - elem.data('$ngScrollSpy.placeholder', placeholder); + elem.prop('ngScrollSpy', { + 'placeholder': placeholder, + 'refreshPlaceholder': function() { + var newPlaceholder = elem.clone(); + if (placeholder[0].parentNode) { + placeholder[0].parentNode.replaceChild(newPlaceholder[0], placeholder[0]); + } + + elem.prop('ngScrollSpy').placeholder = newPlaceholder; + } + }); } - return elem.data('$ngScrollSpy.placeholder'); + return elem.prop('ngScrollSpy').placeholder; }; var affixFn= function(shouldAffixFn, wasAffixed, affixClass, affixOptions, elem) { diff --git a/dist/ngScrollSpy.js b/dist/ngScrollSpy.js index 0641e19..74e1d7e 100755 --- a/dist/ngScrollSpy.js +++ b/dist/ngScrollSpy.js @@ -198,12 +198,22 @@ mod.service('ScrollSpy', function($window) { }); mod.directive('affix', function(ScrollSpy) { - var affixCloneFn= function(elem) { - if (!elem.data('$ngScrollSpy.placeholder')) { + var affixCloneFn= function(elem) { + if (!elem.prop('ngScrollSpy')) { var placeholder = elem.clone(); - elem.data('$ngScrollSpy.placeholder', placeholder); + elem.prop('ngScrollSpy', { + 'placeholder': placeholder, + 'refreshPlaceholder': function() { + var newPlaceholder = elem.clone(); + if (placeholder[0].parentNode) { + placeholder[0].parentNode.replaceChild(newPlaceholder[0], placeholder[0]); + } + + elem.prop('ngScrollSpy').placeholder = newPlaceholder; + } + }); } - return elem.data('$ngScrollSpy.placeholder'); + return elem.prop('ngScrollSpy').placeholder; }; var affixFn= function(shouldAffixFn, wasAffixed, affixClass, affixOptions, elem) { diff --git a/dist/ngScrollSpy.min.js b/dist/ngScrollSpy.min.js index b3eed24..85bde7f 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 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 a=r[c].cond;(a(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){if(!t.data("$ngScrollSpy.placeholder")){var e=t.clone();t.data("$ngScrollSpy.placeholder",e)}return t.data("$ngScrollSpy.placeholder")},i=function(t,e,n,i,r){var l=t(r[0].getBoundingClientRect());l!==e&&(l?(i.placeholder&&r.after(o(r)),r.addClass(n)):(i.placeholder&&o(r).detach(),r.removeClass(n)))},r=function(o,r,l,s){var c,a=!1,u=!1,f=!1;l=t.extend({offset:0,placeholder:!1},l),"top"===o?n=e.onYScroll(function(t){u=a,i(function(e){return a?a=c<=t+l.offset:e.top<=l.offset?(c=e.top=t:n.bottom>=o.height?(c=e.isForced&&0===t?t+n.bottom-o.height-n.height:t+n.bottom-o.height,a=!0):!1},u,r,l,s)})):"left"===o?n=e.onXScroll(function(t){u=a,i(function(e){return a?a=t>=c:e.left<=0?(c=e.left<0?t+e.left:t,a=!0):!1},u,r,l,s)}):"right"===o&&(f=!0,n=e.onXScroll(function(t,n,o){u=a,i(function(n){return a?a=c>=t:n.right>=o.width?(c=e.isForced&&0===t?t+n.right-o.width-n.width:t+n.right-o.width,a=!0):!1},u,r,l,s)})),f&&e.trigger()};return{restrict:"A",scope:{affix:"@",affixClass:"@",affixOptions:"@"},link:function(t,o,i,l){t.affix=t.affix||"top",t.affixClass=t.affixClass||"affix",t.affixOptions=t.affixOptions?t.$eval(t.affixOptions):{},r(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)){o.spyElems=i[0].getElementsByClassName(o.selector),o.spies={},e().onRun=function(){o.spies[o.spyElems[0].id].set()},e().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=n.onYScroll(function(t,e,i){for(var r=null,a=o.spies,u=0;u=h?(d.pos=h,null===r&&(r=d),r.pos=i.maxHeight&&(r=a[l[l.length-1].id]),r.set(),o.$on("destroy",function(){n.removeHandler(c)})})}};return{restrict:"A",scope:{selector:"@",topmargin:"@"},link:o}}]),n.directive("pagemenu",["$compile","$location","$anchorScroll",function(t,n,o){var i=function(i,r){for(var l,s=[],c=[],a=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},u=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 +!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},r=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},i={},l=function(t){var l=o(e),s=r(n,l);if(!s.isEqual||l.hasOverscroll||t){for(var c in i){var a=i[c].cond;(a(l,s)||t)&&i[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 i[c]={cond:t,handler:e},c++,c-1},this.removeHandler=function(t){delete i[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){if(!t.prop("ngScrollSpy")){var e=t.clone();t.prop("ngScrollSpy",{placeholder:e,refreshPlaceholder:function(){var n=t.clone();e[0].parentNode&&e[0].parentNode.replaceChild(n[0],e[0]),t.prop("ngScrollSpy").placeholder=n}})}return t.prop("ngScrollSpy").placeholder},r=function(t,e,n,r,i){var l=t(i[0].getBoundingClientRect());l!==e&&(l?(r.placeholder&&i.after(o(i)),i.addClass(n)):(r.placeholder&&o(i).detach(),i.removeClass(n)))},i=function(o,i,l,s){var c,a=!1,u=!1,f=!1;l=t.extend({offset:0,placeholder:!1},l),"top"===o?n=e.onYScroll(function(t){u=a,r(function(e){return a?a=c<=t+l.offset:e.top<=l.offset?(c=e.top=t:n.bottom>=o.height?(c=e.isForced&&0===t?t+n.bottom-o.height-n.height:t+n.bottom-o.height,a=!0):!1},u,i,l,s)})):"left"===o?n=e.onXScroll(function(t){u=a,r(function(e){return a?a=t>=c:e.left<=0?(c=e.left<0?t+e.left:t,a=!0):!1},u,i,l,s)}):"right"===o&&(f=!0,n=e.onXScroll(function(t,n,o){u=a,r(function(n){return a?a=c>=t:n.right>=o.width?(c=e.isForced&&0===t?t+n.right-o.width-n.width:t+n.right-o.width,a=!0):!1},u,i,l,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,r,i){if(t.isDefined(o.selector)){o.spyElems=r[0].getElementsByClassName(o.selector),o.spies={},e().onRun=function(){o.spies[o.spyElems[0].id].set()},e().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=n.onYScroll(function(t,e,r){for(var i=null,a=o.spies,u=0;u=p?(d.pos=p,null===i&&(i=d),i.pos=r.maxHeight&&(i=a[l[l.length-1].id]),i.set(),o.$on("destroy",function(){n.removeHandler(c)})})}};return{restrict:"A",scope:{selector:"@",topmargin:"@"},link:o}}]),n.directive("pagemenu",["$compile","$location","$anchorScroll",function(t,n,o){var r=function(r,i){for(var l,s=[],c=[],a=function(t){for(var e={link:t.id,text:t.textContent||t.innerText,parent:""},n=t.tagName,o=0;o=0&&n!=s[i];i--);if(0>i)s.push(n),e.push=!0,c.push(l);else for(e.pop=r-1-i;s.length>i+1;)s.pop(),c.pop()}return c.length>0&&(e.parent=c[c.length-1]),l=e.link,e},u=e().items(),f="",d=0;d';else if(p.pop)for(var h=0;h";else 0!==d&&(f+="");f+='
  • ',f+='',f+=p.text,f+=""}f+="
  • ",i.append(t(f)(r)),i.on("click",function(t){var r=t.target.hash.substring(1);n.hash(r),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(){r(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/affix.js b/src/affix.js index ad8bf37..7f9bad9 100644 --- a/src/affix.js +++ b/src/affix.js @@ -1,10 +1,20 @@ mod.directive('affix', function(ScrollSpy) { - var affixCloneFn= function(elem) { - if (!elem.data('$ngScrollSpy.placeholder')) { + var affixCloneFn= function(elem) { + if (!elem.prop('ngScrollSpy')) { var placeholder = elem.clone(); - elem.data('$ngScrollSpy.placeholder', placeholder); + elem.prop('ngScrollSpy', { + 'placeholder': placeholder, + 'refreshPlaceholder': function() { + var newPlaceholder = elem.clone(); + if (placeholder[0].parentNode) { + placeholder[0].parentNode.replaceChild(newPlaceholder[0], placeholder[0]); + } + + elem.prop('ngScrollSpy').placeholder = newPlaceholder; + } + }); } - return elem.data('$ngScrollSpy.placeholder'); + return elem.prop('ngScrollSpy').placeholder; }; var affixFn= function(shouldAffixFn, wasAffixed, affixClass, affixOptions, elem) { From 9e905c6b8f19029d0f2ebabe84e06affb0e495a5 Mon Sep 17 00:00:00 2001 From: Christian Stoller Date: Thu, 14 Apr 2016 16:27:02 +0200 Subject: [PATCH 2/2] Remove 'affix'-class from placeholder clone --- dist/ngScrollSpy.debug.js | 8 +++++--- dist/ngScrollSpy.js | 8 +++++--- dist/ngScrollSpy.min.js | 2 +- src/affix.js | 8 +++++--- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/dist/ngScrollSpy.debug.js b/dist/ngScrollSpy.debug.js index 8ab71a7..ffdb109 100755 --- a/dist/ngScrollSpy.debug.js +++ b/dist/ngScrollSpy.debug.js @@ -198,13 +198,15 @@ mod.service('ScrollSpy', function($window) { }); mod.directive('affix', function(ScrollSpy) { - var affixCloneFn= function(elem) { + var affixCloneFn= function(elem, affixClass) { if (!elem.prop('ngScrollSpy')) { var placeholder = elem.clone(); + placeholder.removeClass(affixClass).addClass('ngscrollspy-placeholder'); elem.prop('ngScrollSpy', { 'placeholder': placeholder, 'refreshPlaceholder': function() { var newPlaceholder = elem.clone(); + newPlaceholder.removeClass(affixClass).addClass('ngscrollspy-placeholder'); if (placeholder[0].parentNode) { placeholder[0].parentNode.replaceChild(newPlaceholder[0], placeholder[0]); } @@ -223,13 +225,13 @@ mod.directive('affix', function(ScrollSpy) { if(affixOptions.placeholder) { // insert cloned element into DOM to serve as a placeholder, // because the original element (elem) will be pulled out of the flow by getting affixed - elem.after(affixCloneFn(elem)); + elem.after(affixCloneFn(elem, affixClass)); } elem.addClass(affixClass); } else { if(affixOptions.placeholder) { // remove clone from DOM again - affixCloneFn(elem).detach(); + affixCloneFn(elem, affixClass).detach(); } elem.removeClass(affixClass); } diff --git a/dist/ngScrollSpy.js b/dist/ngScrollSpy.js index 74e1d7e..0221235 100755 --- a/dist/ngScrollSpy.js +++ b/dist/ngScrollSpy.js @@ -198,13 +198,15 @@ mod.service('ScrollSpy', function($window) { }); mod.directive('affix', function(ScrollSpy) { - var affixCloneFn= function(elem) { + var affixCloneFn= function(elem, affixClass) { if (!elem.prop('ngScrollSpy')) { var placeholder = elem.clone(); + placeholder.removeClass(affixClass).addClass('ngscrollspy-placeholder'); elem.prop('ngScrollSpy', { 'placeholder': placeholder, 'refreshPlaceholder': function() { var newPlaceholder = elem.clone(); + newPlaceholder.removeClass(affixClass).addClass('ngscrollspy-placeholder'); if (placeholder[0].parentNode) { placeholder[0].parentNode.replaceChild(newPlaceholder[0], placeholder[0]); } @@ -223,13 +225,13 @@ mod.directive('affix', function(ScrollSpy) { if(affixOptions.placeholder) { // insert cloned element into DOM to serve as a placeholder, // because the original element (elem) will be pulled out of the flow by getting affixed - elem.after(affixCloneFn(elem)); + elem.after(affixCloneFn(elem, affixClass)); } elem.addClass(affixClass); } else { if(affixOptions.placeholder) { // remove clone from DOM again - affixCloneFn(elem).detach(); + affixCloneFn(elem, affixClass).detach(); } elem.removeClass(affixClass); } diff --git a/dist/ngScrollSpy.min.js b/dist/ngScrollSpy.min.js index 85bde7f..7e6af00 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 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},r=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},i={},l=function(t){var l=o(e),s=r(n,l);if(!s.isEqual||l.hasOverscroll||t){for(var c in i){var a=i[c].cond;(a(l,s)||t)&&i[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 i[c]={cond:t,handler:e},c++,c-1},this.removeHandler=function(t){delete i[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){if(!t.prop("ngScrollSpy")){var e=t.clone();t.prop("ngScrollSpy",{placeholder:e,refreshPlaceholder:function(){var n=t.clone();e[0].parentNode&&e[0].parentNode.replaceChild(n[0],e[0]),t.prop("ngScrollSpy").placeholder=n}})}return t.prop("ngScrollSpy").placeholder},r=function(t,e,n,r,i){var l=t(i[0].getBoundingClientRect());l!==e&&(l?(r.placeholder&&i.after(o(i)),i.addClass(n)):(r.placeholder&&o(i).detach(),i.removeClass(n)))},i=function(o,i,l,s){var c,a=!1,u=!1,f=!1;l=t.extend({offset:0,placeholder:!1},l),"top"===o?n=e.onYScroll(function(t){u=a,r(function(e){return a?a=c<=t+l.offset:e.top<=l.offset?(c=e.top=t:n.bottom>=o.height?(c=e.isForced&&0===t?t+n.bottom-o.height-n.height:t+n.bottom-o.height,a=!0):!1},u,i,l,s)})):"left"===o?n=e.onXScroll(function(t){u=a,r(function(e){return a?a=t>=c:e.left<=0?(c=e.left<0?t+e.left:t,a=!0):!1},u,i,l,s)}):"right"===o&&(f=!0,n=e.onXScroll(function(t,n,o){u=a,r(function(n){return a?a=c>=t:n.right>=o.width?(c=e.isForced&&0===t?t+n.right-o.width-n.width:t+n.right-o.width,a=!0):!1},u,i,l,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,r,i){if(t.isDefined(o.selector)){o.spyElems=r[0].getElementsByClassName(o.selector),o.spies={},e().onRun=function(){o.spies[o.spyElems[0].id].set()},e().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=n.onYScroll(function(t,e,r){for(var i=null,a=o.spies,u=0;u=p?(d.pos=p,null===i&&(i=d),i.pos=r.maxHeight&&(i=a[l[l.length-1].id]),i.set(),o.$on("destroy",function(){n.removeHandler(c)})})}};return{restrict:"A",scope:{selector:"@",topmargin:"@"},link:o}}]),n.directive("pagemenu",["$compile","$location","$anchorScroll",function(t,n,o){var r=function(r,i){for(var l,s=[],c=[],a=function(t){for(var e={link:t.id,text:t.textContent||t.innerText,parent:""},n=t.tagName,o=0;o=0&&n!=s[i];i--);if(0>i)s.push(n),e.push=!0,c.push(l);else for(e.pop=r-1-i;s.length>i+1;)s.pop(),c.pop()}return c.length>0&&(e.parent=c[c.length-1]),l=e.link,e},u=e().items(),f="",d=0;d';else if(p.pop)for(var h=0;h";else 0!==d&&(f+="");f+='
  • ',f+='',f+=p.text,f+=""}f+="
  • ",i.append(t(f)(r)),i.on("click",function(t){var r=t.target.hash.substring(1);n.hash(r),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(){r(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 +!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},r=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},i={},l=function(t){var l=o(e),s=r(n,l);if(!s.isEqual||l.hasOverscroll||t){for(var c in i){var a=i[c].cond;(a(l,s)||t)&&i[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 i[c]={cond:t,handler:e},c++,c-1},this.removeHandler=function(t){delete i[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){if(!t.prop("ngScrollSpy")){var n=t.clone();n.removeClass(e).addClass("ngscrollspy-placeholder"),t.prop("ngScrollSpy",{placeholder:n,refreshPlaceholder:function(){var o=t.clone();o.removeClass(e).addClass("ngscrollspy-placeholder"),n[0].parentNode&&n[0].parentNode.replaceChild(o[0],n[0]),t.prop("ngScrollSpy").placeholder=o}})}return t.prop("ngScrollSpy").placeholder},r=function(t,e,n,r,i){var l=t(i[0].getBoundingClientRect());l!==e&&(l?(r.placeholder&&i.after(o(i,n)),i.addClass(n)):(r.placeholder&&o(i,n).detach(),i.removeClass(n)))},i=function(o,i,l,s){var c,a=!1,u=!1,f=!1;l=t.extend({offset:0,placeholder:!1},l),"top"===o?n=e.onYScroll(function(t){u=a,r(function(e){return a?a=c<=t+l.offset:e.top<=l.offset?(c=e.top=t:n.bottom>=o.height?(c=e.isForced&&0===t?t+n.bottom-o.height-n.height:t+n.bottom-o.height,a=!0):!1},u,i,l,s)})):"left"===o?n=e.onXScroll(function(t){u=a,r(function(e){return a?a=t>=c:e.left<=0?(c=e.left<0?t+e.left:t,a=!0):!1},u,i,l,s)}):"right"===o&&(f=!0,n=e.onXScroll(function(t,n,o){u=a,r(function(n){return a?a=c>=t:n.right>=o.width?(c=e.isForced&&0===t?t+n.right-o.width-n.width:t+n.right-o.width,a=!0):!1},u,i,l,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,r,i){if(t.isDefined(o.selector)){o.spyElems=r[0].getElementsByClassName(o.selector),o.spies={},e().onRun=function(){o.spies[o.spyElems[0].id].set()},e().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=n.onYScroll(function(t,e,r){for(var i=null,a=o.spies,u=0;u=p?(d.pos=p,null===i&&(i=d),i.pos=r.maxHeight&&(i=a[l[l.length-1].id]),i.set(),o.$on("destroy",function(){n.removeHandler(c)})})}};return{restrict:"A",scope:{selector:"@",topmargin:"@"},link:o}}]),n.directive("pagemenu",["$compile","$location","$anchorScroll",function(t,n,o){var r=function(r,i){for(var l,s=[],c=[],a=function(t){for(var e={link:t.id,text:t.textContent||t.innerText,parent:""},n=t.tagName,o=0;o=0&&n!=s[i];i--);if(0>i)s.push(n),e.push=!0,c.push(l);else for(e.pop=r-1-i;s.length>i+1;)s.pop(),c.pop()}return c.length>0&&(e.parent=c[c.length-1]),l=e.link,e},u=e().items(),f="",d=0;d';else if(p.pop)for(var h=0;h";else 0!==d&&(f+="");f+='
  • ',f+='',f+=p.text,f+=""}f+="
  • ",i.append(t(f)(r)),i.on("click",function(t){var r=t.target.hash.substring(1);n.hash(r),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(){r(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/affix.js b/src/affix.js index 7f9bad9..8e6cadf 100644 --- a/src/affix.js +++ b/src/affix.js @@ -1,11 +1,13 @@ mod.directive('affix', function(ScrollSpy) { - var affixCloneFn= function(elem) { + var affixCloneFn= function(elem, affixClass) { if (!elem.prop('ngScrollSpy')) { var placeholder = elem.clone(); + placeholder.removeClass(affixClass).addClass('ngscrollspy-placeholder'); elem.prop('ngScrollSpy', { 'placeholder': placeholder, 'refreshPlaceholder': function() { var newPlaceholder = elem.clone(); + newPlaceholder.removeClass(affixClass).addClass('ngscrollspy-placeholder'); if (placeholder[0].parentNode) { placeholder[0].parentNode.replaceChild(newPlaceholder[0], placeholder[0]); } @@ -24,13 +26,13 @@ mod.directive('affix', function(ScrollSpy) { if(affixOptions.placeholder) { // insert cloned element into DOM to serve as a placeholder, // because the original element (elem) will be pulled out of the flow by getting affixed - elem.after(affixCloneFn(elem)); + elem.after(affixCloneFn(elem, affixClass)); } elem.addClass(affixClass); } else { if(affixOptions.placeholder) { // remove clone from DOM again - affixCloneFn(elem).detach(); + affixCloneFn(elem, affixClass).detach(); } elem.removeClass(affixClass); }