ly-tree-node.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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" @click="toPhone(node.data.mobile)" @longpress="copyPhone(node.data.mobile)">{{node.data.mobile}}</view>
  23. <view class="ly-tree-node-desc-item" @click="toPhone(node.data.phone)" @longpress="copyPhone(node.data.phone)">{{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. // 打电话
  112. toPhone(phone) {
  113. uni.makePhoneCall({
  114. phoneNumber: phone //仅为示例
  115. });
  116. },
  117. // 复制号码
  118. copyPhone(phone){
  119. uni.setClipboardData({
  120. data: phone,
  121. success: function () {
  122. uni.showToast({
  123. title: '复制成功',
  124. icon:'success'
  125. });
  126. }
  127. });
  128. },
  129. getNodeKey(nodeId) {
  130. let node = this.tree.store.root.getChildNodes([nodeId])[0];
  131. return getNodeKey(this.tree.nodeKey, node.data);
  132. },
  133. handleSelectChange(checked, indeterminate) {
  134. if (this.oldChecked !== checked && this.oldIndeterminate !== indeterminate) {
  135. if (this.checkOnlyLeaf && !this.node.isLeaf) return;
  136. if (this.checkboxVisible) {
  137. const allNodes = this.tree.store._getAllNodes();
  138. this.tree.$emit('check-change', {
  139. checked,
  140. indeterminate,
  141. node: this.node,
  142. data: this.node.data,
  143. checkedall: allNodes.every(item => item.checked)
  144. });
  145. } else {
  146. this.tree.$emit('radio-change', {
  147. checked,
  148. node: this.node,
  149. data: this.node.data,
  150. checkedall: false
  151. });
  152. }
  153. }
  154. if (!this.expanded && this.tree.expandOnCheckNode && checked) {
  155. this.handleExpandIconClick();
  156. }
  157. this.oldChecked = checked;
  158. this.indeterminate = indeterminate;
  159. },
  160. handleClick() {
  161. this.tree.store.setCurrentNode(this.node);
  162. this.tree.$emit('current-change', {
  163. node: this.node,
  164. data: this.tree.store.currentNode ? this.tree.store.currentNode.data : null,
  165. currentNode: this.tree.store.currentNode
  166. });
  167. this.tree.currentNode = this.node;
  168. if (this.tree.expandOnClickNode) {
  169. this.handleExpandIconClick();
  170. }
  171. if (this.tree.checkOnClickNode && !this.node.disabled) {
  172. (this.checkboxVisible || this.radioVisible) && this.handleCheckChange(!this.node.checked);
  173. }
  174. this.tree.$emit('node-click', this.node);
  175. },
  176. handleExpandIconClick() {
  177. if (this.node.isLeaf) return;
  178. if (this.expanded) {
  179. this.tree.$emit('node-collapse', this.node);
  180. this.node.collapse();
  181. } else {
  182. this.node.expand();
  183. this.tree.$emit('node-expand', this.node);
  184. if (this.tree.accordion) {
  185. uni.$emit(`${this.tree.elId}-tree-node-expand`, this.node);
  186. }
  187. }
  188. },
  189. handleCheckChange(checked) {
  190. if (this.node.disabled) return;
  191. if (this.checkboxVisible) {
  192. this.node.setChecked(checked, !(this.tree.checkStrictly || this.checkOnlyLeaf));
  193. } else {
  194. this.node.setRadioChecked(checked);
  195. }
  196. this.$nextTick(() => {
  197. this.tree.$emit('check', {
  198. node: this.node,
  199. data: this.node.data,
  200. checkedNodes: this.tree.store.getCheckedNodes(),
  201. checkedKeys: this.tree.store.getCheckedKeys(),
  202. halfCheckedNodes: this.tree.store.getHalfCheckedNodes(),
  203. halfCheckedKeys: this.tree.store.getHalfCheckedKeys()
  204. });
  205. });
  206. }
  207. },
  208. created() {
  209. if (!this.tree) {
  210. throw new Error('Can not find node\'s tree.');
  211. }
  212. this.node = this.tree.store.nodesMap[this.nodeId];
  213. this.highlightCurrent = this.tree.highlightCurrent;
  214. if (this.node.data.icon) {
  215. this.node.icon = "../../static/img/flat-avatar.png"
  216. }
  217. if (this.node.expanded) {
  218. this.expanded = true;
  219. this.childNodeRendered = true;
  220. }
  221. const props = this.tree.props || {};
  222. const childrenKey = props['children'] || 'children';
  223. this.$watch(`node.data.${childrenKey}`, () => {
  224. this.node.updateChildren();
  225. });
  226. if (this.tree.accordion) {
  227. uni.$on(`${this.tree.elId}-tree-node-expand`, node => {
  228. if (this.node.id !== node.id && this.node.level === node.level) {
  229. this.node.collapse();
  230. }
  231. });
  232. }
  233. },
  234. beforeDestroy() {
  235. this.$parent = null;
  236. }
  237. };
  238. </script>
  239. <style>
  240. .ly-tree-node {
  241. white-space: nowrap;
  242. outline: 0
  243. }
  244. .ly-tree-node__content {
  245. display: flex;
  246. align-items: center;
  247. /* justify-content: space-between; */
  248. min-height: 120rpx;
  249. border-bottom: 1px solid #eee;
  250. }
  251. .ly-tree-node__content.is-current {
  252. background-color: #F5F7FA;
  253. }
  254. .ly-tree-node__content>.ly-tree-node__expand-icon {
  255. padding: 12rpx;
  256. }
  257. .ly-tree-node-desc {
  258. /* align-self: flex-end; */
  259. display: flex;
  260. flex-direction: column;
  261. margin-left: auto;
  262. }
  263. .ly-tree-node-desc-item {
  264. margin: 5px;
  265. font-size: 12px !important;
  266. }
  267. .ly-tree-node__checkbox {
  268. display: flex;
  269. margin-right: 16rpx;
  270. width: 40rpx;
  271. height: 40rpx;
  272. }
  273. .ly-tree-node__checkbox>image {
  274. width: 40rpx;
  275. height: 40rpx;
  276. }
  277. .ly-tree-node__expand-icon {
  278. color: #C0C4CC;
  279. font-size: 28rpx;
  280. -webkit-transform: rotate(0);
  281. transform: rotate(0);
  282. -webkit-transition: -webkit-transform .3s ease-in-out;
  283. transition: -webkit-transform .3s ease-in-out;
  284. transition: transform .3s ease-in-out;
  285. transition: transform .3s ease-in-out, -webkit-transform .3s ease-in-out
  286. }
  287. .ly-tree-node__expand-icon.expanded {
  288. -webkit-transform: rotate(90deg);
  289. transform: rotate(90deg)
  290. }
  291. .ly-tree-node__expand-icon.is-leaf {
  292. color: transparent;
  293. }
  294. .ly-tree-node__icon {
  295. width: 34rpx;
  296. height: 34rpx;
  297. overflow: hidden;
  298. margin-right: 16rpx;
  299. }
  300. .ly-tree-node__label {
  301. font-size: 28rpx
  302. }
  303. .ly-tree-node__loading-icon {
  304. margin-right: 16rpx;
  305. font-size: 28rpx;
  306. color: #C0C4CC;
  307. -webkit-animation: rotating 2s linear infinite;
  308. animation: rotating 2s linear infinite
  309. }
  310. .ly-tree-node>.ly-tree-node__children {
  311. overflow: hidden;
  312. background-color: transparent
  313. }
  314. .ly-tree-node>.ly-tree-node__children.collapse-transition {
  315. transition: height .3s ease-in-out;
  316. }
  317. .ly-tree-node.is-expanded>.ly-tree-node__children {
  318. display: block
  319. }
  320. .ly-tree-node_collapse {
  321. overflow: hidden;
  322. padding-top: 0;
  323. padding-bottom: 0;
  324. }
  325. /* lyTree-end */
  326. /* iconfont-start */
  327. @font-face {
  328. font-family: "ly-iconfont";
  329. 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');
  330. }
  331. .ly-iconfont {
  332. font-family: "ly-iconfont" !important;
  333. font-size: 30rpx;
  334. font-style: normal;
  335. -webkit-font-smoothing: antialiased;
  336. -moz-osx-font-smoothing: grayscale;
  337. }
  338. .ly-icon-caret-right:before {
  339. content: "\e8ee";
  340. }
  341. .ly-icon-loading:before {
  342. content: "\e657";
  343. }
  344. /* iconfont-end */
  345. /* animate-start */
  346. @keyframes rotating {
  347. 0% {
  348. -webkit-transform: rotateZ(0);
  349. transform: rotateZ(0)
  350. }
  351. 100% {
  352. -webkit-transform: rotateZ(360deg);
  353. transform: rotateZ(360deg)
  354. }
  355. }
  356. /* animate-end */
  357. </style>