main.js 12 KB

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