Home.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. }
  9. },
  10. beforeMount() {
  11. // console.log('Home beforeMount');
  12. // compile the html src (coming from parent with props or from ajax call)
  13. if(this.html){
  14. // console.log('html', this.html);
  15. this.template = Vue.compile(this.html)
  16. this.$options.staticRenderFns = []
  17. this._staticTrees = []
  18. this.template.staticRenderFns.map(fn => (this.$options.staticRenderFns.push(fn)))
  19. }
  20. },
  21. render(h) {
  22. if(!this.template){
  23. return h('span', 'Loading ...')
  24. }else{
  25. return this.template.render.call(this)
  26. }
  27. },
  28. mounted(){
  29. this.initShowroomCarroussel()
  30. },
  31. methods: {
  32. initShowroomCarroussel(){
  33. console.log("startShowroomCarroussel");
  34. let $showrooms = document.querySelectorAll('.field--name-computed-showrooms-reference > .field__item')
  35. console.log('$showrooms', $showrooms);
  36. // TODO: share media query and variables between scss and js
  37. let column_width= 205
  38. let column_goutiere= 13
  39. let bp = (column_width + column_goutiere )*7 +1
  40. const mediaQuery = window.matchMedia(`(min-width: ${bp}px)`)
  41. if (mediaQuery.matches) {
  42. let $showroomsOdd = []
  43. let $showroomsEven = []
  44. for (var i = 0; i < $showrooms.length; i++) {
  45. if (i%2 === 0) {
  46. $showroomsOdd.push($showrooms[i])
  47. }else{
  48. $showroomsEven.push($showrooms[i])
  49. }
  50. }
  51. console.log('Odd', $showroomsOdd);
  52. console.log('Even', $showroomsEven);
  53. this.switchShowroomCarroussel($showroomsEven, 0)
  54. this.switchShowroomCarroussel($showroomsOdd, 0)
  55. }else{
  56. this.switchShowroomCarroussel($showrooms, 0)
  57. }
  58. },
  59. switchShowroomCarroussel($elmts, i){
  60. // console.log('switchShowroomCarroussel i', $elmts, i);
  61. $elmts[i].classList.add('active')
  62. $elmts[i-1 < 0 ? $elmts.length -1 : i-1].classList.remove('active')
  63. i++
  64. if(i >= $elmts.length){
  65. i = 0
  66. }
  67. setTimeout(this.switchShowroomCarroussel.bind(this,$elmts,i), 5000);
  68. },
  69. onClickLink(e){
  70. console.log("onClickLink", e, this.$router, this.$route);
  71. let path = null;
  72. // find existing router route compared with link href
  73. for (var i = 0; i < this.$router.options.routes.length; i++) {
  74. if (this.$router.options.routes[i].path == e.originalTarget.pathname) {
  75. if (e.originalTarget.pathname !== this.$route.path) {
  76. path = e.originalTarget.pathname
  77. }
  78. break;
  79. }
  80. }
  81. if (path) {
  82. this.$router.push({
  83. path: path
  84. })
  85. }
  86. }
  87. }
  88. }
  89. </script>
  90. <style lang="scss" scoped>
  91. </style>