WithReference.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <template>
  2. <div>
  3. <v-container grid-list-md text-xs-center fluid>
  4. <v-layout row wrap>
  5. <v-flex xs12>
  6. <h1>Experiment with reference</h1>
  7. <v-card dark>
  8. <v-container grid-list-sm fluid>
  9. <v-layout row wrap>
  10. <v-flex
  11. xs12
  12. >
  13. <h1>Configuration</h1>
  14. <v-card-text class="px-0">Extracts per line (horizontal)</v-card-text>
  15. <v-slider
  16. v-model="experimentConfig.x"
  17. always-dirty
  18. persistent-hint
  19. thumb-label="always"
  20. />
  21. <v-card-text class="px-0">Extracts per row (vertical)</v-card-text>
  22. <v-slider
  23. v-model="experimentConfig.y"
  24. always-dirty
  25. persistent-hint
  26. thumb-label="always"
  27. />
  28. <v-btn @click="setConfig" :disabled="!isConfigNew">Confirm</v-btn>
  29. <v-alert v-if="loadingErrorMessage" :value="true" type="error" v-text="loadingErrorMessage" />
  30. </v-flex>
  31. </v-layout>
  32. </v-container>
  33. </v-card>
  34. </v-flex>
  35. <!-- Loading screen -->
  36. <loader v-if="loadingMessage" :message="loadingMessage" />
  37. <!--/ Loading screen -->
  38. <!-- Experiment -->
  39. <template v-else-if="!loadingErrorMessage">
  40. <v-flex xs12 sm6>
  41. <v-card dark color="primary">
  42. <v-card-text class="px-0">Experiment image</v-card-text>
  43. <v-container class="pa-1">
  44. <template v-for="i in extractConfig.x">
  45. <v-layout row wrap :key="`row-${i}`">
  46. <v-flex
  47. v-for="(anExtract, index) in extracts.slice(extractConfig.x * (i - 1), (extractConfig.x * i))"
  48. :key="`extract-${i}-${extractConfig.x}-${extractConfig.y}-${index}-${anExtract.quality}`"
  49. class="pa-0"
  50. >
  51. <v-card flat tile class="d-flex">
  52. <v-img
  53. :src="anExtract.link"
  54. >
  55. <template v-slot:placeholder>
  56. <v-layout
  57. fill-height
  58. align-center
  59. justify-center
  60. ma-0
  61. >
  62. <v-progress-circular indeterminate color="grey lighten-5" />
  63. </v-layout>
  64. </template>
  65. </v-img>
  66. </v-card>
  67. </v-flex>
  68. </v-layout>
  69. </template>
  70. </v-container>
  71. </v-card>
  72. </v-flex>
  73. <v-flex sm6 xs12>
  74. <v-card dark color="primary">
  75. <v-card-text>Reference image</v-card-text>
  76. <v-img v-if="referenceImage" :src="referenceImage" />
  77. </v-card>
  78. </v-flex>
  79. </template>
  80. <!--/ Experiment -->
  81. </v-layout>
  82. </v-container>
  83. </div>
  84. </template>
  85. <script>
  86. import { mapGetters, mapActions } from 'vuex'
  87. import { API_ROUTES } from '@/functions'
  88. import Loader from '@/components/Loader.vue'
  89. export default {
  90. name: 'ExperimentWithReference',
  91. components: {
  92. Loader
  93. },
  94. props: {
  95. sceneId: {
  96. type: String,
  97. required: true
  98. }
  99. },
  100. data() {
  101. return {
  102. referenceImage: null,
  103. qualities: null,
  104. experimentConfig: { // Experiment config sliders
  105. x: 8,
  106. y: 4,
  107. error: null
  108. },
  109. extractConfig: { // Updated when `setConfig` is called
  110. x: 8,
  111. y: 4
  112. },
  113. loadingMessage: null,
  114. loadingErrorMessage: null,
  115. extracts: []
  116. }
  117. },
  118. computed: {
  119. ...mapGetters(['getHostURI']),
  120. isConfigNew() {
  121. return this.extractConfig.x !== this.experimentConfig.x || this.extractConfig.y !== this.experimentConfig.y
  122. }
  123. },
  124. async mounted() {
  125. await this.getReferenceImage()
  126. await this.getQualitiesList()
  127. // Get default extracts : min quality, cut config : x = 4, y = 4
  128. await this.setConfig()
  129. },
  130. methods: {
  131. ...mapActions([]),
  132. async getReferenceImage() {
  133. const URI = `${this.getHostURI}${API_ROUTES.getImage(this.sceneId, 'max')}`
  134. const { data } = await fetch(URI).then(res => res.json())
  135. this.referenceImage = this.getHostURI + data.link
  136. },
  137. async getQualitiesList() {
  138. const URI = `${this.getHostURI}${API_ROUTES.listSceneQualities(this.sceneId)}`
  139. const { data } = await fetch(URI).then(res => res.json())
  140. this.qualities = data
  141. },
  142. async getExtracts(quality = 'min') {
  143. const URI = `${this.getHostURI}${API_ROUTES.getImageExtracts(this.sceneId, quality, this.extractConfig.x, this.extractConfig.y)}`
  144. const { data } = await fetch(URI)
  145. .then(async res => {
  146. res.json = await res.json()
  147. return res
  148. })
  149. .then(res => {
  150. if (!res.ok) throw new Error(res.json.message + res.json.data ? `\n${res.json.data}` : '')
  151. return res.json
  152. })
  153. return data
  154. },
  155. async setConfig() {
  156. // Check if the config is the same
  157. if (this.extracts.length > 0 && !this.isConfigNew) return
  158. this.loadingMessage = 'Loading configuration extracts...'
  159. this.loadingErrorMessage = null
  160. try {
  161. this.extractConfig.x = this.experimentConfig.x
  162. this.extractConfig.y = this.experimentConfig.y
  163. const data = await this.getExtracts()
  164. const hostURI = this.getHostURI
  165. this.extracts = data.extracts.map(url => ({
  166. link: hostURI + url,
  167. quality: data.info.image.quality
  168. }))
  169. }
  170. catch (err) {
  171. console.error('Failed to load new configuration', err)
  172. this.loadingErrorMessage = 'Failed to load new configuration. ' + err.message
  173. }
  174. this.loadingMessage = null
  175. }
  176. }
  177. }
  178. </script>