main.js 10 KB

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