-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathClient.js
68 lines (54 loc) · 1.72 KB
/
Client.js
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
import AttributeTracker from './AttributeTracker'
import BindingManager from './BindingManager'
import Component from './Component'
import { documentLoaded, beforeDocumentUnload } from './documentLifecycle'
import getFallbackConsumer from './getFallbackConsumer'
export default class Client {
constructor (options = {}) {
Object.assign(this, Client.defaultOptions, options)
this.consumer = this.consumer || getFallbackConsumer()
this._componentSelector = `[${this.keyAttribute}][${this.stateAttribute}]`
this._componentTracker =
new AttributeTracker(this.keyAttribute, (element) => (
element.hasAttribute(this.stateAttribute) // ensure matches selector
? new Component(this, element)
: null
))
this._motionTracker =
new AttributeTracker(this.motionAttribute, (element) => (
new BindingManager(this, element)
))
documentLoaded.then(() => { // avoid mutations while loading the document
this._componentTracker.attachRoot(this.root)
this._motionTracker.attachRoot(this.root)
})
if (this.shutdownBeforeUnload) {
beforeDocumentUnload.then(() => this.shutdown())
}
}
log (...args) {
if (this.logging) {
console.log('[Motion]', ...args)
}
}
getComponent (element) {
return this._componentTracker.getManager(
element.closest(this._componentSelector)
)
}
shutdown () {
this._componentTracker.shutdown()
this._motionTracker.shutdown()
}
}
Client.defaultOptions = {
getExtraDataForEvent (_event) {
// noop
},
logging: false,
root: document,
shutdownBeforeUnload: true,
keyAttribute: 'data-motion-key',
stateAttribute: 'data-motion-state',
motionAttribute: 'data-motion'
}