main.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. // TODO: remove language relative prefix from path classes (fr, en, etc)
  98. for (var i = 0; i < path_parts.length; i++) {
  99. if(i == 0){
  100. var c = "path-" + path_parts[i];
  101. }else if (path_parts[i] !== ''){
  102. var c = classes[i-1] +'-'+ path_parts[i];
  103. }
  104. classes.push(c)
  105. }
  106. }
  107. document.querySelector('body').classList.add(...classes);
  108. // trigger router
  109. next();
  110. })
  111. }
  112. function initVSiteBrandingBlock(){
  113. _v_sitebranding_block = new Vue({
  114. store,
  115. router,
  116. el: '#block-sitebranding',
  117. methods: {
  118. onclick(event){
  119. // console.log("Clicked on logo event", event);
  120. let href = event.target.getAttribute('href');
  121. // console.log("Clicked on logo href", href);
  122. this.$router.push(href)
  123. // replaced by router.beforeEach
  124. // this.$store.commit('Common/setPagetitle', null)
  125. }
  126. }
  127. })
  128. }
  129. function initVPagetitleBlock(){
  130. let $blk = document.querySelector('#block-pagetitle')
  131. let $h2 = $blk.querySelector('h2')
  132. // get the loaded pagetitle
  133. let title = $h2.innerText
  134. // if not front recorde the loaded pagetitle in store
  135. if (!_is_front) {
  136. store.commit('Common/setPagetitle', title)
  137. }
  138. // replace in template the pagetitle by vue binding
  139. $h2.innerText = '{{ pagetitle }}'
  140. // create the vue
  141. _v_pagetitle_block = new Vue({
  142. store,
  143. router,
  144. el: $blk,
  145. computed: {
  146. ...mapState({
  147. pagetitle: state => state.Common.pagetitle
  148. })
  149. },
  150. })
  151. }
  152. function initVUserBlock(){
  153. let mount_point = drupalSettings.user.uid !== 0 ? 'block-userblock' : 'block-userlogin';
  154. let props = {
  155. title: "",
  156. loginblock:""
  157. };
  158. switch (mount_point) {
  159. case 'block-userlogin':
  160. let $block = document.getElementById(mount_point);
  161. console.log('initVUserBlock login form html', $block);
  162. props.loginblock = $block.outerHTML.trim()
  163. break;
  164. case 'block-userblock':
  165. default:
  166. break;
  167. }
  168. _v_user_block = new Vue({
  169. store,
  170. // computed: {
  171. // ...mapState({
  172. // isloggedin: state => state.User.isloggedin
  173. // })
  174. // },
  175. created () {
  176. // if already loggedin, call store.user to get the user infos
  177. if(drupalSettings.user.uid !== 0){
  178. this.$store.commit('User/setUid', drupalSettings.user.uid)
  179. this.$store.dispatch('User/getUser')
  180. }
  181. },
  182. render: h => h(VUserBlock, {props:props})
  183. }).$mount('#'+mount_point)
  184. // console.log('initVUserBlock', _v_user_block);
  185. }
  186. function initVHeaderMenu(){
  187. // console.log('initVHeaderMenu');
  188. // adding vuejs attributes has it wont work on twig template (see menu--header.html.twig)
  189. // not working : String contains an invalid character
  190. // document.querySelectorAll(`#block-header a`).forEach(link => {
  191. // console.log(link);
  192. // link.setAttribute('@click.prevent', 'onclick')
  193. // });
  194. _v_header_menu = new Vue({
  195. store,
  196. router,
  197. el: `#block-header`,
  198. methods: {
  199. onclick(event){
  200. // console.log("Clicked on header menu link", event);
  201. let href = event.target.getAttribute('href');
  202. // let title = event.target.innerText;
  203. // console.log("Clicked on header menu link : href", href);
  204. this.$router.push(href)
  205. // replaced by router.beforeEach
  206. // this.$store.commit('Common/setPagetitle', title)
  207. }
  208. }
  209. })
  210. }
  211. function initVMainContent(){
  212. let id = "main-content"
  213. let $main_content = document.querySelector('#'+id)
  214. // console.log('main-content', $main_content);
  215. let main_html = $main_content.innerHTML
  216. _v_main_content = new Vue({
  217. store,
  218. render: h => h(VMainContent, {props:{id:id, html:main_html, isfront:drupalSettings.path.isFront}})
  219. }).$mount('#'+id)
  220. }
  221. function initVSearchBlock(){
  222. // console.log('initVSearchBlock');
  223. let id = "block-materiosapisearchblock"
  224. let $search_block = document.getElementById(id)
  225. var formhtml = null
  226. if($search_block){
  227. // get the search form html to pass it as template to the vue
  228. // we gain display speed vs async downloaded data
  229. formhtml = $search_block.innerHTML
  230. }else{
  231. // else create the empty block to fill it later with async data
  232. $search_block = document.createElement('div')
  233. $search_block.setAttribute('id', id)
  234. // TODO: get region by REST
  235. let $region = document.getElementById('header-bottom');
  236. $region.appendChild($search_block);
  237. }
  238. // in any case create the vue
  239. _v_search_block = new Vue({
  240. store,
  241. render: h => h(VSearchBlock, {props:{blockid:id, formhtml:formhtml}})
  242. }).$mount('#'+id)
  243. }
  244. init()
  245. } // end MaterioTheme()
  246. var materiotheme = new MaterioTheme();
  247. })(Drupal, drupalSettings, drupalDecoupled);