main.js 10 KB

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