ly-tree-node.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. <text class="ly-tree-node__label u-line-1">{{node.label}}</text>
  34. </view>
  35. <view v-if="!renderAfterExpand || childNodeRendered" v-show="expanded" class="ly-tree-node__children"
  36. role="group">
  37. <ly-tree-node v-for="cNodeId in node.childNodesId" :nodeId="cNodeId"
  38. :render-after-expand="renderAfterExpand" :show-checkbox="showCheckbox" :show-radio="showRadio"
  39. :check-only-leaf="checkOnlyLeaf" :key="getNodeKey(cNodeId)" :indent="indent" :icon-class="iconClass">
  40. </ly-tree-node>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. import {
  46. getNodeKey
  47. } from './tool/util.js';
  48. import lyCheckbox from './components/ly-checkbox.vue';
  49. export default {
  50. name: 'LyTreeNode',
  51. componentName: 'LyTreeNode',
  52. components: {
  53. lyCheckbox
  54. },
  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. }
  241. .ly-tree-node__content.is-current {
  242. background-color: #F5F7FA;
  243. }
  244. .ly-tree-node__content>.ly-tree-node__expand-icon {
  245. padding: 12rpx;
  246. }
  247. .ly-tree-node__checkbox {
  248. display: flex;
  249. margin-right: 16rpx;
  250. width: 40rpx;
  251. height: 40rpx;
  252. }
  253. .ly-tree-node__checkbox>image {
  254. width: 40rpx;
  255. height: 40rpx;
  256. }
  257. .ly-tree-node__expand-icon {
  258. color: #C0C4CC;
  259. font-size: 28rpx;
  260. -webkit-transform: rotate(0);
  261. transform: rotate(0);
  262. -webkit-transition: -webkit-transform .3s ease-in-out;
  263. transition: -webkit-transform .3s ease-in-out;
  264. transition: transform .3s ease-in-out;
  265. transition: transform .3s ease-in-out, -webkit-transform .3s ease-in-out
  266. }
  267. .ly-tree-node__expand-icon.expanded {
  268. -webkit-transform: rotate(90deg);
  269. transform: rotate(90deg)
  270. }
  271. .ly-tree-node__expand-icon.is-leaf {
  272. color: transparent;
  273. }
  274. .ly-tree-node__icon {
  275. width: 34rpx;
  276. /* height: 34rpx; */
  277. overflow: hidden;
  278. margin-right: 16rpx;
  279. }
  280. .ly-tree-node__label {
  281. font-size: 28rpx
  282. }
  283. .ly-tree-node__loading-icon {
  284. margin-right: 16rpx;
  285. font-size: 28rpx;
  286. color: #C0C4CC;
  287. -webkit-animation: rotating 2s linear infinite;
  288. animation: rotating 2s linear infinite
  289. }
  290. .ly-tree-node>.ly-tree-node__children {
  291. overflow: hidden;
  292. background-color: transparent
  293. }
  294. .ly-tree-node>.ly-tree-node__children.collapse-transition {
  295. transition: height .3s ease-in-out;
  296. }
  297. .ly-tree-node.is-expanded>.ly-tree-node__children {
  298. display: block
  299. }
  300. .ly-tree-node_collapse {
  301. overflow: hidden;
  302. padding-top: 0;
  303. padding-bottom: 0;
  304. }
  305. /* lyTree-end */
  306. /* iconfont-start */
  307. @font-face {
  308. font-family: "ly-iconfont";
  309. src: url('data:application/x-font-woff2;charset=utf-8;base64,d09GMgABAAAAAAPsAAsAAAAACKwAAAOeAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHEIGVgCDBgqFDIQPATYCJAMMCwgABCAFhG0HQBtfB8gekiSCdAwUAKgCFMA5Hj7H0PeTlABUr57PVyGqugqzSWJnNwWoWJ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uZXTKXKUOSmLSKEQIeYhjqKZcrcIzhMLLRrJMSrA35UF4yGMaWGhPHm733dwJq+Z/NkSJHUXemCirjgpuWrHMD1eC+mQUAAAA=') format('woff2');
  310. }
  311. .ly-iconfont {
  312. font-family: "ly-iconfont" !important;
  313. font-size: 30rpx;
  314. font-style: normal;
  315. -webkit-font-smoothing: antialiased;
  316. -moz-osx-font-smoothing: grayscale;
  317. }
  318. .ly-icon-caret-right:before {
  319. content: "\e8ee";
  320. }
  321. .ly-icon-loading:before {
  322. content: "\e657";
  323. }
  324. /* iconfont-end */
  325. /* animate-start */
  326. @keyframes rotating {
  327. 0% {
  328. -webkit-transform: rotateZ(0);
  329. transform: rotateZ(0)
  330. }
  331. 100% {
  332. -webkit-transform: rotateZ(360deg);
  333. transform: rotateZ(360deg)
  334. }
  335. }
  336. /* animate-end */
  337. </style>