BxDolMenuMoreAuto.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup UnaCore UNA Core
  6. * @{
  7. */
  8. function BxDolMenuMoreAuto(options)
  9. {
  10. this._sObject = options.sObject;
  11. this._iItemsStatic = undefined == options.iItemsStatic ? 0 : options.iItemsStatic;
  12. this._bItemsStaticOnly = undefined == options.bItemsStaticOnly ? 0 : options.bItemsStaticOnly;
  13. this._aHtmlIds = undefined == options.aHtmlIds ? {} : options.aHtmlIds;
  14. this._sKeyWidth = 'bx-mma-width';
  15. this._sClassReady = 'bx-mma-ready';
  16. this._sClassItem = '.bx-menu-item';
  17. this._sClassItemMore = this._sClassItem + '.bx-menu-item-more-auto';
  18. this._sClassItemMoreSubmenu = '.bx-menu-submenu-more-auto';
  19. this._sClassItemStatic = 'bx-menu-item-static';
  20. this._bStaticMode = this._bItemsStaticOnly && this._iItemsStatic > 0;
  21. this._bTrackContainerResize = false;
  22. }
  23. BxDolMenuMoreAuto.prototype.init = function() {
  24. var $this = this;
  25. var oMenu = $('#' + this._aHtmlIds['main']);
  26. if(!oMenu.length)
  27. return;
  28. //--- Initialize ---//
  29. var oItemMore = oMenu.find(this._sClassItemMore);
  30. var oItemMoreSubmenu = oItemMore.find(this._sClassItemMoreSubmenu);
  31. for(var i = 0; i < this._iItemsStatic; i++)
  32. oItemMore.prevAll(this._sClassItem + ':not(.' + this._sClassItemStatic + '):last').addClass(this._sClassItemStatic);
  33. var iMenu = 0;
  34. oMenu.children(this._sClassItem + ':visible').each(function() {
  35. iMenu += $this._getWidth($(this));
  36. });
  37. var iParent = oMenu.parent().width();
  38. var iItemMore = $this._getWidth(oItemMore);
  39. oMenu.css('overflow', 'visible');
  40. if(iMenu >= iParent || this._bStaticMode)
  41. this._moveToSubmenu(oMenu, oItemMore, oItemMoreSubmenu, iParent, iItemMore);
  42. else
  43. oMenu.parents('.bx-menu-more-auto-wrapper:first').css('overflow', 'visible');
  44. //--- Add event handlers ---//
  45. if(!this._bStaticMode) {
  46. $(window).on('resize', function() {
  47. $this.update();
  48. });
  49. if(this._bTrackContainerResize)
  50. setTimeout(function () {
  51. oMenu.find(this._sClassItem).on('resize', function() {
  52. $this.update(true);
  53. });
  54. }, 200);
  55. }
  56. };
  57. BxDolMenuMoreAuto.prototype.update = function(bForceCalculate)
  58. {
  59. if(this._bStaticMode)
  60. return;
  61. var $this = this;
  62. var oMenu = $('#' + this._aHtmlIds['main']);
  63. var oItemMore = oMenu.find(this._sClassItemMore);
  64. var oItemMoreSubmenu = oItemMore.find(this._sClassItemMoreSubmenu);
  65. var iMenu = 0;
  66. oMenu.children(this._sClassItem + ':visible').each(function() {
  67. iMenu += $this._getWidth($(this), $(this).not($this._sClassItemMore).length ? bForceCalculate : false);
  68. });
  69. var iParent = oMenu.parent().width();
  70. var iItemMore = $this._getWidth(oItemMore, false);
  71. if(iMenu > iParent)
  72. this._moveToSubmenu(oMenu, oItemMore, oItemMoreSubmenu, iParent, iItemMore);
  73. if(iMenu < iParent)
  74. this._moveFromSubmenu(oMenu, oItemMore, oItemMoreSubmenu, iParent, iMenu);
  75. };
  76. BxDolMenuMoreAuto.prototype.more = function(oElement)
  77. {
  78. var oElement = $(oElement);
  79. oElement.parents('li:first').find('#' + this._aHtmlIds['more_auto_popup']).dolPopup({
  80. pointer: {
  81. el: oElement
  82. },
  83. moveToDocRoot: false,
  84. cssClass: 'bx-popup-menu'
  85. });
  86. }
  87. BxDolMenuMoreAuto.prototype._moveToSubmenu = function(oMenu, oItemMore, oItemMoreSubmenu, iParent, iItemMore)
  88. {
  89. var $this = this;
  90. var bRelocateOthers = false;
  91. var iWidthTotal = iItemMore;
  92. var oSubmenuItemFirst = oItemMoreSubmenu.children(this._sClassItem + ':first');
  93. oMenu.children(this._sClassItem + ':not(' + this._sClassItemMore + ')').each(function() {
  94. var oItem = $(this);
  95. var iItem = $this._getWidth(oItem);
  96. if(($this._bStaticMode || bRelocateOthers || iWidthTotal + iItem > iParent) && !oItem.hasClass($this._sClassItemStatic)) {
  97. oItem.addClass('bx-def-color-bg-hl-hover');
  98. oItem.find('a').each(function() {
  99. if($(this).hasClass('bx-btn'))
  100. $(this).removeClass('bx-btn').addClass('bx-nobtn');
  101. if($(this).hasClass('bx-btn-height'))
  102. $(this).removeClass('bx-btn-height').addClass('bx-nobtn-height');
  103. });
  104. if(!oSubmenuItemFirst.length)
  105. oItemMoreSubmenu.append(oItem.detach());
  106. else
  107. oSubmenuItemFirst.before(oItem.detach());
  108. bRelocateOthers = true;
  109. return;
  110. }
  111. iWidthTotal += iItem;
  112. });
  113. if(oItemMoreSubmenu.find('li').length)
  114. oItemMore.show(function() {
  115. oMenu.addClass($this._sClassReady).parents('.bx-menu-more-auto-wrapper:first').css('overflow', 'visible');
  116. });
  117. };
  118. BxDolMenuMoreAuto.prototype._moveFromSubmenu = function(oMenu, oItemMore, oItemMoreSubmenu, iParent, iMenu)
  119. {
  120. var $this = this;
  121. var bStopRelocation = false;
  122. var iWidthTotal = iMenu;
  123. oItemMoreSubmenu.children(this._sClassItem).each(function() {
  124. if(bStopRelocation)
  125. return;
  126. var oItem = $(this);
  127. var iItem = $this._getWidth(oItem);
  128. if(iWidthTotal + iItem > iParent) {
  129. bStopRelocation = true;
  130. return;
  131. }
  132. oItem.removeClass('bx-def-color-bg-hl-hover');
  133. oItem.find('a').each(function() {
  134. if($(this).hasClass('bx-nobtn'))
  135. $(this).removeClass('bx-nobtn').addClass('bx-btn');
  136. if($(this).hasClass('bx-nobtn-height'))
  137. $(this).removeClass('bx-nobtn-height').addClass('bx-btn-height');
  138. });
  139. oItemMore.before(oItem.detach());
  140. iWidthTotal += iItem;
  141. });
  142. if(!oItemMoreSubmenu.find('li').length)
  143. oItemMore.hide();
  144. };
  145. BxDolMenuMoreAuto.prototype._getWidth = function(oItem, bForceCalculate)
  146. {
  147. var iItem = parseInt(oItem.attr(this._sKeyWidth));
  148. if(!bForceCalculate && iItem)
  149. return iItem;
  150. iItem = oItem.outerWidth(true);
  151. if(iItem)
  152. oItem.attr(this._sKeyWidth, iItem);
  153. return iItem;
  154. }
  155. /** @} */