main.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 and body classes while using history nav
  47. router.beforeEach((to, from, next) => {
  48. // console.log('router beforeEach to ', to);
  49. // commit new title to store
  50. store.commit('Common/setPagetitle', to.name != 'home' ? to.name : null)
  51. // remove all path related body classes
  52. let body_classes = document.querySelector('body').classList;
  53. let classes_to_rm = [];
  54. for (var i = 0; i < body_classes.length; i++) {
  55. if(body_classes[i].startsWith('path-')){
  56. classes_to_rm.push(body_classes[i]);
  57. }
  58. }
  59. document.querySelector('body').classList.remove(...classes_to_rm);
  60. // add new path classes to body
  61. let classes = [];
  62. if(to.path == '/'){
  63. classes.push('path-home');
  64. }else{
  65. let path_parts = to.path.replace(/^\//, '').split('/');
  66. for (var i = 0; i < path_parts.length; i++) {
  67. if(i == 0){
  68. var c = "path-" + path_parts[i];
  69. }else if (path_parts[i] !== ''){
  70. var c = classes[i-1] +'-'+ path_parts[i];
  71. }
  72. classes.push(c)
  73. }
  74. }
  75. document.querySelector('body').classList.add(...classes);
  76. // trigger router
  77. next();
  78. })
  79. }
  80. function initVSiteBrandingBlock(){
  81. _v_sitebranding_block = new Vue({
  82. store,
  83. router,
  84. el: '#block-sitebranding',
  85. methods: {
  86. onclick(event){
  87. // console.log("Clicked on logo event", event);
  88. let href = event.target.getAttribute('href');
  89. // console.log("Clicked on logo href", href);
  90. this.$router.push(href)
  91. // replaced by router.beforeEach
  92. // this.$store.commit('Common/setPagetitle', null)
  93. }
  94. }
  95. })
  96. }
  97. function initVPagetitleBlock(){
  98. let $blk = document.querySelector('#block-pagetitle')
  99. let $h2 = $blk.querySelector('h2')
  100. // get the loaded pagetitle
  101. let title = $h2.innerText
  102. // if not front recorde the loaded pagetitle in store
  103. if (!_is_front) {
  104. store.commit('Common/setPagetitle', title)
  105. }
  106. // replace in template the pagetitle by vue binding
  107. $h2.innerText = '{{ pagetitle }}'
  108. // create the vue
  109. _v_pagetitle_block = new Vue({
  110. store,
  111. router,
  112. el: $blk,
  113. computed: {
  114. ...mapState({
  115. pagetitle: state => state.Common.pagetitle
  116. })
  117. },
  118. })
  119. }
  120. function initVUserBlock(){
  121. let mount_point = drupalSettings.user.uid !== 0 ? 'block-userblock' : 'block-userlogin';
  122. let props = {
  123. title: "",
  124. loginblock:""
  125. };
  126. switch (mount_point) {
  127. case 'block-userlogin':
  128. let $block = document.getElementById(mount_point);
  129. console.log('initVUserBlock login form html', $block);
  130. props.loginblock = $block.outerHTML.trim()
  131. break;
  132. case 'block-userblock':
  133. default:
  134. break;
  135. }
  136. _v_user_block = new Vue({
  137. store,
  138. // computed: {
  139. // ...mapState({
  140. // isloggedin: state => state.User.isloggedin
  141. // })
  142. // },
  143. created () {
  144. // if already loggedin, call store.user to get the user infos
  145. if(drupalSettings.user.uid !== 0){
  146. this.$store.commit('User/setUid', drupalSettings.user.uid)
  147. this.$store.dispatch('User/getUser')
  148. }
  149. },
  150. render: h => h(VUserBlock, {props:props})
  151. }).$mount('#'+mount_point)
  152. // console.log('initVUserBlock', _v_user_block);
  153. }
  154. function initVHeaderMenu(){
  155. // console.log('initVHeaderMenu');
  156. // adding vuejs attributes has it wont work on twig template (see menu--header.html.twig)
  157. // not working : String contains an invalid character
  158. // document.querySelectorAll(`#block-header a`).forEach(link => {
  159. // console.log(link);
  160. // link.setAttribute('@click.prevent', 'onclick')
  161. // });
  162. _v_header_menu = new Vue({
  163. store,
  164. router,
  165. el: `#block-header`,
  166. methods: {
  167. onclick(event){
  168. // console.log("Clicked on header menu link", event);
  169. let href = event.target.getAttribute('href');
  170. // let title = event.target.innerText;
  171. // console.log("Clicked on header menu link : href", href);
  172. this.$router.push(href)
  173. // replaced by router.beforeEach
  174. // this.$store.commit('Common/setPagetitle', title)
  175. }
  176. }
  177. })
  178. }
  179. function initVMainContent(){
  180. let id = "main-content"
  181. let $main_content = document.querySelector('#'+id)
  182. // console.log('main-content', $main_content);
  183. let main_html = $main_content.innerHTML
  184. _v_main_content = new Vue({
  185. store,
  186. render: h => h(VMainContent, {props:{id:id, html:main_html, isfront:drupalSettings.path.isFront}})
  187. }).$mount('#'+id)
  188. }
  189. function initVSearchBlock(){
  190. // console.log('initVSearchBlock');
  191. let id = "block-materiosapisearchblock"
  192. let $search_block = document.getElementById(id)
  193. var formhtml = null
  194. if($search_block){
  195. // get the search form html to pass it as template to the vue
  196. // we gain display speed vs async downloaded data
  197. formhtml = $search_block.innerHTML
  198. }else{
  199. // else create the empty block to fill it later with async data
  200. $search_block = document.createElement('div')
  201. $search_block.setAttribute('id', id)
  202. // TODO: get region by REST
  203. let $region = document.getElementById('header-bottom');
  204. $region.appendChild($search_block);
  205. }
  206. // in any case create the vue
  207. _v_search_block = new Vue({
  208. store,
  209. render: h => h(VSearchBlock, {props:{blockid:id, formhtml:formhtml}})
  210. }).$mount('#'+id)
  211. }
  212. init()
  213. } // end MaterioTheme()
  214. var materiotheme = new MaterioTheme();
  215. })(Drupal, drupalSettings);