main.js 13 KB

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