-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrag.js
More file actions
278 lines (239 loc) · 7.24 KB
/
frag.js
File metadata and controls
278 lines (239 loc) · 7.24 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// frag.js
export const frag = (function () {
// === Armazena templates compilados ===
const templateCache = {};
// === Sistema Reativo Básico ===
let store = {};
function createStore(initialState) {
store = JSON.parse(JSON.stringify(initialState));
return new Proxy(store, {
set(target, prop, value) {
target[prop] = value;
triggerWatchers(prop);
return true;
}
});
}
const watchers = [];
function watch(callback) {
watchers.push(callback);
}
function triggerWatchers(changedKey) {
watchers.forEach(cb => cb(changedKey));
}
// === Renderização de Templates ===
function compileTemplate(str, data) {
return str.replace(/{{\s*(.*?)\s*}}/g, (_, key) => {
if (key.startsWith('each ')) {
const match = key.match(/each (\w+)(?: as (\w+))?/);
if (!match) return '';
const listKey = match[1];
const itemName = match[2] || 'item';
const list = getNestedValue(data, listKey);
if (!Array.isArray(list)) return '';
return list.map(item => {
return str.split(`{{ each ${listKey} as ${itemName} }}`)[1]
.split(`{{ end }}`)[0]
.replace(new RegExp(`{{\\s*${itemName}\\.`), (_, name) => `{{ ${name}`)
.replace(new RegExp(`{{\\s*${itemName}\\b`, 'g'), (_, name) => `{{ `)
.replace(/\.\b/g, '')
.replace(/(\w+)\./g, '$1_')
.replace(/_/g, '.');
}).join('');
} else if (key.startsWith('this.')) {
return data[key.replace('this.', '')];
} else {
return getNestedValue(data, key);
}
});
}
function getNestedValue(obj, path) {
return path.split('.').reduce((acc, part) => acc && acc[part], obj);
}
// === Criação de Elementos ===
function frag(strings, ...values) {
let el;
// === MODO 1: Template String HTML ===
if (typeof strings === "string" || Array.isArray(strings)) {
const html = strings.map((s, i) => s + (values[i] ?? '')).join('');
const template = document.createElement('template');
template.innerHTML = html.trim();
el = template.content.cloneNode(true);
}
// === MODO 2: Criação Direta com Tag e Props ===
else {
const tag = strings;
const props = values[0] || {};
const children = values.slice(1);
el = document.createElement(tag);
for (const [key, value] of Object.entries(props)) {
if (key.startsWith('on') && typeof value === 'function') {
el.addEventListener(key.slice(2).toLowerCase(), value);
} else if (key === 'style' && typeof value === 'object') {
Object.assign(el.style, value);
} else {
el[key] = value;
}
}
children.flat().forEach(child => {
if (child instanceof Node) {
el.appendChild(child);
} else {
el.appendChild(document.createTextNode(child));
}
});
}
// === Método placeOn(selector, index) ===
el.placeOn = function (selector, index = -1) {
const elements = document.querySelectorAll(selector);
if (!elements.length) {
console.warn(`Nenhum elemento encontrado com o seletor "${selector}".`);
return this;
}
elements.forEach(parent => {
const children = Array.from(parent.children);
const count = children.length;
let position;
if (index < 0) {
position = Math.max(count + index, 0);
} else {
position = Math.min(index, count);
}
if (children[position]) {
parent.insertBefore(this.cloneNode(true), children[position]);
} else {
parent.appendChild(this.cloneNode(true));
}
});
return this;
};
return el;
}
// === Extensibilidade com Plugins ===
frag.extend = function(name, methods) {
Object.assign(frag.prototype, methods);
return this;
};
// === Seleção e Manipulação de Elementos ===
frag.get = function(selector) {
const elements = document.querySelectorAll(selector);
if (!elements.length) {
console.warn(`Nenhum elemento encontrado com o seletor "${selector}".`);
return {
then: () => this,
catch: () => this
};
}
return {
elements,
add(key, value) {
this.elements.forEach(el => {
if (key === 'class' || key === 'className') {
el.classList.add(value);
} else if (el[key] instanceof DOMTokenList) {
el[key].add(value);
} else if (key.startsWith('data-')) {
el.setAttribute(key, value);
} else {
el[key] = value;
}
});
return this;
},
set(key, value) {
this.elements.forEach(el => {
if (key === 'html') {
el.innerHTML = value;
} else if (key === 'text') {
el.textContent = value;
} else {
el[key] = value;
}
});
return this;
},
on(event, handler) {
this.elements.forEach(el => el.addEventListener(event, handler));
return this;
},
off(event, handler) {
this.elements.forEach(el => el.removeEventListener(event, handler));
return this;
},
placeOn(parentSelector, index = -1) {
const parents = document.querySelectorAll(parentSelector);
parents.forEach(parent => {
const children = Array.from(parent.children);
const count = children.length;
let position;
if (index < 0) {
position = Math.max(count + index, 0);
} else {
position = Math.min(index, count);
}
this.elements.forEach(el => {
const clone = el.cloneNode(true);
if (children[position]) {
parent.insertBefore(clone, children[position]);
} else {
parent.appendChild(clone);
}
});
});
return this;
},
then(callback) {
callback?.();
return this;
},
catch() {
return this;
}
};
};
// === Store Reativo ===
frag.createStore = createStore;
frag.watch = watch;
// === HTTP Fetch Básico ===
frag.http = {
get(url, callback) {
fetch(url)
.then(res => res.json())
.then(data => callback(null, data))
.catch(err => callback(err));
},
post(url, body, callback) {
fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
})
.then(res => res.json())
.then(data => callback(null, data))
.catch(err => callback(err));
}
};
// === Serialização de Formulários ===
frag.form = {
serialize(formElement) {
const formData = new FormData(formElement);
const obj = {};
formData.forEach((value, key) => {
obj[key] = value;
});
return obj;
}
};
// === Cache de Templates ===
frag.cache = {
templates: {},
set(name, html) {
this.templates[name] = html;
},
get(name, data) {
const html = this.templates[name];
return html ? compileTemplate(html, data) : '';
}
};
return frag;
})();