ExperimentBaseExtracts.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. // There was an error loading extracts in v-img
  48. // (extracts have probably been removed from the server)
  49. // //
  50. // Get all extracts qualities and remove duplicates
  51. // Then do all the API extract-generation API calls then reload the page
  52. async extractsRemovedFromServerFallback() {
  53. this.loadingMessage = 'Synchronizing with the server...'
  54. try {
  55. const qualities = [...new Set(this.extracts.map(x => x.quality))]
  56. await Promise.all(qualities.map(x => this.getExtracts(x)))
  57. }
  58. catch (err) {
  59. console.error(err)
  60. this.loadingErrorMessage = 'Failed to synchronize with the server. Try reloading the page.'
  61. }
  62. finally {
  63. this.loadingMessage = null
  64. }
  65. },
  66. // Convert a simple API extracts object to get more informations
  67. getExtractFullObject(extractsApiObj) {
  68. return extractsApiObj.extracts.map((url, i) => ({
  69. link: url,
  70. quality: extractsApiObj.info.image.quality,
  71. zone: i + 1,
  72. index: i,
  73. nextQuality: findNearestUpper(extractsApiObj.info.image.quality, this.qualities),
  74. precQuality: findNearestLower(extractsApiObj.info.image.quality, this.qualities),
  75. loading: false
  76. }))
  77. },
  78. // Config was updated, load extracts and save progression
  79. async setExtractConfig(config, configuratorRef) {
  80. if (!config) return
  81. this.loadingMessage = 'Loading configuration extracts...'
  82. this.loadingErrorMessage = null
  83. try {
  84. this.extractConfig.x = config.x
  85. this.extractConfig.y = config.y
  86. this.extractConfig.quality = config.quality
  87. const data = await this.getExtracts(config.quality || undefined)
  88. this.extractsInfos = data.info
  89. // Put extracts in cache if not already there
  90. if (this.extracts.length === 0) this.extracts = this.getExtractFullObject(data)
  91. // If there is a configurator, retract it
  92. if (configuratorRef) configuratorRef.setVisibility(false)
  93. }
  94. catch (err) {
  95. console.error('Failed to load new configuration', err)
  96. this.loadingErrorMessage = 'Failed to load new configuration. ' + err.message
  97. }
  98. finally {
  99. this.loadingMessage = null
  100. this.saveProgress()
  101. }
  102. },
  103. // An action was triggered, load extracts and save progression
  104. async extractAction(event, extractObj) {
  105. const { index, nextQuality, precQuality, quality } = extractObj
  106. const qualityIndex = this.qualities.indexOf(quality)
  107. let action, newQuality
  108. if (event.button === 0) action = 'needLess' // Left click
  109. if (event.button === 2) action = 'needMore' // Right click
  110. if (event.button === 0 && event.ctrlKey) action = 'need10Less' // ctrl + Right click
  111. if (event.button === 2 && event.ctrlKey) action = 'need10More' // ctrl + Left click
  112. if (action === 'needLess') newQuality = precQuality
  113. if (action === 'needMore') newQuality = nextQuality
  114. if (action === 'need10More') {
  115. if (qualityIndex + 10 >= this.qualities.length - 1)
  116. newQuality = this.qualities[this.qualities.length - 1]
  117. else
  118. newQuality = this.qualities[qualityIndex + 10]
  119. }
  120. if (action === 'need10Less') {
  121. if (qualityIndex - 10 <= 0)
  122. newQuality = this.qualities[0]
  123. else
  124. newQuality = this.qualities[qualityIndex - 10]
  125. }
  126. // Do not load a new extract if same quality
  127. if (newQuality === quality) return
  128. // Set loading state
  129. this.extracts[index].loading = true
  130. try {
  131. const collectedData = this.getClickDataObject(event, extractObj, action)
  132. this.sendMessage({ msgId: experimentMsgId.DATA, msg: collectedData })
  133. // Loading new extract
  134. const data = await this.getExtracts(newQuality)
  135. this.extracts[index].link = data.extracts[index]
  136. this.extracts[index].quality = data.info.image.quality
  137. this.extracts[index].nextQuality = findNearestUpper(data.info.image.quality, this.qualities)
  138. this.extracts[index].precQuality = findNearestLower(data.info.image.quality, this.qualities)
  139. this.extracts[index].loading = false
  140. }
  141. catch (err) {
  142. // TODO: toast message if fail
  143. console.error('Failed to load extract', err)
  144. }
  145. finally {
  146. this.extracts[index].loading = false
  147. this.saveProgress()
  148. }
  149. },
  150. getClickDataObject(event, extractObj, action) {
  151. const { index } = extractObj
  152. const clientSideData = {
  153. extractSize: {
  154. width: event.target.clientWidth,
  155. height: event.target.clientHeight
  156. },
  157. imageSize: {
  158. width: event.target.clientWidth * this.extractConfig.x,
  159. height: event.target.clientHeight * this.extractConfig.y
  160. },
  161. clickPosition: {
  162. extract: {
  163. x: event.offsetX,
  164. y: event.offsetY
  165. },
  166. image: {
  167. x: event.offsetX + (this.extracts[index].index % this.extractConfig.x) * event.target.clientWidth,
  168. y: event.offsetY + (Math.floor(this.extracts[index].index / this.extractConfig.x)) * event.target.clientHeight
  169. }
  170. }
  171. }
  172. const calculatedRealData = {}
  173. calculatedRealData.extractSize = {
  174. width: this.extractsInfos.extractsSize.width,
  175. height: this.extractsInfos.extractsSize.height
  176. }
  177. calculatedRealData.imageSize = {
  178. width: this.extractsInfos.image.metadata.width,
  179. height: this.extractsInfos.image.metadata.height
  180. }
  181. calculatedRealData.clickPosition = {
  182. extract: {
  183. x: Math.floor((calculatedRealData.imageSize.width * clientSideData.clickPosition.extract.x) / clientSideData.imageSize.width),
  184. y: Math.floor((calculatedRealData.imageSize.height * clientSideData.clickPosition.extract.y) / clientSideData.imageSize.height)
  185. },
  186. image: {
  187. x: Math.floor((calculatedRealData.imageSize.width * clientSideData.clickPosition.image.x) / clientSideData.imageSize.width),
  188. y: Math.floor((calculatedRealData.imageSize.height * clientSideData.clickPosition.image.y) / clientSideData.imageSize.height)
  189. }
  190. }
  191. // Sending event to WebSocket server
  192. const loggedObj = {
  193. experimentName: this.experimentName,
  194. sceneName: this.sceneName,
  195. extractConfig: this.extractConfig,
  196. clickedExtract: {
  197. link: this.extracts[index].link,
  198. quality: this.extracts[index].quality,
  199. nextQuality: this.extracts[index].nextQuality,
  200. precQuality: this.extracts[index].precQuality,
  201. zone: this.extracts[index].zone,
  202. index: this.extracts[index].index
  203. },
  204. action,
  205. clientSideData,
  206. calculatedRealData
  207. }
  208. return loggedObj
  209. },
  210. // Finish an experiment, sending full data to the server
  211. // Don't forget to surcharge this function when using this mixin to add more data
  212. finishExperiment() {
  213. const obj = {
  214. experimentName: this.experimentName,
  215. sceneName: this.sceneName,
  216. extractConfig: this.extractConfig,
  217. extracts: this.extracts.map(x => ({
  218. index: x.index,
  219. link: x.link,
  220. nextQuality: x.nextQuality,
  221. precQuality: x.precQuality,
  222. quality: x.quality,
  223. zone: x.zone
  224. })),
  225. qualities: this.qualities,
  226. referenceImage: this.referenceImage,
  227. comment: this.comment
  228. }
  229. this.sendMessage({ msgId: experimentMsgId.VALIDATED, msg: obj })
  230. this.setExperimentFinished()
  231. this.$router.push(`/experiments/${this.experimentName}/${this.sceneName}/validated`)
  232. }
  233. }
  234. }
  235. </script>
  236. <style>
  237. /* White border when hovering on extracts */
  238. .extract-hover-border:hover {
  239. z-index: 1;
  240. outline: 2px #f4f4f4 solid;
  241. }
  242. .img-extract-loader {
  243. height: 100%;
  244. width: 0px;
  245. display: flex;
  246. justify-content: center;
  247. align-items: center;
  248. }
  249. </style>