25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

113 lines
3.3 KiB

  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'jquery/ui'
  8. ], function ($) {
  9. 'use strict';
  10. $.widget('mage.floatingHeader', {
  11. options: {
  12. placeholderAttrs: {
  13. 'class': 'page-actions-placeholder'
  14. },
  15. fixedClass: '_fixed',
  16. hiddenClass: '_hidden',
  17. title: '.page-title-wrapper .page-title',
  18. pageMainActions: '.page-main-actions',
  19. contains: '[data-role=modal]'
  20. },
  21. /**
  22. * Widget initialization
  23. * @private
  24. */
  25. _create: function () {
  26. var title = $(this.options.title).text(),
  27. wrapped = this.element.find('.page-actions-buttons').children();
  28. if (this.element.parents(this.options.contains).length) {
  29. return this;
  30. }
  31. this._setVars();
  32. this._bind();
  33. this.element.find('script').remove();
  34. if (wrapped.length) {
  35. wrapped
  36. .unwrap() // .page-actions-buttons
  37. .unwrap(); // .page-actions-inner
  38. }
  39. this.element.wrapInner($('<div></div>', {
  40. 'class': 'page-actions-buttons'
  41. }));
  42. this.element.wrapInner($('<div></div>', {
  43. 'class': 'page-actions-inner', 'data-title': title
  44. }));
  45. this.element.removeClass('floating-header');
  46. },
  47. /**
  48. * Set privat variables on load, for performance purposes
  49. * @private
  50. */
  51. _setVars: function () {
  52. this._placeholder = this.element.before($('<div/>', this.options.placeholderAttrs)).prev();
  53. this._offsetTop = this._placeholder.offset().top;
  54. this._height = this.element
  55. .parents(this.options.pageMainActions)
  56. .outerHeight();
  57. },
  58. /**
  59. * Event binding, will monitor scroll and resize events (resize events left for backward compat)
  60. * @private
  61. */
  62. _bind: function () {
  63. this._on(window, {
  64. scroll: this._handlePageScroll,
  65. resize: this._handlePageScroll
  66. });
  67. },
  68. /**
  69. * Event handler for setting fixed positioning
  70. * @private
  71. */
  72. _handlePageScroll: function () {
  73. var isActive = $(window).scrollTop() > this._offsetTop;
  74. if (isActive) {
  75. this.element
  76. .addClass(this.options.fixedClass)
  77. .parents(this.options.pageMainActions)
  78. .addClass(this.options.hiddenClass);
  79. } else {
  80. this.element
  81. .removeClass(this.options.fixedClass)
  82. .parents(this.options.pageMainActions)
  83. .removeClass(this.options.hiddenClass);
  84. }
  85. this._placeholder.height(isActive ? this._height : '');
  86. },
  87. /**
  88. * Widget destroy functionality
  89. * @private
  90. */
  91. _destroy: function () {
  92. if (this._placeholder) {
  93. this._placeholder.remove();
  94. }
  95. this._off($(window));
  96. }
  97. });
  98. return $.mage.floatingHeader;
  99. });