main.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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, { dialog: true })
  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, loadLanguageAsync } from 'vuejs/i18n'
  28. import VueMeta from 'vue-meta'
  29. Vue.use(VueMeta)
  30. // LOADED IN COMPONENT
  31. // import VueSimpleAccordion from 'vue-simple-accordion'
  32. // Vue.use(VueSimpleAccordion, {
  33. // // ... Options go here
  34. // })
  35. // import 'vue-simple-accordion/dist/vue-simple-accordion.css'
  36. import Vue2TouchEvents from 'vue2-touch-events'
  37. Vue.use(Vue2TouchEvents)
  38. import VUserBlock from 'vuejs/components/Block/UserBlock'
  39. import VMainContent from 'vuejs/components/Content/MainContent'
  40. import VSearchBlock from 'vuejs/components/Block/SearchBlock'
  41. import VLeftContent from 'vuejs/components/Content/LeftContent'
  42. import VHeaderMenu from 'vuejs/components/Content/HeaderMenu'
  43. import { mapState } from 'vuex'
  44. import 'vue-cool-lightbox/dist/vue-cool-lightbox.min.css'
  45. import 'slim-select/slimselect.min.css'
  46. import 'theme/assets/styles/main.scss'
  47. import 'theme/assets/styles/print.scss'
  48. import { MA } from 'vuejs/api/ma-axios'
  49. export let _v_sitebranding_block, _v_user_block, _v_header_menu,
  50. _v_pagetitle_block, _v_search_block,
  51. _v_main_content, _v_left_content
  52. (function (Drupal, drupalSettings, drupalDecoupled) {
  53. const MaterioTheme = function () {
  54. const _is_front = drupalSettings.path.isFront
  55. console.log('drupalSettings', drupalSettings)
  56. // let _I18n
  57. // ___ _ _
  58. // |_ _|_ _ (_) |_
  59. // | || ' \| | _|
  60. // |___|_||_|_|\__|
  61. function init () {
  62. console.log('MaterioTheme init()')
  63. initVues()
  64. }
  65. function checkNoVuePages () {
  66. // return drupalDecoupled.sys_path != '/cart'
  67. // && drupalDecoupled.sys_path.indexOf('checkout') != 1;
  68. // if (drupalDecoupled.route_name.indexOf('commerce') === -1 &&
  69. // drupalDecoupled.route_name.indexOf('flagging_collection') === -1 &&
  70. // drupalDecoupled.route_name.indexOf('user') === -1 &&
  71. // drupalDecoupled.route_name !== 'entity.webform.canonical' &&
  72. // (drupalDecoupled.route_name === 'entity.webform.canonical' && drupalDecoupled.sys_path.indexOf('/webform/multi_joueur') === -1)) {
  73. // return false
  74. // } else {
  75. // return true
  76. // }
  77. if (drupalDecoupled.route_name.indexOf('commerce') !== -1 ||
  78. drupalDecoupled.route_name.indexOf('flagging_collection') !== -1 ||
  79. drupalDecoupled.route_name.indexOf('user') !== -1 ||
  80. drupalDecoupled.route_name.indexOf('entity.webform.canonical') !== -1 ||
  81. drupalDecoupled.route_name.indexOf('entity.webform.confirmation') !== -1) {
  82. console.debug('NO VUEJS')
  83. return true
  84. } else {
  85. console.debug('VUJS')
  86. return false
  87. }
  88. }
  89. function initVues () {
  90. // only launch views if we are not in commerce pages
  91. if (!checkNoVuePages()) {
  92. initVRouter()
  93. initVSiteBrandingBlock()
  94. initVPagetitleBlock()
  95. initVHeaderMenu()
  96. initHamburgerMenu()
  97. initVSearchBlock()
  98. initVMainContent()
  99. initVStore()
  100. initVi18n()
  101. initVLeftContent()
  102. }
  103. initVUserBlock()
  104. }
  105. function initVi18n () {
  106. // i18n.locale = drupalDecoupled.lang_code
  107. // console.log('i18n.messages', i18n.messages)
  108. loadLanguageAsync(drupalDecoupled.lang_code)
  109. .then(() => {
  110. console.log('main.js language loaded')
  111. })
  112. }
  113. function initVStore () {
  114. store.dispatch('Showrooms/getItems')
  115. }
  116. function initVRouter () {
  117. // we need this to update the title and body classes while using history nav
  118. router.beforeEach((to, from, next) => {
  119. console.log('router beforeEach to ', to, router.app.$i18n)
  120. // commit new title to store
  121. let title = null
  122. switch (to.name) {
  123. case 'home':
  124. title = null
  125. break
  126. case 'article':
  127. title = false
  128. break
  129. case 'pricing':
  130. title = i18n.t('materio.' + to.name)
  131. break
  132. default:
  133. title = to.name
  134. }
  135. if (title !== false) {
  136. store.commit('Common/setPagetitle', title)
  137. }
  138. // remove all path related body classes
  139. const body_classes = document.querySelector('body').classList
  140. const classes_to_rm = []
  141. for (let i = 0; i < body_classes.length; i++) {
  142. if (body_classes[i].startsWith('path-')) {
  143. classes_to_rm.push(body_classes[i])
  144. }
  145. }
  146. document.querySelector('body').classList.remove(...classes_to_rm)
  147. // add new path classes to body
  148. const classes = []
  149. if (to.name === 'home') {
  150. classes.push('path-home')
  151. } else {
  152. const path_parts = to.path
  153. .replace(/^\//, '')
  154. .replace(/^\D{2,3}\//, '') // remove language relative prefix from path classes (fr, en, etc)
  155. .split('/')
  156. let c
  157. for (let j = 0; j < path_parts.length; j++) {
  158. if (j === 0) {
  159. c = 'path-' + path_parts[j]
  160. } else if (path_parts[j] !== '') {
  161. c = classes[j - 1] + '-' + path_parts[j]
  162. }
  163. classes.push(c)
  164. }
  165. }
  166. document.querySelector('body').classList.add(...classes)
  167. updateLanguageLinksBlock(to.path)
  168. // close the hamburger menu
  169. store.dispatch('Common/openCloseHamMenu', false)
  170. // trigger router
  171. next()
  172. })
  173. }
  174. function updateLanguageLinksBlock (path) {
  175. // update block language selection
  176. console.log('router beforeEach path ', path)
  177. const links = document.querySelectorAll('#block-languageswitcher a.language-link')
  178. const params = {
  179. path: path
  180. // XDEBUG_SESSION_START: true
  181. }
  182. MA.post('materio_decoupled/path_translation_links?', params)
  183. .then(({ data }) => {
  184. console.log('Path translations links', data)
  185. if (data.error) {
  186. console.warn('error getting translation paths', data.error)
  187. }
  188. links.forEach((link, i) => {
  189. console.log('language link', path, link)
  190. const hreflang = link.getAttribute('hreflang')
  191. link.setAttribute('href', data.links[hreflang].url)
  192. link.setAttribute('data-drupal-link-system-path', data.links[hreflang].sys_path)
  193. link.innerHTML = data.links[hreflang].title
  194. })
  195. })
  196. .catch(error => {
  197. console.warn('Path translations links', error)
  198. })
  199. }
  200. function initVSiteBrandingBlock () {
  201. _v_sitebranding_block = new Vue({
  202. store,
  203. i18n,
  204. router,
  205. el: '#block-sitebranding',
  206. methods: {
  207. onclick (event) {
  208. // console.log('router beforeEach to ', to)
  209. const href = event.target.getAttribute('href')
  210. // console.log('router beforeEach to ', to)
  211. this.$router.push(href)
  212. // replaced by router.beforeEach
  213. // this.$store.commit('Common/setPagetitle', null)
  214. }
  215. }
  216. })
  217. }
  218. function initVPagetitleBlock () {
  219. const $blk = document.querySelector('#block-pagetitle')
  220. const $h2 = $blk.querySelector('h2')
  221. // get the loaded pagetitle
  222. const title = $h2.innerText
  223. // if not front recorde the loaded pagetitle in store
  224. if (!_is_front) {
  225. store.commit('Common/setPagetitle', title)
  226. }
  227. // replace in template the pagetitle by vue binding
  228. $h2.innerText = '{{ pagetitle }}'
  229. // create the vue
  230. _v_pagetitle_block = new Vue({
  231. store,
  232. i18n,
  233. router,
  234. el: $blk,
  235. computed: {
  236. ...mapState({
  237. pagetitle: state => state.Common.pagetitle
  238. })
  239. }
  240. })
  241. }
  242. function initVUserBlock () {
  243. const mount_point = drupalSettings.user.uid !== 0 ? 'block-userblock' : 'block-userlogin'
  244. const props = {
  245. title: '',
  246. loginblock: ''
  247. }
  248. let $block
  249. switch (mount_point) {
  250. case 'block-userlogin':
  251. $block = document.getElementById(mount_point)
  252. console.log('initVUserBlock login form html', $block)
  253. props.loginblock = $block.outerHTML.trim()
  254. break
  255. case 'block-userblock':
  256. default:
  257. break
  258. }
  259. _v_user_block = new Vue({
  260. store,
  261. i18n,
  262. // computed: {
  263. // ...mapState({
  264. // isloggedin: state => state.User.isloggedin
  265. // })
  266. // },
  267. created () {
  268. // if already loggedin, call store.user to get the user infos
  269. if (drupalSettings.user.uid !== 0) {
  270. this.$store.commit('User/setUid', drupalSettings.user.uid)
  271. this.$store.dispatch('User/getUser')
  272. }
  273. },
  274. render: h => h(VUserBlock, { props: props })
  275. }).$mount('#' + mount_point)
  276. // console.log('router beforeEach to ', to)
  277. }
  278. function initVHeaderMenu () {
  279. // console.log('router beforeEach to ', to)
  280. const id = 'block-header'
  281. const $html_obj = document.querySelector('#' + id)
  282. // console.log('router beforeEach to ', to)
  283. const html = $html_obj.outerHTML
  284. _v_header_menu = new Vue({
  285. store,
  286. i18n,
  287. router,
  288. render: h => h(VHeaderMenu, { props: { id: id, dom_html: html } })
  289. }).$mount('#' + id)
  290. }
  291. function initHamburgerMenu () {
  292. const input = document.querySelector('input#header-block-right-toggle')
  293. input.addEventListener('change', (e) => {
  294. console.log('initHamburgerMenu input change ', e)
  295. store.dispatch('Common/openCloseHamMenu', e.currentTarget.checked)
  296. })
  297. }
  298. function initVMainContent () {
  299. const id = 'main-content'
  300. const $main_content = document.querySelector('#' + id)
  301. // console.log('router beforeEach to ', to)
  302. const main_html = $main_content.innerHTML
  303. _v_main_content = new Vue({
  304. store,
  305. i18n,
  306. metaInfo: {
  307. // if no subcomponents specify a metaInfo.title, this title will be used
  308. title: "materiO'",
  309. // all titles will be injected into this template
  310. titleTemplate: "%s | materiO'"
  311. },
  312. render: h => h(VMainContent, { props: { id: id, html: main_html, isfront: drupalSettings.path.isFront } })
  313. }).$mount('#' + id)
  314. }
  315. function initVSearchBlock () {
  316. // console.log('router beforeEach to ', to)
  317. const id = 'block-materiosapisearchblock'
  318. let $search_block = document.getElementById(id)
  319. let formhtml = null
  320. if ($search_block) {
  321. // get the search form html to pass it as template to the vue
  322. // we gain display speed vs async downloaded data
  323. formhtml = $search_block.innerHTML
  324. } else {
  325. // else create the empty block to fill it later with async data
  326. $search_block = document.createElement('div')
  327. $search_block.setAttribute('id', id)
  328. // TODO: get region by REST
  329. const $region = document.getElementById('header-bottom')
  330. $region.appendChild($search_block)
  331. }
  332. // in any case create the vue
  333. _v_search_block = new Vue({
  334. store,
  335. i18n,
  336. render: h => h(VSearchBlock, { props: { blockid: id, formhtml: formhtml } })
  337. }).$mount('#' + id)
  338. }
  339. function initVLeftContent () {
  340. const id = 'content-left'
  341. // const $leftContent = document.getElementById(id)
  342. // in any case create the vue
  343. _v_left_content = new Vue({
  344. store,
  345. i18n,
  346. render: h => h(VLeftContent, { props: { id: id } })
  347. }).$mount('#' + id)
  348. }
  349. init()
  350. } // end MaterioTheme()
  351. // const materiotheme = new MaterioTheme()
  352. MaterioTheme()
  353. })(Drupal, drupalSettings, drupalDecoupled)