v1.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace zin;
  3. require_once dirname(__DIR__) . DS . 'nav' . DS . 'v1.php';
  4. class featureBar extends wg
  5. {
  6. /**
  7. * @var mixed[]
  8. */
  9. protected static $defineProps = array(
  10. 'items?:array',
  11. 'current?:string',
  12. 'link?:string',
  13. 'linkParams?:string=""',
  14. 'module?:string',
  15. 'method?:string',
  16. 'load?: string="table"',
  17. 'loadID?: string',
  18. 'app?: string=""',
  19. 'param?: int=0',
  20. 'searchModule?: string=""',
  21. 'labelCount?: int=-1',
  22. 'isModal?: bool=false'
  23. );
  24. /**
  25. * @var mixed[]
  26. */
  27. protected static $defineBlocks = array
  28. (
  29. 'nav' => array('map' => 'nav'),
  30. 'leading' => array(),
  31. 'trailing' => array()
  32. );
  33. protected function getItems()
  34. {
  35. $items = $this->prop('items');
  36. if(!empty($items)) return array_values($items);
  37. global $app, $lang;
  38. $currentModule = $this->prop('module', $app->rawModule);
  39. $currentMethod = $this->prop('method', $app->rawMethod);
  40. \common::sortFeatureMenu($currentModule, $currentMethod);
  41. $rawItems = \customModel::getFeatureMenu($currentModule, $currentMethod);
  42. if(!is_array($rawItems)) return null;
  43. $current = $this->prop('current', data('browseType'));
  44. $pager = data('pager');
  45. $recTotal = $pager ? $pager->recTotal : data('recTotal');
  46. $recTotal = $this->prop('labelCount') >= 0 ? $this->prop('labelCount') : $recTotal;
  47. $items = array();
  48. $loadID = $this->prop('loadID');
  49. $load = $this->prop('load');
  50. $tab = $this->prop('app');
  51. $param = $this->prop('param') ? $this->prop('param') : data('param');
  52. $searchModule = $this->prop('searchModule');
  53. $commonLink = $this->prop('link');
  54. $itemLink = $this->prop('itemLink');
  55. $isModal = $this->prop('isModal');
  56. data('activeFeature', $current);
  57. if(empty($commonLink)) $commonLink = createLink($app->rawModule, $app->rawMethod, $this->prop('linkParams'));
  58. if(empty($searchModule)) $searchModule = data("config.{$currentModule}.search.module");
  59. if(empty($searchModule)) $searchModule = $currentModule;
  60. foreach($rawItems as $rawItem)
  61. {
  62. if(isset($rawItem->hidden)) continue;
  63. if(isset($rawItem->name) && $isModal && $rawItem->name == 'QUERY') continue;
  64. if(isset($rawItem->type) && $rawItem->type === 'divider')
  65. {
  66. $items[] = array('type' => 'divider');
  67. continue;
  68. }
  69. $link = ($itemLink && isset($itemLink[$rawItem->name])) ? $itemLink[$rawItem->name] : $commonLink;
  70. $isActive = $rawItem->name == $current;
  71. $moreSelects = array();
  72. if($rawItem->name == 'more' && !empty($lang->$currentModule->moreSelects)) $moreSelects = $lang->$currentModule->moreSelects;
  73. if(isset($lang->$currentModule->moreSelects[$currentMethod][$rawItem->name])) $moreSelects = $lang->$currentModule->moreSelects[$currentMethod][$rawItem->name];
  74. if($rawItem->name == 'QUERY' && !empty($lang->custom->queryList)) $moreSelects = $lang->custom->queryList;
  75. if(!empty($moreSelects))
  76. {
  77. $activeText = $rawItem->text;
  78. $subItems = array();
  79. $callback = $this->prop(in_array($rawItem->name, array('more', 'status')) ? 'moreMenuLinkCallback' : 'queryMenuLinkCallback');
  80. $callback = isset($callback[0]) ? $callback[0] : null;
  81. foreach($moreSelects as $key => $text)
  82. {
  83. $url = ($callback instanceof \Closure) ? $callback($key, $text) : str_replace('{key}', (string)$key, $link);
  84. $subItem = array();
  85. $subItem['text'] = $text;
  86. $subItem['active'] = $rawItem->name == 'QUERY' ? $key == $param : $key == $current;
  87. $subItem['attrs'] = ['data-id' => $key, 'data-load' => $load, 'data-target' => $loadID, 'data-app' => $tab, 'data-success' => "() => zui.updateSearchForm('$searchModule')"];
  88. $subItem['url'] = $isModal ? '#featureBar' : $url;
  89. if($isModal) $subItem['onClick'] = jsRaw("() => loadModal('{$url}')");
  90. if($rawItem->name == 'QUERY')
  91. {
  92. $closeLink = createLink('search', 'ajaxRemoveMenu', "queryID={$key}");
  93. $loadUrl = $subItem['url'] . '#featureBar';
  94. $subItem['innerClass'] = 'flex-auto';
  95. $subItem['className'] = 'row gap-0';
  96. $subItem['children'] = array(jsRaw("zui.h('a', {className: 'ajax-submit', 'data-url': '{$closeLink}', 'data-load': '{$loadUrl}'}, zui.h('span', {className: 'close'}))"));
  97. }
  98. $subItems[] = $subItem;
  99. if($key === $current || ($current == 'bysearch' && $key === $param))
  100. {
  101. $isActive = true;
  102. $activeText = $text;
  103. }
  104. }
  105. $items[] = array
  106. (
  107. 'text' => $activeText,
  108. 'active' => $isActive,
  109. 'type' => 'dropdown',
  110. 'caret' => 'down',
  111. 'items' => $subItems,
  112. 'badge' => $isActive && $recTotal != '' ? array('text' => $recTotal, 'class' => 'size-sm canvas ring-0 rounded-md') : null,
  113. 'props' => array('data-id' => $rawItem->name, 'title' => $activeText),
  114. 'textClass' => 'text-ellipsis max-w-32'
  115. );
  116. continue;
  117. }
  118. $url = str_replace('{key}', strval($rawItem->name), $link);
  119. $item = array
  120. (
  121. 'text' => $rawItem->text,
  122. 'active' => $isActive,
  123. 'badge' => $isActive && $recTotal != '' ? array('text' => $recTotal, 'class' => 'size-sm canvas ring-0 rounded-md') : null,
  124. 'props' => array('data-id' => $rawItem->name, 'data-load' => $load, 'data-target' => $loadID, 'data-app' => $tab, 'title' => $rawItem->text),
  125. 'textClass' => 'text-ellipsis max-w-32',
  126. 'url' => $isModal ? '#featureBar' : $url
  127. );
  128. if($isModal) $item['onClick'] = "loadModal('{$url}')";
  129. $items[] = $item;
  130. }
  131. return $items;
  132. }
  133. protected function buildNav()
  134. {
  135. $nav = $this->block('nav');
  136. if(!empty($nav) && $nav[0] instanceof nav) return $nav;
  137. $responsiveNavOptions = [];
  138. $responsiveNavOptions['container'] = 'parent';
  139. $responsiveNavOptions['mergeDropdown'] = true;
  140. $responsiveNavOptions['getContainerSize'] = jsRaw('(container) => (Array.from($(container).children()).reduce((acc, item) => acc - (item.hasAttribute("z-use-responsivenavhelper") ? 0 : (item.clientWidth + 20)), container.clientWidth - 32))');
  141. $responsiveNavOptions['fixedItems'] = jsRaw('(_,ele) => {if($(ele).find(".search-form-toggle").length) {$(ele).css("order", 10000);return true} return false}');
  142. return new nav
  143. (
  144. set::compact(),
  145. set::className('nav-feature'),
  146. set::items($this->getItems()),
  147. zui::create('ResponsiveNavHelper', $responsiveNavOptions),
  148. divorce($this->children())
  149. );
  150. }
  151. protected function build()
  152. {
  153. return div
  154. (
  155. set::id('featureBar'),
  156. $this->block('leading'),
  157. $this->buildNav(),
  158. $this->block('trailing')
  159. );
  160. }
  161. }