@appius-fr/apx 2.2.2

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/APX.mjs ADDED
@@ -0,0 +1,117 @@
1
+ import augmentWithListen from './modules/listen/listen.mjs';
2
+ import augmentWithTristate from './modules/tristate/tristate.mjs';
3
+ import dialog from './modules/dialog/dialog.mjs';
4
+ import { loadCss } from './modules/common.mjs';
5
+
6
+ /**
7
+ * Creates an APX object that wraps one or more HTML elements.
8
+ * @param {string|HTMLElement|NodeList|HTMLCollection|Array<HTMLElement>|jQuery|Function} input - The input to create the APX object from.
9
+ * @returns {Object} The APX object.
10
+ * @throws {Error} If the input type is invalid.
11
+ * @example
12
+ * // XPath
13
+ * const apx1 = APX('//div[@class="example"]');
14
+ *
15
+ * // CSS selector
16
+ * const apx2 = APX('.example');
17
+ *
18
+ * // Single element
19
+ * const element = document.querySelector('.example');
20
+ * const apx3 = APX(element);
21
+ *
22
+ * // NodeList or HTMLCollection
23
+ * const nodeList = document.querySelectorAll('.example');
24
+ * const apx4 = APX(nodeList);
25
+ *
26
+ * // Array of elements
27
+ * const elements = [document.querySelector('.example1'), document.querySelector('.example2')];
28
+ * const apx5 = APX(elements);
29
+ *
30
+ * // jQuery object
31
+ * const $example = $('.example');
32
+ * const apx6 = APX($example);
33
+ *
34
+ * // Function returning an APX object
35
+ * const apx7 = APX(() => APX('.example'));
36
+ */
37
+ const APX = function (input, context = document) {
38
+ let elements;
39
+
40
+ // Validate context
41
+ if (typeof context === 'string') {
42
+ context = document.querySelector(context);
43
+ }
44
+ else if (APX.isAPXObject(context)) {
45
+ context = context.first();
46
+ } else if (!(context instanceof HTMLElement || context instanceof Document)) {
47
+ throw new Error('Invalid context for APX');
48
+ }
49
+
50
+ if (typeof input === 'string' && input.startsWith('//')) {
51
+ // XPath with context
52
+ elements = document.evaluate(input, context, null, XPathResult.ANY_TYPE, null);
53
+ let nodes = [];
54
+ let node = elements.iterateNext();
55
+ while (node) {
56
+ nodes.push(node);
57
+ node = elements.iterateNext();
58
+ }
59
+ elements = nodes;
60
+ } else if (typeof input === 'string') {
61
+ // CSS selector with context
62
+ elements = context.querySelectorAll(input);
63
+ } else if (input instanceof HTMLElement) {
64
+ // Single element within context
65
+ elements = context.contains(input) ? [input] : [];
66
+ } else if (input instanceof NodeList || input instanceof HTMLCollection) {
67
+ // NodeList or HTMLCollection within context
68
+ elements = Array.from(input).filter(el => context.contains(el));
69
+ } else if (Array.isArray(input) && input.every(el => el instanceof HTMLElement)) {
70
+ // Array of elements within context
71
+ elements = input.filter(el => context.contains(el));
72
+ } else if (typeof jQuery != 'undefined' && input instanceof jQuery) {
73
+ // jQuery object within context
74
+ elements = Array.from(input.get()).filter(el => context.contains(el));
75
+ } else if (typeof input === 'function') {
76
+ // Function returning an APX object with context
77
+ elements = APX(input(), context);
78
+ } else {
79
+ throw new Error('Invalid input type for APX');
80
+ }
81
+
82
+ var apx = {
83
+ elements: elements,
84
+ _isAPXObject: true
85
+ };
86
+ apx.length = elements.length;
87
+ apx.each = function (callback) {
88
+ if (this.elements instanceof Array) this.elements.forEach(callback);
89
+ if (this.elements instanceof NodeList) Array.from(this.elements).forEach(callback);
90
+ return this;
91
+ };
92
+ apx.get = function (index) {
93
+ return this.elements[index];
94
+ };
95
+ apx.all = function () {
96
+ if (this.elements instanceof Array) return this.elements;
97
+ if (this.elements instanceof NodeList) return Array.from(this.elements);
98
+ return this;
99
+ };
100
+ apx.first = function () {
101
+ return this.get(0);
102
+ };
103
+ augmentWithListen(apx);
104
+ augmentWithTristate(apx);
105
+ return apx;
106
+ };
107
+
108
+ APX.loadCss = loadCss;
109
+ APX.dialog = dialog;
110
+ APX.isAPXObject = function (obj) {
111
+ return obj && obj._isAPXObject === true;
112
+ }
113
+ APX.is_numeric = (n) => {
114
+ return !isNaN(n - parseFloat(n));
115
+ }
116
+
117
+ export default APX;
package/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # APX DOM Wrapper
2
+
3
+ APX is a versatile DOM wrapper designed to simplify interactions with HTML elements. It provides a unified interface for working with various input types, including XPath, CSS selectors, individual HTMLElements, NodeLists, HTMLCollections, arrays of elements, jQuery objects, and even functions that return APX objects.
4
+
5
+ ## Features
6
+
7
+ - **XPath Support**: Easily select elements using XPath queries.
8
+ - **CSS Selector Support**: Utilize CSS selectors for element selection.
9
+ - **Enhanced Interactions**: Augmented with modules like `listen` for event handling and `tristate` for managing tristate logic.
10
+ - **Utility Functions**: Includes tools like `loadCss`, and `dialog` for extended functionality.
11
+ - **jQuery Compatibility**: Seamlessly works with jQuery objects.
12
+ - **Function Input**: Accepts functions returning an APX object as input.
13
+
14
+ ## Modules
15
+
16
+ APX is enhanced with several modules, each adding specialized functionality:
17
+
18
+ ### `listen`
19
+
20
+ The `listen` module simplifies event handling for wrapped elements. It provides an intuitive way to attach and manage event listeners.
21
+
22
+ Documentation: [`/modules/listen`](./modules/listen/listen.mjs)
23
+
24
+ ### `tristate`
25
+
26
+ The `tristate` module allows you to manage tristate logic (e.g., on/off/indeterminate) for HTML elements efficiently.
27
+
28
+ Documentation: [`/modules/tristate`](./modules/tristate/tristate.mjs)
29
+
30
+ ### `dialog`
31
+
32
+ The `dialog` module provides utilities to create and manage modal dialogs.
33
+
34
+ Documentation: [`/modules/dialog`](./modules/dialog/dialog.mjs)
35
+
36
+ ### Utilities
37
+
38
+ - **`loadCss`**: Dynamically load CSS files into your document.
39
+
40
+ Documentation: [`/modules/common.mjs`](./modules/common.mjs)
41
+
42
+ ## Usage
43
+
44
+ Here are some examples of how you can use APX:
45
+
46
+ ```javascript
47
+ // Using XPath
48
+ const apx1 = APX('//div[@class="example"]');
49
+
50
+ // Using CSS selector
51
+ const apx2 = APX('.example');
52
+
53
+ // Using a single element
54
+ const element = document.querySelector('.example');
55
+ const apx3 = APX(element);
56
+
57
+ // Using NodeList or HTMLCollection
58
+ const nodeList = document.querySelectorAll('.example');
59
+ const apx4 = APX(nodeList);
60
+
61
+ // Using an array of elements
62
+ const elements = [document.querySelector('.example1'), document.querySelector('.example2')];
63
+ const apx5 = APX(elements);
64
+
65
+ // Using a jQuery object
66
+ const $example = $('.example');
67
+ const apx6 = APX($example);
68
+
69
+ // Using a function returning an APX object
70
+ const apx7 = APX(() => APX('.example'));
71
+ ```
72
+
73
+ ## API
74
+
75
+ ### Core Functionality
76
+
77
+ - **`APX(input, context = document)`**: The main function that creates an APX object. Accepts a wide variety of input types:
78
+ - `string`: XPath or CSS selectors.
79
+ - `HTMLElement`: Single DOM element.
80
+ - `NodeList`/`HTMLCollection`: A collection of DOM elements.
81
+ - `Array<HTMLElement>`: An array of DOM elements.
82
+ - `jQuery`: A jQuery object.
83
+ - `Function`: A function that returns an APX object.
84
+ - **Methods on APX objects**:
85
+ - `.each(callback)`: Iterates over elements.
86
+ - `.get(index)`: Retrieves an element by index.
87
+ - `.all()`: Returns all wrapped elements.
88
+ - `.first()`: Returns the first wrapped element.
89
+
90
+ ### Utilities
91
+
92
+ - **`APX.getApxPathURI()`**: Retrieve the URI path for a given element.
93
+ - **`APX.loadCss()`**: Dynamically load CSS files.
94
+ - **`APX.dialog()`**: Manage and display dialogs.
95
+ - **`APX.isAPXObject(obj)`**: Check if an object is an APX object.
96
+ - **`APX.is_numeric(n)`**: Check if a value is numeric.
97
+
98
+ ## Installation
99
+
100
+ To include APX in your project:
101
+
102
+ 1. Clone the repository or download the package.
103
+ 2. Import APX and its modules into your project:
104
+ ```javascript
105
+ import APX from './path-to-apx/apx.mjs';
106
+ ```
107
+
108
+ ## Contributing
109
+
110
+ We welcome contributions! Please refer to the contribution guidelines in the repository.
111
+
112
+ ## License
113
+
114
+ Author : Thibault SAELEN
115
+ Copyright Appius SARL.