subheader.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*!
  2. * Angular Material Design
  3. * https://github.com/angular/material
  4. * @license MIT
  5. * v0.11.4
  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. angular
  28. .module('material.components.subheader', [
  29. 'material.core',
  30. 'material.components.sticky'
  31. ])
  32. .directive('mdSubheader', MdSubheaderDirective);
  33. /**
  34. * @ngdoc directive
  35. * @name mdSubheader
  36. * @module material.components.subheader
  37. *
  38. * @restrict E
  39. *
  40. * @description
  41. * The `<md-subheader>` directive is a subheader for a section. By default it is sticky.
  42. * You can make it not sticky by applying the `md-no-sticky` class to the subheader.
  43. *
  44. *
  45. * @usage
  46. * <hljs lang="html">
  47. * <md-subheader>Online Friends</md-subheader>
  48. * </hljs>
  49. */
  50. function MdSubheaderDirective($mdSticky, $compile, $mdTheming, $mdUtil) {
  51. return {
  52. restrict: 'E',
  53. replace: true,
  54. transclude: true,
  55. template: (
  56. '<div class="md-subheader">' +
  57. ' <div class="md-subheader-inner">' +
  58. ' <span class="md-subheader-content"></span>' +
  59. ' </div>' +
  60. '</div>'
  61. ),
  62. link: function postLink(scope, element, attr, controllers, transclude) {
  63. $mdTheming(element);
  64. var outerHTML = element[0].outerHTML;
  65. function getContent(el) {
  66. return angular.element(el[0].querySelector('.md-subheader-content'));
  67. }
  68. // Transclude the user-given contents of the subheader
  69. // the conventional way.
  70. transclude(scope, function(clone) {
  71. getContent(element).append(clone);
  72. });
  73. // Create another clone, that uses the outer and inner contents
  74. // of the element, that will be 'stickied' as the user scrolls.
  75. if (!element.hasClass('md-no-sticky')) {
  76. transclude(scope, function(clone) {
  77. // If the user adds an ng-if or ng-repeat directly to the md-subheader element, the
  78. // compiled clone below will only be a comment tag (since they replace their elements with
  79. // a comment) which cannot be properly passed to the $mdSticky; so we wrap it in our own
  80. // DIV to ensure we have something $mdSticky can use
  81. var wrapperHtml = '<div class="md-subheader-wrapper">' + outerHTML + '</div>';
  82. var stickyClone = $compile(wrapperHtml)(scope);
  83. // Append the sticky
  84. $mdSticky(scope, element, stickyClone);
  85. // Delay initialization until after any `ng-if`/`ng-repeat`/etc has finished before
  86. // attempting to create the clone
  87. $mdUtil.nextTick(function() {
  88. getContent(stickyClone).append(clone);
  89. });
  90. });
  91. }
  92. }
  93. }
  94. }
  95. MdSubheaderDirective.$inject = ["$mdSticky", "$compile", "$mdTheming", "$mdUtil"];
  96. })(window, window.angular);