main.js 10 KB

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