Folder.vue 11 KB

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