main.js 14 KB

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