NestedDraggable.vue 7.1 KB

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