main.js 8.0 KB

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