main.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* eslint-disable import/first */
  2. import Vue from 'vue'
  3. import InfiniteLoading from 'vue-infinite-loading'
  4. Vue.use(InfiniteLoading, {
  5. props: {
  6. spinner: 'spiral'
  7. // slots.noMore: ''
  8. }
  9. // system: {
  10. // throttleLimit: 50,
  11. // /* other settings need to configure */
  12. // }
  13. })
  14. // import vueVimeoPlayer from 'vue-vimeo-player'
  15. // Vue.use(vueVimeoPlayer)
  16. // import VueYouTubeEmbed from 'vue-youtube-embed'
  17. // Vue.use(VueYouTubeEmbed)
  18. import CoolLightBox from 'vue-cool-lightbox'
  19. Vue.use(CoolLightBox)
  20. import VModal from 'vue-js-modal'
  21. Vue.use(VModal)
  22. import store from 'vuejs/store'
  23. import router from 'vuejs/route'
  24. // import VueI18n from 'vue-i18n'
  25. // Vue.use(VueI18n)
  26. // import * as Locales from 'assets/i18n/locales.json'
  27. import i18n from 'vuejs/i18n'
  28. import VUserBlock from 'vuejs/components/Block/UserBlock'
  29. import VMainContent from 'vuejs/components/Content/MainContent'
  30. import VSearchBlock from 'vuejs/components/Block/SearchBlock'
  31. import VLeftContent from 'vuejs/components/Content/LeftContent'
  32. import { mapState } from 'vuex'
  33. // require('theme/assets/styles/main.scss');
  34. import 'vue-cool-lightbox/dist/vue-cool-lightbox.min.css'
  35. import 'theme/assets/styles/main.scss'
  36. (function (Drupal, drupalSettings, drupalDecoupled) {
  37. const MaterioTheme = function () {
  38. let _v_sitebranding_block, _v_user_block, _v_header_menu,
  39. _v_pagetitle_block, _v_search_block,
  40. _v_main_content, _v_left_content
  41. const _is_front = drupalSettings.path.isFront
  42. console.log('drupalSettings', drupalSettings)
  43. // let _I18n
  44. // ___ _ _
  45. // |_ _|_ _ (_) |_
  46. // | || ' \| | _|
  47. // |___|_||_|_|\__|
  48. function init () {
  49. console.log('MaterioTheme init()')
  50. initVues()
  51. }
  52. function checkNoVuePages () {
  53. // return drupalDecoupled.sys_path != '/cart'
  54. // && drupalDecoupled.sys_path.indexOf('checkout') != 1;
  55. if (drupalDecoupled.route_name.indexOf('commerce') == -1 &&
  56. drupalDecoupled.route_name.indexOf('flagging_collection') == -1 &&
  57. drupalDecoupled.route_name.indexOf('user') == -1) {
  58. return false
  59. } else {
  60. return true
  61. }
  62. }
  63. function initVues () {
  64. // only launch views if we are not in commerce pages
  65. if (!checkNoVuePages()) {
  66. initVi18n()
  67. initVStore()
  68. initVRouter()
  69. initVSiteBrandingBlock()
  70. initVPagetitleBlock()
  71. initVHeaderMenu()
  72. initVMainContent()
  73. initVSearchBlock()
  74. initVLeftContent()
  75. }
  76. initVUserBlock()
  77. }
  78. function initVi18n () {
  79. i18n.locale = drupalDecoupled.lang_code
  80. console.log('i18n.messages', i18n.messages)
  81. // const locales = {
  82. // ...Locales
  83. // }
  84. // _I18n = new VueI18n({
  85. // locale: drupalDecoupled.lang_code,
  86. // locales
  87. // })
  88. // console.log('_I18n', _I18n)
  89. }
  90. function initVStore () {
  91. store.dispatch('Showrooms/getItems')
  92. }
  93. function initVRouter () {
  94. // we need this to update the title and body classes while using history nav
  95. router.beforeEach((to, from, next) => {
  96. // console.log('router beforeEach to ', to);
  97. // commit new title to store
  98. let title = null
  99. switch (to.name) {
  100. case 'home':
  101. title = null
  102. break
  103. case 'article':
  104. title = false
  105. break
  106. default:
  107. title = to.name
  108. }
  109. if (title !== false) {
  110. store.commit('Common/setPagetitle', title)
  111. }
  112. // remove all path related body classes
  113. const body_classes = document.querySelector('body').classList
  114. const classes_to_rm = []
  115. for (var i = 0; i < body_classes.length; i++) {
  116. if (body_classes[i].startsWith('path-')) {
  117. classes_to_rm.push(body_classes[i])
  118. }
  119. }
  120. document.querySelector('body').classList.remove(...classes_to_rm)
  121. // add new path classes to body
  122. const classes = []
  123. if (to.path == '/') {
  124. classes.push('path-home')
  125. } else {
  126. const path_parts = to.path.replace(/^\//, '').split('/')
  127. // TODO: remove language relative prefix from path classes (fr, en, etc)
  128. for (var i = 0; i < path_parts.length; i++) {
  129. if (i == 0) {
  130. var c = 'path-' + path_parts[i]
  131. } else if (path_parts[i] !== '') {
  132. var c = classes[i - 1] + '-' + path_parts[i]
  133. }
  134. classes.push(c)
  135. }
  136. }
  137. document.querySelector('body').classList.add(...classes)
  138. // trigger router
  139. next()
  140. })
  141. }
  142. function initVSiteBrandingBlock () {
  143. _v_sitebranding_block = new Vue({
  144. store,
  145. i18n,
  146. router,
  147. el: '#block-sitebranding',
  148. methods: {
  149. onclick (event) {
  150. // console.log("Clicked on logo event", event);
  151. const href = event.target.getAttribute('href')
  152. // console.log("Clicked on logo href", href);
  153. this.$router.push(href)
  154. // replaced by router.beforeEach
  155. // this.$store.commit('Common/setPagetitle', null)
  156. }
  157. }
  158. })
  159. }
  160. function initVPagetitleBlock () {
  161. const $blk = document.querySelector('#block-pagetitle')
  162. const $h2 = $blk.querySelector('h2')
  163. // get the loaded pagetitle
  164. const title = $h2.innerText
  165. // if not front recorde the loaded pagetitle in store
  166. if (!_is_front) {
  167. store.commit('Common/setPagetitle', title)
  168. }
  169. // replace in template the pagetitle by vue binding
  170. $h2.innerText = '{{ pagetitle }}'
  171. // create the vue
  172. _v_pagetitle_block = new Vue({
  173. store,
  174. i18n,
  175. router,
  176. el: $blk,
  177. computed: {
  178. ...mapState({
  179. pagetitle: state => state.Common.pagetitle
  180. })
  181. }
  182. })
  183. }
  184. function initVUserBlock () {
  185. const mount_point = drupalSettings.user.uid !== 0 ? 'block-userblock' : 'block-userlogin'
  186. const props = {
  187. title: '',
  188. loginblock: ''
  189. }
  190. let $block
  191. switch (mount_point) {
  192. case 'block-userlogin':
  193. $block = document.getElementById(mount_point)
  194. console.log('initVUserBlock login form html', $block)
  195. props.loginblock = $block.outerHTML.trim()
  196. break
  197. case 'block-userblock':
  198. default:
  199. break
  200. }
  201. _v_user_block = new Vue({
  202. store,
  203. i18n,
  204. // computed: {
  205. // ...mapState({
  206. // isloggedin: state => state.User.isloggedin
  207. // })
  208. // },
  209. created () {
  210. // if already loggedin, call store.user to get the user infos
  211. if (drupalSettings.user.uid !== 0) {
  212. this.$store.commit('User/setUid', drupalSettings.user.uid)
  213. this.$store.dispatch('User/getUser')
  214. }
  215. },
  216. render: h => h(VUserBlock, { props: props })
  217. }).$mount('#' + mount_point)
  218. // console.log('initVUserBlock', _v_user_block);
  219. }
  220. function initVHeaderMenu () {
  221. // console.log('initVHeaderMenu');
  222. // adding vuejs attributes has it wont work on twig template (see menu--header.html.twig)
  223. // not working : String contains an invalid character
  224. // document.querySelectorAll(`#block-header a`).forEach(link => {
  225. // console.log(link);
  226. // link.setAttribute('@click.prevent', 'onclick')
  227. // });
  228. _v_header_menu = new Vue({
  229. store,
  230. i18n,
  231. router,
  232. el: '#block-header',
  233. methods: {
  234. onclick (event) {
  235. // console.log("Clicked on header menu link", event);
  236. const href = event.target.getAttribute('href')
  237. // let title = event.target.innerText;
  238. // console.log("Clicked on header menu link : href", href);
  239. this.$router.push(href)
  240. // replaced by router.beforeEach
  241. // this.$store.commit('Common/setPagetitle', title)
  242. }
  243. }
  244. })
  245. }
  246. function initVMainContent () {
  247. const id = 'main-content'
  248. const $main_content = document.querySelector('#' + id)
  249. // console.log('main-content', $main_content);
  250. const main_html = $main_content.innerHTML
  251. _v_main_content = new Vue({
  252. store,
  253. i18n,
  254. render: h => h(VMainContent, { props: { id: id, html: main_html, isfront: drupalSettings.path.isFront } })
  255. }).$mount('#' + id)
  256. }
  257. function initVSearchBlock () {
  258. // console.log('initVSearchBlock');
  259. const id = 'block-materiosapisearchblock'
  260. let $search_block = document.getElementById(id)
  261. let formhtml = null
  262. if ($search_block) {
  263. // get the search form html to pass it as template to the vue
  264. // we gain display speed vs async downloaded data
  265. formhtml = $search_block.innerHTML
  266. } else {
  267. // else create the empty block to fill it later with async data
  268. $search_block = document.createElement('div')
  269. $search_block.setAttribute('id', id)
  270. // TODO: get region by REST
  271. const $region = document.getElementById('header-bottom')
  272. $region.appendChild($search_block)
  273. }
  274. // in any case create the vue
  275. _v_search_block = new Vue({
  276. store,
  277. i18n,
  278. render: h => h(VSearchBlock, { props: { blockid: id, formhtml: formhtml } })
  279. }).$mount('#' + id)
  280. }
  281. function initVLeftContent () {
  282. const id = 'content-left'
  283. const $leftContent = document.getElementById(id)
  284. // in any case create the vue
  285. _v_left_content = new Vue({
  286. store,
  287. i18n,
  288. render: h => h(VLeftContent, { props: { id: id } })
  289. }).$mount('#' + id)
  290. }
  291. init()
  292. } // end MaterioTheme()
  293. const materiotheme = new MaterioTheme()
  294. })(Drupal, drupalSettings, drupalDecoupled)