main.js 8.9 KB

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