@medyll/idae-be 0.89.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 +269 -73
- 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
|
@@ -15,6 +15,7 @@ npm install @medyll/idae-be
|
|
|
15
15
|
- Comprehensive DOM traversal and manipulation
|
|
16
16
|
- Event handling, style management, and attribute control
|
|
17
17
|
- Timer integration for dynamic operations
|
|
18
|
+
- HTTP content loading and insertion
|
|
18
19
|
|
|
19
20
|
## Unique Approach
|
|
20
21
|
|
|
@@ -31,25 +32,19 @@ import { be, toBe } from '@medyll/idae-be';
|
|
|
31
32
|
|
|
32
33
|
// Select the container element
|
|
33
34
|
be('#container')
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
**Comments:**
|
|
49
|
-
|
|
50
|
-
1. **`append`**: Adds a new `<div>` to the container and applies a class and event listener.
|
|
51
|
-
2. **Nested `append`**: Adds a `<span>` inside the appended `<div>` with its own class and event listener.
|
|
52
|
-
3. **`prepend`**: Adds a title at the beginning of the container and styles its children.
|
|
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
|
+
```
|
|
53
48
|
|
|
54
49
|
---
|
|
55
50
|
|
|
@@ -60,27 +55,21 @@ import { be } from '@medyll/idae-be';
|
|
|
60
55
|
|
|
61
56
|
// Add a click event to all buttons inside the container
|
|
62
57
|
be('#container button').on('click', ({ target }) => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
58
|
+
be(target)
|
|
59
|
+
.toggleClass('active')
|
|
60
|
+
.siblings(({ be }) => {
|
|
61
|
+
be.removeClass('active').on('mouseover', () => console.log('Sibling hovered!'));
|
|
62
|
+
});
|
|
68
63
|
});
|
|
69
64
|
|
|
70
65
|
// Fire a custom event and handle it
|
|
71
66
|
be('#container').fire('customEvent', { detailKey: 'detailValue' }, ({ be }) => {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
67
|
+
be.children(({ be }) => {
|
|
68
|
+
be.addClass('custom-event-handled');
|
|
69
|
+
});
|
|
75
70
|
});
|
|
76
71
|
```
|
|
77
72
|
|
|
78
|
-
**Comments:**
|
|
79
|
-
|
|
80
|
-
1. **`on`**: Adds a click event to buttons and toggles a class on the clicked button.
|
|
81
|
-
2. **`siblings`**: Removes a class from sibling buttons and adds a hover event.
|
|
82
|
-
3. **`fire`**: Dispatches a custom event and applies a class to all child elements.
|
|
83
|
-
|
|
84
73
|
---
|
|
85
74
|
|
|
86
75
|
### Example 3: Styling and Attributes
|
|
@@ -90,24 +79,18 @@ import { be } from '@medyll/idae-be';
|
|
|
90
79
|
|
|
91
80
|
// Select an element and update its styles and attributes
|
|
92
81
|
be('#element')
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
+
});
|
|
103
92
|
```
|
|
104
93
|
|
|
105
|
-
**Comments:**
|
|
106
|
-
|
|
107
|
-
1. **`setStyle`**: Applies styles to the selected element and its children.
|
|
108
|
-
2. **`setAttr`**: Sets attributes on the selected element and its children.
|
|
109
|
-
3. **`addClass`**: Adds a class to the element and modifies its siblings.
|
|
110
|
-
|
|
111
94
|
---
|
|
112
95
|
|
|
113
96
|
### Example 4: Timers
|
|
@@ -117,25 +100,18 @@ import { be } from '@medyll/idae-be';
|
|
|
117
100
|
|
|
118
101
|
// Set a timeout to execute a callback after 100ms
|
|
119
102
|
be('#test').timeout(100, ({ be }) => {
|
|
120
|
-
|
|
103
|
+
be.setStyle({ backgroundColor: 'yellow' }).append('<span>Timeout executed</span>');
|
|
121
104
|
});
|
|
122
105
|
|
|
123
|
-
// Set an interval to execute a callback every
|
|
124
|
-
be('#test').interval(100, ({ be }) => {
|
|
125
|
-
be.toggleClass('highlight');
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
// Clear a timeout before it executes
|
|
129
|
-
const timeoutInstance = be('#test').timeout(500, ({ be }) => {
|
|
130
|
-
be.setStyle({ color: 'red' });
|
|
131
|
-
});
|
|
132
|
-
timeoutInstance.clearTimeout();
|
|
133
|
-
|
|
134
|
-
// Clear an interval after 600ms
|
|
106
|
+
// Set an interval to execute a callback every 400ms
|
|
135
107
|
const intervalInstance = be('#test').interval(400, ({ be }) => {
|
|
136
|
-
|
|
108
|
+
be.toggleClass('highlight');
|
|
137
109
|
});
|
|
138
|
-
|
|
110
|
+
|
|
111
|
+
// Clear the interval after 600ms
|
|
112
|
+
setTimeout(() => {
|
|
113
|
+
intervalInstance.clearInterval();
|
|
114
|
+
}, 600);
|
|
139
115
|
```
|
|
140
116
|
|
|
141
117
|
---
|
|
@@ -147,28 +123,248 @@ import { be } from '@medyll/idae-be';
|
|
|
147
123
|
|
|
148
124
|
// Traverse up the DOM tree to find the parent element
|
|
149
125
|
be('#child').up('#parent', ({ be: parent }) => {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
126
|
+
parent.addClass('highlight')
|
|
127
|
+
.children(({ be: child }) => {
|
|
128
|
+
child.setStyle({ color: 'blue' });
|
|
129
|
+
});
|
|
153
130
|
});
|
|
154
131
|
|
|
155
132
|
// Find all siblings of an element and add a class
|
|
156
133
|
be('#target').siblings(({ be: siblings }) => {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
134
|
+
siblings.addClass('sibling-class').children(({ be }) => {
|
|
135
|
+
be.setStyle({ fontWeight: 'bold' });
|
|
136
|
+
});
|
|
160
137
|
});
|
|
161
138
|
|
|
162
139
|
// Find the closest ancestor matching a selector
|
|
163
140
|
be('#child').closest('.ancestor', ({ be: closest }) => {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
141
|
+
closest.setStyle({ border: '2px solid red' }).children(({ be }) => {
|
|
142
|
+
be.addClass('ancestor-child');
|
|
143
|
+
});
|
|
167
144
|
});
|
|
168
145
|
```
|
|
169
146
|
|
|
170
147
|
---
|
|
171
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
|
+
|
|
167
|
+
## API Reference
|
|
168
|
+
|
|
169
|
+
### Core Methods
|
|
170
|
+
|
|
171
|
+
#### `be(selector: string | HTMLElement | HTMLElement[]): Be`
|
|
172
|
+
Create a new Be instance.
|
|
173
|
+
|
|
174
|
+
**Example:**
|
|
175
|
+
```javascript
|
|
176
|
+
const instance = be('#test');
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
#### `toBe(str: string | HTMLElement, options?: { tag?: string }): Be`
|
|
180
|
+
Convert a string or HTMLElement to a Be instance.
|
|
181
|
+
|
|
182
|
+
**Example:**
|
|
183
|
+
```javascript
|
|
184
|
+
const newElement = toBe('<div>Content</div>');
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
#### `createBe(tagOrHtml: string, options?: Object): Be`
|
|
188
|
+
Create a new Be element.
|
|
189
|
+
|
|
190
|
+
**Example:**
|
|
191
|
+
```javascript
|
|
192
|
+
const newElement = createBe('div', { className: 'my-class' });
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
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
|
+
```
|
|
208
|
+
|
|
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.
|
|
211
|
+
|
|
212
|
+
**Example:**
|
|
213
|
+
```javascript
|
|
214
|
+
be('#test').insertHttp('/content.html', 'afterbegin', ({ be }) => {
|
|
215
|
+
console.log(be.html);
|
|
216
|
+
});
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
---
|
|
220
|
+
|
|
221
|
+
### Timers
|
|
222
|
+
|
|
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'));
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
#### `interval(delay: number, callback: HandlerCallBackFn): Be`
|
|
232
|
+
Set an interval for an element.
|
|
233
|
+
|
|
234
|
+
**Example:**
|
|
235
|
+
```javascript
|
|
236
|
+
be('#test').interval(500, () => console.log('Interval executed'));
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
#### `clearTimeout(): Be`
|
|
240
|
+
Clear a timeout.
|
|
241
|
+
|
|
242
|
+
**Example:**
|
|
243
|
+
```javascript
|
|
244
|
+
const timeoutInstance = be('#test').timeout(1000, () => console.log('Timeout executed'));
|
|
245
|
+
timeoutInstance.clearTimeout();
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
#### `clearInterval(): Be`
|
|
249
|
+
Clear an interval.
|
|
250
|
+
|
|
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
|
+
```
|
|
365
|
+
|
|
366
|
+
---
|
|
367
|
+
|
|
172
368
|
## License
|
|
173
369
|
|
|
174
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",
|