main.js 10 KB

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