-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-reactive.js
89 lines (79 loc) · 2.29 KB
/
simple-reactive.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
export class SimpleReactive {
element = 'simple-reactive'
id = Math.random().toString(36).substr(2, 9)
constructor () {
if (!document.simpleReactiveRegistry) {
document.simpleReactiveRegistry = {}
}
document.simpleReactiveRegistry[this.instanceKey] = this
const instance = this
Object.defineProperty(this, 'data', {
value: new Proxy({}, {
get (target, name) {
if (target[name] === undefined) {
throw new Error(`${name} not found in data`)
}
console.info(name, target[name])
return target[name]
},
set (target, p, value, proxy) {
target[p] = value
if (instance._created) {
instance.render()
}
return true
}
}),
writable: false
})
if (this.created instanceof Function) {
this.created()
}
}
get myPathInRegistry () {
return `document.simpleReactiveRegistry['${this.instanceKey}']`
}
get instanceName () {
return this.constructor.name
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
.replace(/([A-Z])([A-Z])(?=[a-z])/g, '$1-$2')
.toLowerCase()
}
get instanceKey () {
return '_' + this.instanceName + '_' + this.id
}
get preparedTemplate () {
return this.template.replace(/on([a-z].+?)="(.+?)"/g, (match, event, method) => {
return `on${event}="${this.myPathInRegistry}.${method}"`
}).replace(/{{(.+?)}}/g, (match, p1) => {
if (p1 === undefined) {
throw new Error(`${p1} not found in component`)
}
return this.data[p1]
})
}
get wrappedTemplate () {
return `<${this.instanceName} simple-reactive="${this.instanceKey}">${this.preparedTemplate}</${this.instanceName}>`
}
render () {
if (this.element) {
const element = document.querySelector(`[simple-reactive="${this.instanceKey}"]`)
console.info(element)
element.innerHTML = this.preparedTemplate
}
}
_beforeBind () {
if (this.beforeBind instanceof Function) {
this.beforeBind()
}
Object.defineProperty(this, '_created', {value: true, writable: true})
}
bind (selector) {
this._beforeBind()
document.querySelector(selector).innerHTML = this.wrappedTemplate
}
toString () {
this._beforeBind()
return this.wrappedTemplate
}
}