main.js 11 KB

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