ly-tree-node.vue 10 KB

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