index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <div>
  3. <slot></slot>
  4. </div>
  5. </template>
  6. <script>
  7. import './style.css'
  8. import ExperimentBase from '@/mixins/ExperimentBase'
  9. import { mapGetters } from 'vuex'
  10. import { API_ROUTES, findNearestUpper, findNearestLower } from '@/functions'
  11. export default {
  12. name: 'ExperimentBaseExtracts',
  13. mixins: [ExperimentBase],
  14. data() {
  15. return {
  16. // Updated when `setConfig` is called
  17. extractConfig: {
  18. x: 4,
  19. y: 4
  20. },
  21. extracts: []
  22. }
  23. },
  24. computed: {
  25. ...mapGetters(['getHostURI'])
  26. },
  27. methods: {
  28. // Load extracts from the API
  29. async getExtracts(quality = 'min') {
  30. const URI = `${this.getHostURI}${API_ROUTES.getImageExtracts(this.sceneName, quality, this.extractConfig.x, this.extractConfig.y)}`
  31. const { data } = await fetch(URI)
  32. .then(async res => {
  33. res.json = await res.json()
  34. return res
  35. })
  36. .then(res => {
  37. if (!res.ok) throw new Error(res.json.message + res.json.data ? `\n${res.json.data}` : '')
  38. return res.json
  39. })
  40. return data
  41. },
  42. // Config was updated, load extracts and save progression
  43. async setConfig(config, configuratorRef) {
  44. if (!config || !configuratorRef) return
  45. this.loadingMessage = 'Loading configuration extracts...'
  46. this.loadingErrorMessage = null
  47. try {
  48. this.extractConfig.x = config.x
  49. this.extractConfig.y = config.y
  50. const data = await this.getExtracts()
  51. const hostURI = this.getHostURI
  52. this.extracts = data.extracts.map((url, i) => ({
  53. link: hostURI + url,
  54. quality: data.info.image.quality,
  55. zone: i + 1,
  56. index: i,
  57. nextQuality: findNearestUpper(data.info.image.quality, this.qualities),
  58. precQuality: findNearestLower(data.info.image.quality, this.qualities),
  59. loading: false
  60. }))
  61. configuratorRef.setVisibility(false)
  62. }
  63. catch (err) {
  64. console.error('Failed to load new configuration', err)
  65. this.loadingErrorMessage = 'Failed to load new configuration. ' + err.message
  66. }
  67. finally {
  68. this.loadingMessage = null
  69. this.saveProgress()
  70. }
  71. },
  72. // An action was triggered, load extracts and save progression
  73. async extractAction(event, extractObj) {
  74. console.log(event, extractObj)
  75. const { index, nextQuality, precQuality, quality } = extractObj
  76. let newQuality
  77. if (event.button === 0) newQuality = precQuality // Left click
  78. if (event.button === 2) newQuality = nextQuality // Right click
  79. // Do not load a new extract if same quality
  80. if (newQuality === quality) return
  81. // Set loading state
  82. this.extracts[index].loading = true
  83. try {
  84. // Loading new extract
  85. const data = await this.getExtracts(newQuality)
  86. this.extracts[index].link = this.getHostURI + data.extracts[index]
  87. this.extracts[index].quality = data.info.image.quality
  88. this.extracts[index].nextQuality = findNearestUpper(data.info.image.quality, this.qualities)
  89. this.extracts[index].precQuality = findNearestLower(data.info.image.quality, this.qualities)
  90. this.extracts[index].loading = false
  91. }
  92. catch (err) {
  93. // TODO: toast message if fail
  94. console.error('Failed to load extract', err)
  95. }
  96. finally {
  97. this.extracts[index].loading = false
  98. this.saveProgress()
  99. }
  100. }
  101. }
  102. }
  103. </script>