index.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <!--
  2. TODO:
  3. - probably refactor and pass event arguments to modals directly without unpacking
  4. -->
  5. <template>
  6. <pw-section class="yellow" :label="$t('collections')" ref="collections">
  7. <add-collection :show="showModalAdd" @hide-modal="displayModalAdd(false)" />
  8. <edit-collection
  9. :show="showModalEdit"
  10. :editingCollection="editingCollection"
  11. :editingCollectionIndex="editingCollectionIndex"
  12. @hide-modal="displayModalEdit(false)"
  13. />
  14. <add-folder
  15. :show="showModalAddFolder"
  16. :collection="editingCollection"
  17. :collectionIndex="editingCollectionIndex"
  18. @hide-modal="displayModalAddFolder(false)"
  19. />
  20. <edit-folder
  21. :show="showModalEditFolder"
  22. :collection="editingCollection"
  23. :collectionIndex="editingCollectionIndex"
  24. :folder="editingFolder"
  25. :folderIndex="editingFolderIndex"
  26. @hide-modal="displayModalEditFolder(false)"
  27. />
  28. <edit-request
  29. :show="showModalEditRequest"
  30. :collectionIndex="editingCollectionIndex"
  31. :folderIndex="editingFolderIndex"
  32. :request="editingRequest"
  33. :requestIndex="editingRequestIndex"
  34. @hide-modal="displayModalEditRequest(false)"
  35. />
  36. <import-export-collections
  37. :show="showModalImportExport"
  38. @hide-modal="displayModalImportExport(false)"
  39. />
  40. <div class="row-wrapper">
  41. <div>
  42. <button class="icon" @click="displayModalAdd(true)">
  43. <i class="material-icons">add</i>
  44. <span>{{ $t("new") }}</span>
  45. </button>
  46. </div>
  47. <div>
  48. <button class="icon" @click="displayModalImportExport(true)">
  49. {{ $t("import_export") }}
  50. </button>
  51. <!-- <a
  52. href="https://github.com/hoppscotch/hoppscotch/wiki/Collections"
  53. target="_blank"
  54. rel="noopener"
  55. >
  56. <button class="icon" v-tooltip="'Wiki'">
  57. <i class="material-icons">help_outline</i>
  58. </button>
  59. </a> -->
  60. </div>
  61. </div>
  62. <p v-if="collections.length === 0" class="info">
  63. <i class="material-icons">help_outline</i> {{ $t("create_new_collection") }}
  64. </p>
  65. <div class="virtual-list">
  66. <ul class="flex-col">
  67. <li v-for="(collection, index) in collections" :key="collection.name">
  68. <collection
  69. :collection-index="index"
  70. :collection="collection"
  71. :doc="doc"
  72. @edit-collection="editCollection(collection, index)"
  73. @add-folder="addFolder(collection, index)"
  74. @edit-folder="editFolder($event)"
  75. @edit-request="editRequest($event)"
  76. @select-collection="$emit('use-collection', collection)"
  77. />
  78. </li>
  79. </ul>
  80. </div>
  81. </pw-section>
  82. </template>
  83. <style scoped lang="scss">
  84. .virtual-list {
  85. max-height: calc(100vh - 232px);
  86. }
  87. </style>
  88. <script>
  89. import { fb } from "~/helpers/fb"
  90. export default {
  91. props: {
  92. doc: Boolean,
  93. },
  94. data() {
  95. return {
  96. showModalAdd: false,
  97. showModalEdit: false,
  98. showModalImportExport: false,
  99. showModalAddFolder: false,
  100. showModalEditFolder: false,
  101. showModalEditRequest: false,
  102. editingCollection: undefined,
  103. editingCollectionIndex: undefined,
  104. editingFolder: undefined,
  105. editingFolderIndex: undefined,
  106. editingRequest: undefined,
  107. editingRequestIndex: undefined,
  108. }
  109. },
  110. computed: {
  111. collections() {
  112. return fb.currentUser !== null
  113. ? fb.currentCollections
  114. : this.$store.state.postwoman.collections
  115. },
  116. },
  117. async mounted() {
  118. this._keyListener = function (e) {
  119. if (e.key === "Escape") {
  120. e.preventDefault()
  121. this.showModalAdd = this.showModalEdit = this.showModalImportExport = this.showModalAddFolder = this.showModalEditFolder = this.showModalEditRequest = false
  122. }
  123. }
  124. document.addEventListener("keydown", this._keyListener.bind(this))
  125. },
  126. methods: {
  127. displayModalAdd(shouldDisplay) {
  128. this.showModalAdd = shouldDisplay
  129. },
  130. displayModalEdit(shouldDisplay) {
  131. this.showModalEdit = shouldDisplay
  132. if (!shouldDisplay) this.resetSelectedData()
  133. },
  134. displayModalImportExport(shouldDisplay) {
  135. this.showModalImportExport = shouldDisplay
  136. },
  137. displayModalAddFolder(shouldDisplay) {
  138. this.showModalAddFolder = shouldDisplay
  139. if (!shouldDisplay) this.resetSelectedData()
  140. },
  141. displayModalEditFolder(shouldDisplay) {
  142. this.showModalEditFolder = shouldDisplay
  143. if (!shouldDisplay) this.resetSelectedData()
  144. },
  145. displayModalEditRequest(shouldDisplay) {
  146. this.showModalEditRequest = shouldDisplay
  147. if (!shouldDisplay) this.resetSelectedData()
  148. },
  149. editCollection(collection, collectionIndex) {
  150. this.$data.editingCollection = collection
  151. this.$data.editingCollectionIndex = collectionIndex
  152. this.displayModalEdit(true)
  153. this.syncCollections()
  154. },
  155. addFolder(collection, collectionIndex) {
  156. this.$data.editingCollection = collection
  157. this.$data.editingCollectionIndex = collectionIndex
  158. this.displayModalAddFolder(true)
  159. this.syncCollections()
  160. },
  161. editFolder(payload) {
  162. const { collection, collectionIndex, folder, folderIndex } = payload
  163. this.$data.editingCollection = collection
  164. this.$data.editingCollectionIndex = collectionIndex
  165. this.$data.editingFolder = folder
  166. this.$data.editingFolderIndex = folderIndex
  167. this.displayModalEditFolder(true)
  168. this.syncCollections()
  169. },
  170. editRequest(payload) {
  171. const { request, collectionIndex, folderIndex, requestIndex } = payload
  172. this.$data.editingCollectionIndex = collectionIndex
  173. this.$data.editingFolderIndex = folderIndex
  174. this.$data.editingRequest = request
  175. this.$data.editingRequestIndex = requestIndex
  176. this.displayModalEditRequest(true)
  177. this.syncCollections()
  178. },
  179. resetSelectedData() {
  180. this.$data.editingCollection = undefined
  181. this.$data.editingCollectionIndex = undefined
  182. this.$data.editingFolder = undefined
  183. this.$data.editingFolderIndex = undefined
  184. this.$data.editingRequest = undefined
  185. this.$data.editingRequestIndex = undefined
  186. },
  187. syncCollections() {
  188. if (fb.currentUser !== null) {
  189. if (fb.currentSettings[0].value) {
  190. fb.writeCollections(JSON.parse(JSON.stringify(this.$store.state.postwoman.collections)))
  191. }
  192. }
  193. },
  194. },
  195. beforeDestroy() {
  196. document.removeEventListener("keydown", this._keyListener)
  197. },
  198. }
  199. </script>