Home.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <script>
  2. import Vue from 'vue'
  3. import productsMixins from 'vuejs/components/productsMixins'
  4. export default {
  5. props: ['html', 'full'], // get the html from parent with props
  6. mixins: [productsMixins],
  7. data() {
  8. return {
  9. template: null, // compiled template from html used in render
  10. showrooms: [],
  11. showroomsOdd: [],
  12. showroomsEven: [],
  13. showroomMode: 1,
  14. showroomInterval: 0,
  15. showroomI:0,
  16. showroomJ:0
  17. }
  18. },
  19. beforeMount() {
  20. console.log('Home beforeMount, full', this.full)
  21. // compile the html src (coming from parent with props or from ajax call)
  22. if (this.html) {
  23. // console.log('html', this.html)
  24. this.compileTemplate()
  25. }
  26. },
  27. render(h) {
  28. if (!this.template) {
  29. return h('span', this.$t('default.Loading…'))
  30. } else {
  31. return this.template.render.call(this)
  32. }
  33. },
  34. mounted(){
  35. // this.initShowroomCarroussel()
  36. },
  37. methods: {
  38. compileTemplate(){
  39. this.template = Vue.compile(this.html)
  40. this.$options.staticRenderFns = []
  41. this._staticTrees = []
  42. this.template.staticRenderFns.map(fn => (this.$options.staticRenderFns.push(fn)))
  43. console.log('compileTemplate, full', this.full)
  44. if (this.full) {
  45. setTimeout(this.initShowroomCarroussel.bind(this), 250)
  46. }
  47. },
  48. initShowroomCarroussel(){
  49. console.log('startShowroomCarroussel')
  50. this.showrooms = document.querySelectorAll('.field--name-computed-showrooms-reference > .field__item')
  51. console.log('showrooms', this.showrooms)
  52. if (!this.showrooms.lenght) {
  53. return
  54. }
  55. for (let i = 0; i < this.showrooms.length; i++) {
  56. if (i%2 === 0) {
  57. this.showroomsOdd.push(this.showrooms[i])
  58. } else {
  59. this.showroomsEven.push(this.showrooms[i])
  60. }
  61. }
  62. console.log('Odd', this.showroomsOdd)
  63. console.log('Even', this.showroomsEven)
  64. // TODO: share media query and variables between scss and js
  65. let column_width= 205
  66. let column_goutiere= 13
  67. let bp = (column_width + column_goutiere )*7 +1
  68. const mediaQuery = window.matchMedia(`(min-width: ${bp}px)`)
  69. // Register event listener
  70. mediaQuery.addListener(this.checkShowroomMode)
  71. this.checkShowroomMode(mediaQuery)
  72. // this.showroomInterval = setInterval(this.switchShowroomCarroussel.bind(this), 5000);
  73. // console.log('this.showroomInterval', this.showroomInterval)
  74. // this.switchShowroomCarroussel()
  75. },
  76. checkShowroomMode(mq){
  77. // default mode 1
  78. let newmode = 1
  79. if (mq.matches) {
  80. // if mediaquery match switch to mode 2
  81. newmode = 2
  82. }
  83. if(newmode !== this.showroomMode) {
  84. // if new mode different from old mode
  85. // reset sowrooms classes
  86. for (let i = 0; i < this.showrooms.length; i++) {
  87. this.showrooms[i].classList.remove('active')
  88. }
  89. // record new mode
  90. this.showroomMode = newmode
  91. // clear interval
  92. // if (this.showroomInterval) {
  93. clearInterval(this.showroomInterval)
  94. this.showroomInterval = 0
  95. // }
  96. // reset indexes
  97. this.showroomI = 0
  98. this.showroomJ = 0
  99. }
  100. // in any case (re)launch the animation
  101. this.showroomInterval = setInterval(this.switchShowroomCarroussel.bind(this), 15000);
  102. console.log('this.showroomInterval', this.showroomInterval)
  103. this.switchShowroomCarroussel()
  104. },
  105. switchShowroomCarroussel(){
  106. // console.log('switchShowroomCarroussel i', $elmts, i)
  107. if (this.showroomMode === 1) {
  108. this.showrooms[this.showroomI].classList.add('active')
  109. this.showrooms[this.showroomI-1 < 0 ? this.showrooms.length -1 : this.showroomI-1].classList.remove('active')
  110. this.showroomI = this.showroomI+1 >= this.showrooms.length ? 0 : this.showroomI+1
  111. } else {
  112. this.showroomsOdd[this.showroomI].classList.add('active')
  113. this.showroomsOdd[this.showroomI-1 < 0 ? this.showroomsOdd.length -1 : this.showroomI-1].classList.remove('active')
  114. this.showroomI = this.showroomI+1 >= this.showroomsOdd.length ? 0 : this.showroomI+1
  115. this.showroomsEven[this.showroomJ].classList.add('active')
  116. this.showroomsEven[this.showroomJ-1 < 0 ? this.showroomsEven.length -1 : this.showroomJ-1].classList.remove('active')
  117. this.showroomJ = this.showroomJ+1 >= this.showroomsEven.length ? 0 : this.showroomJ+1
  118. }
  119. },
  120. onClickLink(e){
  121. console.log("onClickLink", e, this.$router, this.$route)
  122. if (e.originalTarget.protocol == "mailto:") {
  123. // https://stackoverflow.com/questions/10172499/mailto-using-javascript
  124. window.location.href = e.originalTarget.href
  125. }else {
  126. let path = null;
  127. // find existing router route compared with link href
  128. for (let i = 0; i < this.$router.options.routes.length; i++) {
  129. if (this.$router.options.routes[i].path == e.originalTarget.pathname) {
  130. if (e.originalTarget.pathname !== this.$route.path) {
  131. path = e.originalTarget.pathname
  132. }
  133. break
  134. }
  135. }
  136. if (path) {
  137. this.$router.push({
  138. path: path
  139. })
  140. }
  141. }
  142. },
  143. onClickFieldLabel(e){
  144. console.log("onClickFieldLabel", e, this.$router, this.$route)
  145. }
  146. },
  147. watch: {
  148. html: function(val) {
  149. // console.log('html prop changed', val)
  150. this.compileTemplate()
  151. }
  152. }
  153. }
  154. </script>
  155. <style lang="scss" scoped>
  156. </style>