ExperimentBaseExtracts.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <template>
  2. <div>
  3. <slot></slot>
  4. </div>
  5. </template>
  6. <script>
  7. import ExperimentBase from '@/mixins/ExperimentBase'
  8. import { mapGetters } from 'vuex'
  9. import { API_ROUTES, findNearestUpper, findNearestLower } from '@/functions'
  10. import { EXPERIMENT as experimentMsgId } from '@/../config.messagesId'
  11. export default {
  12. name: 'ExperimentBaseExtracts',
  13. mixins: [ExperimentBase],
  14. data() {
  15. return {
  16. // Updated when `setExtractConfig` is called
  17. extractConfig: {
  18. x: null,
  19. y: null
  20. },
  21. extracts: [],
  22. showHoverBorder: null
  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 setExtractConfig(config, configuratorRef) {
  45. if (!config) 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. // If there is a configurator, retract it
  63. if (configuratorRef) configuratorRef.setVisibility(false)
  64. }
  65. catch (err) {
  66. console.error('Failed to load new configuration', err)
  67. this.loadingErrorMessage = 'Failed to load new configuration. ' + err.message
  68. }
  69. finally {
  70. this.loadingMessage = null
  71. this.saveProgress()
  72. }
  73. },
  74. // An action was triggered, load extracts and save progression
  75. async extractAction(event, extractObj) {
  76. console.log(event, extractObj)
  77. const { index, nextQuality, precQuality, quality } = extractObj
  78. let newQuality
  79. if (event.button === 0) newQuality = precQuality // Left click
  80. if (event.button === 2) newQuality = nextQuality // Right click
  81. // Do not load a new extract if same quality
  82. if (newQuality === quality) return
  83. // Set loading state
  84. this.extracts[index].loading = true
  85. try {
  86. // Loading new extract
  87. const data = await this.getExtracts(newQuality)
  88. this.extracts[index].link = this.getHostURI + data.extracts[index]
  89. this.extracts[index].quality = data.info.image.quality
  90. this.extracts[index].nextQuality = findNearestUpper(data.info.image.quality, this.qualities)
  91. this.extracts[index].precQuality = findNearestLower(data.info.image.quality, this.qualities)
  92. this.extracts[index].loading = false
  93. // Sending event to WebSocket server
  94. // this.sendMessage({ msgId: experimentMsgId.DATA, msg: obj })
  95. }
  96. catch (err) {
  97. // TODO: toast message if fail
  98. console.error('Failed to load extract', err)
  99. }
  100. finally {
  101. this.extracts[index].loading = false
  102. this.saveProgress()
  103. }
  104. },
  105. // Finish an experiment, sending full data to the server
  106. // Don't forget to surcharge this function when using this mixin to add more data
  107. finishExperiment() {
  108. const obj = {
  109. experimentName: this.experimentName,
  110. sceneName: this.sceneName,
  111. extractConfig: this.extractConfig,
  112. extracts: this.extracts.map(x => ({
  113. index: x.index,
  114. link: x.link,
  115. nextQuality: x.nextQuality,
  116. precQuality: x.precQuality,
  117. quality: x.quality,
  118. zone: x.zone
  119. })),
  120. qualities: this.qualities,
  121. referenceImage: this.referenceImage
  122. }
  123. this.sendMessage({ msgId: experimentMsgId.VALIDATED, msg: obj })
  124. this.setExperimentDone({ experimentName: this.experimentName, sceneName: this.sceneName, done: true })
  125. this.$router.push(`/experiments/${this.experimentName}`)
  126. }
  127. }
  128. }
  129. </script>
  130. <style>
  131. /* White border when hovering on extracts */
  132. .extract-hover-border:hover {
  133. z-index: 1;
  134. outline: 2px #f4f4f4 solid;
  135. }
  136. .img-extract-loader {
  137. height: 100%;
  138. width: 0px;
  139. display: flex;
  140. justify-content: center;
  141. align-items: center;
  142. }
  143. </style>