Folder.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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="file-plus"
  35. :title="t('request.new')"
  36. class="hidden group-hover:inline-flex"
  37. @click.native="$emit('add-request', { path: folderPath })"
  38. />
  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. :on-shown="() => tippyActions.focus()"
  54. >
  55. <template #trigger>
  56. <ButtonSecondary
  57. v-tippy="{ theme: 'tooltip' }"
  58. :title="t('action.more')"
  59. svg="more-vertical"
  60. />
  61. </template>
  62. <div
  63. ref="tippyActions"
  64. class="flex flex-col focus:outline-none"
  65. tabindex="0"
  66. role="menu"
  67. @keyup.r="requestAction.$el.click()"
  68. @keyup.n="folderAction.$el.click()"
  69. @keyup.e="edit.$el.click()"
  70. @keyup.delete="deleteAction.$el.click()"
  71. @keyup.x="exportAction.$el.click()"
  72. @keyup.escape="options.tippy().hide()"
  73. >
  74. <SmartItem
  75. ref="requestAction"
  76. svg="file-plus"
  77. :label="$t('request.new')"
  78. :shortcut="['R']"
  79. @click.native="
  80. () => {
  81. $emit('add-request', { path: folderPath })
  82. options.tippy().hide()
  83. }
  84. "
  85. />
  86. <SmartItem
  87. ref="folderAction"
  88. svg="folder-plus"
  89. :label="t('folder.new')"
  90. :shortcut="['N']"
  91. @click.native="
  92. () => {
  93. $emit('add-folder', { folder, path: folderPath })
  94. options.tippy().hide()
  95. }
  96. "
  97. />
  98. <SmartItem
  99. ref="edit"
  100. svg="edit"
  101. :label="t('action.edit')"
  102. :shortcut="['E']"
  103. @click.native="
  104. () => {
  105. $emit('edit-folder', {
  106. folder,
  107. folderIndex,
  108. collectionIndex,
  109. folderPath,
  110. })
  111. options.tippy().hide()
  112. }
  113. "
  114. />
  115. <SmartItem
  116. ref="exportAction"
  117. svg="download"
  118. :label="$t('export.title')"
  119. :shortcut="['X']"
  120. @click.native="
  121. () => {
  122. exportFolder()
  123. options.tippy().hide()
  124. }
  125. "
  126. />
  127. <SmartItem
  128. ref="deleteAction"
  129. svg="trash-2"
  130. :label="t('action.delete')"
  131. :shortcut="['⌫']"
  132. @click.native="
  133. () => {
  134. removeFolder()
  135. options.tippy().hide()
  136. }
  137. "
  138. />
  139. </div>
  140. </tippy>
  141. </span>
  142. </div>
  143. </div>
  144. <div v-if="showChildren || isFiltered" class="flex">
  145. <div
  146. class="bg-dividerLight cursor-nsResize flex ml-5.5 transform transition w-1 hover:bg-dividerDark hover:scale-x-125"
  147. @click="toggleShowChildren()"
  148. ></div>
  149. <div class="flex flex-col flex-1 truncate">
  150. <CollectionsMyFolder
  151. v-for="(subFolder, subFolderIndex) in folder.folders"
  152. :key="`subFolder-${subFolderIndex}`"
  153. :folder="subFolder"
  154. :folder-index="subFolderIndex"
  155. :collection-index="collectionIndex"
  156. :doc="doc"
  157. :save-request="saveRequest"
  158. :collections-type="collectionsType"
  159. :folder-path="`${folderPath}/${subFolderIndex}`"
  160. :picked="picked"
  161. @add-request="$emit('add-request', $event)"
  162. @add-folder="$emit('add-folder', $event)"
  163. @edit-folder="$emit('edit-folder', $event)"
  164. @edit-request="$emit('edit-request', $event)"
  165. @duplicate-request="$emit('duplicate-request', $event)"
  166. @update-team-collections="$emit('update-team-collections')"
  167. @select="$emit('select', $event)"
  168. @remove-request="$emit('remove-request', $event)"
  169. @remove-folder="$emit('remove-folder', $event)"
  170. />
  171. <CollectionsMyRequest
  172. v-for="(request, index) in folder.requests"
  173. :key="`request-${index}`"
  174. :request="request"
  175. :collection-index="collectionIndex"
  176. :folder-index="folderIndex"
  177. :folder-name="folder.name"
  178. :folder-path="folderPath"
  179. :request-index="index"
  180. :doc="doc"
  181. :picked="picked"
  182. :save-request="saveRequest"
  183. :collections-type="collectionsType"
  184. @edit-request="$emit('edit-request', $event)"
  185. @duplicate-request="$emit('duplicate-request', $event)"
  186. @select="$emit('select', $event)"
  187. @remove-request="$emit('remove-request', $event)"
  188. />
  189. <div
  190. v-if="
  191. folder.folders &&
  192. folder.folders.length === 0 &&
  193. folder.requests &&
  194. folder.requests.length === 0
  195. "
  196. class="flex flex-col items-center justify-center p-4 text-secondaryLight"
  197. >
  198. <img
  199. :src="`/images/states/${$colorMode.value}/pack.svg`"
  200. loading="lazy"
  201. class="inline-flex flex-col object-contain object-center w-16 h-16 mb-4"
  202. :alt="`${t('empty.folder')}`"
  203. />
  204. <span class="text-center">
  205. {{ t("empty.folder") }}
  206. </span>
  207. </div>
  208. </div>
  209. </div>
  210. </div>
  211. </template>
  212. <script lang="ts">
  213. import { defineComponent, ref } from "@nuxtjs/composition-api"
  214. import { useI18n } from "~/helpers/utils/composables"
  215. import { moveRESTRequest } from "~/newstore/collections"
  216. export default defineComponent({
  217. name: "Folder",
  218. props: {
  219. folder: { type: Object, default: () => {} },
  220. folderIndex: { type: Number, default: null },
  221. collectionIndex: { type: Number, default: null },
  222. folderPath: { type: String, default: null },
  223. doc: Boolean,
  224. saveRequest: Boolean,
  225. isFiltered: Boolean,
  226. collectionsType: { type: Object, default: () => {} },
  227. picked: { type: Object, default: () => {} },
  228. },
  229. setup() {
  230. const t = useI18n()
  231. return {
  232. tippyActions: ref<any | null>(null),
  233. options: ref<any | null>(null),
  234. requestAction: ref<any | null>(null),
  235. folderAction: ref<any | null>(null),
  236. edit: ref<any | null>(null),
  237. deleteAction: ref<any | null>(null),
  238. exportAction: ref<any | null>(null),
  239. t,
  240. }
  241. },
  242. data() {
  243. return {
  244. showChildren: false,
  245. dragging: false,
  246. prevCursor: "",
  247. cursor: "",
  248. }
  249. },
  250. computed: {
  251. isSelected(): boolean {
  252. return (
  253. this.picked &&
  254. this.picked.pickedType === "my-folder" &&
  255. this.picked.folderPath === this.folderPath
  256. )
  257. },
  258. getCollectionIcon() {
  259. if (this.isSelected) return "check-circle"
  260. else if (!this.showChildren && !this.isFiltered) return "folder"
  261. else if (this.showChildren || this.isFiltered) return "folder-open"
  262. else return "folder"
  263. },
  264. },
  265. methods: {
  266. exportFolder() {
  267. const folderJSON = JSON.stringify(this.folder)
  268. const file = new Blob([folderJSON], { type: "application/json" })
  269. const a = document.createElement("a")
  270. const url = URL.createObjectURL(file)
  271. a.href = url
  272. a.download = `${this.folder.name}.json`
  273. document.body.appendChild(a)
  274. a.click()
  275. this.$toast.success(this.$t("state.download_started").toString())
  276. setTimeout(() => {
  277. document.body.removeChild(a)
  278. URL.revokeObjectURL(url)
  279. }, 1000)
  280. },
  281. toggleShowChildren() {
  282. if (this.$props.saveRequest)
  283. this.$emit("select", {
  284. picked: {
  285. pickedType: "my-folder",
  286. collectionIndex: this.collectionIndex,
  287. folderName: this.folder.name,
  288. folderPath: this.folderPath,
  289. },
  290. })
  291. this.showChildren = !this.showChildren
  292. },
  293. removeFolder() {
  294. this.$emit("remove-folder", {
  295. folder: this.folder,
  296. folderPath: this.folderPath,
  297. })
  298. },
  299. dropEvent({ dataTransfer }) {
  300. this.dragging = !this.dragging
  301. const folderPath = dataTransfer.getData("folderPath")
  302. const requestIndex = dataTransfer.getData("requestIndex")
  303. moveRESTRequest(folderPath, requestIndex, this.folderPath)
  304. },
  305. },
  306. })
  307. </script>