Home.vue 5.4 KB

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