main.js 9.0 KB

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