main.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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.replace(/^\//, '').split('/')
  125. // TODO: remove language relative prefix from path classes (fr, en, etc)
  126. for (var i = 0; i < path_parts.length; i++) {
  127. if (i == 0) {
  128. var c = 'path-' + path_parts[i]
  129. } else if (path_parts[i] !== '') {
  130. var c = classes[i - 1] + '-' + path_parts[i]
  131. }
  132. classes.push(c)
  133. }
  134. }
  135. document.querySelector('body').classList.add(...classes)
  136. // trigger router
  137. next()
  138. })
  139. }
  140. function initVSiteBrandingBlock () {
  141. _v_sitebranding_block = new Vue({
  142. store,
  143. i18n,
  144. router,
  145. el: '#block-sitebranding',
  146. methods: {
  147. onclick (event) {
  148. // console.log("Clicked on logo event", event);
  149. const href = event.target.getAttribute('href')
  150. // console.log("Clicked on logo href", href);
  151. this.$router.push(href)
  152. // replaced by router.beforeEach
  153. // this.$store.commit('Common/setPagetitle', null)
  154. }
  155. }
  156. })
  157. }
  158. function initVPagetitleBlock () {
  159. const $blk = document.querySelector('#block-pagetitle')
  160. const $h2 = $blk.querySelector('h2')
  161. // get the loaded pagetitle
  162. const title = $h2.innerText
  163. // if not front recorde the loaded pagetitle in store
  164. if (!_is_front) {
  165. store.commit('Common/setPagetitle', title)
  166. }
  167. // replace in template the pagetitle by vue binding
  168. $h2.innerText = '{{ pagetitle }}'
  169. // create the vue
  170. _v_pagetitle_block = new Vue({
  171. store,
  172. i18n,
  173. router,
  174. el: $blk,
  175. computed: {
  176. ...mapState({
  177. pagetitle: state => state.Common.pagetitle
  178. })
  179. }
  180. })
  181. }
  182. function initVUserBlock () {
  183. const mount_point = drupalSettings.user.uid !== 0 ? 'block-userblock' : 'block-userlogin'
  184. const props = {
  185. title: '',
  186. loginblock: ''
  187. }
  188. let $block
  189. switch (mount_point) {
  190. case 'block-userlogin':
  191. $block = document.getElementById(mount_point)
  192. console.log('initVUserBlock login form html', $block)
  193. props.loginblock = $block.outerHTML.trim()
  194. break
  195. case 'block-userblock':
  196. default:
  197. break
  198. }
  199. _v_user_block = new Vue({
  200. store,
  201. i18n,
  202. // computed: {
  203. // ...mapState({
  204. // isloggedin: state => state.User.isloggedin
  205. // })
  206. // },
  207. created () {
  208. // if already loggedin, call store.user to get the user infos
  209. if (drupalSettings.user.uid !== 0) {
  210. this.$store.commit('User/setUid', drupalSettings.user.uid)
  211. this.$store.dispatch('User/getUser')
  212. }
  213. },
  214. render: h => h(VUserBlock, { props: props })
  215. }).$mount('#' + mount_point)
  216. // console.log('initVUserBlock', _v_user_block);
  217. }
  218. function initVHeaderMenu () {
  219. // console.log('initVHeaderMenu');
  220. // adding vuejs attributes has it wont work on twig template (see menu--header.html.twig)
  221. // not working : String contains an invalid character
  222. // document.querySelectorAll(`#block-header a`).forEach(link => {
  223. // console.log(link);
  224. // link.setAttribute('@click.prevent', 'onclick')
  225. // });
  226. _v_header_menu = new Vue({
  227. store,
  228. i18n,
  229. router,
  230. el: '#block-header',
  231. methods: {
  232. onclick (event) {
  233. // console.log("Clicked on header menu link", event);
  234. const href = event.target.getAttribute('href')
  235. // let title = event.target.innerText;
  236. // console.log("Clicked on header menu link : href", href);
  237. this.$router.push(href)
  238. // replaced by router.beforeEach
  239. // this.$store.commit('Common/setPagetitle', title)
  240. }
  241. }
  242. })
  243. }
  244. function initVMainContent () {
  245. const id = 'main-content'
  246. const $main_content = document.querySelector('#' + id)
  247. // console.log('main-content', $main_content);
  248. const main_html = $main_content.innerHTML
  249. _v_main_content = new Vue({
  250. store,
  251. i18n,
  252. metaInfo: {
  253. // if no subcomponents specify a metaInfo.title, this title will be used
  254. title: "materiO'",
  255. // all titles will be injected into this template
  256. titleTemplate: "%s | materiO'"
  257. },
  258. render: h => h(VMainContent, { props: { id: id, html: main_html, isfront: drupalSettings.path.isFront } })
  259. }).$mount('#' + id)
  260. }
  261. function initVSearchBlock () {
  262. // console.log('initVSearchBlock');
  263. const id = 'block-materiosapisearchblock'
  264. let $search_block = document.getElementById(id)
  265. let formhtml = null
  266. if ($search_block) {
  267. // get the search form html to pass it as template to the vue
  268. // we gain display speed vs async downloaded data
  269. formhtml = $search_block.innerHTML
  270. } else {
  271. // else create the empty block to fill it later with async data
  272. $search_block = document.createElement('div')
  273. $search_block.setAttribute('id', id)
  274. // TODO: get region by REST
  275. const $region = document.getElementById('header-bottom')
  276. $region.appendChild($search_block)
  277. }
  278. // in any case create the vue
  279. _v_search_block = new Vue({
  280. store,
  281. i18n,
  282. render: h => h(VSearchBlock, { props: { blockid: id, formhtml: formhtml } })
  283. }).$mount('#' + id)
  284. }
  285. function initVLeftContent () {
  286. const id = 'content-left'
  287. const $leftContent = document.getElementById(id)
  288. // in any case create the vue
  289. _v_left_content = new Vue({
  290. store,
  291. i18n,
  292. render: h => h(VLeftContent, { props: { id: id } })
  293. }).$mount('#' + id)
  294. }
  295. init()
  296. } // end MaterioTheme()
  297. const materiotheme = new MaterioTheme()
  298. })(Drupal, drupalSettings, drupalDecoupled)