ly-tree-node.vue 11 KB

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