main.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_pagetitle_block, _v_user_block, _v_main_content, _v_search_block;
  24. var _is_front = drupalSettings.path.isFront;
  25. console.log('drupalSettings', drupalSettings);
  26. // ___ _ _
  27. // |_ _|_ _ (_) |_
  28. // | || ' \| | _|
  29. // |___|_||_|_|\__|
  30. function init(){
  31. console.log("MaterioTheme init()")
  32. initVues()
  33. }
  34. function initVues(){
  35. initVSiteBrandingBlock()
  36. initVPagetitleBlock()
  37. initVUserBlock()
  38. initVMainContent()
  39. initVSearchBlock()
  40. }
  41. function initVSiteBrandingBlock(){
  42. _v_sitebranding_block = new Vue({
  43. store,
  44. router,
  45. el: '#block-sitebranding',
  46. methods: {
  47. onclick(event){
  48. // console.log("Clicked on logo event", event);
  49. let href = event.target.getAttribute('href');
  50. // console.log("Clicked on logo href", href);
  51. this.$router.push(href)
  52. this.$store.commit('Common/setPagetitle', null)
  53. }
  54. }
  55. })
  56. }
  57. function initVPagetitleBlock(){
  58. let $blk = document.querySelector('#block-pagetitle')
  59. let $h2 = $blk.querySelector('h2')
  60. // get the loaded pagetitle
  61. let title = $h2.innerText
  62. // if not front recorde the loaded pagetitle in store
  63. if (!_is_front) {
  64. store.commit('Common/setPagetitle', title)
  65. }
  66. // replace in template the pagetitle by vue binding
  67. $h2.innerText = '{{ pagetitle }}'
  68. // create the vue
  69. _v_pagetitle_block = new Vue({
  70. store,
  71. router,
  72. el: $blk,
  73. computed: {
  74. ...mapState({
  75. pagetitle: state => state.Common.pagetitle
  76. })
  77. },
  78. })
  79. }
  80. function initVUserBlock(){
  81. let mount_point = drupalSettings.user.uid !== 0 ? 'block-userblock' : 'block-userlogin';
  82. let props = {
  83. title: "",
  84. loginblock:""
  85. };
  86. switch (mount_point) {
  87. case 'block-userlogin':
  88. let $block = document.getElementById(mount_point);
  89. console.log('initVUserBlock login form html', $block);
  90. props.loginblock = $block.outerHTML.trim()
  91. break;
  92. case 'block-userblock':
  93. default:
  94. break;
  95. }
  96. _v_user_block = new Vue({
  97. store,
  98. // computed: {
  99. // ...mapState({
  100. // isloggedin: state => state.User.isloggedin
  101. // })
  102. // },
  103. created () {
  104. // if already loggedin, call store.user to get the user infos
  105. if(drupalSettings.user.uid !== 0){
  106. this.$store.commit('User/setUid', drupalSettings.user.uid)
  107. this.$store.dispatch('User/getUser')
  108. }
  109. },
  110. render: h => h(VUserBlock, {props:props})
  111. }).$mount('#'+mount_point)
  112. // console.log('initVUserBlock', _v_user_block);
  113. }
  114. function initVMainContent(){
  115. let id = "main-content"
  116. let $main_content = document.querySelector('#'+id)
  117. // console.log('main-content', $main_content);
  118. let main_html = $main_content.innerHTML
  119. _v_main_content = new Vue({
  120. store,
  121. render: h => h(VMainContent, {props:{id:id, html:main_html, isfront:drupalSettings.path.isFront}})
  122. }).$mount('#'+id)
  123. // console.log('initTestVContent', v_test_content);
  124. }
  125. function initVSearchBlock(){
  126. // console.log('initVSearchBlock');
  127. let id = "block-materiosapisearchblock"
  128. let $search_block = document.getElementById(id)
  129. var formhtml = null
  130. if($search_block){
  131. // get the search form html to pass it as template to the vue
  132. // we gain display speed vs async downloaded data
  133. formhtml = $search_block.innerHTML
  134. }else{
  135. // else create the empty block to fill it later with async data
  136. $search_block = document.createElement('div')
  137. $search_block.setAttribute('id', id)
  138. // TODO: get region by REST
  139. let $region = document.getElementById('header-bottom');
  140. $region.appendChild($search_block);
  141. }
  142. // in any case create the vue
  143. _v_search_block = new Vue({
  144. store,
  145. render: h => h(VSearchBlock, {props:{blockid:id, formhtml:formhtml}})
  146. }).$mount('#'+id)
  147. }
  148. init()
  149. } // end MaterioTheme()
  150. var materiotheme = new MaterioTheme();
  151. })(Drupal, drupalSettings);