ly-tree-node.vue 10 KB

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