Skip to content

Commit

Permalink
Merge pull request #1805 from c2corg/js-size-optim
Browse files Browse the repository at this point in the history
Screen plugin optimization
  • Loading branch information
brunobesson authored Apr 12, 2021
2 parents af7e0f6 + aa9850a commit af16005
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
44 changes: 33 additions & 11 deletions src/js/vue-plugins/screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,60 @@ export default function install(Vue) {

data() {
return {
width: window.innerWidth,
height: window.innerHeight,
matchingQueryIndex: -1,
};
},

// https://bulma.io/documentation/modifiers/responsive-helpers/
computed: {
isMobile() {
return this.width <= BREAKPOINT_MOBILE;
return this.matchingQueryIndex === 0;
},
isTablet() {
return this.width > BREAKPOINT_MOBILE && this.width <= BREAKPOINT_TABLET;
return this.matchingQueryIndex === 1;
},
isDesktop() {
return this.width > BREAKPOINT_TABLET && this.width <= BREAKPOINT_DESKTOP;
return this.matchingQueryIndex === 2;
},
isWidescreen() {
return this.width > BREAKPOINT_DESKTOP && this.width <= BREAKPOINT_WIDESCREEN;
return this.matchingQueryIndex === 3;
},
isFullHD() {
return this.width > BREAKPOINT_WIDESCREEN;
return this.matchingQueryIndex === -1;
},
},

created() {
window.addEventListener('resize', this.onResize);
this.mediaQueryLists = [
BREAKPOINT_MOBILE,
BREAKPOINT_TABLET,
BREAKPOINT_DESKTOP,
BREAKPOINT_WIDESCREEN,
].map((breakpoint) => window.matchMedia(`only screen and (max-width: ${breakpoint}px)`));
this.mediaQueryLists.forEach((mediaQueryList) => {
if (mediaQueryList.addEventListener) {
mediaQueryList.addEventListener('change', this.onMediaQueryChange);
} else {
// support Safari < 14
mediaQueryList.addListener(this.onMediaQueryChange);
}
});
this.onMediaQueryChange(); // init
},

beforeDestroy() {
this.mediaQueryLists.forEach((mediaQueryList) => {
if (mediaQueryList.removeEventListener) {
mediaQueryList.removeEventListener('change', this.onMediaQueryChange);
} else {
mediaQueryList.removeListener(this.onMediaQueryChange);
}
});
},

methods: {
onResize() {
this.width = window.innerWidth;
this.height = window.innerHeight;
onMediaQueryChange() {
this.matchingQueryIndex = this.mediaQueryLists.findIndex((mediaQueryList) => mediaQueryList.matches);
},
},
});
Expand Down
5 changes: 4 additions & 1 deletion src/views/portals/FeedView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
</div>
<feed-widget :type="isPersonal && $user.isLogged ? 'personal' : 'default'" hide-empty-documents />
</div>
<div class="column is-hidden-mobile is-5-tablet is-5-desktop is-4-widescreen is-3-fullhd">
<div
v-if="!$screen.isMobile"
class="column is-hidden-mobile is-5-tablet is-5-desktop is-4-widescreen is-3-fullhd"
>
<h3 class="title is-3" v-translate>Last forum topics</h3>

<forum-widget wide class="box is-paddingless" />
Expand Down

0 comments on commit af16005

Please sign in to comment.