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.

125 lines
4.4 KiB

  1. /*!
  2. * Angular Material Design
  3. * https://github.com/angular/material
  4. * @license MIT
  5. * v1.1.1
  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"];
  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) {
  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. // Transclude the user-given contents of the subheader
  85. // the conventional way.
  86. transclude(scope, function(clone) {
  87. getContent(element).append(clone);
  88. });
  89. // Create another clone, that uses the outer and inner contents
  90. // of the element, that will be 'stickied' as the user scrolls.
  91. if (!element.hasClass('md-no-sticky')) {
  92. transclude(scope, function(clone) {
  93. // If the user adds an ng-if or ng-repeat directly to the md-subheader element, the
  94. // compiled clone below will only be a comment tag (since they replace their elements with
  95. // a comment) which cannot be properly passed to the $mdSticky; so we wrap it in our own
  96. // DIV to ensure we have something $mdSticky can use
  97. var wrapper = $compile('<div class="md-subheader-wrapper">' + outerHTML + '</div>')(scope);
  98. // Delay initialization until after any `ng-if`/`ng-repeat`/etc has finished before
  99. // attempting to create the clone
  100. $mdUtil.nextTick(function() {
  101. // Append our transcluded clone into the wrapper.
  102. // We don't have to recompile the element again, because the clone is already
  103. // compiled in it's transclusion scope. If we recompile the outerHTML of the new clone, we would lose
  104. // our ngIf's and other previous registered bindings / properties.
  105. getContent(wrapper).append(clone);
  106. });
  107. // Make the element sticky and provide the stickyClone our self, to avoid recompilation of the subheader
  108. // element.
  109. $mdSticky(scope, element, wrapper);
  110. });
  111. }
  112. }
  113. };
  114. }
  115. })(window, window.angular);