Collection.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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-tippy="{ theme: 'tooltip' }"
  34. svg="file-plus"
  35. :title="$t('request.new')"
  36. class="hidden group-hover:inline-flex"
  37. @click.native="
  38. $emit('add-request', {
  39. path: `${collectionIndex}`,
  40. })
  41. "
  42. />
  43. <ButtonSecondary
  44. v-tippy="{ theme: 'tooltip' }"
  45. svg="folder-plus"
  46. :title="$t('folder.new')"
  47. class="hidden group-hover:inline-flex"
  48. @click.native="
  49. $emit('add-folder', {
  50. path: `${collectionIndex}`,
  51. })
  52. "
  53. />
  54. <span>
  55. <tippy
  56. ref="options"
  57. interactive
  58. trigger="click"
  59. theme="popover"
  60. arrow
  61. :on-shown="() => tippyActions.focus()"
  62. >
  63. <template #trigger>
  64. <ButtonSecondary
  65. v-tippy="{ theme: 'tooltip' }"
  66. :title="$t('action.more')"
  67. svg="more-vertical"
  68. />
  69. </template>
  70. <div
  71. ref="tippyActions"
  72. class="flex flex-col focus:outline-none"
  73. tabindex="0"
  74. role="menu"
  75. @keyup.r="requestAction.$el.click()"
  76. @keyup.n="folderAction.$el.click()"
  77. @keyup.e="edit.$el.click()"
  78. @keyup.delete="deleteAction.$el.click()"
  79. @keyup.escape="options.tippy().hide()"
  80. >
  81. <SmartItem
  82. ref="requestAction"
  83. svg="file-plus"
  84. :label="`${$t('request.new')}`"
  85. :shortcut="['R']"
  86. @click.native="
  87. () => {
  88. $emit('add-request', {
  89. path: `${collectionIndex}`,
  90. })
  91. options.tippy().hide()
  92. }
  93. "
  94. />
  95. <SmartItem
  96. ref="folderAction"
  97. svg="folder-plus"
  98. :label="`${$t('folder.new')}`"
  99. :shortcut="['N']"
  100. @click.native="
  101. () => {
  102. $emit('add-folder', {
  103. path: `${collectionIndex}`,
  104. })
  105. options.tippy().hide()
  106. }
  107. "
  108. />
  109. <SmartItem
  110. ref="edit"
  111. svg="edit"
  112. :label="`${$t('action.edit')}`"
  113. :shortcut="['E']"
  114. @click.native="
  115. () => {
  116. $emit('edit-collection')
  117. options.tippy().hide()
  118. }
  119. "
  120. />
  121. <SmartItem
  122. ref="deleteAction"
  123. svg="trash-2"
  124. :label="`${$t('action.delete')}`"
  125. :shortcut="['⌫']"
  126. @click.native="
  127. () => {
  128. confirmRemove = true
  129. options.tippy().hide()
  130. }
  131. "
  132. />
  133. </div>
  134. </tippy>
  135. </span>
  136. </div>
  137. </div>
  138. <div v-if="showChildren || isFiltered" class="flex">
  139. <div
  140. class="bg-dividerLight cursor-nsResize flex ml-5.5 transform transition w-1 hover:bg-dividerDark hover:scale-x-125"
  141. @click="toggleShowChildren()"
  142. ></div>
  143. <div class="flex flex-col flex-1 truncate">
  144. <CollectionsGraphqlFolder
  145. v-for="(folder, index) in collection.folders"
  146. :key="`folder-${String(index)}`"
  147. :picked="picked"
  148. :saving-mode="savingMode"
  149. :folder="folder"
  150. :folder-index="index"
  151. :folder-path="`${collectionIndex}/${String(index)}`"
  152. :collection-index="collectionIndex"
  153. :doc="doc"
  154. :is-filtered="isFiltered"
  155. @add-request="$emit('add-request', $event)"
  156. @add-folder="$emit('add-folder', $event)"
  157. @edit-folder="$emit('edit-folder', $event)"
  158. @edit-request="$emit('edit-request', $event)"
  159. @duplicate-request="$emit('duplicate-request', $event)"
  160. @select="$emit('select', $event)"
  161. />
  162. <CollectionsGraphqlRequest
  163. v-for="(request, index) in collection.requests"
  164. :key="`request-${String(index)}`"
  165. :picked="picked"
  166. :saving-mode="savingMode"
  167. :request="request"
  168. :collection-index="collectionIndex"
  169. :folder-index="-1"
  170. :folder-name="collection.name"
  171. :folder-path="`${collectionIndex}`"
  172. :request-index="index"
  173. :doc="doc"
  174. @edit-request="$emit('edit-request', $event)"
  175. @duplicate-request="$emit('duplicate-request', $event)"
  176. @select="$emit('select', $event)"
  177. />
  178. <div
  179. v-if="
  180. collection.folders.length === 0 && collection.requests.length === 0
  181. "
  182. class="flex flex-col items-center justify-center p-4 text-secondaryLight"
  183. >
  184. <img
  185. :src="`/images/states/${$colorMode.value}/pack.svg`"
  186. loading="lazy"
  187. class="inline-flex flex-col object-contain object-center w-16 h-16 mb-4"
  188. :alt="`${$t('empty.collection')}`"
  189. />
  190. <span class="text-center">
  191. {{ $t("empty.collection") }}
  192. </span>
  193. </div>
  194. </div>
  195. </div>
  196. <SmartConfirmModal
  197. :show="confirmRemove"
  198. :title="`${$t('confirm.remove_collection')}`"
  199. @hide-modal="confirmRemove = false"
  200. @resolve="removeCollection"
  201. />
  202. </div>
  203. </template>
  204. <script lang="ts">
  205. import { defineComponent, ref } from "@nuxtjs/composition-api"
  206. import {
  207. removeGraphqlCollection,
  208. moveGraphqlRequest,
  209. } from "~/newstore/collections"
  210. export default defineComponent({
  211. props: {
  212. picked: { type: Object, default: null },
  213. // Whether the viewing context is related to picking (activates 'select' events)
  214. savingMode: { type: Boolean, default: false },
  215. collectionIndex: { type: Number, default: null },
  216. collection: { type: Object, default: () => {} },
  217. doc: Boolean,
  218. isFiltered: Boolean,
  219. },
  220. setup() {
  221. return {
  222. tippyActions: ref<any | null>(null),
  223. options: ref<any | null>(null),
  224. requestAction: ref<any | null>(null),
  225. folderAction: ref<any | null>(null),
  226. edit: ref<any | null>(null),
  227. deleteAction: ref<any | null>(null),
  228. }
  229. },
  230. data() {
  231. return {
  232. showChildren: false,
  233. dragging: false,
  234. selectedFolder: {},
  235. confirmRemove: false,
  236. }
  237. },
  238. computed: {
  239. isSelected(): boolean {
  240. return (
  241. this.picked &&
  242. this.picked.pickedType === "gql-my-collection" &&
  243. this.picked.collectionIndex === this.collectionIndex
  244. )
  245. },
  246. getCollectionIcon() {
  247. if (this.isSelected) return "check-circle"
  248. else if (!this.showChildren && !this.isFiltered) return "folder"
  249. else if (this.showChildren || this.isFiltered) return "folder-open"
  250. else return "folder"
  251. },
  252. },
  253. methods: {
  254. pick() {
  255. this.$emit("select", {
  256. picked: {
  257. pickedType: "gql-my-collection",
  258. collectionIndex: this.collectionIndex,
  259. },
  260. })
  261. },
  262. toggleShowChildren() {
  263. if (this.savingMode) {
  264. this.pick()
  265. }
  266. this.showChildren = !this.showChildren
  267. },
  268. removeCollection() {
  269. // Cancel pick if picked collection is deleted
  270. if (
  271. this.picked &&
  272. this.picked.pickedType === "gql-my-collection" &&
  273. this.picked.collectionIndex === this.collectionIndex
  274. ) {
  275. this.$emit("select", { picked: null })
  276. }
  277. removeGraphqlCollection(this.collectionIndex)
  278. this.$toast.success(`${this.$t("state.deleted")}`)
  279. },
  280. dropEvent({ dataTransfer }: any) {
  281. this.dragging = !this.dragging
  282. const folderPath = dataTransfer.getData("folderPath")
  283. const requestIndex = dataTransfer.getData("requestIndex")
  284. moveGraphqlRequest(folderPath, requestIndex, `${this.collectionIndex}`)
  285. },
  286. },
  287. })
  288. </script>