Folder.vue 8.9 KB

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