Request.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <div class="flex flex-col" :class="[{ 'bg-primaryLight': dragging }]">
  3. <div
  4. class="flex items-stretch group"
  5. draggable="true"
  6. @dragstart="dragStart"
  7. @dragover.stop
  8. @dragleave="dragging = false"
  9. @dragend="dragging = false"
  10. @contextmenu.prevent="$refs.options.tippy().show()"
  11. >
  12. <span
  13. class="cursor-pointer flex px-2 w-16 items-center justify-center truncate"
  14. :class="getRequestLabelColor(request.method)"
  15. @click="!doc ? selectRequest() : {}"
  16. >
  17. <SmartIcon
  18. v-if="isSelected"
  19. class="svg-icons"
  20. :class="{ 'text-green-500': isSelected }"
  21. name="check-circle"
  22. />
  23. <span v-else class="truncate">
  24. {{ request.method }}
  25. </span>
  26. </span>
  27. <span
  28. class="cursor-pointer flex flex-1 min-w-0 py-2 pr-2 transition items-center group-hover:text-secondaryDark"
  29. @click="!doc ? selectRequest() : {}"
  30. >
  31. <span class="truncate"> {{ request.name }} </span>
  32. <span
  33. v-if="
  34. active &&
  35. active.originLocation === 'user-collection' &&
  36. active.folderPath === folderPath &&
  37. active.requestIndex === requestIndex
  38. "
  39. class="rounded-full bg-green-500 flex-shrink-0 h-1.5 mx-3 w-1.5"
  40. ></span>
  41. </span>
  42. <div class="flex">
  43. <ButtonSecondary
  44. v-if="!saveRequest && !doc"
  45. v-tippy="{ theme: 'tooltip' }"
  46. svg="rotate-ccw"
  47. :title="$t('action.restore')"
  48. class="hidden group-hover:inline-flex"
  49. @click.native="!doc ? selectRequest() : {}"
  50. />
  51. <span>
  52. <tippy
  53. ref="options"
  54. interactive
  55. trigger="click"
  56. theme="popover"
  57. arrow
  58. >
  59. <template #trigger>
  60. <ButtonSecondary
  61. v-tippy="{ theme: 'tooltip' }"
  62. :title="$t('action.more')"
  63. svg="more-vertical"
  64. />
  65. </template>
  66. <SmartItem
  67. svg="edit"
  68. :label="$t('action.edit')"
  69. @click.native="
  70. $emit('edit-request', {
  71. collectionIndex,
  72. folderIndex,
  73. folderName,
  74. request,
  75. requestIndex,
  76. folderPath,
  77. })
  78. $refs.options.tippy().hide()
  79. "
  80. />
  81. <SmartItem
  82. svg="copy"
  83. :label="$t('action.duplicate')"
  84. @click.native="
  85. $emit('duplicate-request', {
  86. collectionIndex,
  87. folderIndex,
  88. folderName,
  89. request,
  90. requestIndex,
  91. folderPath,
  92. })
  93. $refs.options.tippy().hide()
  94. "
  95. />
  96. <SmartItem
  97. svg="trash-2"
  98. color="red"
  99. :label="$t('action.delete')"
  100. @click.native="
  101. confirmRemove = true
  102. $refs.options.tippy().hide()
  103. "
  104. />
  105. </tippy>
  106. </span>
  107. </div>
  108. </div>
  109. <SmartConfirmModal
  110. :show="confirmRemove"
  111. :title="$t('confirm.remove_request')"
  112. @hide-modal="confirmRemove = false"
  113. @resolve="removeRequest"
  114. />
  115. </div>
  116. </template>
  117. <script>
  118. import { defineComponent } from "@nuxtjs/composition-api"
  119. import { translateToNewRequest } from "@hoppscotch/data"
  120. import { useReadonlyStream } from "~/helpers/utils/composables"
  121. import {
  122. restSaveContext$,
  123. setRESTRequest,
  124. setRESTSaveContext,
  125. } from "~/newstore/RESTSession"
  126. export default defineComponent({
  127. props: {
  128. request: { type: Object, default: () => {} },
  129. collectionIndex: { type: Number, default: null },
  130. folderIndex: { type: Number, default: null },
  131. folderName: { type: String, default: null },
  132. // eslint-disable-next-line vue/require-default-prop
  133. requestIndex: [Number, String],
  134. doc: Boolean,
  135. saveRequest: Boolean,
  136. collectionsType: { type: Object, default: () => {} },
  137. folderPath: { type: String, default: null },
  138. picked: { type: Object, default: () => {} },
  139. },
  140. setup() {
  141. const active = useReadonlyStream(restSaveContext$, null)
  142. return {
  143. active,
  144. }
  145. },
  146. data() {
  147. return {
  148. dragging: false,
  149. requestMethodLabels: {
  150. get: "text-green-500",
  151. post: "text-yellow-500",
  152. put: "text-blue-500",
  153. delete: "text-red-500",
  154. default: "text-gray-500",
  155. },
  156. confirmRemove: false,
  157. }
  158. },
  159. computed: {
  160. isSelected() {
  161. return (
  162. this.picked &&
  163. this.picked.pickedType === "my-request" &&
  164. this.picked.folderPath === this.folderPath &&
  165. this.picked.requestIndex === this.requestIndex
  166. )
  167. },
  168. },
  169. methods: {
  170. selectRequest() {
  171. if (
  172. this.active &&
  173. this.active.originLocation === "user-collection" &&
  174. this.active.folderPath === this.folderPath &&
  175. this.active.requestIndex === this.requestIndex
  176. ) {
  177. setRESTSaveContext(null)
  178. return
  179. }
  180. if (this.$props.saveRequest)
  181. this.$emit("select", {
  182. picked: {
  183. pickedType: "my-request",
  184. collectionIndex: this.collectionIndex,
  185. folderPath: this.folderPath,
  186. folderName: this.folderName,
  187. requestIndex: this.requestIndex,
  188. },
  189. })
  190. else {
  191. setRESTRequest(translateToNewRequest(this.request), {
  192. originLocation: "user-collection",
  193. folderPath: this.folderPath,
  194. requestIndex: this.requestIndex,
  195. })
  196. }
  197. },
  198. dragStart({ dataTransfer }) {
  199. this.dragging = !this.dragging
  200. dataTransfer.setData("folderPath", this.folderPath)
  201. dataTransfer.setData("requestIndex", this.requestIndex)
  202. },
  203. removeRequest() {
  204. this.$emit("remove-request", {
  205. collectionIndex: this.$props.collectionIndex,
  206. folderName: this.$props.folderName,
  207. folderPath: this.folderPath,
  208. requestIndex: this.$props.requestIndex,
  209. })
  210. },
  211. getRequestLabelColor(method) {
  212. return (
  213. this.requestMethodLabels[method.toLowerCase()] ||
  214. this.requestMethodLabels.default
  215. )
  216. },
  217. },
  218. })
  219. </script>