@medyll/idae-be 0.88.0 → 0.90.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +312 -96
- package/dist/be.d.ts +16 -0
- package/dist/be.js +22 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/modules/http.d.ts +78 -0
- package/dist/modules/http.js +120 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
1
|
# @medyll/idae-be
|
|
3
2
|
|
|
4
3
|
A DOM walk and manipulation library with a callback-based approach for precise element targeting.
|
|
@@ -16,139 +15,356 @@ npm install @medyll/idae-be
|
|
|
16
15
|
- Comprehensive DOM traversal and manipulation
|
|
17
16
|
- Event handling, style management, and attribute control
|
|
18
17
|
- Timer integration for dynamic operations
|
|
18
|
+
- HTTP content loading and insertion
|
|
19
19
|
|
|
20
20
|
## Unique Approach
|
|
21
21
|
|
|
22
22
|
Unlike jQuery and other chained libraries, `@medyll/idae-be` always returns the root object. This approach allows for consistent chaining while using callbacks to manipulate targeted elements. This design provides more control and clarity in complex DOM operations.
|
|
23
23
|
|
|
24
|
+
---
|
|
25
|
+
|
|
24
26
|
## Basic Usage
|
|
25
27
|
|
|
28
|
+
### Example 1: DOM Manipulation with Callbacks
|
|
29
|
+
|
|
26
30
|
```javascript
|
|
27
31
|
import { be, toBe } from '@medyll/idae-be';
|
|
28
32
|
|
|
33
|
+
// Select the container element
|
|
29
34
|
be('#container')
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
.append(toBe('<div>New content</div>'), ({ be }) => {
|
|
36
|
+
be.addClass('highlight')
|
|
37
|
+
.on('click', () => console.log('Clicked!'))
|
|
38
|
+
.append(toBe('<span>Nested content</span>'), ({ be }) => {
|
|
39
|
+
be.addClass('nested').on('mouseover', () => console.log('Hovered!'));
|
|
40
|
+
});
|
|
41
|
+
})
|
|
42
|
+
.prepend(toBe('<h1>Title</h1>'), ({ be }) => {
|
|
43
|
+
be.addClass('title').children(({ be }) => {
|
|
44
|
+
be.setStyle({ color: 'blue' });
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
### Example 2: Event Handling and Traversal
|
|
52
|
+
|
|
53
|
+
```javascript
|
|
54
|
+
import { be } from '@medyll/idae-be';
|
|
55
|
+
|
|
56
|
+
// Add a click event to all buttons inside the container
|
|
57
|
+
be('#container button').on('click', ({ target }) => {
|
|
58
|
+
be(target)
|
|
59
|
+
.toggleClass('active')
|
|
60
|
+
.siblings(({ be }) => {
|
|
61
|
+
be.removeClass('active').on('mouseover', () => console.log('Sibling hovered!'));
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Fire a custom event and handle it
|
|
66
|
+
be('#container').fire('customEvent', { detailKey: 'detailValue' }, ({ be }) => {
|
|
67
|
+
be.children(({ be }) => {
|
|
68
|
+
be.addClass('custom-event-handled');
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
### Example 3: Styling and Attributes
|
|
76
|
+
|
|
77
|
+
```javascript
|
|
78
|
+
import { be } from '@medyll/idae-be';
|
|
79
|
+
|
|
80
|
+
// Select an element and update its styles and attributes
|
|
81
|
+
be('#element')
|
|
82
|
+
.setStyle({ backgroundColor: 'yellow', fontSize: '16px' }, ({ be }) => {
|
|
83
|
+
be.setAttr('data-role', 'admin').children(({ be }) => {
|
|
84
|
+
be.setStyle({ color: 'red' }).setAttr('data-child', 'true');
|
|
85
|
+
});
|
|
86
|
+
})
|
|
87
|
+
.addClass('styled-element', ({ be }) => {
|
|
88
|
+
be.siblings(({ be }) => {
|
|
89
|
+
be.setStyle({ opacity: '0.5' });
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
### Example 4: Timers
|
|
97
|
+
|
|
98
|
+
```javascript
|
|
99
|
+
import { be } from '@medyll/idae-be';
|
|
100
|
+
|
|
101
|
+
// Set a timeout to execute a callback after 100ms
|
|
102
|
+
be('#test').timeout(100, ({ be }) => {
|
|
103
|
+
be.setStyle({ backgroundColor: 'yellow' }).append('<span>Timeout executed</span>');
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// Set an interval to execute a callback every 400ms
|
|
107
|
+
const intervalInstance = be('#test').interval(400, ({ be }) => {
|
|
108
|
+
be.toggleClass('highlight');
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// Clear the interval after 600ms
|
|
112
|
+
setTimeout(() => {
|
|
113
|
+
intervalInstance.clearInterval();
|
|
114
|
+
}, 600);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
### Example 5: Walk
|
|
120
|
+
|
|
121
|
+
```javascript
|
|
122
|
+
import { be } from '@medyll/idae-be';
|
|
123
|
+
|
|
124
|
+
// Traverse up the DOM tree to find the parent element
|
|
125
|
+
be('#child').up('#parent', ({ be: parent }) => {
|
|
126
|
+
parent.addClass('highlight')
|
|
127
|
+
.children(({ be: child }) => {
|
|
128
|
+
child.setStyle({ color: 'blue' });
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Find all siblings of an element and add a class
|
|
133
|
+
be('#target').siblings(({ be: siblings }) => {
|
|
134
|
+
siblings.addClass('sibling-class').children(({ be }) => {
|
|
135
|
+
be.setStyle({ fontWeight: 'bold' });
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// Find the closest ancestor matching a selector
|
|
140
|
+
be('#child').closest('.ancestor', ({ be: closest }) => {
|
|
141
|
+
closest.setStyle({ border: '2px solid red' }).children(({ be }) => {
|
|
142
|
+
be.addClass('ancestor-child');
|
|
143
|
+
});
|
|
144
|
+
});
|
|
39
145
|
```
|
|
40
146
|
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
### Example 6: HTTP Content Loading and Insertion
|
|
150
|
+
|
|
151
|
+
```javascript
|
|
152
|
+
import { be } from '@medyll/idae-be';
|
|
153
|
+
|
|
154
|
+
// Load content from a URL and update the element
|
|
155
|
+
be('#test').updateHttp('/content.html', ({ be }) => {
|
|
156
|
+
console.log('Content loaded:', be.html);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// Load content and insert it at a specific position
|
|
160
|
+
be('#test').insertHttp('/content.html', 'afterbegin', ({ be }) => {
|
|
161
|
+
console.log('Content inserted:', be.html);
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
41
167
|
## API Reference
|
|
42
168
|
|
|
43
169
|
### Core Methods
|
|
44
170
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
- `createBe(tagOrHtml: string, options?: Object): Be` - Create a new Be element
|
|
171
|
+
#### `be(selector: string | HTMLElement | HTMLElement[]): Be`
|
|
172
|
+
Create a new Be instance.
|
|
48
173
|
|
|
49
|
-
|
|
174
|
+
**Example:**
|
|
175
|
+
```javascript
|
|
176
|
+
const instance = be('#test');
|
|
177
|
+
```
|
|
50
178
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
- `prepend(content: string | HTMLElement): Be`
|
|
54
|
-
- `insert(mode: 'afterbegin' | 'afterend' | 'beforebegin' | 'beforeend', element: HTMLElement | Be): Be`
|
|
55
|
-
- `remove(): Be`
|
|
56
|
-
- `replace(content: string | HTMLElement): Be`
|
|
57
|
-
- `clear(): Be`
|
|
58
|
-
- `normalize(): Be`
|
|
59
|
-
- `wrap(tag?: string): Be`
|
|
179
|
+
#### `toBe(str: string | HTMLElement, options?: { tag?: string }): Be`
|
|
180
|
+
Convert a string or HTMLElement to a Be instance.
|
|
60
181
|
|
|
61
|
-
|
|
182
|
+
**Example:**
|
|
183
|
+
```javascript
|
|
184
|
+
const newElement = toBe('<div>Content</div>');
|
|
185
|
+
```
|
|
62
186
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
- `previous(selector?: string, callback?: HandlerCallBackFn): Be`
|
|
66
|
-
- `siblings(selector?: string, callback?: HandlerCallBackFn): Be`
|
|
67
|
-
- `children(selector?: string, callback?: HandlerCallBackFn): Be`
|
|
68
|
-
- `closest(selector: string, callback?: HandlerCallBackFn): Be`
|
|
69
|
-
- `firstChild(selector?: string, callback?: HandlerCallBackFn): Be`
|
|
70
|
-
- `lastChild(selector?: string, callback?: HandlerCallBackFn): Be`
|
|
71
|
-
- `find(selector: string, callback?: HandlerCallBackFn): Be`
|
|
72
|
-
- `findAll(selector: string, callback?: HandlerCallBackFn): Be`
|
|
73
|
-
- `without(selector: string, callback?: HandlerCallBackFn): Be`
|
|
187
|
+
#### `createBe(tagOrHtml: string, options?: Object): Be`
|
|
188
|
+
Create a new Be element.
|
|
74
189
|
|
|
75
|
-
|
|
190
|
+
**Example:**
|
|
191
|
+
```javascript
|
|
192
|
+
const newElement = createBe('div', { className: 'my-class' });
|
|
193
|
+
```
|
|
76
194
|
|
|
77
|
-
|
|
78
|
-
- `getStyle(property: string): string | null`
|
|
79
|
-
- `unsetStyle(properties: string[]): Be`
|
|
80
|
-
- `addClass(className: string): Be`
|
|
81
|
-
- `removeClass(className: string): Be`
|
|
82
|
-
- `toggleClass(className: string): Be`
|
|
83
|
-
- `replaceClass(oldClass: string, newClass: string): Be`
|
|
195
|
+
---
|
|
84
196
|
|
|
85
|
-
###
|
|
197
|
+
### HTTP Methods
|
|
198
|
+
|
|
199
|
+
#### `updateHttp(url: string, callback?: HandlerCallBackFn): Be`
|
|
200
|
+
Loads content from a URL and updates the element's content.
|
|
201
|
+
|
|
202
|
+
**Example:**
|
|
203
|
+
```javascript
|
|
204
|
+
be('#test').updateHttp('/content.html', ({ be }) => {
|
|
205
|
+
console.log(be.html);
|
|
206
|
+
});
|
|
207
|
+
```
|
|
86
208
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
- `fire(eventName: string, detail?: any): Be`
|
|
209
|
+
#### `insertHttp(url: string, mode?: 'afterbegin' | 'afterend' | 'beforebegin' | 'beforeend', callback?: HandlerCallBackFn): Be`
|
|
210
|
+
Loads content from a URL and inserts it into the element at a specified position.
|
|
90
211
|
|
|
91
|
-
|
|
212
|
+
**Example:**
|
|
213
|
+
```javascript
|
|
214
|
+
be('#test').insertHttp('/content.html', 'afterbegin', ({ be }) => {
|
|
215
|
+
console.log(be.html);
|
|
216
|
+
});
|
|
217
|
+
```
|
|
92
218
|
|
|
93
|
-
|
|
94
|
-
- `getAttr(name: string): string | null`
|
|
95
|
-
- `deleteAttr(name: string): Be`
|
|
96
|
-
- `setData(key: string, value: string): Be`
|
|
97
|
-
- `getData(key: string): string | null`
|
|
98
|
-
- `deleteData(key: string): Be`
|
|
219
|
+
---
|
|
99
220
|
|
|
100
221
|
### Timers
|
|
101
222
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
```javascript
|
|
110
|
-
be('#app')
|
|
111
|
-
.append(toBe('<div id="content"></div>'), ({ be }) => {
|
|
112
|
-
be
|
|
113
|
-
.append(toBe('<button>Add Item</button>'), ({ be }) => {
|
|
114
|
-
be.on('click', () => {
|
|
115
|
-
content.append(toBe('<p>New item</p>'), ({ be }) => {
|
|
116
|
-
be
|
|
117
|
-
.addClass('item')
|
|
118
|
-
.setStyle({ color: 'blue' })
|
|
119
|
-
.on('click', ({ target }) => {
|
|
120
|
-
be(target).toggleClass('selected');
|
|
121
|
-
});
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
})
|
|
125
|
-
.children('.item', ({ be }) => {
|
|
126
|
-
be.setStyle({ padding: '5px', margin: '2px' });
|
|
127
|
-
});
|
|
128
|
-
})
|
|
129
|
-
.up(({ be }) => {
|
|
130
|
-
be.setStyle({ backgroundColor: '#f0f0f0' });
|
|
131
|
-
});
|
|
223
|
+
#### `timeout(delay: number, callback: HandlerCallBackFn): Be`
|
|
224
|
+
Set a timeout for an element.
|
|
225
|
+
|
|
226
|
+
**Example:**
|
|
227
|
+
```javascript
|
|
228
|
+
be('#test').timeout(1000, () => console.log('Timeout executed'));
|
|
132
229
|
```
|
|
133
230
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
2. Event handling with dynamic content addition
|
|
137
|
-
3. Traversal and bulk operations on children
|
|
138
|
-
4. Accessing and styling parent elements
|
|
231
|
+
#### `interval(delay: number, callback: HandlerCallBackFn): Be`
|
|
232
|
+
Set an interval for an element.
|
|
139
233
|
|
|
140
|
-
|
|
234
|
+
**Example:**
|
|
235
|
+
```javascript
|
|
236
|
+
be('#test').interval(500, () => console.log('Interval executed'));
|
|
237
|
+
```
|
|
141
238
|
|
|
142
|
-
|
|
239
|
+
#### `clearTimeout(): Be`
|
|
240
|
+
Clear a timeout.
|
|
143
241
|
|
|
144
|
-
|
|
242
|
+
**Example:**
|
|
243
|
+
```javascript
|
|
244
|
+
const timeoutInstance = be('#test').timeout(1000, () => console.log('Timeout executed'));
|
|
245
|
+
timeoutInstance.clearTimeout();
|
|
246
|
+
```
|
|
145
247
|
|
|
146
|
-
|
|
248
|
+
#### `clearInterval(): Be`
|
|
249
|
+
Clear an interval.
|
|
147
250
|
|
|
148
|
-
|
|
251
|
+
**Example:**
|
|
252
|
+
```javascript
|
|
253
|
+
const intervalInstance = be('#test').interval(500, () => console.log('Interval executed'));
|
|
254
|
+
intervalInstance.clearInterval();
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
---
|
|
258
|
+
|
|
259
|
+
### Traversal
|
|
260
|
+
|
|
261
|
+
#### `up(selector?: string, callback?: HandlerCallBackFn): Be`
|
|
262
|
+
Traverse up the DOM tree.
|
|
263
|
+
|
|
264
|
+
**Example:**
|
|
265
|
+
```javascript
|
|
266
|
+
be('#child').up();
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
#### `next(selector?: string, callback?: HandlerCallBackFn): Be`
|
|
270
|
+
Traverse to the next sibling.
|
|
271
|
+
|
|
272
|
+
**Example:**
|
|
273
|
+
```javascript
|
|
274
|
+
be('#sibling1').next();
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
#### `previous(selector?: string, callback?: HandlerCallBackFn): Be`
|
|
278
|
+
Traverse to the previous sibling.
|
|
279
|
+
|
|
280
|
+
**Example:**
|
|
281
|
+
```javascript
|
|
282
|
+
be('#sibling2').previous();
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
#### `siblings(selector?: string, callback?: HandlerCallBackFn): Be`
|
|
286
|
+
Find all sibling elements.
|
|
287
|
+
|
|
288
|
+
**Example:**
|
|
289
|
+
```javascript
|
|
290
|
+
be('#child').siblings();
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
#### `children(selector?: string, callback?: HandlerCallBackFn): Be`
|
|
294
|
+
Find all child elements.
|
|
295
|
+
|
|
296
|
+
**Example:**
|
|
297
|
+
```javascript
|
|
298
|
+
be('#parent').children();
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
#### `closest(selector: string, callback?: HandlerCallBackFn): Be`
|
|
302
|
+
Find the closest ancestor matching a selector.
|
|
303
|
+
|
|
304
|
+
**Example:**
|
|
305
|
+
```javascript
|
|
306
|
+
be('#child').closest('#ancestor');
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
---
|
|
310
|
+
|
|
311
|
+
### Styling
|
|
312
|
+
|
|
313
|
+
#### `setStyle(styles: Record<string, string>): Be`
|
|
314
|
+
Set CSS styles for an element.
|
|
315
|
+
|
|
316
|
+
**Example:**
|
|
317
|
+
```javascript
|
|
318
|
+
be('#test').setStyle({ color: 'red', fontSize: '16px' });
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
#### `getStyle(property: string): string | null`
|
|
322
|
+
Get the value of a CSS property.
|
|
323
|
+
|
|
324
|
+
**Example:**
|
|
325
|
+
```javascript
|
|
326
|
+
const color = be('#test').getStyle('color');
|
|
327
|
+
console.log(color); // Output: "red"
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
#### `unsetStyle(property: string): Be`
|
|
331
|
+
Remove a CSS property from an element.
|
|
332
|
+
|
|
333
|
+
**Example:**
|
|
334
|
+
```javascript
|
|
335
|
+
be('#test').unsetStyle('color');
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
---
|
|
339
|
+
|
|
340
|
+
### Events
|
|
341
|
+
|
|
342
|
+
#### `on(eventName: string, handler: EventListener): Be`
|
|
343
|
+
Add an event listener to an element.
|
|
344
|
+
|
|
345
|
+
**Example:**
|
|
346
|
+
```javascript
|
|
347
|
+
be('#test').on('click', () => console.log('Clicked!'));
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
#### `off(eventName: string, handler: EventListener): Be`
|
|
351
|
+
Remove an event listener from an element.
|
|
352
|
+
|
|
353
|
+
**Example:**
|
|
354
|
+
```javascript
|
|
355
|
+
be('#test').off('click', handler);
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
#### `fire(eventName: string, detail?: any): Be`
|
|
359
|
+
Dispatch a custom event.
|
|
360
|
+
|
|
361
|
+
**Example:**
|
|
362
|
+
```javascript
|
|
363
|
+
be('#test').fire('customEvent', { key: 'value' });
|
|
364
|
+
```
|
|
149
365
|
|
|
150
|
-
|
|
366
|
+
---
|
|
151
367
|
|
|
152
368
|
## License
|
|
153
369
|
|
|
154
|
-
This project is licensed under the MIT License.
|
|
370
|
+
This project is licensed under the MIT License.
|
package/dist/be.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { PositionHandler, type PositionHandlerHandle } from './modules/position.
|
|
|
9
9
|
import { WalkHandler, type WalkHandlerHandle } from './modules/walk.js';
|
|
10
10
|
import { TextHandler, type TextHandlerHandle } from './modules/text.js';
|
|
11
11
|
import { TimersHandler, type TimerHandlerHandle } from './modules/timers.js';
|
|
12
|
+
import { HttpHandler, type HttpHandlerHandle } from './modules/http.js';
|
|
12
13
|
export declare class Be {
|
|
13
14
|
[key: string]: unknown;
|
|
14
15
|
inputNode: HTMLElement | HTMLElement[] | string;
|
|
@@ -91,6 +92,10 @@ export declare class Be {
|
|
|
91
92
|
interval: TimersHandler['interval'];
|
|
92
93
|
clearTimeout: TimersHandler['clearTimeout'];
|
|
93
94
|
clearInterval: TimersHandler['clearInterval'];
|
|
95
|
+
http: (actions: HttpHandlerHandle) => Be;
|
|
96
|
+
private httpHandler;
|
|
97
|
+
updateHttp: HttpHandler['update'];
|
|
98
|
+
insertHttp: HttpHandler['insert'];
|
|
94
99
|
private constructor();
|
|
95
100
|
/**
|
|
96
101
|
* Normalizes the input to ensure `node` is always an HTMLElement or an array of HTMLElements.
|
|
@@ -124,6 +129,17 @@ export declare class Be {
|
|
|
124
129
|
data?: T;
|
|
125
130
|
headers?: Record<string, string>;
|
|
126
131
|
}): Promise<any>;
|
|
132
|
+
/**
|
|
133
|
+
* Iterates over nodes based on the type of `this.isWhat` and applies a callback function to each node.
|
|
134
|
+
*
|
|
135
|
+
* @param callback - A function to be executed for each node. Receives the current node as an argument.
|
|
136
|
+
* @param firstChild - Optional. If `true`, stops further iteration after the first child is processed.
|
|
137
|
+
*
|
|
138
|
+
* The behavior of the method depends on the value of `this.isWhat`:
|
|
139
|
+
* - `'element'`: Applies the callback to a single HTMLElement (`this.inputNode`).
|
|
140
|
+
* - `'array'`: Iterates over an array of HTMLElements (`this.inputNode`) and applies the callback to each.
|
|
141
|
+
* - `'qy'`: Selects elements using a query selector string (`this.inputNode`) and applies the callback to each.
|
|
142
|
+
*/
|
|
127
143
|
eachNode(callback: (el: HTMLElement) => void, firstChild?: boolean): void;
|
|
128
144
|
get html(): string | null;
|
|
129
145
|
get node(): HTMLElement | HTMLElement[];
|
package/dist/be.js
CHANGED
|
@@ -9,6 +9,7 @@ import { PositionHandler } from './modules/position.js';
|
|
|
9
9
|
import { WalkHandler } from './modules/walk.js';
|
|
10
10
|
import { TextHandler } from './modules/text.js';
|
|
11
11
|
import { TimersHandler } from './modules/timers.js';
|
|
12
|
+
import { HttpHandler } from './modules/http.js';
|
|
12
13
|
export class Be {
|
|
13
14
|
inputNode;
|
|
14
15
|
isWhat;
|
|
@@ -101,6 +102,11 @@ export class Be {
|
|
|
101
102
|
interval;
|
|
102
103
|
clearTimeout;
|
|
103
104
|
clearInterval;
|
|
105
|
+
// http
|
|
106
|
+
http;
|
|
107
|
+
httpHandler;
|
|
108
|
+
updateHttp;
|
|
109
|
+
insertHttp;
|
|
104
110
|
constructor(input) {
|
|
105
111
|
if (input instanceof Be) {
|
|
106
112
|
return input;
|
|
@@ -152,6 +158,10 @@ export class Be {
|
|
|
152
158
|
this.timerHandler = new TimersHandler(this);
|
|
153
159
|
this.timers = this.handle(this.timerHandler);
|
|
154
160
|
this.attach(TimersHandler);
|
|
161
|
+
// http
|
|
162
|
+
this.httpHandler = new HttpHandler(this);
|
|
163
|
+
this.http = this.handle(this.httpHandler);
|
|
164
|
+
this.attach(HttpHandler, 'Http');
|
|
155
165
|
}
|
|
156
166
|
/**
|
|
157
167
|
* Normalizes the input to ensure `node` is always an HTMLElement or an array of HTMLElements.
|
|
@@ -211,7 +221,7 @@ export class Be {
|
|
|
211
221
|
* @returns The created `Be` element.
|
|
212
222
|
*/
|
|
213
223
|
static toBe(str, options = {}) {
|
|
214
|
-
const { tag = '
|
|
224
|
+
const { tag = 'div' } = options;
|
|
215
225
|
let beElem;
|
|
216
226
|
if (str instanceof HTMLElement) {
|
|
217
227
|
beElem = be(str);
|
|
@@ -263,6 +273,17 @@ export class Be {
|
|
|
263
273
|
headers: options.headers || {}
|
|
264
274
|
}).then((response) => response.json());
|
|
265
275
|
}
|
|
276
|
+
/**
|
|
277
|
+
* Iterates over nodes based on the type of `this.isWhat` and applies a callback function to each node.
|
|
278
|
+
*
|
|
279
|
+
* @param callback - A function to be executed for each node. Receives the current node as an argument.
|
|
280
|
+
* @param firstChild - Optional. If `true`, stops further iteration after the first child is processed.
|
|
281
|
+
*
|
|
282
|
+
* The behavior of the method depends on the value of `this.isWhat`:
|
|
283
|
+
* - `'element'`: Applies the callback to a single HTMLElement (`this.inputNode`).
|
|
284
|
+
* - `'array'`: Iterates over an array of HTMLElements (`this.inputNode`) and applies the callback to each.
|
|
285
|
+
* - `'qy'`: Selects elements using a query selector string (`this.inputNode`) and applies the callback to each.
|
|
286
|
+
*/
|
|
266
287
|
eachNode(callback, firstChild) {
|
|
267
288
|
switch (this.isWhat) {
|
|
268
289
|
case 'element':
|
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export * from './modules/timers.js';
|
|
|
6
6
|
export * from './modules/text.js';
|
|
7
7
|
export * from './modules/styles.js';
|
|
8
8
|
export * from './modules/position.js';
|
|
9
|
+
export * from './modules/http.js';
|
|
9
10
|
export * from './modules/events.js';
|
|
10
11
|
export * from './modules/dom.js';
|
|
11
12
|
export * from './modules/data.js';
|
package/dist/index.js
CHANGED
|
@@ -7,6 +7,7 @@ export * from './modules/timers.js';
|
|
|
7
7
|
export * from './modules/text.js';
|
|
8
8
|
export * from './modules/styles.js';
|
|
9
9
|
export * from './modules/position.js';
|
|
10
|
+
export * from './modules/http.js';
|
|
10
11
|
export * from './modules/events.js';
|
|
11
12
|
export * from './modules/dom.js';
|
|
12
13
|
export * from './modules/data.js';
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Be } from '../be.js';
|
|
2
|
+
import type { CommonHandler, HandlerCallBackFn } from '../types.js';
|
|
3
|
+
declare enum httpMethods {
|
|
4
|
+
update = "update",
|
|
5
|
+
insert = "insert"
|
|
6
|
+
}
|
|
7
|
+
export interface HttpHandlerHandle {
|
|
8
|
+
update?: {
|
|
9
|
+
url: string;
|
|
10
|
+
options?: {
|
|
11
|
+
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
12
|
+
data?: Record<string, unknown>;
|
|
13
|
+
headers?: Record<string, string>;
|
|
14
|
+
};
|
|
15
|
+
callback?: HandlerCallBackFn;
|
|
16
|
+
};
|
|
17
|
+
insert?: {
|
|
18
|
+
url: string;
|
|
19
|
+
mode?: 'afterbegin' | 'afterend' | 'beforebegin' | 'beforeend';
|
|
20
|
+
callback?: HandlerCallBackFn;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Handles HTTP operations for Be elements.
|
|
25
|
+
*/
|
|
26
|
+
export declare class HttpHandler implements CommonHandler<HttpHandler, HttpHandlerHandle> {
|
|
27
|
+
private beElement;
|
|
28
|
+
static methods: httpMethods[];
|
|
29
|
+
constructor(beElement: Be);
|
|
30
|
+
methods: string[];
|
|
31
|
+
/**
|
|
32
|
+
* Handles HTTP actions like loading content or inserting it into the DOM.
|
|
33
|
+
* @param actions - The actions to perform.
|
|
34
|
+
* @returns The Be instance for method chaining.
|
|
35
|
+
*/
|
|
36
|
+
handle(actions: HttpHandlerHandle): Be;
|
|
37
|
+
/**
|
|
38
|
+
* Loads content from a URL and updates the element's content.
|
|
39
|
+
* Can be called with two or three arguments:
|
|
40
|
+
* - `update(url: string, callback?: HandlerCallBackFn)`
|
|
41
|
+
* - `update(url: string, options?: { method?: string; data?: object; headers?: object }, callback?: HandlerCallBackFn)`
|
|
42
|
+
*
|
|
43
|
+
* @param url - The URL to fetch content from.
|
|
44
|
+
* @param optionsOrCallback - Optional configuration for the HTTP request or a callback function.
|
|
45
|
+
* @param callback - Optional callback function if options are provided.
|
|
46
|
+
* @returns The Be instance for method chaining.
|
|
47
|
+
* @example
|
|
48
|
+
* // Call with two arguments
|
|
49
|
+
* be('#test').updateHttp('/content.html', ({ be }) => console.log(be.html));
|
|
50
|
+
*
|
|
51
|
+
* // Call with three arguments
|
|
52
|
+
* be('#test').updateHttp('/content.html', { method: 'POST', data: { key: 'value' } }, ({ be }) => console.log(be.html));
|
|
53
|
+
*/
|
|
54
|
+
update(url: string, optionsOrCallback?: {
|
|
55
|
+
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
56
|
+
data?: Record<string, unknown>;
|
|
57
|
+
headers?: Record<string, string>;
|
|
58
|
+
} | HandlerCallBackFn, callback?: HandlerCallBackFn): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Loads content from a URL and inserts it into the element at a specified position.
|
|
61
|
+
* Can be called with two or three arguments:
|
|
62
|
+
* - `insert(url: string, callback?: HandlerCallBackFn)`
|
|
63
|
+
* - `insert(url: string, mode?: 'afterbegin' | 'afterend' | 'beforebegin' | 'beforeend', callback?: HandlerCallBackFn)`
|
|
64
|
+
*
|
|
65
|
+
* @param url - The URL to fetch content from.
|
|
66
|
+
* @param modeOrCallback - Optional position to insert the content or a callback function.
|
|
67
|
+
* @param callback - Optional callback function if mode is provided.
|
|
68
|
+
* @returns The Be instance for method chaining.
|
|
69
|
+
* @example
|
|
70
|
+
* // Call with two arguments
|
|
71
|
+
* be('#test').insertHttp('/content.html', ({ be }) => console.log(be.html));
|
|
72
|
+
*
|
|
73
|
+
* // Call with three arguments
|
|
74
|
+
* be('#test').insertHttp('/content.html', 'afterbegin', ({ be }) => console.log(be.html));
|
|
75
|
+
*/
|
|
76
|
+
insert(url: string, modeOrCallback?: 'afterbegin' | 'afterend' | 'beforebegin' | 'beforeend' | HandlerCallBackFn, callback?: HandlerCallBackFn): Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
export {};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { Be } from '../be.js';
|
|
2
|
+
import { DomHandler } from './dom.js';
|
|
3
|
+
var httpMethods;
|
|
4
|
+
(function (httpMethods) {
|
|
5
|
+
httpMethods["update"] = "update";
|
|
6
|
+
httpMethods["insert"] = "insert";
|
|
7
|
+
})(httpMethods || (httpMethods = {}));
|
|
8
|
+
/**
|
|
9
|
+
* Handles HTTP operations for Be elements.
|
|
10
|
+
*/
|
|
11
|
+
export class HttpHandler {
|
|
12
|
+
beElement;
|
|
13
|
+
static methods = Object.values(httpMethods);
|
|
14
|
+
constructor(beElement) {
|
|
15
|
+
this.beElement = beElement;
|
|
16
|
+
}
|
|
17
|
+
methods = HttpHandler.methods;
|
|
18
|
+
/**
|
|
19
|
+
* Handles HTTP actions like loading content or inserting it into the DOM.
|
|
20
|
+
* @param actions - The actions to perform.
|
|
21
|
+
* @returns The Be instance for method chaining.
|
|
22
|
+
*/
|
|
23
|
+
handle(actions) {
|
|
24
|
+
Object.entries(actions).forEach(([method, props]) => {
|
|
25
|
+
switch (method) {
|
|
26
|
+
case 'update':
|
|
27
|
+
this.update(props.url, props.options, props.callback);
|
|
28
|
+
break;
|
|
29
|
+
case 'insert':
|
|
30
|
+
this.insert(props.url, props.mode, props.callback);
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
return this.beElement;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Loads content from a URL and updates the element's content.
|
|
38
|
+
* Can be called with two or three arguments:
|
|
39
|
+
* - `update(url: string, callback?: HandlerCallBackFn)`
|
|
40
|
+
* - `update(url: string, options?: { method?: string; data?: object; headers?: object }, callback?: HandlerCallBackFn)`
|
|
41
|
+
*
|
|
42
|
+
* @param url - The URL to fetch content from.
|
|
43
|
+
* @param optionsOrCallback - Optional configuration for the HTTP request or a callback function.
|
|
44
|
+
* @param callback - Optional callback function if options are provided.
|
|
45
|
+
* @returns The Be instance for method chaining.
|
|
46
|
+
* @example
|
|
47
|
+
* // Call with two arguments
|
|
48
|
+
* be('#test').updateHttp('/content.html', ({ be }) => console.log(be.html));
|
|
49
|
+
*
|
|
50
|
+
* // Call with three arguments
|
|
51
|
+
* be('#test').updateHttp('/content.html', { method: 'POST', data: { key: 'value' } }, ({ be }) => console.log(be.html));
|
|
52
|
+
*/
|
|
53
|
+
async update(url, optionsOrCallback, callback) {
|
|
54
|
+
let options;
|
|
55
|
+
// Detect if the second argument is an options object or a callback function
|
|
56
|
+
if (typeof optionsOrCallback === 'function') {
|
|
57
|
+
callback = optionsOrCallback;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
options = optionsOrCallback;
|
|
61
|
+
}
|
|
62
|
+
const response = await fetch(url, {
|
|
63
|
+
method: options?.method || 'GET',
|
|
64
|
+
body: options?.data ? JSON.stringify(options.data) : undefined,
|
|
65
|
+
headers: {
|
|
66
|
+
'Content-Type': 'application/json',
|
|
67
|
+
...options?.headers
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
const content = await response.text();
|
|
71
|
+
this.beElement.eachNode((el) => {
|
|
72
|
+
const beElem = Be.elem(el);
|
|
73
|
+
beElem.update(content);
|
|
74
|
+
callback?.({
|
|
75
|
+
fragment: content,
|
|
76
|
+
be: beElem,
|
|
77
|
+
root: this.beElement
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Loads content from a URL and inserts it into the element at a specified position.
|
|
83
|
+
* Can be called with two or three arguments:
|
|
84
|
+
* - `insert(url: string, callback?: HandlerCallBackFn)`
|
|
85
|
+
* - `insert(url: string, mode?: 'afterbegin' | 'afterend' | 'beforebegin' | 'beforeend', callback?: HandlerCallBackFn)`
|
|
86
|
+
*
|
|
87
|
+
* @param url - The URL to fetch content from.
|
|
88
|
+
* @param modeOrCallback - Optional position to insert the content or a callback function.
|
|
89
|
+
* @param callback - Optional callback function if mode is provided.
|
|
90
|
+
* @returns The Be instance for method chaining.
|
|
91
|
+
* @example
|
|
92
|
+
* // Call with two arguments
|
|
93
|
+
* be('#test').insertHttp('/content.html', ({ be }) => console.log(be.html));
|
|
94
|
+
*
|
|
95
|
+
* // Call with three arguments
|
|
96
|
+
* be('#test').insertHttp('/content.html', 'afterbegin', ({ be }) => console.log(be.html));
|
|
97
|
+
*/
|
|
98
|
+
async insert(url, modeOrCallback, callback) {
|
|
99
|
+
let mode;
|
|
100
|
+
// Detect if the second argument is a mode or a callback function
|
|
101
|
+
if (typeof modeOrCallback === 'function') {
|
|
102
|
+
callback = modeOrCallback;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
mode = modeOrCallback;
|
|
106
|
+
}
|
|
107
|
+
const response = await fetch(url);
|
|
108
|
+
const content = await response.text();
|
|
109
|
+
this.beElement.eachNode((el) => {
|
|
110
|
+
const beElem = Be.elem(el);
|
|
111
|
+
const domHandler = new DomHandler(beElem);
|
|
112
|
+
domHandler.insert(mode || 'beforeend', content);
|
|
113
|
+
callback?.({
|
|
114
|
+
fragment: content,
|
|
115
|
+
be: beElem,
|
|
116
|
+
root: this.beElement
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@medyll/idae-be",
|
|
3
3
|
"scope": "@medyll",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.90.0",
|
|
5
5
|
"description": "A powerful DOM manipulation library with a callback-based approach for precise element targeting. Provides consistent chaining, comprehensive DOM traversal, event handling, style management, and more. Written in TypeScript for modern browsers.",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"dev": "vite dev",
|