main.js 12 KB

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