Collection.vue 12 KB

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