NestedDraggable.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <draggable tag="div"
  3. :list="lists"
  4. :group="{ name: 'docs-nested' }"
  5. :animation="150"
  6. :disabled="disabled || readonly"
  7. @sort="handleClick('sort')">
  8. <div v-for="detail in lists" :key="detail.id" class="docs-group" :class="{readonly:readonly}">
  9. <div class="item">
  10. <div class="dashed"></div>
  11. <div class="header">
  12. <div class="tip"><img :src="detail.icon"/></div>
  13. <div class="title" @click="handleClick('open', detail)">{{ detail.title }}</div>
  14. </div>
  15. <div v-if="!readonly" class="info">
  16. <Icon type="md-create" @click="handleClick('edit', detail)"/>
  17. <Icon type="md-add" @click="handleClick('add', detail)"/>
  18. <Icon type="md-trash" @click="handleClick('delete', detail)"/>
  19. </div>
  20. </div>
  21. <nested-draggable
  22. v-if="typeof detail.children === 'object' && detail.children !== null"
  23. :lists="detail.children"
  24. :isChildren="true"
  25. :disabled="disabled"
  26. :readonly="readonly"
  27. @change="handleClick"/>
  28. </div>
  29. </draggable>
  30. </template>
  31. <style lang="scss" scoped>
  32. .docs-group {
  33. cursor: move;
  34. &.readonly {
  35. cursor: default;
  36. }
  37. .docs-group {
  38. padding-left: 14px;
  39. background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAABS2lUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4KPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMxNDAgNzkuMTYwNDUxLCAyMDE3LzA1LzA2LTAxOjA4OjIxICAgICAgICAiPgogPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIi8+CiA8L3JkZjpSREY+CjwveDp4bXBtZXRhPgo8P3hwYWNrZXQgZW5kPSJyIj8+LUNEtwAAAEtJREFUSIntzzEVwAAMQkFSKfi3FKzQqQ5oJm5h5P3ZXQMYkrgwtk+OPo8kSzo7bGFcC+NaGNfCuBbGtTCuhXEtzB+SHAAGAEm/7wv2LKvDNoBjfgAAAABJRU5ErkJggg==) no-repeat -2px -9px;
  40. margin-left: 12px;
  41. border-left: 1px dotted #ddd;
  42. }
  43. .item {
  44. padding: 4px 0;
  45. background-color: #ffffff;
  46. border: solid 1px #ffffff;
  47. line-height: 24px;
  48. position: relative;
  49. .dashed {
  50. position: absolute;
  51. margin: 0;
  52. padding: 0;
  53. top: 16px;
  54. right: 0;
  55. left: 20px;
  56. height: 2px;
  57. border-width: 0 0 1px 0;
  58. border-bottom: dashed 1px #eee;
  59. }
  60. .header {
  61. display: inline-block;
  62. position: relative;
  63. background: #fff;
  64. padding: 0 8px;
  65. cursor: pointer;
  66. .tip {
  67. display: inline-block;
  68. position: relative;
  69. > img {
  70. display: inline-block;
  71. width: 14px;
  72. height: 14px;
  73. margin-top: 5px;
  74. vertical-align: top;
  75. }
  76. }
  77. .title {
  78. display: inline-block;
  79. border-bottom: 1px solid transparent;
  80. cursor: pointer;
  81. padding: 0 3px;
  82. color: #555555;
  83. }
  84. }
  85. .info {
  86. position: absolute;
  87. background: #fff;
  88. padding-left: 12px;
  89. color: #666;
  90. right: 0;
  91. top: 5px;
  92. > i {
  93. padding: 0 2px;
  94. transition: all 0.2s;
  95. cursor: pointer;
  96. &:hover {
  97. transform: scale(1.2);
  98. }
  99. }
  100. }
  101. }
  102. }
  103. </style>
  104. <script>
  105. import draggable from "vuedraggable";
  106. export default {
  107. name: "NestedDraggable",
  108. props: {
  109. lists: {
  110. required: true,
  111. type: Array
  112. },
  113. isChildren: {
  114. type: Boolean,
  115. default: false,
  116. },
  117. disabled: {
  118. type: Boolean,
  119. default: false,
  120. },
  121. readonly: {
  122. type: Boolean,
  123. default: false,
  124. }
  125. },
  126. data() {
  127. return {
  128. listSortData: '',
  129. }
  130. },
  131. components: {
  132. draggable
  133. },
  134. mounted() {
  135. this.listSortData = this.getSort(this.lists);
  136. },
  137. methods: {
  138. getSort(lists, parentid = 0) {
  139. let sortData = "";
  140. lists.forEach((item) => {
  141. sortData+= item.id + ":" + parentid + ";" + this.getSort(item.children, item.id);
  142. });
  143. return sortData;
  144. },
  145. handleClick(act, detail) {
  146. if (act == 'sort') {
  147. if (this.isChildren) {
  148. this.$emit("change", act, detail);
  149. } else {
  150. let tempSortData = this.getSort(this.lists);
  151. if (tempSortData != this.listSortData) {
  152. this.listSortData = tempSortData;
  153. this.$emit("change", act, tempSortData);
  154. }
  155. }
  156. return;
  157. }
  158. this.$emit("change", act, detail)
  159. }
  160. }
  161. };
  162. </script>