ly-tree-node.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <template>
  2. <view
  3. ref="node"
  4. name="LyTreeNode"
  5. v-show="node.visible"
  6. class="ly-tree-node"
  7. :class="{
  8. 'is-expanded': expanded,
  9. 'is-hidden': !node.visible,
  10. 'is-checked': !node.disabled && node.checked
  11. }"
  12. role="treeitem"
  13. @tap.stop="handleClick"
  14. >
  15. <view
  16. class="ly-tree-node__content"
  17. :class="{
  18. 'is-current': node.isCurrent && highlightCurrent
  19. }"
  20. :style="{
  21. 'padding-left': (node.level - 1) * indent + 'px'
  22. }"
  23. >
  24. <text
  25. @tap.stop="handleExpandIconClick"
  26. :class="[
  27. {
  28. 'is-leaf': node.isLeaf,
  29. expanded: !node.isLeaf && node.expanded
  30. },
  31. 'ly-tree-node__expand-icon',
  32. iconClass ? iconClass : 'ly-iconfont ly-icon-caret-right'
  33. ]"
  34. ></text>
  35. <ly-checkbox
  36. v-if="checkboxVisible || radioVisible"
  37. :type="checkboxVisible ? 'checkbox' : 'radio'"
  38. :checked="node.checked"
  39. :indeterminate="node.indeterminate"
  40. :disabled="!!node.disabled"
  41. @check="handleCheckChange(!node.checked)"
  42. />
  43. <text v-if="node.loading" class="ly-tree-node__loading-icon ly-iconfont ly-icon-loading"></text>
  44. <template v-if="node.icon && node.icon.length > 0">
  45. <image v-if="node.icon.indexOf('/') !== -1" class="ly-tree-node__icon" mode="widthFix" :src="node.icon" @error="handleImageError"></image>
  46. <text v-else class="ly-tree-node__icon" :class="node.icon"></text>
  47. </template>
  48. <text class="ly-tree-node__label">{{ node.label }}</text>
  49. <view class="btn-view">
  50. <u-icon @click="edit(node)" name="edit-pen-fill" color="#2979ff" size="32" style="margin-right: 30rpx;"></u-icon>
  51. <u-icon @click="deldata(node)" name="trash" color="#fa3534" size="32"></u-icon>
  52. </view>
  53. </view>
  54. <view v-if="!renderAfterExpand || childNodeRendered" v-show="expanded" class="ly-tree-node__children" role="group">
  55. <ly-tree-node
  56. v-for="cNodeId in node.childNodesId"
  57. :nodeId="cNodeId"
  58. :render-after-expand="renderAfterExpand"
  59. :show-checkbox="showCheckbox"
  60. :show-radio="showRadio"
  61. :check-only-leaf="checkOnlyLeaf"
  62. :key="getNodeKey(cNodeId)"
  63. :indent="indent"
  64. :icon-class="iconClass"
  65. ></ly-tree-node>
  66. </view>
  67. </view>
  68. </template>
  69. <script>
  70. import { getNodeKey } from './tool/util.js';
  71. import lyCheckbox from './components/ly-checkbox.vue';
  72. export default {
  73. name: 'LyTreeNode',
  74. componentName: 'LyTreeNode',
  75. components: {
  76. lyCheckbox
  77. },
  78. props: {
  79. nodeId: [Number, String],
  80. renderAfterExpand: {
  81. type: Boolean,
  82. default: true
  83. },
  84. checkOnlyLeaf: {
  85. type: Boolean,
  86. default: false
  87. },
  88. showCheckbox: {
  89. type: Boolean,
  90. default: false
  91. },
  92. showRadio: {
  93. type: Boolean,
  94. default: false
  95. },
  96. indent: Number,
  97. iconClass: String
  98. },
  99. data() {
  100. return {
  101. node: {
  102. indeterminate: false,
  103. checked: false,
  104. expanded: false
  105. },
  106. expanded: false,
  107. childNodeRendered: false,
  108. oldChecked: null,
  109. oldIndeterminate: null,
  110. highlightCurrent: false
  111. };
  112. },
  113. inject: ['tree'],
  114. computed: {
  115. checkboxVisible() {
  116. if (this.checkOnlyLeaf) {
  117. return this.showCheckbox && this.node.isLeaf;
  118. }
  119. return this.showCheckbox;
  120. },
  121. radioVisible() {
  122. if (this.checkOnlyLeaf) {
  123. return this.showRadio && this.node.isLeaf;
  124. }
  125. return this.showRadio;
  126. }
  127. },
  128. watch: {
  129. 'node.indeterminate'(val) {
  130. this.handleSelectChange(this.node.checked, val);
  131. },
  132. 'node.checked'(val) {
  133. this.handleSelectChange(val, this.node.indeterminate);
  134. },
  135. 'node.expanded'(val) {
  136. this.$nextTick(() => (this.expanded = val));
  137. if (val) {
  138. this.childNodeRendered = true;
  139. }
  140. }
  141. },
  142. methods: {
  143. getNodeKey(nodeId) {
  144. let node = this.tree.store.root.getChildNodes([nodeId])[0];
  145. return getNodeKey(this.tree.nodeKey, node.data);
  146. },
  147. handleSelectChange(checked, indeterminate) {
  148. if (this.oldChecked !== checked && this.oldIndeterminate !== indeterminate) {
  149. if (this.checkOnlyLeaf && !this.node.isLeaf) return;
  150. if (this.checkboxVisible) {
  151. const allNodes = this.tree.store._getAllNodes();
  152. this.tree.$emit('check-change', {
  153. checked,
  154. indeterminate,
  155. node: this.node,
  156. data: this.node.data,
  157. checkedall: allNodes.every(item => item.checked)
  158. });
  159. } else {
  160. this.tree.$emit('radio-change', {
  161. checked,
  162. node: this.node,
  163. data: this.node.data,
  164. checkedall: false
  165. });
  166. }
  167. }
  168. if (!this.expanded && this.tree.expandOnCheckNode && checked) {
  169. this.handleExpandIconClick();
  170. }
  171. this.oldChecked = checked;
  172. this.indeterminate = indeterminate;
  173. },
  174. handleClick() {
  175. this.tree.store.setCurrentNode(this.node);
  176. this.tree.$emit('current-change', {
  177. node: this.node,
  178. data: this.tree.store.currentNode ? this.tree.store.currentNode.data : null,
  179. currentNode: this.tree.store.currentNode
  180. });
  181. this.tree.currentNode = this.node;
  182. if (this.tree.expandOnClickNode) {
  183. this.handleExpandIconClick();
  184. }
  185. if (this.tree.checkOnClickNode && !this.node.disabled) {
  186. (this.checkboxVisible || this.radioVisible) && this.handleCheckChange(!this.node.checked);
  187. }
  188. this.tree.$emit('node-click', this.node);
  189. },
  190. handleExpandIconClick() {
  191. if (this.node.isLeaf) return;
  192. if (this.expanded) {
  193. this.tree.$emit('node-collapse', this.node);
  194. this.node.collapse();
  195. } else {
  196. this.node.expand();
  197. this.tree.$emit('node-expand', this.node);
  198. if (this.tree.accordion) {
  199. uni.$emit(`${this.tree.elId}-tree-node-expand`, this.node);
  200. }
  201. }
  202. },
  203. handleCheckChange(checked) {
  204. if (this.node.disabled) return;
  205. if (this.checkboxVisible) {
  206. this.node.setChecked(checked, !(this.tree.checkStrictly || this.checkOnlyLeaf));
  207. } else {
  208. this.node.setRadioChecked(checked);
  209. }
  210. this.$nextTick(() => {
  211. this.tree.$emit('check', {
  212. node: this.node,
  213. data: this.node.data,
  214. checkedNodes: this.tree.store.getCheckedNodes(),
  215. checkedKeys: this.tree.store.getCheckedKeys(),
  216. halfCheckedNodes: this.tree.store.getHalfCheckedNodes(),
  217. halfCheckedKeys: this.tree.store.getHalfCheckedKeys()
  218. });
  219. });
  220. },
  221. handleImageError() {
  222. this.node.icon = this.tree.defaultNodeIcon;
  223. },
  224. edit(node){
  225. this.tree.$emit('edit',node)
  226. },
  227. deldata(node){
  228. this.tree.$emit('deldata',node)
  229. }
  230. },
  231. created() {
  232. if (!this.tree) {
  233. throw new Error("Can not find node's tree.");
  234. }
  235. this.node = this.tree.store.nodesMap[this.nodeId];
  236. this.highlightCurrent = this.tree.highlightCurrent;
  237. if (this.node.expanded) {
  238. this.expanded = true;
  239. this.childNodeRendered = true;
  240. }
  241. const props = this.tree.props || {};
  242. const childrenKey = props['children'] || 'children';
  243. this.$watch(`node.data.${childrenKey}`, () => {
  244. this.node.updateChildren();
  245. });
  246. if (this.tree.accordion) {
  247. uni.$on(`${this.tree.elId}-tree-node-expand`, node => {
  248. if (this.node.id !== node.id && this.node.level === node.level) {
  249. this.node.collapse();
  250. }
  251. });
  252. }
  253. },
  254. beforeDestroy() {
  255. this.$parent = null;
  256. },
  257. };
  258. </script>
  259. <style lang="scss" scoped>
  260. .ly-tree-node {
  261. white-space: nowrap;
  262. outline: 0;
  263. }
  264. .ly-tree-node__content {
  265. display: flex;
  266. align-items: center;
  267. height: 70rpx;
  268. position: relative;
  269. .btn-view {
  270. position: absolute;
  271. top: 50%;
  272. transform: translateY(-50%);
  273. right: 0;
  274. }
  275. }
  276. .ly-tree-node__content.is-current {
  277. background-color: #f5f7fa;
  278. }
  279. .ly-tree-node__content > .ly-tree-node__expand-icon {
  280. padding: 12rpx;
  281. }
  282. .ly-tree-node__checkbox {
  283. display: flex;
  284. margin-right: 16rpx;
  285. width: 40rpx;
  286. height: 40rpx;
  287. }
  288. .ly-tree-node__checkbox > image {
  289. width: 40rpx;
  290. height: 40rpx;
  291. }
  292. .ly-tree-node__expand-icon {
  293. color: #c0c4cc;
  294. font-size: 28rpx;
  295. -webkit-transform: rotate(0);
  296. transform: rotate(0);
  297. -webkit-transition: -webkit-transform 0.3s ease-in-out;
  298. transition: -webkit-transform 0.3s ease-in-out;
  299. transition: transform 0.3s ease-in-out;
  300. transition: transform 0.3s ease-in-out, -webkit-transform 0.3s ease-in-out;
  301. }
  302. .ly-tree-node__expand-icon.expanded {
  303. -webkit-transform: rotate(90deg);
  304. transform: rotate(90deg);
  305. }
  306. .ly-tree-node__expand-icon.is-leaf {
  307. color: transparent;
  308. }
  309. .ly-tree-node__icon {
  310. width: 34rpx;
  311. height: 34rpx;
  312. overflow: hidden;
  313. margin-right: 16rpx;
  314. }
  315. .ly-tree-node__label {
  316. font-size: 28rpx;
  317. }
  318. .ly-tree-node__loading-icon {
  319. margin-right: 16rpx;
  320. font-size: 28rpx;
  321. color: #c0c4cc;
  322. -webkit-animation: rotating 2s linear infinite;
  323. animation: rotating 2s linear infinite;
  324. }
  325. .ly-tree-node > .ly-tree-node__children {
  326. overflow: hidden;
  327. background-color: transparent;
  328. }
  329. .ly-tree-node > .ly-tree-node__children.collapse-transition {
  330. transition: height 0.3s ease-in-out;
  331. }
  332. .ly-tree-node.is-expanded > .ly-tree-node__children {
  333. display: block;
  334. }
  335. .ly-tree-node_collapse {
  336. overflow: hidden;
  337. padding-top: 0;
  338. padding-bottom: 0;
  339. }
  340. /* lyTree-end */
  341. /* iconfont-start */
  342. @font-face {
  343. font-family: 'ly-iconfont';
  344. src: url('data:application/x-font-woff2;charset=utf-8;base64,d09GMgABAAAAAAPsAAsAAAAACKwAAAOeAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHEIGVgCDBgqFDIQPATYCJAMMCwgABCAFhG0HQBtfB8gekiSCdAwUAKgCFMA5Hj7H0PeTlABUr57PVyGqugqzSWJnNwWoWJjx/9rUr4TPL1ZSQpU2mycqwoRwIN3p+MkqMqyEW+OtMBLPSUBb8v//XtWMKTavxYIUsT/Wy1qbQzkBDOYEKGB7dVpPyVqgCnJNwvMvhZl10nMCtQbFoPVhY8ZDncJfF4grbqpQ13AqE52hWqgcOFrEQ6hWnW5VfMCD7Pfjn4WoI6nI/K0bl0MNGPBz0qcflVqYnvCA4vNDPUXGPFCIw8HgtsqiOK9SrW2smm6sVITElWlpISMdVBn8wyMJopLfXg+myZ48KCrSkvj9g37U1ItbXYke4APwXxK3N4TuehyBfmM0I3zbNdt7uk3VnjPtzX0rnIl7z7bZvb/thHohsu9QuykKo+Cws4nL7LsPmI3n2qN9B9upZEIKd4hu0NCKi0rt7fNtdl+I1N25hOJMDQK6odS123tROR7Pg8toEhDaF+kR0TYjxW6M58F5+ZNQOxmZHtE2g+IYjxjlNy/yIRQpCmrgq5R4/3jx8PvT8Ha8d3/xiLnt4EGyaDnznzRv8vpyZ+9TFHf/ntX9e59A+b6+fPHd5+dy0wYHVvHOroWbnWe879O9DnL53bN/gUHuwm28b/n8i/V3ry4E3IoXNqS6Rvs0LhJxeNVjoUkM3LKosU+0a6rh45FVvLt+2oz7Zd53b4QOy7/9snDXHbqVu+A+f8r7PnM2H8kXrWm5c8/vLu7LqRee7HW637mz3kHc5U/RCXf25d7G8tkdgEfwIpzpkknGpaMw3ww55q9Mn9OQNyua/wB/49OOWydn4eL/6roCfjx6FMmcxfJStYRKfd3UwoHiML4rF4uMSK+SvYTuNxMHrpl8yd3Q6v32cAeo/KFaowBJlQHIqo3zi3geKtRZhErVlqDWnOGn67QRKkWpwaw1AkKza5A0egFZszf8In4HFTp9h0rNUQm1NqP1lXUmgyuDBVUlNYi2gHA98FnokUreOZaac1xV1JlMMZGKEs+QdCLVrgynPhUcO0pzzYyUjDAReGSYeBl13YCEIrCpLhOWlGE+mWRD35TQAw8UawRKJVEGQrMAwekCPpaMlpTOz49FmeZwqcREX1t3Ikoo4dMTaQmpBfzhRn9R30uZXTKXKUOSmLSKEQIeYhjqKZcrcIzhMLLRrJMSrA35UF4yGMaWGhPHm733dwJq+Z/NkSJHUXemCirjgpuWrHMD1eC+mQUAAAA=')
  345. format('woff2');
  346. }
  347. .ly-iconfont {
  348. font-family: 'ly-iconfont' !important;
  349. font-size: 30rpx;
  350. font-style: normal;
  351. -webkit-font-smoothing: antialiased;
  352. -moz-osx-font-smoothing: grayscale;
  353. }
  354. .ly-icon-caret-right:before {
  355. content: '\e8ee';
  356. }
  357. .ly-icon-loading:before {
  358. content: '\e657';
  359. }
  360. /* iconfont-end */
  361. /* animate-start */
  362. @keyframes rotating {
  363. 0% {
  364. -webkit-transform: rotateZ(0);
  365. transform: rotateZ(0);
  366. }
  367. 100% {
  368. -webkit-transform: rotateZ(360deg);
  369. transform: rotateZ(360deg);
  370. }
  371. }
  372. /* animate-end */
  373. </style>