main.js 11 KB

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