main.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import Vue from 'vue'
  2. import InfiniteLoading from 'vue-infinite-loading';
  3. Vue.use(InfiniteLoading, {
  4. props: {
  5. spinner: 'spiral',
  6. // slots.noMore: ''
  7. },
  8. // system: {
  9. // throttleLimit: 50,
  10. // /* other settings need to configure */
  11. // }
  12. });
  13. import store from 'vuejs/store'
  14. import router from 'vuejs/route'
  15. import VUserBlock from 'vuejs/components/Block/UserBlock'
  16. import VMainContent from 'vuejs/components/Content/MainContent'
  17. import VSearchBlock from 'vuejs/components/Block/SearchBlock'
  18. import { mapState } from 'vuex'
  19. // require('theme/assets/styles/main.scss');
  20. import 'theme/assets/styles/main.scss'
  21. (function(Drupal, drupalSettings) {
  22. var MaterioTheme = function(){
  23. var _v_sitebranding_block, _v_user_block, _v_header_menu
  24. , _v_pagetitle_block, _v_search_block
  25. , _v_main_content;
  26. var _is_front = drupalSettings.path.isFront;
  27. console.log('drupalSettings', drupalSettings);
  28. // ___ _ _
  29. // |_ _|_ _ (_) |_
  30. // | || ' \| | _|
  31. // |___|_||_|_|\__|
  32. function init(){
  33. console.log("MaterioTheme init()")
  34. initVues()
  35. }
  36. function initVues(){
  37. initVRouter();
  38. initVSiteBrandingBlock()
  39. initVPagetitleBlock()
  40. initVUserBlock()
  41. initVHeaderMenu()
  42. initVMainContent()
  43. initVSearchBlock()
  44. }
  45. function initVRouter(){
  46. // we need this to update the title while using history nav
  47. router.beforeEach((to, from, next) => {
  48. store.commit('Common/setPagetitle', to.name != 'home' ? to.name : null)
  49. next();
  50. })
  51. }
  52. function initVSiteBrandingBlock(){
  53. _v_sitebranding_block = new Vue({
  54. store,
  55. router,
  56. el: '#block-sitebranding',
  57. methods: {
  58. onclick(event){
  59. // console.log("Clicked on logo event", event);
  60. let href = event.target.getAttribute('href');
  61. // console.log("Clicked on logo href", href);
  62. this.$router.push(href)
  63. // replaced by router.beforeEach
  64. // this.$store.commit('Common/setPagetitle', null)
  65. }
  66. }
  67. })
  68. }
  69. function initVPagetitleBlock(){
  70. let $blk = document.querySelector('#block-pagetitle')
  71. let $h2 = $blk.querySelector('h2')
  72. // get the loaded pagetitle
  73. let title = $h2.innerText
  74. // if not front recorde the loaded pagetitle in store
  75. if (!_is_front) {
  76. store.commit('Common/setPagetitle', title)
  77. }
  78. // replace in template the pagetitle by vue binding
  79. $h2.innerText = '{{ pagetitle }}'
  80. // create the vue
  81. _v_pagetitle_block = new Vue({
  82. store,
  83. router,
  84. el: $blk,
  85. computed: {
  86. ...mapState({
  87. pagetitle: state => state.Common.pagetitle
  88. })
  89. },
  90. })
  91. }
  92. function initVUserBlock(){
  93. let mount_point = drupalSettings.user.uid !== 0 ? 'block-userblock' : 'block-userlogin';
  94. let props = {
  95. title: "",
  96. loginblock:""
  97. };
  98. switch (mount_point) {
  99. case 'block-userlogin':
  100. let $block = document.getElementById(mount_point);
  101. console.log('initVUserBlock login form html', $block);
  102. props.loginblock = $block.outerHTML.trim()
  103. break;
  104. case 'block-userblock':
  105. default:
  106. break;
  107. }
  108. _v_user_block = new Vue({
  109. store,
  110. // computed: {
  111. // ...mapState({
  112. // isloggedin: state => state.User.isloggedin
  113. // })
  114. // },
  115. created () {
  116. // if already loggedin, call store.user to get the user infos
  117. if(drupalSettings.user.uid !== 0){
  118. this.$store.commit('User/setUid', drupalSettings.user.uid)
  119. this.$store.dispatch('User/getUser')
  120. }
  121. },
  122. render: h => h(VUserBlock, {props:props})
  123. }).$mount('#'+mount_point)
  124. // console.log('initVUserBlock', _v_user_block);
  125. }
  126. function initVHeaderMenu(){
  127. // console.log('initVHeaderMenu');
  128. // adding vuejs attributes has it wont work on twig template (see menu--header.html.twig)
  129. // not working : String contains an invalid character
  130. // document.querySelectorAll(`#block-header a`).forEach(link => {
  131. // console.log(link);
  132. // link.setAttribute('@click.prevent', 'onclick')
  133. // });
  134. _v_header_menu = new Vue({
  135. store,
  136. router,
  137. el: `#block-header`,
  138. methods: {
  139. onclick(event){
  140. // console.log("Clicked on header menu link", event);
  141. let href = event.target.getAttribute('href');
  142. let title = event.target.innerText;
  143. // console.log("Clicked on header menu link : href", href);
  144. this.$router.push(href)
  145. // replaced by router.beforeEach
  146. // this.$store.commit('Common/setPagetitle', title)
  147. }
  148. }
  149. })
  150. }
  151. function initVMainContent(){
  152. let id = "main-content"
  153. let $main_content = document.querySelector('#'+id)
  154. // console.log('main-content', $main_content);
  155. let main_html = $main_content.innerHTML
  156. _v_main_content = new Vue({
  157. store,
  158. render: h => h(VMainContent, {props:{id:id, html:main_html, isfront:drupalSettings.path.isFront}})
  159. }).$mount('#'+id)
  160. }
  161. function initVSearchBlock(){
  162. // console.log('initVSearchBlock');
  163. let id = "block-materiosapisearchblock"
  164. let $search_block = document.getElementById(id)
  165. var formhtml = null
  166. if($search_block){
  167. // get the search form html to pass it as template to the vue
  168. // we gain display speed vs async downloaded data
  169. formhtml = $search_block.innerHTML
  170. }else{
  171. // else create the empty block to fill it later with async data
  172. $search_block = document.createElement('div')
  173. $search_block.setAttribute('id', id)
  174. // TODO: get region by REST
  175. let $region = document.getElementById('header-bottom');
  176. $region.appendChild($search_block);
  177. }
  178. // in any case create the vue
  179. _v_search_block = new Vue({
  180. store,
  181. render: h => h(VSearchBlock, {props:{blockid:id, formhtml:formhtml}})
  182. }).$mount('#'+id)
  183. }
  184. init()
  185. } // end MaterioTheme()
  186. var materiotheme = new MaterioTheme();
  187. })(Drupal, drupalSettings);