ExperimentBaseExtracts.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. extractsInfos: null,
  23. showHoverBorder: null,
  24. lockConfig: null,
  25. comment: null
  26. }
  27. },
  28. computed: {
  29. ...mapGetters(['getHostURI'])
  30. },
  31. methods: {
  32. // Load extracts from the API
  33. async getExtracts(quality = 'min') {
  34. const URI = `${this.getHostURI}${API_ROUTES.getImageExtracts(this.sceneName, quality, this.extractConfig.x, this.extractConfig.y)}`
  35. const { data } = await fetch(URI)
  36. .then(async res => {
  37. res.json = await res.json()
  38. return res
  39. })
  40. .then(res => {
  41. if (!res.ok) throw new Error(res.json.message + res.json.data ? `\n${res.json.data}` : '')
  42. return res.json
  43. })
  44. data.extracts = data.extracts.map(x => this.getHostURI + x)
  45. return data
  46. },
  47. // Convert a simple API extracts object to get more informations
  48. getExtractFullObject(extractsApiObj) {
  49. return extractsApiObj.extracts.map((url, i) => ({
  50. link: url,
  51. quality: extractsApiObj.info.image.quality,
  52. zone: i + 1,
  53. index: i,
  54. nextQuality: findNearestUpper(extractsApiObj.info.image.quality, this.qualities),
  55. precQuality: findNearestLower(extractsApiObj.info.image.quality, this.qualities),
  56. loading: false
  57. }))
  58. },
  59. // Config was updated, load extracts and save progression
  60. async setExtractConfig(config, configuratorRef) {
  61. if (!config) return
  62. this.loadingMessage = 'Loading configuration extracts...'
  63. this.loadingErrorMessage = null
  64. try {
  65. this.extractConfig.x = config.x
  66. this.extractConfig.y = config.y
  67. this.extractConfig.quality = config.quality
  68. const data = await this.getExtracts(config.quality || undefined)
  69. this.extractsInfos = data.info
  70. this.extracts = this.getExtractFullObject(data)
  71. // If there is a configurator, retract it
  72. if (configuratorRef) configuratorRef.setVisibility(false)
  73. }
  74. catch (err) {
  75. console.error('Failed to load new configuration', err)
  76. this.loadingErrorMessage = 'Failed to load new configuration. ' + err.message
  77. }
  78. finally {
  79. this.loadingMessage = null
  80. this.saveProgress()
  81. }
  82. },
  83. // An action was triggered, load extracts and save progression
  84. async extractAction(event, extractObj) {
  85. const { index, nextQuality, precQuality, quality } = extractObj
  86. let action, newQuality
  87. if (event.button === 0) action = 'needLess' // Left click
  88. if (event.button === 2) action = 'needMore' // Right click
  89. if (action === 'needLess') newQuality = precQuality
  90. if (action === 'needMore') newQuality = nextQuality
  91. // Do not load a new extract if same quality
  92. if (newQuality === quality) return
  93. // Set loading state
  94. this.extracts[index].loading = true
  95. try {
  96. const collectedData = this.getClickDataObject(event, extractObj, action)
  97. this.sendMessage({ msgId: experimentMsgId.DATA, msg: collectedData })
  98. // Loading new extract
  99. const data = await this.getExtracts(newQuality)
  100. this.extracts[index].link = data.extracts[index]
  101. this.extracts[index].quality = data.info.image.quality
  102. this.extracts[index].nextQuality = findNearestUpper(data.info.image.quality, this.qualities)
  103. this.extracts[index].precQuality = findNearestLower(data.info.image.quality, this.qualities)
  104. this.extracts[index].loading = false
  105. }
  106. catch (err) {
  107. // TODO: toast message if fail
  108. console.error('Failed to load extract', err)
  109. }
  110. finally {
  111. this.extracts[index].loading = false
  112. this.saveProgress()
  113. }
  114. },
  115. getClickDataObject(event, extractObj, action) {
  116. const { index } = extractObj
  117. const clientSideData = {
  118. extractSize: {
  119. width: event.target.clientWidth,
  120. height: event.target.clientHeight
  121. },
  122. imageSize: {
  123. width: event.target.clientWidth * this.extractConfig.x,
  124. height: event.target.clientHeight * this.extractConfig.y
  125. },
  126. clickPosition: {
  127. extract: {
  128. x: event.offsetX,
  129. y: event.offsetY
  130. },
  131. image: {
  132. x: event.offsetX + (this.extracts[index].index % this.extractConfig.x) * event.target.clientWidth,
  133. y: event.offsetY + (Math.floor(this.extracts[index].index / this.extractConfig.x)) * event.target.clientHeight
  134. }
  135. }
  136. }
  137. const calculatedRealData = {}
  138. calculatedRealData.extractSize = {
  139. width: this.extractsInfos.extractsSize.width,
  140. height: this.extractsInfos.extractsSize.height
  141. }
  142. calculatedRealData.imageSize = {
  143. width: this.extractsInfos.image.metadata.width,
  144. height: this.extractsInfos.image.metadata.height
  145. }
  146. calculatedRealData.clickPosition = {
  147. extract: {
  148. x: Math.floor((calculatedRealData.imageSize.width * clientSideData.clickPosition.extract.x) / clientSideData.imageSize.width),
  149. y: Math.floor((calculatedRealData.imageSize.height * clientSideData.clickPosition.extract.y) / clientSideData.imageSize.height)
  150. },
  151. image: {
  152. x: Math.floor((calculatedRealData.imageSize.width * clientSideData.clickPosition.image.x) / clientSideData.imageSize.width),
  153. y: Math.floor((calculatedRealData.imageSize.height * clientSideData.clickPosition.image.y) / clientSideData.imageSize.height)
  154. }
  155. }
  156. // Sending event to WebSocket server
  157. const loggedObj = {
  158. experimentName: this.experimentName,
  159. sceneName: this.sceneName,
  160. extractConfig: this.extractConfig,
  161. clickedExtract: {
  162. link: this.extracts[index].link,
  163. quality: this.extracts[index].quality,
  164. nextQuality: this.extracts[index].nextQuality,
  165. precQuality: this.extracts[index].precQuality,
  166. zone: this.extracts[index].zone,
  167. index: this.extracts[index].index
  168. },
  169. action,
  170. clientSideData,
  171. calculatedRealData
  172. }
  173. return loggedObj
  174. },
  175. // Finish an experiment, sending full data to the server
  176. // Don't forget to surcharge this function when using this mixin to add more data
  177. finishExperiment() {
  178. const obj = {
  179. experimentName: this.experimentName,
  180. sceneName: this.sceneName,
  181. extractConfig: this.extractConfig,
  182. extracts: this.extracts.map(x => ({
  183. index: x.index,
  184. link: x.link,
  185. nextQuality: x.nextQuality,
  186. precQuality: x.precQuality,
  187. quality: x.quality,
  188. zone: x.zone
  189. })),
  190. qualities: this.qualities,
  191. referenceImage: this.referenceImage,
  192. comment: this.comment
  193. }
  194. this.sendMessage({ msgId: experimentMsgId.VALIDATED, msg: obj })
  195. this.setExperimentFinished()
  196. this.$router.push(`/experiments/${this.experimentName}/${this.sceneName}/validated`)
  197. }
  198. }
  199. }
  200. </script>
  201. <style>
  202. /* White border when hovering on extracts */
  203. .extract-hover-border:hover {
  204. z-index: 1;
  205. outline: 2px #f4f4f4 solid;
  206. }
  207. .img-extract-loader {
  208. height: 100%;
  209. width: 0px;
  210. display: flex;
  211. justify-content: center;
  212. align-items: center;
  213. }
  214. </style>