Folder.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <template>
  2. <div class="flex flex-col" :class="[{ 'bg-primaryLight': dragging }]">
  3. <div
  4. class="flex items-center group"
  5. @dragover.prevent
  6. @drop.prevent="dropEvent"
  7. @dragover="dragging = true"
  8. @drop="dragging = false"
  9. @dragleave="dragging = false"
  10. @dragend="dragging = false"
  11. >
  12. <span
  13. class="cursor-pointer flex px-4 justify-center items-center"
  14. @click="toggleShowChildren()"
  15. >
  16. <SmartIcon
  17. class="svg-icons"
  18. :class="{ 'text-green-500': isSelected }"
  19. :name="getCollectionIcon"
  20. />
  21. </span>
  22. <span
  23. class="
  24. cursor-pointer
  25. flex flex-1
  26. min-w-0
  27. py-2
  28. pr-2
  29. transition
  30. group-hover:text-secondaryDark
  31. "
  32. @click="toggleShowChildren()"
  33. >
  34. <span class="truncate">
  35. {{ folder.name ? folder.name : folder.title }}
  36. </span>
  37. </span>
  38. <div class="flex">
  39. <ButtonSecondary
  40. v-tippy="{ theme: 'tooltip' }"
  41. svg="folder-plus"
  42. :title="$t('folder.new')"
  43. class="hidden group-hover:inline-flex"
  44. @click.native="$emit('add-folder', { folder, path: folderPath })"
  45. />
  46. <span>
  47. <tippy
  48. ref="options"
  49. interactive
  50. trigger="click"
  51. theme="popover"
  52. arrow
  53. >
  54. <template #trigger>
  55. <ButtonSecondary
  56. v-tippy="{ theme: 'tooltip' }"
  57. :title="$t('action.more')"
  58. svg="more-vertical"
  59. />
  60. </template>
  61. <SmartItem
  62. svg="folder-plus"
  63. :label="$t('folder.new')"
  64. @click.native="
  65. $emit('add-folder', { folder, path: folderPath })
  66. $refs.options.tippy().hide()
  67. "
  68. />
  69. <SmartItem
  70. svg="edit"
  71. :label="$t('action.edit')"
  72. @click.native="
  73. $emit('edit-folder', {
  74. folder,
  75. folderIndex,
  76. collectionIndex,
  77. folderPath,
  78. })
  79. $refs.options.tippy().hide()
  80. "
  81. />
  82. <SmartItem
  83. svg="trash-2"
  84. color="red"
  85. :label="$t('action.delete')"
  86. @click.native="
  87. confirmRemove = true
  88. $refs.options.tippy().hide()
  89. "
  90. />
  91. </tippy>
  92. </span>
  93. </div>
  94. </div>
  95. <div v-if="showChildren || isFiltered">
  96. <CollectionsMyFolder
  97. v-for="(subFolder, subFolderIndex) in folder.folders"
  98. :key="`subFolder-${subFolderIndex}`"
  99. class="border-l border-dividerLight ml-6"
  100. :folder="subFolder"
  101. :folder-index="subFolderIndex"
  102. :collection-index="collectionIndex"
  103. :doc="doc"
  104. :save-request="saveRequest"
  105. :collections-type="collectionsType"
  106. :folder-path="`${folderPath}/${subFolderIndex}`"
  107. :picked="picked"
  108. @add-folder="$emit('add-folder', $event)"
  109. @edit-folder="$emit('edit-folder', $event)"
  110. @edit-request="$emit('edit-request', $event)"
  111. @update-team-collections="$emit('update-team-collections')"
  112. @select="$emit('select', $event)"
  113. @remove-request="removeRequest"
  114. />
  115. <CollectionsMyRequest
  116. v-for="(request, index) in folder.requests"
  117. :key="`request-${index}`"
  118. class="border-l border-dividerLight ml-6"
  119. :request="request"
  120. :collection-index="collectionIndex"
  121. :folder-index="folderIndex"
  122. :folder-name="folder.name"
  123. :folder-path="folderPath"
  124. :request-index="index"
  125. :doc="doc"
  126. :picked="picked"
  127. :save-request="saveRequest"
  128. :collections-type="collectionsType"
  129. @edit-request="$emit('edit-request', $event)"
  130. @select="$emit('select', $event)"
  131. @remove-request="removeRequest"
  132. />
  133. <div
  134. v-if="
  135. folder.folders &&
  136. folder.folders.length === 0 &&
  137. folder.requests &&
  138. folder.requests.length === 0
  139. "
  140. class="
  141. border-l border-dividerLight
  142. flex flex-col
  143. text-secondaryLight
  144. ml-6
  145. p-4
  146. items-center
  147. justify-center
  148. "
  149. >
  150. <i class="opacity-75 pb-2 material-icons">folder_open</i>
  151. <span class="text-center">
  152. {{ $t("empty.folder") }}
  153. </span>
  154. </div>
  155. </div>
  156. <SmartConfirmModal
  157. :show="confirmRemove"
  158. :title="$t('confirm.remove_folder')"
  159. @hide-modal="confirmRemove = false"
  160. @resolve="removeFolder"
  161. />
  162. </div>
  163. </template>
  164. <script>
  165. import { defineComponent } from "@nuxtjs/composition-api"
  166. import {
  167. removeRESTFolder,
  168. removeRESTRequest,
  169. moveRESTRequest,
  170. } from "~/newstore/collections"
  171. export default defineComponent({
  172. name: "Folder",
  173. props: {
  174. folder: { type: Object, default: () => {} },
  175. folderIndex: { type: Number, default: null },
  176. collectionIndex: { type: Number, default: null },
  177. folderPath: { type: String, default: null },
  178. doc: Boolean,
  179. saveRequest: Boolean,
  180. isFiltered: Boolean,
  181. collectionsType: { type: Object, default: () => {} },
  182. picked: { type: Object, default: () => {} },
  183. },
  184. data() {
  185. return {
  186. showChildren: false,
  187. dragging: false,
  188. confirmRemove: false,
  189. prevCursor: "",
  190. cursor: "",
  191. }
  192. },
  193. computed: {
  194. isSelected() {
  195. return (
  196. this.picked &&
  197. this.picked.pickedType === "my-folder" &&
  198. this.picked.folderPath === this.folderPath
  199. )
  200. },
  201. getCollectionIcon() {
  202. if (this.isSelected) return "check-circle"
  203. else if (!this.showChildren && !this.isFiltered) return "folder"
  204. else if (this.showChildren || this.isFiltered) return "folder-minus"
  205. else return "folder"
  206. },
  207. },
  208. methods: {
  209. toggleShowChildren() {
  210. if (this.$props.saveRequest)
  211. this.$emit("select", {
  212. picked: {
  213. pickedType: "my-folder",
  214. collectionIndex: this.collectionIndex,
  215. folderName: this.folder.name,
  216. folderPath: this.folderPath,
  217. },
  218. })
  219. this.showChildren = !this.showChildren
  220. },
  221. removeFolder() {
  222. // TODO: Bubble it up ?
  223. // Cancel pick if picked folder was deleted
  224. if (
  225. this.picked &&
  226. this.picked.pickedType === "my-folder" &&
  227. this.picked.folderPath === this.folderPath
  228. ) {
  229. this.$emit("select", { picked: null })
  230. }
  231. removeRESTFolder(this.folderPath)
  232. this.$toast.success(this.$t("state.deleted"), {
  233. icon: "delete",
  234. })
  235. },
  236. dropEvent({ dataTransfer }) {
  237. this.dragging = !this.dragging
  238. const folderPath = dataTransfer.getData("folderPath")
  239. const requestIndex = dataTransfer.getData("requestIndex")
  240. moveRESTRequest(folderPath, requestIndex, this.folderPath)
  241. },
  242. removeRequest({ requestIndex }) {
  243. // TODO: Bubble it up to root ?
  244. // Cancel pick if the picked item is being deleted
  245. if (
  246. this.picked &&
  247. this.picked.pickedType === "my-request" &&
  248. this.picked.folderPath === this.folderPath &&
  249. this.picked.requestIndex === requestIndex
  250. ) {
  251. this.$emit("select", { picked: null })
  252. }
  253. removeRESTRequest(this.folderPath, requestIndex)
  254. },
  255. },
  256. })
  257. </script>