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.

131 lines
4.8 KiB

7 years ago
  1. /*!
  2. * Angular Material Design
  3. * https://github.com/angular/material
  4. * @license MIT
  5. * v1.1.3
  6. */
  7. (function( window, angular, undefined ){
  8. "use strict";
  9. /**
  10. * @ngdoc module
  11. * @name material.components.subheader
  12. * @description
  13. * SubHeader module
  14. *
  15. * Subheaders are special list tiles that delineate distinct sections of a
  16. * list or grid list and are typically related to the current filtering or
  17. * sorting criteria. Subheader tiles are either displayed inline with tiles or
  18. * can be associated with content, for example, in an adjacent column.
  19. *
  20. * Upon scrolling, subheaders remain pinned to the top of the screen and remain
  21. * pinned until pushed on or off screen by the next subheader. @see [Material
  22. * Design Specifications](https://www.google.com/design/spec/components/subheaders.html)
  23. *
  24. * > To improve the visual grouping of content, use the system color for your subheaders.
  25. *
  26. */
  27. MdSubheaderDirective['$inject'] = ["$mdSticky", "$compile", "$mdTheming", "$mdUtil", "$mdAria"];
  28. angular
  29. .module('material.components.subheader', [
  30. 'material.core',
  31. 'material.components.sticky'
  32. ])
  33. .directive('mdSubheader', MdSubheaderDirective);
  34. /**
  35. * @ngdoc directive
  36. * @name mdSubheader
  37. * @module material.components.subheader
  38. *
  39. * @restrict E
  40. *
  41. * @description
  42. * The `md-subheader` directive creates a sticky subheader for a section.
  43. *
  44. * Developers are able to disable the stickiness of the subheader by using the following markup
  45. *
  46. * <hljs lang="html">
  47. * <md-subheader class="md-no-sticky">Not Sticky</md-subheader>
  48. * </hljs>
  49. *
  50. * ### Notes
  51. * - The `md-subheader` directive uses the <a ng-href="api/service/$mdSticky">$mdSticky</a> service
  52. * to make the subheader sticky.
  53. *
  54. * > Whenever the current browser doesn't support stickiness natively, the subheader
  55. * will be compiled twice to create a sticky clone of the subheader.
  56. *
  57. * @usage
  58. * <hljs lang="html">
  59. * <md-subheader>Online Friends</md-subheader>
  60. * </hljs>
  61. */
  62. function MdSubheaderDirective($mdSticky, $compile, $mdTheming, $mdUtil, $mdAria) {
  63. return {
  64. restrict: 'E',
  65. replace: true,
  66. transclude: true,
  67. template: (
  68. '<div class="md-subheader _md">' +
  69. ' <div class="md-subheader-inner">' +
  70. ' <div class="md-subheader-content"></div>' +
  71. ' </div>' +
  72. '</div>'
  73. ),
  74. link: function postLink(scope, element, attr, controllers, transclude) {
  75. $mdTheming(element);
  76. element.addClass('_md');
  77. // Remove the ngRepeat attribute from the root element, because we don't want to compile
  78. // the ngRepeat for the sticky clone again.
  79. $mdUtil.prefixer().removeAttribute(element, 'ng-repeat');
  80. var outerHTML = element[0].outerHTML;
  81. function getContent(el) {
  82. return angular.element(el[0].querySelector('.md-subheader-content'));
  83. }
  84. // Set the ARIA attributes on the original element since it keeps it's original place in
  85. // the DOM, whereas the clones are in reverse order. Should be done after the outerHTML,
  86. // in order to avoid having multiple element be marked as headers.
  87. attr.$set('role', 'heading');
  88. $mdAria.expect(element, 'aria-level', '2');
  89. // Transclude the user-given contents of the subheader
  90. // the conventional way.
  91. transclude(scope, function(clone) {
  92. getContent(element).append(clone);
  93. });
  94. // Create another clone, that uses the outer and inner contents
  95. // of the element, that will be 'stickied' as the user scrolls.
  96. if (!element.hasClass('md-no-sticky')) {
  97. transclude(scope, function(clone) {
  98. // If the user adds an ng-if or ng-repeat directly to the md-subheader element, the
  99. // compiled clone below will only be a comment tag (since they replace their elements with
  100. // a comment) which cannot be properly passed to the $mdSticky; so we wrap it in our own
  101. // DIV to ensure we have something $mdSticky can use
  102. var wrapper = $compile('<div class="md-subheader-wrapper" aria-hidden="true">' + outerHTML + '</div>')(scope);
  103. // Delay initialization until after any `ng-if`/`ng-repeat`/etc has finished before
  104. // attempting to create the clone
  105. $mdUtil.nextTick(function() {
  106. // Append our transcluded clone into the wrapper.
  107. // We don't have to recompile the element again, because the clone is already
  108. // compiled in it's transclusion scope. If we recompile the outerHTML of the new clone, we would lose
  109. // our ngIf's and other previous registered bindings / properties.
  110. getContent(wrapper).append(clone);
  111. });
  112. // Make the element sticky and provide the stickyClone our self, to avoid recompilation of the subheader
  113. // element.
  114. $mdSticky(scope, element, wrapper);
  115. });
  116. }
  117. }
  118. };
  119. }
  120. })(window, window.angular);