-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframe.js
109 lines (85 loc) · 2.28 KB
/
frame.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
class FrameworkReactive {
dependencies = new Map();
constructor(options) {
this.origin = options.info();
const self = this;
this.$data = this.makeProxy(this.origin, self);
}
makeProxy(origin, self) {
return new Proxy(origin, {
get(target, name) {
if(Reflect.has(target, name)) {
self.track(target, name);
return Reflect.get(target, name)
};
console.warn(`La propiedad ${name} no existe`);
return ''
},
set(target, name, value) {
Reflect.set(target, name, value);
self.trigger(name);
}
});
}
mount() {
this.makeDirectives();
}
makeDirectives() {
this.makeFText();
this.makeFModel();
this.makeFBind();
}
makeFText() {
document.querySelectorAll('*[f-text]').forEach(el => {
const name = el.getAttribute('f-text');
this.fText(el, this.$data, name);
});
}
makeFModel() {
document.querySelectorAll('*[f-model]').forEach(el => {
const name = el.getAttribute('f-model');
this.fModel(el, this.$data, name);
el.addEventListener('input', () => {
Reflect.set(this.$data, name, el.value);
});
});
}
makeFBind() {
Array.from(document.querySelectorAll('*'))
.filter(el => [...el.attributes].some(attr => attr.name.startsWith('f-bind:')))
.forEach(el => [...el.attributes].forEach(attribute => {
const name = attribute.value;
const attr = attribute.name.split(":").pop();
Reflect.set(el, attr, Reflect.get(this.$data, name));
})
);
}
track(target, name) {
if(!this.dependencies.has(name)) {
const effect = () => {
document.querySelectorAll(`*[f-text=${name}]`).forEach(el => {
this.fText(el, target, name);
});
document.querySelectorAll(`*[f-model=${name}]`).forEach(el => {
this.fModel(el, target, name);
});
};
this.dependencies.set(name, effect);
}
}
trigger(name) {
const effect = this.dependencies.get(name);
effect();
}
fText(el, target, name) {
el.innerText = Reflect.get(target, name);
}
fModel(el, target, name) {
el.value = Reflect.get(target ,name);
}
}
const Frameworks = {
createApp(options) {
return new FrameworkReactive(options)
}
}