BxDolMenuMoreAuto.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. }
  22. BxDolMenuMoreAuto.prototype.init = function() {
  23. var $this = this;
  24. var oMenu = $('#' + this._aHtmlIds['main']);
  25. if(!oMenu.length)
  26. return;
  27. //--- Initialize ---//
  28. var oItemMore = oMenu.find(this._sClassItemMore);
  29. var oItemMoreSubmenu = oItemMore.find(this._sClassItemMoreSubmenu);
  30. for(var i = 0; i < this._iItemsStatic; i++)
  31. oItemMore.prevAll(this._sClassItem + ':not(.' + this._sClassItemStatic + '):last').addClass(this._sClassItemStatic);
  32. var iMenu = 0;
  33. oMenu.children(this._sClassItem + ':visible').each(function() {
  34. iMenu += $this._getWidth($(this));
  35. });
  36. var iParent = oMenu.parent().width();
  37. var iItemMore = oItemMore.outerWidth(true);
  38. oMenu.css('overflow', 'visible');
  39. if(iMenu >= iParent || this._bStaticMode)
  40. this._moveToSubmenu(oMenu, oItemMore, oItemMoreSubmenu, iParent, iItemMore);
  41. //--- Add event handlers ---//
  42. if(!this._bStaticMode) {
  43. $(window).on('resize', function() {
  44. $this.update();
  45. });
  46. setTimeout(function () {
  47. oMenu.find(this._sClassItem).on('resize', function() {
  48. $this.update(true);
  49. });
  50. }, 200);
  51. }
  52. };
  53. BxDolMenuMoreAuto.prototype.update = function(bForceCalculate)
  54. {
  55. if(this._bStaticMode)
  56. return;
  57. var $this = this;
  58. var oMenu = $('#' + this._aHtmlIds['main']);
  59. var oItemMore = oMenu.find(this._sClassItemMore);
  60. var oItemMoreSubmenu = oItemMore.find(this._sClassItemMoreSubmenu);
  61. var iMenu = 0;
  62. oMenu.children(this._sClassItem + ':visible').each(function() {
  63. iMenu += $this._getWidth($(this), bForceCalculate);
  64. });
  65. var iParent = oMenu.parent().width();
  66. var iItemMore = oItemMore.outerWidth(true);
  67. if(iMenu > iParent)
  68. this._moveToSubmenu(oMenu, oItemMore, oItemMoreSubmenu, iParent, iItemMore);
  69. if(iMenu < iParent)
  70. this._moveFromSubmenu(oMenu, oItemMore, oItemMoreSubmenu, iParent, iMenu);
  71. };
  72. BxDolMenuMoreAuto.prototype.more = function(oElement)
  73. {
  74. var oElement = $(oElement);
  75. oElement.parents('li:first').find('#' + this._aHtmlIds['more_auto_popup']).dolPopup({
  76. pointer: {
  77. el: oElement
  78. },
  79. moveToDocRoot: false
  80. });
  81. }
  82. BxDolMenuMoreAuto.prototype._moveToSubmenu = function(oMenu, oItemMore, oItemMoreSubmenu, iParent, iItemMore)
  83. {
  84. var $this = this;
  85. var bRelocateOthers = false;
  86. var iWidthTotal = iItemMore;
  87. var oSubmenuItemFirst = oItemMoreSubmenu.children(this._sClassItem + ':first');
  88. oMenu.children(this._sClassItem + ':not(' + this._sClassItemMore + ')').each(function() {
  89. var oItem = $(this);
  90. var iItem = $this._getWidth(oItem);
  91. if(($this._bStaticMode || bRelocateOthers || iWidthTotal + iItem > iParent) && !oItem.hasClass($this._sClassItemStatic)) {
  92. oItem.addClass('bx-def-color-bg-hl-hover');
  93. if(!oSubmenuItemFirst.length)
  94. oItemMoreSubmenu.append(oItem.detach());
  95. else
  96. oSubmenuItemFirst.before(oItem.detach());
  97. bRelocateOthers = true;
  98. return;
  99. }
  100. iWidthTotal += iItem;
  101. });
  102. if(oItemMoreSubmenu.find('li').length)
  103. oItemMore.show(function() {
  104. oMenu.addClass($this._sClassReady).parents('.bx-menu-more-auto-wrapper:first').css('overflow', 'visible');
  105. });
  106. };
  107. BxDolMenuMoreAuto.prototype._moveFromSubmenu = function(oMenu, oItemMore, oItemMoreSubmenu, iParent, iMenu)
  108. {
  109. var $this = this;
  110. var bStopRelocation = false;
  111. var iWidthTotal = iMenu;
  112. oItemMoreSubmenu.children(this._sClassItem).each(function() {
  113. if(bStopRelocation)
  114. return;
  115. var oItem = $(this);
  116. var iItem = $this._getWidth(oItem);
  117. if(iWidthTotal + iItem > iParent) {
  118. bStopRelocation = true;
  119. return;
  120. }
  121. oItem.removeClass('bx-def-color-bg-hl-hover');
  122. oItemMore.before(oItem.detach());
  123. iWidthTotal += iItem;
  124. });
  125. if(!oItemMoreSubmenu.find('li').length)
  126. oItemMore.hide();
  127. };
  128. BxDolMenuMoreAuto.prototype._getWidth = function(oItem, bForceCalculate)
  129. {
  130. var iItem = parseInt(oItem.attr(this._sKeyWidth));
  131. if(!bForceCalculate && iItem)
  132. return iItem;
  133. iItem = oItem.outerWidth(true);
  134. if(iItem)
  135. oItem.attr(this._sKeyWidth, iItem);
  136. return iItem;
  137. }
  138. /** @} */