Request.vue 6.4 KB

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