|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +angular.module('mgcrea.bootstrap.affix', ['mgcrea.jqlite.dimensions']) |
| 4 | + |
| 5 | + .provider('$affix', function() { |
| 6 | + |
| 7 | + var jqLite = angular.element; |
| 8 | + |
| 9 | + var defaults = this.defaults = { |
| 10 | + offsetTop: 'auto' |
| 11 | + }; |
| 12 | + |
| 13 | + this.$get = function($window, dimensions) { |
| 14 | + |
| 15 | + var windowEl = jqLite($window); |
| 16 | + var bodyEl = jqLite($window.document.body); |
| 17 | + |
| 18 | + function AffixFactory(element, config) { |
| 19 | + |
| 20 | + var $affix = {}; |
| 21 | + |
| 22 | + // Common vars |
| 23 | + var options = angular.extend({}, defaults, config); |
| 24 | + |
| 25 | + // Initial private vars |
| 26 | + var reset = 'affix affix-top affix-bottom', |
| 27 | + initialAffixTop = 0, |
| 28 | + initialOffsetTop = 0, |
| 29 | + affixed = null, |
| 30 | + unpin = null; |
| 31 | + var parent = element.parent(); |
| 32 | + if (options.offsetParent) { |
| 33 | + if (options.offsetParent.match(/^\d+$/)) { |
| 34 | + for (var i = 0; i < (options.offsetParent * 1) - 1; i++) { |
| 35 | + parent = parent.parent(); |
| 36 | + } |
| 37 | + } |
| 38 | + else { |
| 39 | + parent = jqLite(options.offsetParent); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Options: offsets |
| 44 | + var offsetTop = 0; |
| 45 | + if(options.offsetTop) { |
| 46 | + if(options.offsetTop === 'auto' || options.offsetTop.match(/^[-+]\d+$/)) { |
| 47 | + initialAffixTop -= options.offsetTop * 1; |
| 48 | + if(options.offsetParent) { |
| 49 | + offsetTop = dimensions.offset(parent[0]).top + (options.offsetTop * 1); |
| 50 | + } |
| 51 | + else { |
| 52 | + offsetTop = dimensions.offset(element[0]).top - dimensions.css(element[0], 'marginTop', true) + (options.offsetTop * 1); |
| 53 | + } |
| 54 | + } |
| 55 | + else { |
| 56 | + offsetTop = options.offsetTop * 1; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + var offsetBottom = 0; |
| 61 | + if(options.offsetBottom) { |
| 62 | + if(options.offsetParent && options.offsetBottom.match(/^[-+]\d+$/)) { |
| 63 | + // add 1 pixel due to rounding problems... |
| 64 | + offsetBottom = $window.document.body.scrollHeight - (dimensions.offset(parent[0]).top + dimensions.height(parent[0])) + (options.offsetBottom * 1) + 1; |
| 65 | + } |
| 66 | + else { |
| 67 | + offsetBottom = options.offsetBottom * 1; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + $affix.init = function() { |
| 72 | + |
| 73 | + initialOffsetTop = dimensions.offset(element[0]).top + initialAffixTop; |
| 74 | + |
| 75 | + // Bind events |
| 76 | + windowEl.on('scroll', this.checkPosition); |
| 77 | + windowEl.on('click', this.checkPositionWithEventLoop); |
| 78 | + // Both of these checkPosition() calls are necessary for the case where |
| 79 | + // the user hits refresh after scrolling to the bottom of the page. |
| 80 | + this.checkPosition(); |
| 81 | + this.checkPositionWithEventLoop(); |
| 82 | + |
| 83 | + }; |
| 84 | + |
| 85 | + $affix.destroy = function() { |
| 86 | + |
| 87 | + // Unbind events |
| 88 | + windowEl.off('scroll', this.checkPosition); |
| 89 | + windowEl.off('click', this.checkPositionWithEventLoop); |
| 90 | + |
| 91 | + }; |
| 92 | + |
| 93 | + $affix.checkPositionWithEventLoop = function() { |
| 94 | + |
| 95 | + setTimeout(this.checkPosition, 1); |
| 96 | + |
| 97 | + }; |
| 98 | + |
| 99 | + $affix.checkPosition = function() { |
| 100 | + // if (!this.$element.is(':visible')) return |
| 101 | + |
| 102 | + var scrollTop = $window.pageYOffset; |
| 103 | + var position = dimensions.offset(element[0]); |
| 104 | + var elementHeight = dimensions.height(element[0]); |
| 105 | + |
| 106 | + // Get required affix class according to position |
| 107 | + var affix = getRequiredAffixClass(unpin, position, elementHeight); |
| 108 | + |
| 109 | + // Did affix status changed this last check? |
| 110 | + if(affixed === affix) return; |
| 111 | + affixed = affix; |
| 112 | + |
| 113 | + // Add proper affix class |
| 114 | + element.removeClass(reset).addClass('affix' + ((affix !== 'middle') ? '-' + affix : '')); |
| 115 | + |
| 116 | + if(affix === 'top') { |
| 117 | + unpin = null; |
| 118 | + element.css('position', (options.offsetParent) ? '' : 'relative'); |
| 119 | + element.css('top', ''); |
| 120 | + } else if(affix === 'bottom') { |
| 121 | + if (options.offsetUnpin) { |
| 122 | + unpin = -(options.offsetUnpin * 1); |
| 123 | + } |
| 124 | + else { |
| 125 | + // Calculate unpin threshold when affixed to bottom. |
| 126 | + // Hopefully the browser scrolls pixel by pixel. |
| 127 | + unpin = position.top - scrollTop; |
| 128 | + } |
| 129 | + element.css('position', (options.offsetParent) ? '' : 'relative'); |
| 130 | + element.css('top', (options.offsetParent) ? '' : ((bodyEl[0].offsetHeight - offsetBottom - elementHeight - initialOffsetTop) + 'px')); |
| 131 | + } else { // affix === 'middle' |
| 132 | + unpin = null; |
| 133 | + element.css('position', 'fixed'); |
| 134 | + element.css('top', initialAffixTop + 'px'); |
| 135 | + } |
| 136 | + |
| 137 | + }; |
| 138 | + |
| 139 | + // Private methods |
| 140 | + |
| 141 | + function getRequiredAffixClass(unpin, position, elementHeight) { |
| 142 | + |
| 143 | + var scrollTop = $window.pageYOffset; |
| 144 | + var scrollHeight = $window.document.body.scrollHeight; |
| 145 | + |
| 146 | + if(scrollTop <= offsetTop) { |
| 147 | + return 'top'; |
| 148 | + } else if(unpin !== null && (scrollTop + unpin <= position.top)) { |
| 149 | + return 'middle'; |
| 150 | + } else if(offsetBottom !== null && (position.top + elementHeight + initialAffixTop >= scrollHeight - offsetBottom)) { |
| 151 | + return 'bottom'; |
| 152 | + } else { |
| 153 | + return 'middle'; |
| 154 | + } |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + $affix.init(); |
| 159 | + return $affix; |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | + return AffixFactory; |
| 164 | + |
| 165 | + }; |
| 166 | + |
| 167 | + }) |
| 168 | + |
| 169 | + .directive('wgAffix', function($affix, dimensions) { |
| 170 | + |
| 171 | + var forEach = angular.forEach; |
| 172 | + var isDefined = angular.isDefined; |
| 173 | + var jqLite = angular.element; |
| 174 | + |
| 175 | + return { |
| 176 | + restrict: 'EAC', |
| 177 | + link: function postLink(scope, element, attr) { |
| 178 | + |
| 179 | + var options = {scope: scope, offsetTop: 'auto'}; |
| 180 | + forEach(['offsetTop', 'offsetBottom', 'offsetParent', 'offsetUnpin'], function(key) { |
| 181 | + if(isDefined(attr[key])) options[key] = attr[key]; |
| 182 | + }); |
| 183 | + |
| 184 | + var affix = $affix(element, options); |
| 185 | + scope.$on('$destroy', function() { |
| 186 | + options = null; |
| 187 | + affix = null; |
| 188 | + }); |
| 189 | + |
| 190 | + } |
| 191 | + }; |
| 192 | + |
| 193 | + }); |
0 commit comments