Parameters.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <template>
  2. <AppSection label="parameters">
  3. <div
  4. class="
  5. bg-primary
  6. border-b border-dividerLight
  7. flex flex-1
  8. top-upperSecondaryStickyFold
  9. pl-4
  10. z-10
  11. sticky
  12. items-center
  13. justify-between
  14. "
  15. >
  16. <label class="font-semibold text-secondaryLight">
  17. {{ $t("request.parameter_list") }}
  18. </label>
  19. <div class="flex">
  20. <ButtonSecondary
  21. v-tippy="{ theme: 'tooltip' }"
  22. to="https://docs.hoppscotch.io/features/parameters"
  23. blank
  24. :title="$t('app.wiki')"
  25. svg="help-circle"
  26. />
  27. <ButtonSecondary
  28. v-tippy="{ theme: 'tooltip' }"
  29. :title="$t('action.clear_all')"
  30. svg="trash-2"
  31. :disabled="bulkMode"
  32. @click.native="clearContent"
  33. />
  34. <ButtonSecondary
  35. v-tippy="{ theme: 'tooltip' }"
  36. :title="$t('state.bulk_mode')"
  37. svg="edit"
  38. :class="{ '!text-accent': bulkMode }"
  39. @click.native="bulkMode = !bulkMode"
  40. />
  41. <ButtonSecondary
  42. v-tippy="{ theme: 'tooltip' }"
  43. :title="$t('add.new')"
  44. svg="plus"
  45. :disabled="bulkMode"
  46. @click.native="addParam"
  47. />
  48. </div>
  49. </div>
  50. <div v-if="bulkMode" ref="bulkEditor"></div>
  51. <div v-else>
  52. <div
  53. v-for="(param, index) in params$"
  54. :key="`param-${index}`"
  55. class="divide-x divide-dividerLight border-b border-dividerLight flex"
  56. >
  57. <SmartEnvInput
  58. v-model="param.key"
  59. :placeholder="`${$t('count.parameter', { count: index + 1 })}`"
  60. styles="
  61. bg-transparent
  62. flex
  63. flex-1
  64. py-1
  65. px-4
  66. "
  67. @change="
  68. updateParam(index, {
  69. key: $event,
  70. value: param.value,
  71. active: param.active,
  72. })
  73. "
  74. />
  75. <SmartEnvInput
  76. v-model="param.value"
  77. :placeholder="`${$t('count.value', { count: index + 1 })}`"
  78. styles="
  79. bg-transparent
  80. flex
  81. flex-1
  82. py-1
  83. px-4
  84. "
  85. @change="
  86. updateParam(index, {
  87. key: param.key,
  88. value: $event,
  89. active: param.active,
  90. })
  91. "
  92. />
  93. <span>
  94. <ButtonSecondary
  95. v-tippy="{ theme: 'tooltip' }"
  96. :title="
  97. param.hasOwnProperty('active')
  98. ? param.active
  99. ? $t('action.turn_off')
  100. : $t('action.turn_on')
  101. : $t('action.turn_off')
  102. "
  103. :svg="
  104. param.hasOwnProperty('active')
  105. ? param.active
  106. ? 'check-circle'
  107. : 'circle'
  108. : 'check-circle'
  109. "
  110. color="green"
  111. @click.native="
  112. updateParam(index, {
  113. key: param.key,
  114. value: param.value,
  115. active: param.hasOwnProperty('active') ? !param.active : false,
  116. })
  117. "
  118. />
  119. </span>
  120. <span>
  121. <ButtonSecondary
  122. v-tippy="{ theme: 'tooltip' }"
  123. :title="$t('action.remove')"
  124. svg="trash"
  125. color="red"
  126. @click.native="deleteParam(index)"
  127. />
  128. </span>
  129. </div>
  130. <div
  131. v-if="params$.length === 0"
  132. class="
  133. flex flex-col
  134. text-secondaryLight
  135. p-4
  136. items-center
  137. justify-center
  138. "
  139. >
  140. <img
  141. :src="`/images/states/${$colorMode.value}/add_files.svg`"
  142. loading="lazy"
  143. class="
  144. flex-col
  145. my-4
  146. object-contain object-center
  147. h-16
  148. w-16
  149. inline-flex
  150. "
  151. />
  152. <span class="text-center pb-4">
  153. {{ $t("empty.parameters") }}
  154. </span>
  155. <ButtonSecondary
  156. :label="`${$t('add.new')}`"
  157. svg="plus"
  158. filled
  159. @click.native="addParam"
  160. />
  161. </div>
  162. </div>
  163. </AppSection>
  164. </template>
  165. <script setup lang="ts">
  166. import { ref, useContext, watch } from "@nuxtjs/composition-api"
  167. import { useCodemirror } from "~/helpers/editor/codemirror"
  168. import { HoppRESTParam } from "~/helpers/types/HoppRESTRequest"
  169. import { useReadonlyStream } from "~/helpers/utils/composables"
  170. import {
  171. restParams$,
  172. addRESTParam,
  173. updateRESTParam,
  174. deleteRESTParam,
  175. deleteAllRESTParams,
  176. setRESTParams,
  177. } from "~/newstore/RESTSession"
  178. import "codemirror/mode/yaml/yaml"
  179. const {
  180. $toast,
  181. app: { i18n },
  182. } = useContext()
  183. const t = i18n.t.bind(i18n)
  184. const bulkMode = ref(false)
  185. const bulkParams = ref("")
  186. watch(bulkParams, () => {
  187. try {
  188. const transformation = bulkParams.value.split("\n").map((item) => ({
  189. key: item.substring(0, item.indexOf(":")).trim().replace(/^\/\//, ""),
  190. value: item.substring(item.indexOf(":") + 1).trim(),
  191. active: !item.trim().startsWith("//"),
  192. }))
  193. setRESTParams(transformation)
  194. } catch (e) {
  195. $toast.error(`${t("error.something_went_wrong")}`, {
  196. icon: "error_outline",
  197. })
  198. console.error(e)
  199. }
  200. })
  201. const bulkEditor = ref<any | null>(null)
  202. useCodemirror(bulkEditor, bulkParams, {
  203. extendedEditorConfig: {
  204. mode: "text/x-yaml",
  205. placeholder: `${t("state.bulk_mode_placeholder")}`,
  206. },
  207. linter: null,
  208. completer: null,
  209. })
  210. const params$ = useReadonlyStream(restParams$, [])
  211. watch(
  212. params$,
  213. (newValue) => {
  214. if (
  215. (newValue[newValue.length - 1]?.key !== "" ||
  216. newValue[newValue.length - 1]?.value !== "") &&
  217. newValue.length
  218. )
  219. addParam()
  220. },
  221. { deep: true }
  222. )
  223. const addParam = () => {
  224. addRESTParam({ key: "", value: "", active: true })
  225. }
  226. const updateParam = (index: number, item: HoppRESTParam) => {
  227. updateRESTParam(index, item)
  228. }
  229. const deleteParam = (index: number) => {
  230. deleteRESTParam(index)
  231. }
  232. const clearContent = () => {
  233. deleteAllRESTParams()
  234. }
  235. </script>