forked from FrontendMatter/material-design-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrawer.js
More file actions
215 lines (186 loc) · 4.17 KB
/
Copy pathdrawer.js
File metadata and controls
215 lines (186 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { handler } from 'dom-factory'
/**
* A navigation drawer that can slide in from the left or right
* @param {HTMLElement} element
* @return {Object}
*/
export const drawerComponent = () => ({
/**
* Public properties.
* @type {Object}
*/
properties: {
/**
* The opened state of the drawer.
* @type {Object}
*/
opened: {
type: Boolean,
reflectToAttribute: true
},
/**
* The drawer does not have a scrim.
* @type {Object}
*/
persistent: {
type: Boolean,
reflectToAttribute: true
},
/**
* The alignment of the drawer on the screen ('left', 'right', 'start' or 'end').
* 'start' computes to left and 'end' to right in LTR and RTL layouts.
* @type {Object}
*/
align: {
reflectToAttribute: true,
value: 'left'
},
/**
* The computed drawer position on the screen ('left' or 'right').
* @type {Object}
*/
position: {
reflectToAttribute: true
}
},
/**
* Property change observers.
* @type {Array}
*/
observers: [
'_resetPosition(align)',
'_fireChange(opened, persistent, align, position)',
'_onChangedState(_drawerState)'
],
/**
* Event listeners.
* @type {Array}
*/
listeners: [
'_onTransitionend(transitionend)',
'scrim._onClickScrim(click)'
],
// The current drawer state
_drawerState: 0,
// Possible drawer states
_DRAWER_STATE: {
INIT: 0,
OPENED: 1,
OPENED_PERSISTENT: 2,
CLOSED: 3
},
/**
* The drawer content HTMLElement
* @return {HTMLElement}
*/
get contentContainer () {
return this.element.querySelector('.mdk-drawer__content')
},
/**
* The drawer scrim HTMLElement
* @return {HTMLElement}
*/
get scrim () {
let scrim = this.element.querySelector('.mdk-drawer__scrim')
if (!scrim) {
scrim = document.createElement('DIV')
this.element.insertBefore(scrim, this.element.childNodes[0])
scrim.classList.add('mdk-drawer__scrim')
}
return scrim
},
/**
* Get the width of the drawer.
* @return {String}
*/
getWidth () {
return this.contentContainer.offsetWidth
},
/**
* Toggles the drawer opened state.
*/
toggle () {
this.opened = !this.opened
},
/**
* Closes the drawer.
*/
close () {
this.opened = false
},
/**
* Opens the drawer.
*/
open () {
this.opened = true
},
_isRTL () {
return window.getComputedStyle(this.element).direction === 'rtl'
},
_setTransitionDuration (duration) {
this.contentContainer.style.transitionDuration = duration
this.scrim.style.transitionDuration = duration
},
_resetDrawerState () {
let oldState = this._drawerState
if (this.opened) {
this._drawerState = this.persistent
? this._DRAWER_STATE.OPENED_PERSISTENT : this._DRAWER_STATE.OPENED
}
else {
this._drawerState = this._DRAWER_STATE.CLOSED
}
if (oldState !== this._drawerState) {
if (this._drawerState === this._DRAWER_STATE.OPENED) {
document.body.style.overflow = 'hidden'
}
else {
document.body.style.overflow = ''
}
}
},
_resetPosition () {
switch (this.align) {
case 'start':
this.position = this._isRTL() ? 'right' : 'left'
return
case 'end':
this.position = this._isRTL() ? 'left' : 'right'
return
}
this.position = this.align
},
_fireChange () {
this.fire('mdk-drawer-change')
},
_fireChanged () {
this.fire('mdk-drawer-changed')
},
_onTransitionend (event) {
let target = event.target
if (target === this.contentContainer || target === this.scrim) {
this._resetDrawerState()
}
},
_onClickScrim (event) {
event.preventDefault()
this.close()
},
_onChangedState (newState, oldState) {
if (oldState !== this._DRAWER_STATE.INIT) {
this._fireChanged()
}
},
/**
* Initialize component
*/
init () {
this._resetPosition()
this._setTransitionDuration('0s')
setTimeout(() => {
this._setTransitionDuration('')
this._resetDrawerState()
}, 0)
}
})
handler.register('mdk-drawer', drawerComponent)