Folder.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <template>
  2. <div class="flex flex-col" :class="[{ 'bg-primaryLight': dragging }]">
  3. <div
  4. class="flex items-stretch 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. @contextmenu.prevent="options.tippy().show()"
  12. >
  13. <span
  14. class="flex items-center justify-center px-4 cursor-pointer"
  15. @click="toggleShowChildren()"
  16. >
  17. <SmartIcon
  18. class="svg-icons"
  19. :class="{ 'text-accent': isSelected }"
  20. :name="getCollectionIcon"
  21. />
  22. </span>
  23. <span
  24. class="flex flex-1 min-w-0 py-2 pr-2 cursor-pointer transition group-hover:text-secondaryDark"
  25. @click="toggleShowChildren()"
  26. >
  27. <span class="truncate" :class="{ 'text-accent': isSelected }">
  28. {{ folder.name ? folder.name : folder.title }}
  29. </span>
  30. </span>
  31. <div class="flex">
  32. <ButtonSecondary
  33. v-tippy="{ theme: 'tooltip' }"
  34. svg="folder-plus"
  35. :title="$t('folder.new')"
  36. class="hidden group-hover:inline-flex"
  37. @click.native="$emit('add-folder', { folder, path: folderPath })"
  38. />
  39. <span>
  40. <tippy
  41. ref="options"
  42. interactive
  43. trigger="click"
  44. theme="popover"
  45. arrow
  46. :on-shown="() => tippyActions.focus()"
  47. >
  48. <template #trigger>
  49. <ButtonSecondary
  50. v-tippy="{ theme: 'tooltip' }"
  51. :title="$t('action.more')"
  52. svg="more-vertical"
  53. />
  54. </template>
  55. <div
  56. ref="tippyActions"
  57. class="flex flex-col focus:outline-none"
  58. tabindex="0"
  59. role="menu"
  60. @keyup.n="folderAction.$el.click()"
  61. @keyup.e="edit.$el.click()"
  62. @keyup.delete="deleteAction.$el.click()"
  63. @keyup.escape="options.tippy().hide()"
  64. >
  65. <SmartItem
  66. ref="folderAction"
  67. svg="folder-plus"
  68. :label="`${$t('folder.new')}`"
  69. :shortcut="['N']"
  70. @click.native="
  71. () => {
  72. $emit('add-folder', { folder, path: folderPath })
  73. options.tippy().hide()
  74. }
  75. "
  76. />
  77. <SmartItem
  78. ref="edit"
  79. svg="edit"
  80. :label="`${$t('action.edit')}`"
  81. :shortcut="['E']"
  82. @click.native="
  83. () => {
  84. $emit('edit-folder', { folder, folderPath })
  85. options.tippy().hide()
  86. }
  87. "
  88. />
  89. <SmartItem
  90. ref="deleteAction"
  91. svg="trash-2"
  92. :label="`${$t('action.delete')}`"
  93. :shortcut="['⌫']"
  94. @click.native="
  95. () => {
  96. confirmRemove = true
  97. options.tippy().hide()
  98. }
  99. "
  100. />
  101. </div>
  102. </tippy>
  103. </span>
  104. </div>
  105. </div>
  106. <div v-if="showChildren || isFiltered" class="flex">
  107. <div
  108. class="bg-dividerLight cursor-nsResize flex ml-5.5 transform transition w-1 hover:bg-dividerDark hover:scale-x-125"
  109. @click="toggleShowChildren()"
  110. ></div>
  111. <div class="flex flex-col flex-1 truncate">
  112. <CollectionsGraphqlFolder
  113. v-for="(subFolder, subFolderIndex) in folder.folders"
  114. :key="`subFolder-${String(subFolderIndex)}`"
  115. :picked="picked"
  116. :saving-mode="savingMode"
  117. :folder="subFolder"
  118. :folder-index="subFolderIndex"
  119. :folder-path="`${folderPath}/${String(subFolderIndex)}`"
  120. :collection-index="collectionIndex"
  121. :doc="doc"
  122. :is-filtered="isFiltered"
  123. @add-folder="$emit('add-folder', $event)"
  124. @edit-folder="$emit('edit-folder', $event)"
  125. @edit-request="$emit('edit-request', $event)"
  126. @duplicate-request="$emit('duplicate-request', $event)"
  127. @select="$emit('select', $event)"
  128. />
  129. <CollectionsGraphqlRequest
  130. v-for="(request, index) in folder.requests"
  131. :key="`request-${String(index)}`"
  132. :picked="picked"
  133. :saving-mode="savingMode"
  134. :request="request"
  135. :collection-index="collectionIndex"
  136. :folder-index="folderIndex"
  137. :folder-path="folderPath"
  138. :folder-name="folder.name"
  139. :request-index="index"
  140. :doc="doc"
  141. @edit-request="$emit('edit-request', $event)"
  142. @duplicate-request="$emit('duplicate-request', $event)"
  143. @select="$emit('select', $event)"
  144. />
  145. <div
  146. v-if="
  147. folder.folders &&
  148. folder.folders.length === 0 &&
  149. folder.requests &&
  150. folder.requests.length === 0
  151. "
  152. class="flex flex-col items-center justify-center p-4 text-secondaryLight"
  153. >
  154. <img
  155. :src="`/images/states/${$colorMode.value}/pack.svg`"
  156. loading="lazy"
  157. class="inline-flex flex-col object-contain object-center w-16 h-16 mb-4"
  158. :alt="`${$t('empty.folder')}`"
  159. />
  160. <span class="text-center">
  161. {{ $t("empty.folder") }}
  162. </span>
  163. </div>
  164. </div>
  165. </div>
  166. <SmartConfirmModal
  167. :show="confirmRemove"
  168. :title="`${$t('confirm.remove_folder')}`"
  169. @hide-modal="confirmRemove = false"
  170. @resolve="removeFolder"
  171. />
  172. </div>
  173. </template>
  174. <script lang="ts">
  175. import { defineComponent, ref } from "@nuxtjs/composition-api"
  176. import { removeGraphqlFolder, moveGraphqlRequest } from "~/newstore/collections"
  177. export default defineComponent({
  178. name: "Folder",
  179. props: {
  180. picked: { type: Object, default: null },
  181. // Whether the request is in a selectable mode (activates 'select' event)
  182. savingMode: { type: Boolean, default: false },
  183. folder: { type: Object, default: () => {} },
  184. folderIndex: { type: Number, default: null },
  185. collectionIndex: { type: Number, default: null },
  186. folderPath: { type: String, default: null },
  187. doc: Boolean,
  188. isFiltered: Boolean,
  189. },
  190. setup() {
  191. return {
  192. tippyActions: ref<any | null>(null),
  193. options: ref<any | null>(null),
  194. folderAction: ref<any | null>(null),
  195. edit: ref<any | null>(null),
  196. deleteAction: ref<any | null>(null),
  197. }
  198. },
  199. data() {
  200. return {
  201. showChildren: false,
  202. dragging: false,
  203. confirmRemove: false,
  204. }
  205. },
  206. computed: {
  207. isSelected(): boolean {
  208. return (
  209. this.picked &&
  210. this.picked.pickedType === "gql-my-folder" &&
  211. this.picked.folderPath === this.folderPath
  212. )
  213. },
  214. getCollectionIcon() {
  215. if (this.isSelected) return "check-circle"
  216. else if (!this.showChildren && !this.isFiltered) return "folder"
  217. else if (this.showChildren || this.isFiltered) return "folder-open"
  218. else return "folder"
  219. },
  220. },
  221. methods: {
  222. pick() {
  223. this.$emit("select", {
  224. picked: {
  225. pickedType: "gql-my-folder",
  226. folderPath: this.folderPath,
  227. },
  228. })
  229. },
  230. toggleShowChildren() {
  231. if (this.savingMode) {
  232. this.pick()
  233. }
  234. this.showChildren = !this.showChildren
  235. },
  236. removeFolder() {
  237. // Cancel pick if the picked folder is deleted
  238. if (
  239. this.picked &&
  240. this.picked.pickedType === "gql-my-folder" &&
  241. this.picked.folderPath === this.folderPath
  242. ) {
  243. this.$emit("select", { picked: null })
  244. }
  245. removeGraphqlFolder(this.folderPath)
  246. this.$toast.success(`${this.$t("state.deleted")}`)
  247. },
  248. dropEvent({ dataTransfer }: any) {
  249. this.dragging = !this.dragging
  250. const folderPath = dataTransfer.getData("folderPath")
  251. const requestIndex = dataTransfer.getData("requestIndex")
  252. moveGraphqlRequest(folderPath, requestIndex, this.folderPath)
  253. },
  254. },
  255. })
  256. </script>