main.js 8.5 KB

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