BxBaseMenuMoreAuto.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. *
  8. * @{
  9. */
  10. define('BX_DEF_MENU_ITEM_MORE_AUTO', 'more-auto');
  11. class BxBaseMenuMoreAuto extends BxTemplMenu
  12. {
  13. protected static $_sTmplContentItemMore;
  14. protected static $_sTmplContentItemMorePopup;
  15. protected $_sTmplNameItemMore;
  16. protected $_sTmplNameItemMorePopup;
  17. protected $_bMoreAuto;
  18. protected $_iMoreAutoItemsStatic;
  19. protected $_bMoreAutoItemsStaticOnly;
  20. protected $_sJsClassMoreAuto;
  21. protected $_sJsObjectMoreAuto;
  22. protected $_sJsCallMoreAuto;
  23. protected $_aHtmlIds;
  24. public function __construct($aObject, $oTemplate = false)
  25. {
  26. parent::__construct($aObject, $oTemplate);
  27. $this->_sTmplNameItemMore = 'menu_item_more.html';
  28. $this->_sTmplNameItemMorePopup = 'menu_item_more_popup.html';
  29. $this->_bMoreAuto = null;
  30. $this->_iMoreAutoItemsStatic = (int)$aObject['persistent'];
  31. $this->_bMoreAutoItemsStaticOnly = false;
  32. $this->_sJsClassMoreAuto = 'BxDolMenuMoreAuto';
  33. $this->_sJsObjectMoreAuto = 'oMenuMoreAuto' . bx_gen_method_name($this->_sObject);
  34. /*
  35. * Not guarded by 'if(!{js_object})': content loaded dynamically (htmx) brings a new menu element under the same
  36. * object name, and init() itself skips an element that is already set up.
  37. */
  38. $this->_sJsCallMoreAuto = "var {js_object} = new {js_class}({js_params}); {js_object}.init();";
  39. $sPrefix = str_replace('_', '-', $this->_sObject);
  40. $this->_aHtmlIds = array(
  41. 'main' => $sPrefix,
  42. 'more_auto_popup' => $sPrefix . '-ma-popup',
  43. );
  44. }
  45. public function getCode()
  46. {
  47. $sResult = parent::getCode();
  48. if($this->_isMoreAuto()) {
  49. $this->_oTemplate->addJs(array('BxDolMenuMoreAuto.js'));
  50. if(!empty($sResult))
  51. $sResult = $this->_oTemplate->parseHtmlByName('menu_more_auto.html', array(
  52. 'menu' => $sResult
  53. ));
  54. }
  55. return $sResult;
  56. }
  57. public function isMoreAuto()
  58. {
  59. return $this->_isMoreAuto();
  60. }
  61. protected function _isMoreAuto()
  62. {
  63. if(is_bool($this->_bMoreAuto))
  64. return $this->_bMoreAuto;
  65. if(!isset($this->_aObject['menu_items']))
  66. $this->_aObject['menu_items'] = $this->getMenuItemsRaw();
  67. $sItem = BX_DEF_MENU_ITEM_MORE_AUTO;
  68. if(!empty($this->_aObject['menu_items'][$sItem]) && is_array($this->_aObject['menu_items'][$sItem]) && (int)$this->_aObject['menu_items'][$sItem]['active'] == 1)
  69. $this->_bMoreAuto = true;
  70. else
  71. $this->_bMoreAuto = false;
  72. return $this->_bMoreAuto;
  73. }
  74. protected function _getTemplateVars()
  75. {
  76. $aResult = parent::_getTemplateVars();
  77. $bMoreAuto = $this->_isMoreAuto();
  78. return array_merge($aResult, [
  79. 'html_id' => $this->_getHtmlIdMain(),
  80. 'bx_if:show_more_auto_class' => [
  81. 'condition' => $bMoreAuto,
  82. 'content' => []
  83. ],
  84. 'js_code' => $bMoreAuto ? $this->_getJsCodeMoreAuto() : ''
  85. ]);
  86. }
  87. protected function _getJsCodeMoreAuto()
  88. {
  89. return $this->_oTemplate->_wrapInTagJsCode(bx_replace_markers($this->_sJsCallMoreAuto, [
  90. 'js_class' => $this->_getJsClassMoreAuto(),
  91. 'js_object' => $this->_getJsObjectMoreAuto(),
  92. 'js_params' => json_encode([
  93. 'sObject' => $this->_sObject,
  94. 'iItemsStatic' => $this->_iMoreAutoItemsStatic,
  95. 'bItemsStaticOnly' => $this->_bMoreAutoItemsStaticOnly ? 1 : 0,
  96. 'aHtmlIds' => $this->_getHtmlIds()
  97. ])
  98. ]));
  99. }
  100. protected function _getMenuItem ($aItem)
  101. {
  102. $aItem['popup'] = '';
  103. if($aItem['name'] != BX_DEF_MENU_ITEM_MORE_AUTO)
  104. return parent::_getMenuItem($aItem);
  105. $aItem['onclick'] = $this->_getJsObjectMoreAuto() . '.more(this);';
  106. $aItem['popup'] = BxTemplFunctions::getInstance()->transBox($this->_aHtmlIds['more_auto_popup'], $this->_oTemplate->parseHtmlByContent($this->_getTmplContentItemMorePopup(), array(
  107. 'content' => ''
  108. )), true);
  109. return parent::_getMenuItem($aItem);
  110. }
  111. /**
  112. * The 'more' trigger opens a popup that is rendered next to it (the __popup__ marker), so it is a disclosure
  113. * for a dialog, not a plain link. Its accessible name comes from the generic title/title_system handling in
  114. * BxBaseMenu::_getMenuTitle().
  115. */
  116. protected function _getMenuAttrs ($aMenuItem)
  117. {
  118. $sAttrs = parent::_getMenuAttrs($aMenuItem);
  119. if(!empty($aMenuItem['name']) && $aMenuItem['name'] == BX_DEF_MENU_ITEM_MORE_AUTO)
  120. $sAttrs .= ' aria-haspopup="dialog" aria-expanded="false" aria-controls="' . bx_html_attribute($this->_aHtmlIds['more_auto_popup']) . '"';
  121. return $sAttrs;
  122. }
  123. protected function _getHtmlIds()
  124. {
  125. return $this->_aHtmlIds;
  126. }
  127. protected function _getHtmlIdMain()
  128. {
  129. return $this->_aHtmlIds['main'];
  130. }
  131. protected function _getJsClassMoreAuto()
  132. {
  133. return $this->_sJsClassMoreAuto;
  134. }
  135. protected function _getJsObjectMoreAuto()
  136. {
  137. return $this->_sJsObjectMoreAuto;
  138. }
  139. protected function _getTmplContentItemMore()
  140. {
  141. if(empty(self::$_sTmplContentItemMore))
  142. self::$_sTmplContentItemMore = $this->_oTemplate->getHtml($this->_sTmplNameItemMore);
  143. return self::$_sTmplContentItemMore;
  144. }
  145. protected function _getTmplContentItemMorePopup()
  146. {
  147. if(empty(self::$_sTmplContentItemMorePopup))
  148. self::$_sTmplContentItemMorePopup = $this->_oTemplate->getHtml($this->_sTmplNameItemMorePopup);
  149. return self::$_sTmplContentItemMorePopup;
  150. }
  151. }
  152. /** @} */