main.js 7.6 KB

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