NestedDraggable.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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: inline-block;
  86. position: relative;
  87. background: #fff;
  88. padding: 0 8px;
  89. cursor: pointer;
  90. .tip {
  91. display: inline-block;
  92. position: relative;
  93. > img {
  94. display: inline-block;
  95. width: 14px;
  96. height: 14px;
  97. margin-top: 5px;
  98. vertical-align: top;
  99. }
  100. }
  101. .title {
  102. display: inline-block;
  103. border-bottom: 1px solid transparent;
  104. cursor: pointer;
  105. padding: 0 3px;
  106. color: #555555;
  107. &.active {
  108. color: #0396f2;
  109. }
  110. }
  111. }
  112. .info {
  113. position: absolute;
  114. background: #fff;
  115. padding-left: 12px;
  116. color: #666;
  117. right: 3px;
  118. top: 5px;
  119. > i {
  120. padding: 0 2px;
  121. transition: all 0.2s;
  122. cursor: pointer;
  123. &:hover {
  124. transform: scale(1.2);
  125. }
  126. }
  127. }
  128. }
  129. }
  130. </style>
  131. <script>
  132. import draggable from "vuedraggable";
  133. export default {
  134. name: "NestedDraggable",
  135. props: {
  136. lists: {
  137. required: true,
  138. type: Array
  139. },
  140. isChildren: {
  141. type: Boolean,
  142. default: false,
  143. },
  144. disabled: {
  145. type: Boolean,
  146. default: false,
  147. },
  148. readonly: {
  149. type: Boolean,
  150. default: false,
  151. },
  152. activeid: {
  153. default: '',
  154. }
  155. },
  156. data() {
  157. return {
  158. listSortData: '',
  159. childrenHidden: false,
  160. }
  161. },
  162. components: {
  163. draggable
  164. },
  165. mounted() {
  166. this.listSortData = this.getSort(this.lists);
  167. },
  168. methods: {
  169. getSort(lists, parentid = 0) {
  170. let sortData = "";
  171. lists.forEach((item) => {
  172. sortData+= item.id + ":" + parentid + ";" + this.getSort(item.children, item.id);
  173. });
  174. return sortData;
  175. },
  176. handleClick(act, detail) {
  177. if (act == 'open') {
  178. if (detail.type == 'folder') {
  179. this.$set(detail, 'hiddenChildren', !detail.hiddenChildren)
  180. return;
  181. }
  182. }
  183. if (act == 'sort') {
  184. if (this.isChildren) {
  185. this.$emit("change", act, detail);
  186. } else {
  187. let tempSortData = this.getSort(this.lists);
  188. if (tempSortData != this.listSortData) {
  189. this.listSortData = tempSortData;
  190. this.$emit("change", act, tempSortData);
  191. }
  192. }
  193. return;
  194. }
  195. this.$emit("change", act, detail)
  196. }
  197. }
  198. };
  199. </script>