main.js 8.4 KB

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