main.js 11 KB

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