Request.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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="trash-2"
  99. color="red"
  100. :label="$t('action.delete')"
  101. @click.native="
  102. confirmRemove = true
  103. $refs.options.tippy().hide()
  104. "
  105. />
  106. </tippy>
  107. </span>
  108. </div>
  109. </div>
  110. <SmartConfirmModal
  111. :show="confirmRemove"
  112. :title="$t('confirm.remove_request')"
  113. @hide-modal="confirmRemove = false"
  114. @resolve="removeRequest"
  115. />
  116. </div>
  117. </template>
  118. <script>
  119. import { defineComponent } from "@nuxtjs/composition-api"
  120. import { translateToNewRequest } from "~/helpers/types/HoppRESTRequest"
  121. import { useReadonlyStream } from "~/helpers/utils/composables"
  122. import {
  123. restSaveContext$,
  124. setRESTRequest,
  125. setRESTSaveContext,
  126. } from "~/newstore/RESTSession"
  127. export default defineComponent({
  128. props: {
  129. request: { type: Object, default: () => {} },
  130. collectionIndex: { type: Number, default: null },
  131. folderIndex: { type: Number, default: null },
  132. folderName: { type: String, default: null },
  133. // eslint-disable-next-line vue/require-default-prop
  134. requestIndex: [Number, String],
  135. doc: Boolean,
  136. saveRequest: Boolean,
  137. collectionsType: { type: Object, default: () => {} },
  138. folderPath: { type: String, default: null },
  139. picked: { type: Object, default: () => {} },
  140. },
  141. setup() {
  142. const active = useReadonlyStream(restSaveContext$, null)
  143. return {
  144. active,
  145. }
  146. },
  147. data() {
  148. return {
  149. dragging: false,
  150. requestMethodLabels: {
  151. get: "text-green-500",
  152. post: "text-yellow-500",
  153. put: "text-blue-500",
  154. delete: "text-red-500",
  155. default: "text-gray-500",
  156. },
  157. confirmRemove: false,
  158. }
  159. },
  160. computed: {
  161. isSelected() {
  162. return (
  163. this.picked &&
  164. this.picked.pickedType === "my-request" &&
  165. this.picked.folderPath === this.folderPath &&
  166. this.picked.requestIndex === this.requestIndex
  167. )
  168. },
  169. },
  170. methods: {
  171. selectRequest() {
  172. if (
  173. this.active &&
  174. this.active.originLocation === "user-collection" &&
  175. this.active.folderPath === this.folderPath &&
  176. this.active.requestIndex === this.requestIndex
  177. ) {
  178. setRESTSaveContext(null)
  179. return
  180. }
  181. if (this.$props.saveRequest)
  182. this.$emit("select", {
  183. picked: {
  184. pickedType: "my-request",
  185. collectionIndex: this.collectionIndex,
  186. folderPath: this.folderPath,
  187. folderName: this.folderName,
  188. requestIndex: this.requestIndex,
  189. },
  190. })
  191. else {
  192. setRESTRequest(translateToNewRequest(this.request), {
  193. originLocation: "user-collection",
  194. folderPath: this.folderPath,
  195. requestIndex: this.requestIndex,
  196. })
  197. }
  198. },
  199. dragStart({ dataTransfer }) {
  200. this.dragging = !this.dragging
  201. dataTransfer.setData("folderPath", this.folderPath)
  202. dataTransfer.setData("requestIndex", this.requestIndex)
  203. },
  204. removeRequest() {
  205. this.$emit("remove-request", {
  206. collectionIndex: this.$props.collectionIndex,
  207. folderName: this.$props.folderName,
  208. folderPath: this.folderPath,
  209. requestIndex: this.$props.requestIndex,
  210. })
  211. },
  212. getRequestLabelColor(method) {
  213. return (
  214. this.requestMethodLabels[method.toLowerCase()] ||
  215. this.requestMethodLabels.default
  216. )
  217. },
  218. },
  219. })
  220. </script>