Request.vue 6.3 KB

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