v1.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace zin;
  3. class treeEditor extends wg
  4. {
  5. /**
  6. * @var mixed[]
  7. */
  8. protected static $defineProps = array(
  9. 'items: array',
  10. 'type?: string',
  11. 'id?: string',
  12. 'icon?: string',
  13. 'class?: string',
  14. 'sortable?: array',
  15. 'itemProps?: array',
  16. 'onSort?: function',
  17. 'canSortTo?: function',
  18. 'selected?: string',
  19. 'canUpdateOrder?: bool=false',
  20. 'canEdit?: bool=false',
  21. 'canDelete?: bool=false',
  22. 'canSplit?: bool=true',
  23. 'checkbox?: bool=false',
  24. 'checkOnClick?: bool=false',
  25. 'preserve?: bool=true'
  26. );
  27. protected function build()
  28. {
  29. $this->setProp('items', $this->buildTree($this->prop('items')));
  30. $treeProps = $this->props->pick(array('items', 'activeClass', 'activeIcon', 'activeKey', 'onClickItem', 'defaultNestedShow', 'changeActiveKey', 'isDropdownMenu', 'collapsedIcon', 'expandedIcon', 'normalIcon', 'itemActions', 'hover', 'onClick', 'sortable', 'itemProps', 'onSort', 'canSortTo', 'checkbox', 'checkOnClick', 'preserve'));
  31. $id = $this->prop('id');
  32. if(empty($id))
  33. {
  34. global $app;
  35. $id = "treeEditor-{$app->rawModule}-{$app->rawMethod}";
  36. }
  37. $treeType = (!empty($treeProps['onSort']) || !empty($treeProps['sortable'])) ? 'sortableTree' : 'tree';
  38. return div
  39. (
  40. setStyle('--menu-selected-bg', 'none'),
  41. zui::$treeType
  42. (
  43. set::_id($id),
  44. set::_tag('menu'),
  45. set::lines(),
  46. set::preserve($id),
  47. set($treeProps)
  48. )
  49. );
  50. }
  51. /**
  52. * @param mixed[] $items
  53. */
  54. private function buildTree($items)
  55. {
  56. global $app;
  57. $canEdit = $this->prop('canEdit');
  58. $canDelete = $this->prop('canDelete');
  59. $canSplit = $this->prop('canSplit');
  60. $editType = $this->prop('type');
  61. $selected = $this->prop('selected');
  62. $typeList = array('task' => 'T', 'bug' => 'B', 'case' => 'C', 'feedback' => 'F', 'ticket' => 'T');
  63. $viewType = data('viewType') ? data('viewType') : '';
  64. $sortTree = $this->prop('sortable') || $this->prop('onSort');
  65. foreach($items as $key => $item)
  66. {
  67. $item = (array)$item;
  68. $itemCanSplit = isset($item['canSplit']) ? $item['canSplit'] : $canSplit;
  69. if(!isset($item['content']))
  70. {
  71. if(!isset($item['text'])) $item['text'] = $item['name'];
  72. if(!isset($item['url'])) $item['url'] = '';
  73. $item['titleAttrs']['data-app'] = $app->tab;
  74. $item['titleAttrs']['title'] = $item['text'];
  75. if(isset($item['type']) && isset($typeList[$item['type']])) $item['text'] = array('html' => $item['text'] . '<span class="text-gray ml-1">[' . $typeList[$item['type']] . ']</span>');
  76. $item['innerClass'] = 'py-0';
  77. $item['titleClass'] = 'text-clip';
  78. $item['selected'] = (!empty($selected) && !empty($item['id']) && $selected == $item['id']) || !empty($item['active']);
  79. if(isset($item['type']) && $item['type'] == 'product')
  80. {
  81. $item['icon'] = 'product';
  82. }
  83. elseif(isset($item['type']) && $item['type'] == 'story' && $editType != 'story')
  84. {
  85. $item['actions'] = array();
  86. $item['actions']['items'] = array();
  87. if($canEdit && $editType != 'task') $item['actions']['items'][] = array('key' => 'edit', 'icon' => 'edit', 'data-toggle' => 'modal', 'url' => createLink('tree', 'edit', 'moduleID=' . $item['id'] . '&type=' . ($viewType ? $viewType : $item['type'])));
  88. if($itemCanSplit) $item['actions']['items'][] = array('key' => 'view', 'icon' => 'split', 'url' => $item['url'], 'data-app' => $app->tab);
  89. }
  90. elseif(isset($item['type']) && $item['type'] != 'branch')
  91. {
  92. if($sortTree) $item['trailingIcon'] = 'move muted cursor-move';
  93. if(!isset($item['actions'])) $item['actions'] = array();
  94. if(!isset($item['actions']['items'])) $item['actions']['items'] = array();
  95. if($canEdit) $item['actions']['items'][] = array('key' => 'edit', 'icon' => 'edit', 'data-toggle' => 'modal', 'url' => createLink('tree', 'edit', 'moduleID=' . $item['id'] . '&type=' . $item['type']));
  96. if($canDelete) $item['actions']['items'][] = array('key' => 'delete', 'icon' => 'trash', 'className' => 'btn ghost toolbar-item square size-sm rounded ajax-submit', 'url' => createLink('tree', 'delete', 'module=' . $item['id']));
  97. if($itemCanSplit) $item['actions']['items'][] = array('key' => 'view', 'icon' => 'split', 'url' => $item['url'], 'data-app' => $app->tab);
  98. }
  99. }
  100. if(!empty($item['children']))
  101. {
  102. $item['items'] = !empty($item['children']['url']) ? $item['children'] : $this->buildTree($item['children']);
  103. unset($item['children']);
  104. }
  105. unset($item['type']);
  106. $items[$key] = $item;
  107. }
  108. return $items;
  109. }
  110. }