Home.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <script>
  2. import Vue from 'vue'
  3. export default {
  4. props: ['html'], // 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');
  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.template = Vue.compile(this.html)
  23. this.$options.staticRenderFns = []
  24. this._staticTrees = []
  25. this.template.staticRenderFns.map(fn => (this.$options.staticRenderFns.push(fn)))
  26. }
  27. },
  28. render(h) {
  29. if(!this.template){
  30. return h('span', 'Loading ...')
  31. }else{
  32. return this.template.render.call(this)
  33. }
  34. },
  35. mounted(){
  36. this.initShowroomCarroussel()
  37. },
  38. methods: {
  39. initShowroomCarroussel(){
  40. console.log("startShowroomCarroussel");
  41. this.showrooms = document.querySelectorAll('.field--name-computed-showrooms-reference > .field__item')
  42. console.log('showrooms', this.showrooms);
  43. for (var i = 0; i < this.showrooms.length; i++) {
  44. if (i%2 === 0) {
  45. this.showroomsOdd.push(this.showrooms[i])
  46. }else{
  47. this.showroomsEven.push(this.showrooms[i])
  48. }
  49. }
  50. console.log('Odd', this.showroomsOdd);
  51. console.log('Even', this.showroomsEven);
  52. // TODO: share media query and variables between scss and js
  53. let column_width= 205
  54. let column_goutiere= 13
  55. let bp = (column_width + column_goutiere )*7 +1
  56. const mediaQuery = window.matchMedia(`(min-width: ${bp}px)`)
  57. // Register event listener
  58. mediaQuery.addListener(this.checkShowroomMode)
  59. this.checkShowroomMode(mediaQuery)
  60. // this.showroomInterval = setInterval(this.switchShowroomCarroussel.bind(this), 5000);
  61. // console.log('this.showroomInterval', this.showroomInterval);
  62. // this.switchShowroomCarroussel()
  63. },
  64. checkShowroomMode(mq){
  65. // default mode 1
  66. let newmode = 1
  67. if (mq.matches) {
  68. // if mediaquery match switch to mode 2
  69. newmode = 2
  70. }
  71. if(newmode !== this.showroomMode) {
  72. // if new mode different from old mode
  73. // reset sowrooms classes
  74. for (var i = 0; i < this.showrooms.length; i++) {
  75. this.showrooms[i].classList.remove('active')
  76. }
  77. // record new mode
  78. this.showroomMode = newmode
  79. // clear interval
  80. // if (this.showroomInterval) {
  81. clearInterval(this.showroomInterval)
  82. this.showroomInterval = 0
  83. // }
  84. // reset indexes
  85. this.showroomI = 0
  86. this.showroomJ = 0
  87. }
  88. // in any case (re)launch the animation
  89. this.showroomInterval = setInterval(this.switchShowroomCarroussel.bind(this), 5000);
  90. console.log('this.showroomInterval', this.showroomInterval);
  91. this.switchShowroomCarroussel()
  92. },
  93. switchShowroomCarroussel(){
  94. // console.log('switchShowroomCarroussel i', $elmts, i);
  95. if (this.showroomMode === 1) {
  96. this.showrooms[this.showroomI].classList.add('active')
  97. this.showrooms[this.showroomI-1 < 0 ? this.showrooms.length -1 : this.showroomI-1].classList.remove('active')
  98. this.showroomI = this.showroomI+1 >= this.showrooms.length ? 0 : this.showroomI+1
  99. }else{
  100. this.showroomsOdd[this.showroomI].classList.add('active')
  101. this.showroomsOdd[this.showroomI-1 < 0 ? this.showroomsOdd.length -1 : this.showroomI-1].classList.remove('active')
  102. this.showroomI = this.showroomI+1 >= this.showroomsOdd.length ? 0 : this.showroomI+1
  103. this.showroomsEven[this.showroomJ].classList.add('active')
  104. this.showroomsEven[this.showroomJ-1 < 0 ? this.showroomsEven.length -1 : this.showroomJ-1].classList.remove('active')
  105. this.showroomJ = this.showroomJ+1 >= this.showroomsEven.length ? 0 : this.showroomJ+1
  106. }
  107. },
  108. onClickLink(e){
  109. console.log("onClickLink", e, this.$router, this.$route);
  110. let path = null;
  111. // find existing router route compared with link href
  112. for (var i = 0; i < this.$router.options.routes.length; i++) {
  113. if (this.$router.options.routes[i].path == e.originalTarget.pathname) {
  114. if (e.originalTarget.pathname !== this.$route.path) {
  115. path = e.originalTarget.pathname
  116. }
  117. break;
  118. }
  119. }
  120. if (path) {
  121. this.$router.push({
  122. path: path
  123. })
  124. }
  125. }
  126. }
  127. }
  128. </script>
  129. <style lang="scss" scoped>
  130. </style>