main.js 9.7 KB

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