main.js 11 KB

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