main.js 7.8 KB

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