5
5
@touchstart =" onTouchStart"
6
6
@touchend =" onTouchEnd"
7
7
>
8
+ <VueMasteryBanner v-if =" isBannerOpen" @close-banner =" closeBanner" ref =' vueMasteryBanner' />
8
9
<Navbar v-if =" shouldShowNavbar" @toggle-sidebar =" toggleSidebar" />
9
10
10
11
<div class =" sidebar-mask" @click =" toggleSidebar(false)" />
@@ -36,6 +37,7 @@ import Home from '@theme/components/Home.vue'
36
37
import Navbar from ' @theme/components/Navbar.vue'
37
38
import Page from ' @theme/components/Page.vue'
38
39
import Sidebar from ' @theme/components/Sidebar.vue'
40
+ import VueMasteryBanner from ' @theme/components/sponsors/VueMasteryBanner.vue'
39
41
import { resolveSidebarItems } from ' ../util'
40
42
41
43
export default {
@@ -45,12 +47,17 @@ export default {
45
47
Home,
46
48
Page,
47
49
Sidebar,
48
- Navbar
50
+ Navbar,
51
+ VueMasteryBanner
49
52
},
50
53
51
54
data () {
52
55
return {
53
- isSidebarOpen: false
56
+ isSidebarOpen: false ,
57
+ isBannerOpen: true ,
58
+ isMenuFixed: false ,
59
+ nameStorage: ' vuemastery-black-firday-2020-banner' ,
60
+ menuPosition: 0
54
61
}
55
62
},
56
63
@@ -94,7 +101,9 @@ export default {
94
101
{
95
102
' no-navbar' : ! this .shouldShowNavbar ,
96
103
' sidebar-open' : this .isSidebarOpen ,
97
- ' no-sidebar' : ! this .shouldShowSidebar
104
+ ' no-sidebar' : ! this .shouldShowSidebar ,
105
+ ' vuemastery-menu-fixed' : this .isMenuFixed ,
106
+ ' vuemastery-promo' : this .isBannerOpen
98
107
},
99
108
userPageClass
100
109
]
@@ -105,6 +114,13 @@ export default {
105
114
this .$router .afterEach (() => {
106
115
this .isSidebarOpen = false
107
116
})
117
+
118
+ // Load component according to user preferences
119
+ if (! localStorage .getItem (this .nameStorage )) {
120
+ this .initBanner ()
121
+ } else {
122
+ this .isBannerOpen = false
123
+ }
108
124
},
109
125
110
126
methods: {
@@ -131,6 +147,54 @@ export default {
131
147
this .toggleSidebar (false )
132
148
}
133
149
}
150
+ },
151
+
152
+ // Vue Mastery Banner
153
+ initBanner () {
154
+ // Add event listeners
155
+ this .toggleBannerEvents (true )
156
+ // Add class to the body to push fixed elements
157
+ this .isBannerOpen = true
158
+ // Get the menu position
159
+ this .getMenuPosition ()
160
+ // Check current page offset position
161
+ this .isMenuFixed = this .isUnderBanner ()
162
+ },
163
+
164
+ closeBanner (e ) {
165
+ // Remove events
166
+ this .toggleBannerEvents (false )
167
+ // Hide the banner
168
+ this .isBannerOpen = false
169
+ // Save action in the local storage
170
+ localStorage .setItem (this .nameStorage , true )
171
+ },
172
+
173
+ getMenuPosition () {
174
+ this .menuPosition = this .$refs .vueMasteryBanner .$el .clientHeight
175
+ },
176
+
177
+ isUnderBanner () {
178
+ return window .pageYOffset > this .menuPosition
179
+ },
180
+
181
+ fixMenuAfterBanner () {
182
+ if (this .isUnderBanner ()) {
183
+ if (! this .isMenuFixed ) {
184
+ // The menu will be fixed
185
+ this .isMenuFixed = true
186
+ }
187
+ } else if (this .isMenuFixed ) {
188
+ // The menu stay under the banner
189
+ this .isMenuFixed = false
190
+ }
191
+ },
192
+
193
+ toggleBannerEvents (on ) {
194
+ // Add or remove event listerners attached to the DOM
195
+ let method = on ? " addEventListener" : " removeEventListener"
196
+ window [method](" resize" , this .getMenuPosition )
197
+ window [method](" scroll" , this .fixMenuAfterBanner )
134
198
}
135
199
}
136
200
}
0 commit comments