Home.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.length, this.showrooms)
  52. if (!this.showrooms.length) {
  53. return
  54. }
  55. // console.log('TEST')
  56. for (let i = 0; i < this.showrooms.length; i++) {
  57. if (i%2 === 0) {
  58. this.showroomsOdd.push(this.showrooms[i])
  59. } else {
  60. this.showroomsEven.push(this.showrooms[i])
  61. }
  62. }
  63. console.log('Odd', this.showroomsOdd)
  64. console.log('Even', this.showroomsEven)
  65. // TODO: share media query and variables between scss and js
  66. let column_width= 205
  67. let column_goutiere= 13
  68. let bp = (column_width + column_goutiere )*7 +1
  69. const mediaQuery = window.matchMedia(`(min-width: ${bp}px)`)
  70. // Register event listener
  71. mediaQuery.addListener(this.checkShowroomMode)
  72. this.checkShowroomMode(mediaQuery)
  73. // this.showroomInterval = setInterval(this.switchShowroomCarroussel.bind(this), 5000);
  74. // console.log('this.showroomInterval', this.showroomInterval)
  75. // this.switchShowroomCarroussel()
  76. },
  77. checkShowroomMode(mq){
  78. console.log('checkShowroomMode', mq)
  79. // default mode 1
  80. let newmode = 1
  81. if (mq.matches) {
  82. // if mediaquery match switch to mode 2
  83. newmode = 2
  84. }
  85. if(newmode !== this.showroomMode) {
  86. // if new mode different from old mode
  87. // reset sowrooms classes
  88. for (let i = 0; i < this.showrooms.length; i++) {
  89. this.showrooms[i].classList.remove('active')
  90. }
  91. // record new mode
  92. this.showroomMode = newmode
  93. // clear interval
  94. // if (this.showroomInterval) {
  95. clearInterval(this.showroomInterval)
  96. this.showroomInterval = 0
  97. // }
  98. // reset indexes
  99. this.showroomI = 0
  100. this.showroomJ = 0
  101. }
  102. // in any case (re)launch the animation
  103. this.showroomInterval = setInterval(this.switchShowroomCarroussel.bind(this), 15000);
  104. console.log('this.showroomInterval', this.showroomInterval)
  105. this.switchShowroomCarroussel()
  106. },
  107. switchShowroomCarroussel(){
  108. console.log('switchShowroomCarroussel')
  109. if (this.showroomMode === 1) {
  110. this.showrooms[this.showroomI].classList.add('active')
  111. this.showrooms[this.showroomI-1 < 0 ? this.showrooms.length -1 : this.showroomI-1].classList.remove('active')
  112. this.showroomI = this.showroomI+1 >= this.showrooms.length ? 0 : this.showroomI+1
  113. } else {
  114. this.showroomsOdd[this.showroomI].classList.add('active')
  115. this.showroomsOdd[this.showroomI-1 < 0 ? this.showroomsOdd.length -1 : this.showroomI-1].classList.remove('active')
  116. this.showroomI = this.showroomI+1 >= this.showroomsOdd.length ? 0 : this.showroomI+1
  117. this.showroomsEven[this.showroomJ].classList.add('active')
  118. this.showroomsEven[this.showroomJ-1 < 0 ? this.showroomsEven.length -1 : this.showroomJ-1].classList.remove('active')
  119. this.showroomJ = this.showroomJ+1 >= this.showroomsEven.length ? 0 : this.showroomJ+1
  120. }
  121. },
  122. onClickLink(e){
  123. console.log("onClickLink", e, this.$router, this.$route)
  124. // record the target before event finish the propagation as currentTarget will be deleted after
  125. let target = e.currentTarget
  126. console.log('target', target)
  127. if (target.protocol == "mailto:") {
  128. // https://stackoverflow.com/questions/10172499/mailto-using-javascript
  129. window.location.href = target.href
  130. }else {
  131. let path = null;
  132. let article = null;
  133. // if we have an article link with appropriate data attributes
  134. // 'data-id'
  135. // 'data-entity-type'
  136. // 'data-bundle'
  137. if (target.dataset.entityType == 'node' && target.dataset.bundle == 'article') {
  138. let matches = target.pathname.match(/^\/\w{2}\/[^\/]+\/(.*)/i)
  139. console.log('matches', matches)
  140. article = {
  141. nid: target.dataset.id,
  142. alias: matches[1]
  143. }
  144. } else {
  145. // find existing router route compared with link href
  146. let pathbase = target.pathname.match(/^(\/\w{2}\/[^\/]+)\/?.*/i)
  147. console.log('pathbase', pathbase)
  148. for (let i = 0; i < this.$router.options.routes.length; i++) {
  149. if (this.$router.options.routes[i].path == pathbase[1]) {
  150. if (target.pathname !== this.$route.path) {
  151. path = target.pathname
  152. }
  153. break
  154. }
  155. }
  156. }
  157. if (article) {
  158. this.$router.push({
  159. name:`article`,
  160. params: { alias: article.alias, id: article.nid }
  161. })
  162. } else if (path) {
  163. this.$router.push({
  164. path: path
  165. })
  166. }
  167. }
  168. },
  169. onClickFieldLabel(e, part){
  170. console.log("onClickFieldLabel", part, e, this.$router, this.$route)
  171. }
  172. },
  173. watch: {
  174. html: function(val) {
  175. // console.log('html prop changed', val)
  176. this.compileTemplate()
  177. }
  178. }
  179. }
  180. </script>
  181. <style lang="scss" scoped>
  182. </style>