index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. import { EXPERIMENT as experimentMsgId } from '@/../config.messagesId'
  12. export default {
  13. name: 'ExperimentBaseExtracts',
  14. mixins: [ExperimentBase],
  15. data() {
  16. return {
  17. // Updated when `setConfig` is called
  18. extractConfig: {
  19. x: 4,
  20. y: 4
  21. },
  22. extracts: []
  23. }
  24. },
  25. computed: {
  26. ...mapGetters(['getHostURI'])
  27. },
  28. methods: {
  29. // Load extracts from the API
  30. async getExtracts(quality = 'min') {
  31. const URI = `${this.getHostURI}${API_ROUTES.getImageExtracts(this.sceneName, quality, this.extractConfig.x, this.extractConfig.y)}`
  32. const { data } = await fetch(URI)
  33. .then(async res => {
  34. res.json = await res.json()
  35. return res
  36. })
  37. .then(res => {
  38. if (!res.ok) throw new Error(res.json.message + res.json.data ? `\n${res.json.data}` : '')
  39. return res.json
  40. })
  41. return data
  42. },
  43. // Config was updated, load extracts and save progression
  44. async setConfig(config, configuratorRef) {
  45. if (!config || !configuratorRef) return
  46. this.loadingMessage = 'Loading configuration extracts...'
  47. this.loadingErrorMessage = null
  48. try {
  49. this.extractConfig.x = config.x
  50. this.extractConfig.y = config.y
  51. const data = await this.getExtracts()
  52. const hostURI = this.getHostURI
  53. this.extracts = data.extracts.map((url, i) => ({
  54. link: hostURI + url,
  55. quality: data.info.image.quality,
  56. zone: i + 1,
  57. index: i,
  58. nextQuality: findNearestUpper(data.info.image.quality, this.qualities),
  59. precQuality: findNearestLower(data.info.image.quality, this.qualities),
  60. loading: false
  61. }))
  62. configuratorRef.setVisibility(false)
  63. }
  64. catch (err) {
  65. console.error('Failed to load new configuration', err)
  66. this.loadingErrorMessage = 'Failed to load new configuration. ' + err.message
  67. }
  68. finally {
  69. this.loadingMessage = null
  70. this.saveProgress()
  71. }
  72. },
  73. // An action was triggered, load extracts and save progression
  74. async extractAction(event, extractObj) {
  75. console.log(event, extractObj)
  76. const { index, nextQuality, precQuality, quality } = extractObj
  77. let newQuality
  78. if (event.button === 0) newQuality = precQuality // Left click
  79. if (event.button === 2) newQuality = nextQuality // Right click
  80. // Do not load a new extract if same quality
  81. if (newQuality === quality) return
  82. // Set loading state
  83. this.extracts[index].loading = true
  84. try {
  85. // Loading new extract
  86. const data = await this.getExtracts(newQuality)
  87. this.extracts[index].link = this.getHostURI + data.extracts[index]
  88. this.extracts[index].quality = data.info.image.quality
  89. this.extracts[index].nextQuality = findNearestUpper(data.info.image.quality, this.qualities)
  90. this.extracts[index].precQuality = findNearestLower(data.info.image.quality, this.qualities)
  91. this.extracts[index].loading = false
  92. // Sending event to WebSocket server
  93. // this.sendMessage({ msgId: experimentMsgId.DATA, msg: obj })
  94. }
  95. catch (err) {
  96. // TODO: toast message if fail
  97. console.error('Failed to load extract', err)
  98. }
  99. finally {
  100. this.extracts[index].loading = false
  101. this.saveProgress()
  102. }
  103. },
  104. // Finish an experiment, sending full data to the server
  105. // Don't forget to surcharge this function when using this mixin to add more data
  106. finishExperiment() {
  107. const obj = {
  108. experimentName: this.experimentName,
  109. sceneName: this.sceneName,
  110. extractConfig: this.extractConfig,
  111. extracts: this.extracts.map(x => ({
  112. index: x.index,
  113. link: x.link,
  114. nextQuality: x.nextQuality,
  115. precQuality: x.precQuality,
  116. quality: x.quality,
  117. zone: x.zone
  118. })),
  119. qualities: this.qualities,
  120. referenceImage: this.referenceImage
  121. }
  122. this.sendMessage({ msgId: experimentMsgId.VALIDATED, msg: obj })
  123. this.setExperimentDone({ experimentName: this.experimentName, sceneName: this.sceneName, done: true })
  124. this.$router.push(`/experiments/${this.experimentName}`)
  125. }
  126. }
  127. }
  128. </script>