main.js 12 KB

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