main.js 12 KB

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