Collection.vue 8.1 KB

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