floating-header.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/>', {
  40. 'class': 'page-actions-buttons'
  41. }));
  42. this.element.wrapInner($('<div/>', {
  43. 'class': 'page-actions-inner', 'data-title': title
  44. }));
  45. },
  46. /**
  47. * Set privat variables on load, for performance purposes
  48. * @private
  49. */
  50. _setVars: function () {
  51. this._placeholder = this.element.before($('<div/>', this.options.placeholderAttrs)).prev();
  52. this._offsetTop = this._placeholder.offset().top;
  53. this._height = this.element
  54. .parents(this.options.pageMainActions)
  55. .outerHeight();
  56. },
  57. /**
  58. * Event binding, will monitor scroll and resize events (resize events left for backward compat)
  59. * @private
  60. */
  61. _bind: function () {
  62. this._on(window, {
  63. scroll: this._handlePageScroll,
  64. resize: this._handlePageScroll
  65. });
  66. },
  67. /**
  68. * Event handler for setting fixed positioning
  69. * @private
  70. */
  71. _handlePageScroll: function () {
  72. var isActive = $(window).scrollTop() > this._offsetTop;
  73. if (isActive) {
  74. this.element
  75. .addClass(this.options.fixedClass)
  76. .parents(this.options.pageMainActions)
  77. .addClass(this.options.hiddenClass);
  78. } else {
  79. this.element
  80. .removeClass(this.options.fixedClass)
  81. .parents(this.options.pageMainActions)
  82. .removeClass(this.options.hiddenClass);
  83. }
  84. this._placeholder.height(isActive ? this._height : '');
  85. },
  86. /**
  87. * Widget destroy functionality
  88. * @private
  89. */
  90. _destroy: function () {
  91. this._placeholder.remove();
  92. this._off($(window));
  93. }
  94. });
  95. return $.mage.floatingHeader;
  96. });