ly-tree-node.vue 11 KB

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