main.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. // LOADED IN COMPONENT
  19. // import CoolLightBox from 'vue-cool-lightbox'
  20. // Vue.use(CoolLightBox)
  21. import VModal from 'vue-js-modal'
  22. Vue.use(VModal, { dialog: true })
  23. import router from 'vuejs/route'
  24. import store from 'vuejs/store'
  25. // import VueI18n from 'vue-i18n'
  26. // Vue.use(VueI18n)
  27. // import * as Locales from 'assets/i18n/locales.json'
  28. import { i18n, loadLanguageAsync } from 'vuejs/i18n'
  29. import VueMeta from 'vue-meta'
  30. Vue.use(VueMeta)
  31. // LOADED IN COMPONENT
  32. // import VueSimpleAccordion from 'vue-simple-accordion'
  33. // Vue.use(VueSimpleAccordion, {
  34. // // ... Options go here
  35. // })
  36. // import 'vue-simple-accordion/dist/vue-simple-accordion.css'
  37. import Vue2TouchEvents from 'vue2-touch-events'
  38. Vue.use(Vue2TouchEvents)
  39. import VUserBlock from 'vuejs/components/Block/UserBlock'
  40. import VMainContent from 'vuejs/components/Content/MainContent'
  41. import VSearchBlock from 'vuejs/components/Block/SearchBlock'
  42. import VLeftContent from 'vuejs/components/Content/LeftContent'
  43. import VHeaderMenu from 'vuejs/components/Content/HeaderMenu'
  44. import VLanguageSwitcher from 'vuejs/components/Content/LanguageSwitcher'
  45. import GlobCoolLightBox from 'vuejs/components/Content/GlobCoolLightBox'
  46. import { mapState } from 'vuex'
  47. // import 'vue-cool-lightbox/dist/vue-cool-lightbox.min.css'
  48. import 'slim-select/slimselect.min.css'
  49. import 'theme/assets/styles/main.scss'
  50. import 'theme/assets/styles/print.scss'
  51. import MA from 'vuejs/api/ma-axios'
  52. export let _v_sitebranding_block, _v_user_block, _v_header_menu, _v_language_switcher,
  53. _v_pagetitle_block, _v_search_block,
  54. _v_main_content, _v_left_content,
  55. _v_glob_coollightbox
  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. // drupalDecoupled.route_name !== 'entity.webform.canonical' &&
  76. // (drupalDecoupled.route_name === 'entity.webform.canonical' && drupalDecoupled.sys_path.indexOf('/webform/multi_joueur') === -1)) {
  77. // return false
  78. // } else {
  79. // return true
  80. // }
  81. if (drupalDecoupled.route_name.indexOf('commerce') !== -1 ||
  82. drupalDecoupled.route_name.indexOf('flagging_collection') !== -1 ||
  83. drupalDecoupled.route_name.indexOf('user') !== -1 ||
  84. drupalDecoupled.route_name.indexOf('profile') !== -1 ||
  85. drupalDecoupled.route_name.indexOf('entity.webform.canonical') !== -1 ||
  86. drupalDecoupled.route_name.indexOf('entity.webform.confirmation') !== -1 ||
  87. drupalDecoupled.route_name.indexOf('materio_expo.qr_controller_getfile') !== -1 ||
  88. (drupalDecoupled.route_name === 'entity.node.canonical' && drupalDecoupled.entity_bundle === 'simplenews_issue') ||
  89. drupalDecoupled.route_name.indexOf('simplenews') !== -1
  90. ) {
  91. console.debug('NO VUEJS')
  92. return true
  93. } else {
  94. console.debug('VUJS')
  95. return false
  96. }
  97. }
  98. function initVues () {
  99. // only launch views if we are not in commerce pages
  100. if (!checkNoVuePages()) {
  101. initVRouter()
  102. initVSiteBrandingBlock()
  103. initVPagetitleBlock()
  104. initVHeaderMenu()
  105. initVLanguageSwitcher()
  106. initHamburgerMenu()
  107. initVSearchBlock()
  108. initVMainContent()
  109. initVStore()
  110. initVi18n()
  111. initVLeftContent()
  112. initCoolLightBox()
  113. }
  114. initVUserBlock()
  115. }
  116. function initVi18n () {
  117. // i18n.locale = drupalDecoupled.lang_code
  118. // console.log('i18n.messages', i18n.messages)
  119. loadLanguageAsync(drupalDecoupled.lang_code)
  120. .then(() => {
  121. console.log('main.js language loaded')
  122. })
  123. }
  124. function initVStore () {
  125. store.dispatch('Showrooms/getShowrooms')
  126. }
  127. function initVRouter () {
  128. // we need this to update the title and body classes while using history nav
  129. router.beforeEach((to, from, next) => {
  130. console.log('router beforeEach to ', to, router.app.$i18n)
  131. // commit new title to store
  132. let title = null
  133. switch (to.name) {
  134. case 'home':
  135. title = null
  136. break
  137. case 'article':
  138. title = false
  139. break
  140. case 'pricing':
  141. title = i18n.t('materio.' + to.name)
  142. break
  143. default:
  144. title = to.name
  145. }
  146. if (title !== false) {
  147. store.commit('Common/setPagetitle', title)
  148. }
  149. // remove all path related body classes
  150. const body_classes = document.querySelector('body').classList
  151. const classes_to_rm = []
  152. for (let i = 0; i < body_classes.length; i++) {
  153. if (body_classes[i].startsWith('path-')) {
  154. classes_to_rm.push(body_classes[i])
  155. }
  156. }
  157. document.querySelector('body').classList.remove(...classes_to_rm)
  158. // add new path classes to body
  159. const classes = []
  160. if (to.name === 'home') {
  161. classes.push('path-home')
  162. } else {
  163. const path_parts = to.path
  164. .replace(/^\//, '')
  165. .replace(/^\D{2,3}\//, '') // remove language relative prefix from path classes (fr, en, etc)
  166. .split('/')
  167. let c
  168. for (let j = 0; j < path_parts.length; j++) {
  169. if (j === 0) {
  170. c = 'path-' + path_parts[j]
  171. } else if (path_parts[j] !== '') {
  172. c = classes[j - 1] + '-' + path_parts[j]
  173. }
  174. classes.push(c)
  175. }
  176. }
  177. document.querySelector('body').classList.add(...classes)
  178. updateLanguageLinksBlock(to.path)
  179. // close the hamburger menu
  180. store.dispatch('Common/openCloseHamMenu', false)
  181. // trigger router
  182. next()
  183. })
  184. }
  185. function initVLanguageSwitcher () {
  186. console.log('initVLanguageSwitcher')
  187. const id = 'block-languageswitcher'
  188. const $html_obj = document.querySelector('#' + id)
  189. console.log('initVLanguageSwitcher $html_obj', $html_obj)
  190. const html = $html_obj.outerHTML
  191. console.log('initVLanguageSwitcher html', html)
  192. _v_language_switcher = new Vue({
  193. store,
  194. i18n,
  195. router,
  196. render: h => h(VLanguageSwitcher, { props: { id: id, dom_html: html } })
  197. }).$mount('#' + id)
  198. }
  199. function updateLanguageLinksBlock (path) {
  200. // update block language selection
  201. console.log('router beforeEach path ', path)
  202. const links = document.querySelectorAll('#block-languageswitcher a.language-link')
  203. const params = {
  204. path: path
  205. // XDEBUG_SESSION_START: true
  206. }
  207. MA.post('materio_decoupled/path_translation_links?', params)
  208. .then(({ data }) => {
  209. console.log('Path translations links', data)
  210. if (data.error) {
  211. console.warn('error getting translation paths', data.error)
  212. }
  213. links.forEach((link, i) => {
  214. console.log('language link', path, link)
  215. const hreflang = link.getAttribute('hreflang')
  216. link.setAttribute('href', data.links[hreflang].url)
  217. link.setAttribute('data-drupal-link-system-path', data.links[hreflang].sys_path)
  218. link.innerHTML = data.links[hreflang].title
  219. })
  220. })
  221. .catch(error => {
  222. console.warn('Path translations links', error)
  223. })
  224. }
  225. function initVSiteBrandingBlock () {
  226. _v_sitebranding_block = new Vue({
  227. store,
  228. i18n,
  229. router,
  230. el: '#block-sitebranding',
  231. methods: {
  232. onclick (event) {
  233. // console.log('router beforeEach to ', to)
  234. const href = event.target.getAttribute('href')
  235. // console.log('router beforeEach to ', to)
  236. this.$router.push(href)
  237. // replaced by router.beforeEach
  238. // this.$store.commit('Common/setPagetitle', null)
  239. }
  240. }
  241. })
  242. }
  243. function initVPagetitleBlock () {
  244. const $blk = document.querySelector('#block-pagetitle')
  245. const $h2 = $blk.querySelector('h2')
  246. // get the loaded pagetitle
  247. const title = $h2.innerText
  248. // if not front recorde the loaded pagetitle in store
  249. if (!_is_front) {
  250. store.commit('Common/setPagetitle', title)
  251. }
  252. // replace in template the pagetitle by vue binding
  253. $h2.innerText = '{{ pagetitle }}'
  254. // create the vue
  255. _v_pagetitle_block = new Vue({
  256. store,
  257. i18n,
  258. router,
  259. el: $blk,
  260. computed: {
  261. ...mapState({
  262. pagetitle: state => state.Common.pagetitle
  263. })
  264. }
  265. })
  266. }
  267. function initVUserBlock () {
  268. const mount_point = drupalSettings.user.uid !== 0 ? 'block-userblock' : 'block-userlogin'
  269. const props = {
  270. title: '',
  271. loginblock: ''
  272. }
  273. let $block
  274. switch (mount_point) {
  275. case 'block-userlogin':
  276. $block = document.getElementById(mount_point)
  277. console.log('initVUserBlock login form html', $block)
  278. props.loginblock = $block.outerHTML.trim()
  279. break
  280. case 'block-userblock':
  281. default:
  282. break
  283. }
  284. _v_user_block = new Vue({
  285. store,
  286. i18n,
  287. // computed: {
  288. // ...mapState({
  289. // isloggedin: state => state.User.isloggedin
  290. // })
  291. // },
  292. created () {
  293. // if already loggedin, call store.user to get the user infos
  294. if (drupalSettings.user.uid !== 0) {
  295. this.$store.commit('User/setUid', drupalSettings.user.uid)
  296. this.$store.dispatch('User/getUser')
  297. }
  298. },
  299. render: h => h(VUserBlock, { props: props })
  300. }).$mount('#' + mount_point)
  301. // console.log('router beforeEach to ', to)
  302. }
  303. function initVHeaderMenu () {
  304. // console.log('router beforeEach to ', to)
  305. const id = 'block-header'
  306. const $html_obj = document.querySelector('#' + id)
  307. // console.log('router beforeEach to ', to)
  308. const html = $html_obj.outerHTML
  309. _v_header_menu = new Vue({
  310. store,
  311. i18n,
  312. router,
  313. render: h => h(VHeaderMenu, { props: { id: id, dom_html: html } })
  314. }).$mount('#' + id)
  315. }
  316. function initHamburgerMenu () {
  317. const input = document.querySelector('input#header-block-right-toggle')
  318. input.addEventListener('change', (e) => {
  319. console.log('initHamburgerMenu input change ', e)
  320. store.dispatch('Common/openCloseHamMenu', e.currentTarget.checked)
  321. })
  322. }
  323. function initVMainContent () {
  324. const id = 'main-content'
  325. const $main_content = document.querySelector('#' + id)
  326. // console.log('router beforeEach to ', to)
  327. const main_html = $main_content.innerHTML
  328. _v_main_content = new Vue({
  329. store,
  330. i18n,
  331. metaInfo: {
  332. // if no subcomponents specify a metaInfo.title, this title will be used
  333. title: "materiO'",
  334. // all titles will be injected into this template
  335. titleTemplate: "%s | materiO'"
  336. },
  337. render: h => h(VMainContent, { props: { id: id, html: main_html, isfront: drupalSettings.path.isFront } })
  338. }).$mount('#' + id)
  339. }
  340. function initVSearchBlock () {
  341. // console.log('router beforeEach to ', to)
  342. const id = 'block-materiosapisearchblock'
  343. let $search_block = document.getElementById(id)
  344. let formhtml = null
  345. if ($search_block) {
  346. // get the search form html to pass it as template to the vue
  347. // we gain display speed vs async downloaded data
  348. formhtml = $search_block.innerHTML
  349. } else {
  350. // else create the empty block to fill it later with async data
  351. $search_block = document.createElement('div')
  352. $search_block.setAttribute('id', id)
  353. // TODO: get region by REST
  354. const $region = document.getElementById('header-bottom')
  355. $region.appendChild($search_block)
  356. }
  357. // in any case create the vue
  358. _v_search_block = new Vue({
  359. store,
  360. i18n,
  361. render: h => h(VSearchBlock, { props: { blockid: id, formhtml: formhtml } })
  362. }).$mount('#' + id)
  363. }
  364. function initVLeftContent () {
  365. const id = 'content-left'
  366. // const $leftContent = document.getElementById(id)
  367. // in any case create the vue
  368. _v_left_content = new Vue({
  369. store,
  370. i18n,
  371. render: h => h(VLeftContent, { props: { id: id } })
  372. }).$mount('#' + id)
  373. }
  374. function initCoolLightBox () {
  375. const id = 'glog-coollightbox'
  376. const $clb_elmt = document.createElement('div')
  377. $clb_elmt.setAttribute('id', id)
  378. const $body = document.querySelector('body')
  379. $body.appendChild($clb_elmt)
  380. _v_glob_coollightbox = new Vue({
  381. store,
  382. i18n,
  383. render: h => h(GlobCoolLightBox, { props: { } })
  384. }).$mount('#' + id)
  385. console.log('_v_glob_coollightbox', _v_glob_coollightbox)
  386. }
  387. init()
  388. } // end MaterioTheme()
  389. // const materiotheme = new MaterioTheme()
  390. MaterioTheme()
  391. })(Drupal, drupalSettings, drupalDecoupled)