Folder.vue 11 KB

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