main.js 12 KB

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