Collection.vue 10 KB

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