main.js 9.0 KB

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