main.js 13 KB

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