main.js 11 KB

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