main.js 13 KB

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