ly-tree-node.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <template>
  2. <view ref="node" name="LyTreeNode" v-show="node.visible" class="ly-tree-node" :class="{
  3. 'is-expanded': expanded,
  4. 'is-hidden': !node.visible,
  5. 'is-checked': !node.disabled && node.checked
  6. }" role="treeitem" @tap.stop="handleClick">
  7. <view class="ly-tree-node__content" :class="{
  8. 'is-current': node.isCurrent && highlightCurrent
  9. }" :style="{
  10. 'padding-left': (node.level - 1) * indent + 'px'
  11. }">
  12. <view class="">
  13. <text :class="node.data.icon" class="u-m-r-12"></text>
  14. <text class="ly-tree-node__label u-line-1">{{node.label}}</text>
  15. </view>
  16. <view class="" @tap.stop="handleExpandIconClick">
  17. <u-icon :name="iconName" v-if="node.data.hasChildren"></u-icon>
  18. </view>
  19. </view>
  20. <view v-if="!renderAfterExpand || childNodeRendered" v-show="expanded" class="ly-tree-node__children"
  21. role="group">
  22. <ly-tree-node v-for="cNodeId in node.childNodesId" :nodeId="cNodeId"
  23. :render-after-expand="renderAfterExpand" :show-checkbox="showCheckbox" :show-radio="showRadio"
  24. :check-only-leaf="checkOnlyLeaf" :key="getNodeKey(cNodeId)" :indent="indent" :icon-class="iconClass">
  25. </ly-tree-node>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. import {
  31. getNodeKey
  32. } from './tool/util.js';
  33. export default {
  34. name: 'LyTreeNode',
  35. componentName: 'LyTreeNode',
  36. props: {
  37. nodeId: [Number, String],
  38. renderAfterExpand: {
  39. type: Boolean,
  40. default: true
  41. },
  42. checkOnlyLeaf: {
  43. type: Boolean,
  44. default: false
  45. },
  46. showCheckbox: {
  47. type: Boolean,
  48. default: false
  49. },
  50. showRadio: {
  51. type: Boolean,
  52. default: false
  53. },
  54. indent: Number,
  55. iconClass: String
  56. },
  57. data() {
  58. return {
  59. iconName: 'arrow-down',
  60. node: {
  61. indeterminate: false,
  62. checked: false,
  63. expanded: false
  64. },
  65. expanded: false,
  66. childNodeRendered: false,
  67. oldChecked: null,
  68. oldIndeterminate: null,
  69. highlightCurrent: false
  70. };
  71. },
  72. inject: ['tree'],
  73. computed: {
  74. checkboxVisible() {
  75. if (this.checkOnlyLeaf) {
  76. return this.showCheckbox && this.node.isLeaf;
  77. }
  78. return this.showCheckbox;
  79. },
  80. radioVisible() {
  81. if (this.checkOnlyLeaf) {
  82. return this.showRadio && this.node.isLeaf;
  83. }
  84. return this.showRadio;
  85. }
  86. },
  87. watch: {
  88. 'node.indeterminate'(val) {
  89. this.handleSelectChange(this.node.checked, val);
  90. },
  91. 'node.checked'(val) {
  92. this.handleSelectChange(val, this.node.indeterminate);
  93. },
  94. 'node.expanded'(val) {
  95. this.$nextTick(() => this.expanded = val);
  96. if (val) {
  97. this.childNodeRendered = true;
  98. }
  99. }
  100. },
  101. methods: {
  102. getNodeKey(nodeId) {
  103. let node = this.tree.store.root.getChildNodes([nodeId])[0];
  104. return getNodeKey(this.tree.nodeKey, node.data);
  105. },
  106. handleSelectChange(checked, indeterminate) {
  107. if (this.oldChecked !== checked && this.oldIndeterminate !== indeterminate) {
  108. if (this.checkOnlyLeaf && !this.node.isLeaf) return;
  109. if (this.checkboxVisible) {
  110. const allNodes = this.tree.store._getAllNodes();
  111. this.tree.$emit('check-change', {
  112. checked,
  113. indeterminate,
  114. node: this.node,
  115. data: this.node.data,
  116. checkedall: allNodes.every(item => item.checked)
  117. });
  118. } else {
  119. this.tree.$emit('radio-change', {
  120. checked,
  121. node: this.node,
  122. data: this.node.data,
  123. checkedall: false
  124. });
  125. }
  126. }
  127. if (!this.expanded && this.tree.expandOnCheckNode && checked) {
  128. this.handleExpandIconClick();
  129. }
  130. this.oldChecked = checked;
  131. this.indeterminate = indeterminate;
  132. },
  133. handleClick() {
  134. this.tree.store.setCurrentNode(this.node);
  135. this.tree.$emit('current-change', {
  136. node: this.node,
  137. data: this.tree.store.currentNode ? this.tree.store.currentNode.data : null,
  138. currentNode: this.tree.store.currentNode
  139. });
  140. this.tree.currentNode = this.node;
  141. if (this.tree.expandOnClickNode) {
  142. this.handleExpandIconClick();
  143. }
  144. if (this.tree.checkOnClickNode && !this.node.disabled) {
  145. (this.checkboxVisible || this.radioVisible) && this.handleCheckChange(!this.node.checked);
  146. }
  147. this.tree.$emit('node-click', this.node);
  148. },
  149. handleExpandIconClick() {
  150. if (this.node.isLeaf) return;
  151. if (this.expanded) {
  152. this.tree.$emit('node-collapse', this.node);
  153. this.node.collapse();
  154. this.iconName = 'arrow-down'
  155. } else {
  156. this.node.expand();
  157. this.iconName = 'arrow-up'
  158. // this.tree.$emit('node-expand', this.node);
  159. if (this.tree.accordion) {
  160. // uni.$emit(`${this.tree.elId}-tree-node-expand`, this.node);
  161. }
  162. }
  163. },
  164. handleCheckChange(checked) {
  165. if (this.node.disabled) return;
  166. if (this.checkboxVisible) {
  167. this.node.setChecked(checked, !(this.tree.checkStrictly || this.checkOnlyLeaf));
  168. } else {
  169. this.node.setRadioChecked(checked);
  170. }
  171. this.$nextTick(() => {
  172. this.tree.$emit('check', {
  173. node: this.node,
  174. data: this.node.data,
  175. checkedNodes: this.tree.store.getCheckedNodes(),
  176. checkedKeys: this.tree.store.getCheckedKeys(),
  177. halfCheckedNodes: this.tree.store.getHalfCheckedNodes(),
  178. halfCheckedKeys: this.tree.store.getHalfCheckedKeys()
  179. });
  180. });
  181. uni.$emit('updateKey')
  182. },
  183. handleImageError() {
  184. this.node.icon = this.tree.defaultNodeIcon;
  185. }
  186. },
  187. created() {
  188. if (!this.tree) {
  189. throw new Error('Can not find node\'s tree.');
  190. }
  191. this.node = this.tree.store.nodesMap[this.nodeId];
  192. this.highlightCurrent = this.tree.highlightCurrent;
  193. if (this.node.expanded) {
  194. this.expanded = true;
  195. this.childNodeRendered = true;
  196. }
  197. const props = this.tree.props || {};
  198. const childrenKey = props['children'] || 'children';
  199. this.$watch(`node.data.${childrenKey}`, () => {
  200. this.node.updateChildren();
  201. });
  202. if (this.tree.accordion) {
  203. uni.$on(`${this.tree.elId}-tree-node-expand`, node => {
  204. if (this.node.id !== node.id && this.node.level === node.level) {
  205. this.node.collapse();
  206. }
  207. });
  208. }
  209. },
  210. beforeDestroy() {
  211. this.$parent = null;
  212. }
  213. };
  214. </script>
  215. <style>
  216. .ly-tree-node {
  217. white-space: nowrap;
  218. outline: 0
  219. }
  220. .ly-tree-node__content {
  221. display: flex;
  222. align-items: center;
  223. height: 70rpx;
  224. margin-bottom: 20rpx;
  225. justify-content: space-between;
  226. }
  227. .ly-tree-node__label_box {
  228. display: flex;
  229. flex-direction: column;
  230. }
  231. .ly-tree-node__content.is-current {
  232. background-color: #F5F7FA;
  233. }
  234. .ly-tree-node__content>.ly-tree-node__expand-icon {
  235. padding: 12rpx;
  236. }
  237. .ly-tree-node__checkbox {
  238. display: flex;
  239. margin-right: 16rpx;
  240. width: 40rpx;
  241. height: 40rpx;
  242. }
  243. .ly-tree-node__checkbox>image {
  244. width: 40rpx;
  245. height: 40rpx;
  246. }
  247. .ly-tree-node__expand-icon {
  248. color: #C0C4CC;
  249. font-size: 28rpx;
  250. -webkit-transform: rotate(0);
  251. transform: rotate(0);
  252. -webkit-transition: -webkit-transform .3s ease-in-out;
  253. transition: -webkit-transform .3s ease-in-out;
  254. transition: transform .3s ease-in-out;
  255. transition: transform .3s ease-in-out, -webkit-transform .3s ease-in-out
  256. }
  257. .ly-tree-node__expand-icon.expanded {
  258. -webkit-transform: rotate(90deg);
  259. transform: rotate(90deg)
  260. }
  261. .ly-tree-node__expand-icon.is-leaf {
  262. color: transparent;
  263. }
  264. .ly-tree-node__label {
  265. font-size: 28rpx
  266. }
  267. .ly-tree-node__loading-icon {
  268. margin-right: 16rpx;
  269. font-size: 28rpx;
  270. color: #C0C4CC;
  271. -webkit-animation: rotating 2s linear infinite;
  272. animation: rotating 2s linear infinite
  273. }
  274. .ly-tree-node>.ly-tree-node__children {
  275. overflow: hidden;
  276. background-color: transparent
  277. }
  278. .ly-tree-node>.ly-tree-node__children.collapse-transition {
  279. transition: height .3s ease-in-out;
  280. }
  281. .ly-tree-node.is-expanded>.ly-tree-node__children {
  282. display: block
  283. }
  284. .ly-tree-node_collapse {
  285. overflow: hidden;
  286. padding-top: 0;
  287. padding-bottom: 0;
  288. }
  289. /* lyTree-end */
  290. /* iconfont-start */
  291. @font-face {
  292. font-family: "ly-iconfont";
  293. 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=') format('woff2');
  294. }
  295. .ly-iconfont {
  296. font-family: "ly-iconfont" !important;
  297. font-size: 30rpx;
  298. font-style: normal;
  299. -webkit-font-smoothing: antialiased;
  300. -moz-osx-font-smoothing: grayscale;
  301. }
  302. .ly-icon-caret-right:before {
  303. content: "\e8ee";
  304. }
  305. .ly-icon-loading:before {
  306. content: "\e657";
  307. }
  308. /* iconfont-end */
  309. /* animate-start */
  310. @keyframes rotating {
  311. 0% {
  312. -webkit-transform: rotateZ(0);
  313. transform: rotateZ(0)
  314. }
  315. 100% {
  316. -webkit-transform: rotateZ(360deg);
  317. transform: rotateZ(360deg)
  318. }
  319. }
  320. /* animate-end */
  321. </style>