main.js 8.2 KB

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