You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

363 lines
11 KiB

7 years ago
  1. /*!
  2. * Angular Material Design
  3. * https://github.com/angular/material
  4. * @license MIT
  5. * v1.1.3
  6. */
  7. goog.provide('ngmaterial.components.sticky');
  8. goog.require('ngmaterial.components.content');
  9. goog.require('ngmaterial.core');
  10. /**
  11. * @ngdoc module
  12. * @name material.components.sticky
  13. * @description
  14. * Sticky effects for md
  15. *
  16. */
  17. MdSticky['$inject'] = ["$mdConstant", "$$rAF", "$mdUtil", "$compile"];
  18. angular
  19. .module('material.components.sticky', [
  20. 'material.core',
  21. 'material.components.content'
  22. ])
  23. .factory('$mdSticky', MdSticky);
  24. /**
  25. * @ngdoc service
  26. * @name $mdSticky
  27. * @module material.components.sticky
  28. *
  29. * @description
  30. * The `$mdSticky`service provides a mixin to make elements sticky.
  31. *
  32. * Whenever the current browser supports stickiness natively, the `$mdSticky` service will just
  33. * use the native browser stickiness.
  34. *
  35. * By default the `$mdSticky` service compiles the cloned element, when not specified through the `elementClone`
  36. * parameter, in the same scope as the actual element lives.
  37. *
  38. *
  39. * <h3>Notes</h3>
  40. * When using an element which is containing a compiled directive, which changed its DOM structure during compilation,
  41. * you should compile the clone yourself using the plain template.<br/><br/>
  42. * See the right usage below:
  43. * <hljs lang="js">
  44. * angular.module('myModule')
  45. * .directive('stickySelect', function($mdSticky, $compile) {
  46. * var SELECT_TEMPLATE =
  47. * '<md-select ng-model="selected">' +
  48. * '<md-option>Option 1</md-option>' +
  49. * '</md-select>';
  50. *
  51. * return {
  52. * restrict: 'E',
  53. * replace: true,
  54. * template: SELECT_TEMPLATE,
  55. * link: function(scope,element) {
  56. * $mdSticky(scope, element, $compile(SELECT_TEMPLATE)(scope));
  57. * }
  58. * };
  59. * });
  60. * </hljs>
  61. *
  62. * @usage
  63. * <hljs lang="js">
  64. * angular.module('myModule')
  65. * .directive('stickyText', function($mdSticky, $compile) {
  66. * return {
  67. * restrict: 'E',
  68. * template: '<span>Sticky Text</span>',
  69. * link: function(scope,element) {
  70. * $mdSticky(scope, element);
  71. * }
  72. * };
  73. * });
  74. * </hljs>
  75. *
  76. * @returns A `$mdSticky` function that takes three arguments:
  77. * - `scope`
  78. * - `element`: The element that will be 'sticky'
  79. * - `elementClone`: A clone of the element, that will be shown
  80. * when the user starts scrolling past the original element.
  81. * If not provided, it will use the result of `element.clone()` and compiles it in the given scope.
  82. */
  83. function MdSticky($mdConstant, $$rAF, $mdUtil, $compile) {
  84. var browserStickySupport = $mdUtil.checkStickySupport();
  85. /**
  86. * Registers an element as sticky, used internally by directives to register themselves
  87. */
  88. return function registerStickyElement(scope, element, stickyClone) {
  89. var contentCtrl = element.controller('mdContent');
  90. if (!contentCtrl) return;
  91. if (browserStickySupport) {
  92. element.css({
  93. position: browserStickySupport,
  94. top: 0,
  95. 'z-index': 2
  96. });
  97. } else {
  98. var $$sticky = contentCtrl.$element.data('$$sticky');
  99. if (!$$sticky) {
  100. $$sticky = setupSticky(contentCtrl);
  101. contentCtrl.$element.data('$$sticky', $$sticky);
  102. }
  103. // Compile our cloned element, when cloned in this service, into the given scope.
  104. var cloneElement = stickyClone || $compile(element.clone())(scope);
  105. var deregister = $$sticky.add(element, cloneElement);
  106. scope.$on('$destroy', deregister);
  107. }
  108. };
  109. function setupSticky(contentCtrl) {
  110. var contentEl = contentCtrl.$element;
  111. // Refresh elements is very expensive, so we use the debounced
  112. // version when possible.
  113. var debouncedRefreshElements = $$rAF.throttle(refreshElements);
  114. // setupAugmentedScrollEvents gives us `$scrollstart` and `$scroll`,
  115. // more reliable than `scroll` on android.
  116. setupAugmentedScrollEvents(contentEl);
  117. contentEl.on('$scrollstart', debouncedRefreshElements);
  118. contentEl.on('$scroll', onScroll);
  119. var self;
  120. return self = {
  121. prev: null,
  122. current: null, //the currently stickied item
  123. next: null,
  124. items: [],
  125. add: add,
  126. refreshElements: refreshElements
  127. };
  128. /***************
  129. * Public
  130. ***************/
  131. // Add an element and its sticky clone to this content's sticky collection
  132. function add(element, stickyClone) {
  133. stickyClone.addClass('md-sticky-clone');
  134. var item = {
  135. element: element,
  136. clone: stickyClone
  137. };
  138. self.items.push(item);
  139. $mdUtil.nextTick(function() {
  140. contentEl.prepend(item.clone);
  141. });
  142. debouncedRefreshElements();
  143. return function remove() {
  144. self.items.forEach(function(item, index) {
  145. if (item.element[0] === element[0]) {
  146. self.items.splice(index, 1);
  147. item.clone.remove();
  148. }
  149. });
  150. debouncedRefreshElements();
  151. };
  152. }
  153. function refreshElements() {
  154. // Sort our collection of elements by their current position in the DOM.
  155. // We need to do this because our elements' order of being added may not
  156. // be the same as their order of display.
  157. self.items.forEach(refreshPosition);
  158. self.items = self.items.sort(function(a, b) {
  159. return a.top < b.top ? -1 : 1;
  160. });
  161. // Find which item in the list should be active,
  162. // based upon the content's current scroll position
  163. var item;
  164. var currentScrollTop = contentEl.prop('scrollTop');
  165. for (var i = self.items.length - 1; i >= 0; i--) {
  166. if (currentScrollTop > self.items[i].top) {
  167. item = self.items[i];
  168. break;
  169. }
  170. }
  171. setCurrentItem(item);
  172. }
  173. /***************
  174. * Private
  175. ***************/
  176. // Find the `top` of an item relative to the content element,
  177. // and also the height.
  178. function refreshPosition(item) {
  179. // Find the top of an item by adding to the offsetHeight until we reach the
  180. // content element.
  181. var current = item.element[0];
  182. item.top = 0;
  183. item.left = 0;
  184. item.right = 0;
  185. while (current && current !== contentEl[0]) {
  186. item.top += current.offsetTop;
  187. item.left += current.offsetLeft;
  188. if ( current.offsetParent ){
  189. item.right += current.offsetParent.offsetWidth - current.offsetWidth - current.offsetLeft; //Compute offsetRight
  190. }
  191. current = current.offsetParent;
  192. }
  193. item.height = item.element.prop('offsetHeight');
  194. var defaultVal = $mdUtil.floatingScrollbars() ? '0' : undefined;
  195. $mdUtil.bidi(item.clone, 'margin-left', item.left, defaultVal);
  196. $mdUtil.bidi(item.clone, 'margin-right', defaultVal, item.right);
  197. }
  198. // As we scroll, push in and select the correct sticky element.
  199. function onScroll() {
  200. var scrollTop = contentEl.prop('scrollTop');
  201. var isScrollingDown = scrollTop > (onScroll.prevScrollTop || 0);
  202. // Store the previous scroll so we know which direction we are scrolling
  203. onScroll.prevScrollTop = scrollTop;
  204. //
  205. // AT TOP (not scrolling)
  206. //
  207. if (scrollTop === 0) {
  208. // If we're at the top, just clear the current item and return
  209. setCurrentItem(null);
  210. return;
  211. }
  212. //
  213. // SCROLLING DOWN (going towards the next item)
  214. //
  215. if (isScrollingDown) {
  216. // If we've scrolled down past the next item's position, sticky it and return
  217. if (self.next && self.next.top <= scrollTop) {
  218. setCurrentItem(self.next);
  219. return;
  220. }
  221. // If the next item is close to the current one, push the current one up out of the way
  222. if (self.current && self.next && self.next.top - scrollTop <= self.next.height) {
  223. translate(self.current, scrollTop + (self.next.top - self.next.height - scrollTop));
  224. return;
  225. }
  226. }
  227. //
  228. // SCROLLING UP (not at the top & not scrolling down; must be scrolling up)
  229. //
  230. if (!isScrollingDown) {
  231. // If we've scrolled up past the previous item's position, sticky it and return
  232. if (self.current && self.prev && scrollTop < self.current.top) {
  233. setCurrentItem(self.prev);
  234. return;
  235. }
  236. // If the next item is close to the current one, pull the current one down into view
  237. if (self.next && self.current && (scrollTop >= (self.next.top - self.current.height))) {
  238. translate(self.current, scrollTop + (self.next.top - scrollTop - self.current.height));
  239. return;
  240. }
  241. }
  242. //
  243. // Otherwise, just move the current item to the proper place (scrolling up or down)
  244. //
  245. if (self.current) {
  246. translate(self.current, scrollTop);
  247. }
  248. }
  249. function setCurrentItem(item) {
  250. if (self.current === item) return;
  251. // Deactivate currently active item
  252. if (self.current) {
  253. translate(self.current, null);
  254. setStickyState(self.current, null);
  255. }
  256. // Activate new item if given
  257. if (item) {
  258. setStickyState(item, 'active');
  259. }
  260. self.current = item;
  261. var index = self.items.indexOf(item);
  262. // If index === -1, index + 1 = 0. It works out.
  263. self.next = self.items[index + 1];
  264. self.prev = self.items[index - 1];
  265. setStickyState(self.next, 'next');
  266. setStickyState(self.prev, 'prev');
  267. }
  268. function setStickyState(item, state) {
  269. if (!item || item.state === state) return;
  270. if (item.state) {
  271. item.clone.attr('sticky-prev-state', item.state);
  272. item.element.attr('sticky-prev-state', item.state);
  273. }
  274. item.clone.attr('sticky-state', state);
  275. item.element.attr('sticky-state', state);
  276. item.state = state;
  277. }
  278. function translate(item, amount) {
  279. if (!item) return;
  280. if (amount === null || amount === undefined) {
  281. if (item.translateY) {
  282. item.translateY = null;
  283. item.clone.css($mdConstant.CSS.TRANSFORM, '');
  284. }
  285. } else {
  286. item.translateY = amount;
  287. $mdUtil.bidi( item.clone, $mdConstant.CSS.TRANSFORM,
  288. 'translate3d(' + item.left + 'px,' + amount + 'px,0)',
  289. 'translateY(' + amount + 'px)'
  290. );
  291. }
  292. }
  293. }
  294. // Android 4.4 don't accurately give scroll events.
  295. // To fix this problem, we setup a fake scroll event. We say:
  296. // > If a scroll or touchmove event has happened in the last DELAY milliseconds,
  297. // then send a `$scroll` event every animationFrame.
  298. // Additionally, we add $scrollstart and $scrollend events.
  299. function setupAugmentedScrollEvents(element) {
  300. var SCROLL_END_DELAY = 200;
  301. var isScrolling;
  302. var lastScrollTime;
  303. element.on('scroll touchmove', function() {
  304. if (!isScrolling) {
  305. isScrolling = true;
  306. $$rAF.throttle(loopScrollEvent);
  307. element.triggerHandler('$scrollstart');
  308. }
  309. element.triggerHandler('$scroll');
  310. lastScrollTime = +$mdUtil.now();
  311. });
  312. function loopScrollEvent() {
  313. if (+$mdUtil.now() - lastScrollTime > SCROLL_END_DELAY) {
  314. isScrolling = false;
  315. element.triggerHandler('$scrollend');
  316. } else {
  317. element.triggerHandler('$scroll');
  318. $$rAF.throttle(loopScrollEvent);
  319. }
  320. }
  321. }
  322. }
  323. ngmaterial.components.sticky = angular.module("material.components.sticky");