index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <AppSection
  3. label="collections"
  4. :class="{ 'rounded border border-divider': savingMode }"
  5. >
  6. <div
  7. class="
  8. divide-y divide-dividerLight
  9. border-b border-dividerLight
  10. flex flex-col
  11. top-sidebarPrimaryStickyFold
  12. z-10
  13. sticky
  14. "
  15. :class="{ 'bg-primary': !savingMode }"
  16. >
  17. <input
  18. v-if="showCollActions"
  19. v-model="filterText"
  20. type="search"
  21. autocomplete="off"
  22. :placeholder="$t('action.search')"
  23. class="bg-transparent flex w-full py-2 px-4"
  24. />
  25. <div class="flex flex-1 justify-between">
  26. <ButtonSecondary
  27. svg="plus"
  28. :label="$t('action.new')"
  29. class="!rounded-none"
  30. @click.native="displayModalAdd(true)"
  31. />
  32. <div class="flex">
  33. <ButtonSecondary
  34. v-tippy="{ theme: 'tooltip' }"
  35. to="https://docs.hoppscotch.io/features/collections"
  36. blank
  37. :title="$t('app.wiki')"
  38. svg="help-circle"
  39. />
  40. <ButtonSecondary
  41. v-if="showCollActions"
  42. v-tippy="{ theme: 'tooltip' }"
  43. :title="$t('modal.import_export')"
  44. svg="archive"
  45. @click.native="displayModalImportExport(true)"
  46. />
  47. </div>
  48. </div>
  49. </div>
  50. <div class="flex-col">
  51. <CollectionsGraphqlCollection
  52. v-for="(collection, index) in filteredCollections"
  53. :key="`collection-${index}`"
  54. :picked="picked"
  55. :name="collection.name"
  56. :collection-index="index"
  57. :collection="collection"
  58. :doc="doc"
  59. :is-filtered="filterText.length > 0"
  60. :saving-mode="savingMode"
  61. @edit-collection="editCollection(collection, index)"
  62. @add-folder="addFolder($event)"
  63. @edit-folder="editFolder($event)"
  64. @edit-request="editRequest($event)"
  65. @select-collection="$emit('use-collection', collection)"
  66. @select="$emit('select', $event)"
  67. />
  68. </div>
  69. <div
  70. v-if="collections.length === 0"
  71. class="flex flex-col text-secondaryLight p-4 items-center justify-center"
  72. >
  73. <span class="text-center pb-4">
  74. {{ $t("empty.collections") }}
  75. </span>
  76. <ButtonSecondary
  77. :label="$t('add.new')"
  78. filled
  79. @click.native="displayModalAdd(true)"
  80. />
  81. </div>
  82. <div
  83. v-if="!(filteredCollections.length !== 0 || collections.length === 0)"
  84. class="flex flex-col text-secondaryLight p-4 items-center justify-center"
  85. >
  86. <i class="opacity-75 pb-2 material-icons">manage_search</i>
  87. <span class="text-center">
  88. {{ $t("state.nothing_found") }} "{{ filterText }}"
  89. </span>
  90. </div>
  91. <CollectionsGraphqlAdd
  92. :show="showModalAdd"
  93. @hide-modal="displayModalAdd(false)"
  94. />
  95. <CollectionsGraphqlEdit
  96. :show="showModalEdit"
  97. :editing-collection="editingCollection"
  98. :editing-collection-index="editingCollectionIndex"
  99. @hide-modal="displayModalEdit(false)"
  100. />
  101. <CollectionsGraphqlAddFolder
  102. :show="showModalAddFolder"
  103. :folder-path="editingFolderPath"
  104. @add-folder="onAddFolder($event)"
  105. @hide-modal="displayModalAddFolder(false)"
  106. />
  107. <CollectionsGraphqlEditFolder
  108. :show="showModalEditFolder"
  109. :collection-index="editingCollectionIndex"
  110. :folder="editingFolder"
  111. :folder-index="editingFolderIndex"
  112. :folder-path="editingFolderPath"
  113. @hide-modal="displayModalEditFolder(false)"
  114. />
  115. <CollectionsGraphqlEditRequest
  116. :show="showModalEditRequest"
  117. :folder-path="editingFolderPath"
  118. :request="editingRequest"
  119. :request-index="editingRequestIndex"
  120. @hide-modal="displayModalEditRequest(false)"
  121. />
  122. <CollectionsGraphqlImportExport
  123. :show="showModalImportExport"
  124. @hide-modal="displayModalImportExport(false)"
  125. />
  126. </AppSection>
  127. </template>
  128. <script>
  129. import { defineComponent } from "@nuxtjs/composition-api"
  130. import clone from "lodash/clone"
  131. import { useReadonlyStream } from "~/helpers/utils/composables"
  132. import { graphqlCollections$, addGraphqlFolder } from "~/newstore/collections"
  133. export default defineComponent({
  134. props: {
  135. // Whether to activate the ability to pick items (activates 'select' events)
  136. savingMode: { type: Boolean, default: false },
  137. doc: { type: Boolean, default: false },
  138. picked: { type: Object, default: null },
  139. // Whether to show the 'New' and 'Import/Export' actions
  140. showCollActions: { type: Boolean, default: true },
  141. },
  142. setup() {
  143. return {
  144. collections: useReadonlyStream(graphqlCollections$, []),
  145. }
  146. },
  147. data() {
  148. return {
  149. showModalAdd: false,
  150. showModalEdit: false,
  151. showModalImportExport: false,
  152. showModalAddFolder: false,
  153. showModalEditFolder: false,
  154. showModalEditRequest: false,
  155. editingCollection: undefined,
  156. editingCollectionIndex: undefined,
  157. editingFolder: undefined,
  158. editingFolderName: undefined,
  159. editingFolderIndex: undefined,
  160. editingFolderPath: undefined,
  161. editingRequest: undefined,
  162. editingRequestIndex: undefined,
  163. filterText: "",
  164. }
  165. },
  166. computed: {
  167. filteredCollections() {
  168. const collections = clone(this.collections)
  169. if (!this.filterText) return collections
  170. const filterText = this.filterText.toLowerCase()
  171. const filteredCollections = []
  172. for (const collection of collections) {
  173. const filteredRequests = []
  174. const filteredFolders = []
  175. for (const request of collection.requests) {
  176. if (request.name.toLowerCase().includes(filterText))
  177. filteredRequests.push(request)
  178. }
  179. for (const folder of collection.folders) {
  180. const filteredFolderRequests = []
  181. for (const request of folder.requests) {
  182. if (request.name.toLowerCase().includes(filterText))
  183. filteredFolderRequests.push(request)
  184. }
  185. if (filteredFolderRequests.length > 0) {
  186. const filteredFolder = Object.assign({}, folder)
  187. filteredFolder.requests = filteredFolderRequests
  188. filteredFolders.push(filteredFolder)
  189. }
  190. }
  191. if (filteredRequests.length + filteredFolders.length > 0) {
  192. const filteredCollection = Object.assign({}, collection)
  193. filteredCollection.requests = filteredRequests
  194. filteredCollection.folders = filteredFolders
  195. filteredCollections.push(filteredCollection)
  196. }
  197. }
  198. return filteredCollections
  199. },
  200. },
  201. methods: {
  202. displayModalAdd(shouldDisplay) {
  203. this.showModalAdd = shouldDisplay
  204. },
  205. displayModalEdit(shouldDisplay) {
  206. this.showModalEdit = shouldDisplay
  207. if (!shouldDisplay) this.resetSelectedData()
  208. },
  209. displayModalImportExport(shouldDisplay) {
  210. this.showModalImportExport = shouldDisplay
  211. },
  212. displayModalAddFolder(shouldDisplay) {
  213. this.showModalAddFolder = shouldDisplay
  214. if (!shouldDisplay) this.resetSelectedData()
  215. },
  216. displayModalEditFolder(shouldDisplay) {
  217. this.showModalEditFolder = shouldDisplay
  218. if (!shouldDisplay) this.resetSelectedData()
  219. },
  220. displayModalEditRequest(shouldDisplay) {
  221. this.showModalEditRequest = shouldDisplay
  222. if (!shouldDisplay) this.resetSelectedData()
  223. },
  224. editCollection(collection, collectionIndex) {
  225. this.$data.editingCollection = collection
  226. this.$data.editingCollectionIndex = collectionIndex
  227. this.displayModalEdit(true)
  228. },
  229. onAddFolder({ name, path }) {
  230. addGraphqlFolder(name, path)
  231. this.displayModalAddFolder(false)
  232. },
  233. addFolder(payload) {
  234. const { path } = payload
  235. this.$data.editingFolderPath = path
  236. this.displayModalAddFolder(true)
  237. },
  238. editFolder(payload) {
  239. const { folder, folderPath } = payload
  240. this.editingFolder = folder
  241. this.editingFolderPath = folderPath
  242. this.displayModalEditFolder(true)
  243. },
  244. editRequest(payload) {
  245. const {
  246. collectionIndex,
  247. folderIndex,
  248. folderName,
  249. request,
  250. requestIndex,
  251. folderPath,
  252. } = payload
  253. this.$data.editingFolderPath = folderPath
  254. this.$data.editingCollectionIndex = collectionIndex
  255. this.$data.editingFolderIndex = folderIndex
  256. this.$data.editingFolderName = folderName
  257. this.$data.editingRequest = request
  258. this.$data.editingRequestIndex = requestIndex
  259. this.displayModalEditRequest(true)
  260. },
  261. resetSelectedData() {
  262. this.$data.editingCollection = undefined
  263. this.$data.editingCollectionIndex = undefined
  264. this.$data.editingFolder = undefined
  265. this.$data.editingFolderIndex = undefined
  266. this.$data.editingRequest = undefined
  267. this.$data.editingRequestIndex = undefined
  268. },
  269. },
  270. })
  271. </script>