grainjs 1.0.2 → 1.1.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 +23 -71
- package/dist/cjs/index.js +5 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/lib/PriorityQueue.d.ts +1 -1
- package/dist/cjs/lib/_computed_queue.js +3 -3
- package/dist/cjs/lib/_computed_queue.js.map +1 -1
- package/dist/cjs/lib/binding.d.ts +11 -4
- package/dist/cjs/lib/binding.js +5 -5
- package/dist/cjs/lib/binding.js.map +1 -1
- package/dist/cjs/lib/computed.d.ts +49 -28
- package/dist/cjs/lib/computed.js +38 -52
- package/dist/cjs/lib/computed.js.map +1 -1
- package/dist/cjs/lib/dispose.d.ts +109 -96
- package/dist/cjs/lib/dispose.js +106 -80
- package/dist/cjs/lib/dispose.js.map +1 -1
- package/dist/cjs/lib/dom.d.ts +38 -18
- package/dist/cjs/lib/dom.js +44 -20
- package/dist/cjs/lib/dom.js.map +1 -1
- package/dist/cjs/lib/domComponent.d.ts +56 -48
- package/dist/cjs/lib/domComponent.js +66 -1
- package/dist/cjs/lib/domComponent.js.map +1 -1
- package/dist/cjs/lib/domComputed.d.ts +31 -21
- package/dist/cjs/lib/domComputed.js +14 -11
- package/dist/cjs/lib/domComputed.js.map +1 -1
- package/dist/cjs/lib/domDispose.d.ts +27 -12
- package/dist/cjs/lib/domDispose.js +26 -11
- package/dist/cjs/lib/domDispose.js.map +1 -1
- package/dist/cjs/lib/domForEach.d.ts +4 -3
- package/dist/cjs/lib/domForEach.js +10 -9
- package/dist/cjs/lib/domForEach.js.map +1 -1
- package/dist/cjs/lib/domImpl.d.ts +33 -10
- package/dist/cjs/lib/domImpl.js +28 -9
- package/dist/cjs/lib/domImpl.js.map +1 -1
- package/dist/cjs/lib/domMethods.d.ts +93 -47
- package/dist/cjs/lib/domMethods.js +88 -46
- package/dist/cjs/lib/domMethods.js.map +1 -1
- package/dist/cjs/lib/domevent.d.ts +87 -62
- package/dist/cjs/lib/domevent.js +84 -59
- package/dist/cjs/lib/domevent.js.map +1 -1
- package/dist/cjs/lib/emit.d.ts +62 -32
- package/dist/cjs/lib/emit.js +67 -53
- package/dist/cjs/lib/emit.js.map +1 -1
- package/dist/cjs/lib/kowrap.d.ts +6 -3
- package/dist/cjs/lib/kowrap.js +6 -3
- package/dist/cjs/lib/kowrap.js.map +1 -1
- package/dist/cjs/lib/obsArray.d.ts +91 -53
- package/dist/cjs/lib/obsArray.js +87 -55
- package/dist/cjs/lib/obsArray.js.map +1 -1
- package/dist/cjs/lib/observable.d.ts +25 -15
- package/dist/cjs/lib/observable.js +29 -18
- package/dist/cjs/lib/observable.js.map +1 -1
- package/dist/cjs/lib/pureComputed.d.ts +12 -15
- package/dist/cjs/lib/pureComputed.js +15 -18
- package/dist/cjs/lib/pureComputed.js.map +1 -1
- package/dist/cjs/lib/styled.d.ts +78 -61
- package/dist/cjs/lib/styled.js +26 -79
- package/dist/cjs/lib/styled.js.map +1 -1
- package/dist/cjs/lib/subscribe.d.ts +41 -37
- package/dist/cjs/lib/subscribe.js +31 -40
- package/dist/cjs/lib/subscribe.js.map +1 -1
- package/dist/cjs/lib/util.js +1 -0
- package/dist/cjs/lib/util.js.map +1 -1
- package/dist/cjs/lib/widgets/input.d.ts +3 -1
- package/dist/cjs/lib/widgets/input.js +6 -4
- package/dist/cjs/lib/widgets/input.js.map +1 -1
- package/dist/cjs/lib/widgets/select.d.ts +4 -2
- package/dist/cjs/lib/widgets/select.js +7 -5
- package/dist/cjs/lib/widgets/select.js.map +1 -1
- package/dist/esm/lib/_computed_queue.js +3 -3
- package/dist/esm/lib/_computed_queue.js.map +1 -1
- package/dist/esm/lib/binding.js +2 -2
- package/dist/esm/lib/binding.js.map +1 -1
- package/dist/esm/lib/computed.js +36 -50
- package/dist/esm/lib/computed.js.map +1 -1
- package/dist/esm/lib/dispose.js +104 -78
- package/dist/esm/lib/dispose.js.map +1 -1
- package/dist/esm/lib/dom.js +38 -18
- package/dist/esm/lib/dom.js.map +1 -1
- package/dist/esm/lib/domComponent.js +65 -0
- package/dist/esm/lib/domComponent.js.map +1 -1
- package/dist/esm/lib/domComputed.js +10 -7
- package/dist/esm/lib/domComputed.js.map +1 -1
- package/dist/esm/lib/domDispose.js +26 -11
- package/dist/esm/lib/domDispose.js.map +1 -1
- package/dist/esm/lib/domForEach.js +3 -2
- package/dist/esm/lib/domForEach.js.map +1 -1
- package/dist/esm/lib/domImpl.js +26 -7
- package/dist/esm/lib/domImpl.js.map +1 -1
- package/dist/esm/lib/domMethods.js +77 -35
- package/dist/esm/lib/domMethods.js.map +1 -1
- package/dist/esm/lib/domevent.js +84 -59
- package/dist/esm/lib/domevent.js.map +1 -1
- package/dist/esm/lib/emit.js +67 -53
- package/dist/esm/lib/emit.js.map +1 -1
- package/dist/esm/lib/kowrap.js +5 -2
- package/dist/esm/lib/kowrap.js.map +1 -1
- package/dist/esm/lib/obsArray.js +82 -50
- package/dist/esm/lib/obsArray.js.map +1 -1
- package/dist/esm/lib/observable.js +26 -15
- package/dist/esm/lib/observable.js.map +1 -1
- package/dist/esm/lib/pureComputed.js +15 -18
- package/dist/esm/lib/pureComputed.js.map +1 -1
- package/dist/esm/lib/styled.js +24 -77
- package/dist/esm/lib/styled.js.map +1 -1
- package/dist/esm/lib/subscribe.js +27 -36
- package/dist/esm/lib/subscribe.js.map +1 -1
- package/dist/esm/lib/util.js +1 -0
- package/dist/esm/lib/util.js.map +1 -1
- package/dist/esm/lib/widgets/input.js +3 -1
- package/dist/esm/lib/widgets/input.js.map +1 -1
- package/dist/esm/lib/widgets/select.js +3 -1
- package/dist/esm/lib/widgets/select.js.map +1 -1
- package/dist/grain-full.debug.js +2146 -3062
- package/dist/grain-full.debug.js.map +7 -0
- package/dist/grain-full.min.js +6 -2
- package/dist/grain-full.min.js.map +7 -1
- package/lib/binding.ts +9 -2
- package/lib/computed.ts +56 -56
- package/lib/dispose.ts +110 -85
- package/lib/dom.ts +39 -20
- package/lib/domComponent.ts +66 -57
- package/lib/domComputed.ts +29 -19
- package/lib/domDispose.ts +28 -11
- package/lib/domForEach.ts +7 -3
- package/lib/domImpl.ts +30 -7
- package/lib/domMethods.ts +101 -46
- package/lib/domevent.ts +85 -60
- package/lib/emit.ts +64 -50
- package/lib/kowrap.ts +5 -2
- package/lib/obsArray.ts +89 -54
- package/lib/observable.ts +26 -15
- package/lib/pureComputed.ts +16 -22
- package/lib/styled.ts +85 -71
- package/lib/subscribe.ts +41 -45
- package/lib/util.ts +1 -0
- package/lib/widgets/input.ts +3 -1
- package/lib/widgets/select.ts +3 -1
- package/package.json +38 -27
|
@@ -12,10 +12,11 @@ import { MaybeObsArray } from './obsArray';
|
|
|
12
12
|
* If the created nodes are removed from their parent externally, forEach() will cope with it, but
|
|
13
13
|
* will consider these elements as no longer owned, and will not run domDispose() on them.
|
|
14
14
|
*
|
|
15
|
-
* Note that itemCreateFunc()
|
|
16
|
-
*
|
|
15
|
+
* Note that itemCreateFunc() is called with an index as the second argument, but that index is
|
|
16
|
+
* only accurate at the time of the call, and will stop reflecting the true index if more items
|
|
17
|
+
* are inserted or removed before it.
|
|
17
18
|
*
|
|
18
19
|
* If you'd like to map the DOM node back to its source item, use dom.data() and dom.getData() in
|
|
19
20
|
* itemCreateFunc().
|
|
20
21
|
*/
|
|
21
|
-
export declare function forEach<T>(obsArray: MaybeObsArray<T>, itemCreateFunc: (item: T) => Node | null): DomContents;
|
|
22
|
+
export declare function forEach<T>(obsArray: MaybeObsArray<T>, itemCreateFunc: (item: T, index: number) => Node | null): DomContents;
|
|
@@ -19,8 +19,9 @@ const browserGlobals_1 = require("./browserGlobals");
|
|
|
19
19
|
* If the created nodes are removed from their parent externally, forEach() will cope with it, but
|
|
20
20
|
* will consider these elements as no longer owned, and will not run domDispose() on them.
|
|
21
21
|
*
|
|
22
|
-
* Note that itemCreateFunc()
|
|
23
|
-
*
|
|
22
|
+
* Note that itemCreateFunc() is called with an index as the second argument, but that index is
|
|
23
|
+
* only accurate at the time of the call, and will stop reflecting the true index if more items
|
|
24
|
+
* are inserted or removed before it.
|
|
24
25
|
*
|
|
25
26
|
* If you'd like to map the DOM node back to its source item, use dom.data() and dom.getData() in
|
|
26
27
|
* itemCreateFunc().
|
|
@@ -30,18 +31,18 @@ function forEach(obsArray, itemCreateFunc) {
|
|
|
30
31
|
const markerPost = browserGlobals_1.G.document.createComment('b');
|
|
31
32
|
return [markerPre, markerPost, (elem) => {
|
|
32
33
|
if (Array.isArray(obsArray)) {
|
|
33
|
-
domComputed_1.replaceContent(markerPre, markerPost, obsArray.map(itemCreateFunc));
|
|
34
|
+
(0, domComputed_1.replaceContent)(markerPre, markerPost, obsArray.map(itemCreateFunc));
|
|
34
35
|
return;
|
|
35
36
|
}
|
|
36
|
-
const nodes = obsArray_1.computedArray(obsArray, itemCreateFunc);
|
|
37
|
+
const nodes = (0, obsArray_1.computedArray)(obsArray, itemCreateFunc);
|
|
37
38
|
// Be sure to dispose the newly-created array when the DOM it's associated with is gone.
|
|
38
|
-
domDispose_1.autoDisposeElem(markerPost, nodes);
|
|
39
|
+
(0, domDispose_1.autoDisposeElem)(markerPost, nodes);
|
|
39
40
|
nodes.addListener((newArr, oldArr, splice) => {
|
|
40
41
|
if (splice) {
|
|
41
42
|
// Remove the elements that are gone.
|
|
42
43
|
for (const node of splice.deleted) {
|
|
43
44
|
if (node && node.parentNode === elem) {
|
|
44
|
-
domDispose_1.domDispose(node);
|
|
45
|
+
(0, domDispose_1.domDispose)(node);
|
|
45
46
|
elem.removeChild(node);
|
|
46
47
|
}
|
|
47
48
|
}
|
|
@@ -57,15 +58,15 @@ function forEach(obsArray, itemCreateFunc) {
|
|
|
57
58
|
}
|
|
58
59
|
}
|
|
59
60
|
// Insert the new elements.
|
|
60
|
-
const content = domImpl_1.frag(newArr.slice(splice.start, endIndex));
|
|
61
|
+
const content = (0, domImpl_1.frag)(newArr.slice(splice.start, endIndex));
|
|
61
62
|
elem.insertBefore(content, nextElem);
|
|
62
63
|
}
|
|
63
64
|
}
|
|
64
65
|
else {
|
|
65
|
-
domComputed_1.replaceContent(markerPre, markerPost, newArr);
|
|
66
|
+
(0, domComputed_1.replaceContent)(markerPre, markerPost, newArr);
|
|
66
67
|
}
|
|
67
68
|
});
|
|
68
|
-
domComputed_1.replaceContent(markerPre, markerPost, nodes.get());
|
|
69
|
+
(0, domComputed_1.replaceContent)(markerPre, markerPost, nodes.get());
|
|
69
70
|
}];
|
|
70
71
|
}
|
|
71
72
|
exports.forEach = forEach;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"domForEach.js","sourceRoot":"","sources":["../../../lib/domForEach.ts"],"names":[],"mappings":";;;AAAA,+CAA0D;AAC1D,6CAAyD;AACzD,uCAA+B;AAC/B,yCAAkE;AAElE,mFAAmF;AACnF,qDAAmC;AAEnC
|
|
1
|
+
{"version":3,"file":"domForEach.js","sourceRoot":"","sources":["../../../lib/domForEach.ts"],"names":[],"mappings":";;;AAAA,+CAA0D;AAC1D,6CAAyD;AACzD,uCAA+B;AAC/B,yCAAkE;AAElE,mFAAmF;AACnF,qDAAmC;AAEnC;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAgB,OAAO,CACrB,QAA0B,EAC1B,cAAqD;IAErD,MAAM,SAAS,GAAG,kBAAC,CAAC,QAAQ,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;IAChD,MAAM,UAAU,GAAG,kBAAC,CAAC,QAAQ,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;IACjD,OAAO,CAAC,SAAS,EAAE,UAAU,EAAE,CAAC,IAAU,EAAE,EAAE;YAC5C,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;gBAC3B,IAAA,4BAAc,EAAC,SAAS,EAAE,UAAU,EAAE,QAAQ,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC;gBACpE,OAAO;aACR;YAED,MAAM,KAAK,GAAwB,IAAA,wBAAa,EAAC,QAAQ,EAA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AS,EAAE,UAAU,EAAE,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC;QACrD,CAAC,CAAC,CAAC;AACL,CAAC;AAjDD,0BAiDC"}
|
|
@@ -1,26 +1,30 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export type DomMethod<T = Node> = (elem: T) => DomArg<T> | void;
|
|
2
|
+
export type DomElementMethod = DomMethod<HTMLElement>;
|
|
3
|
+
/**
|
|
4
|
+
* Object mapping attribute names to attribute values. When applied to a DOM element, null and
|
|
5
|
+
* undefined values are omitted, and booleans are either omitted or set to empty string.
|
|
6
|
+
*/
|
|
3
7
|
export interface IAttrObj {
|
|
4
8
|
[attrName: string]: string | boolean | null | undefined;
|
|
5
9
|
}
|
|
6
|
-
export
|
|
10
|
+
export type DomArg<T = Node> = Node | string | void | null | undefined | IDomArgs<T> | DomMethod<T> | (T extends Element ? IAttrObj : never);
|
|
7
11
|
export interface IDomArgs<T = Node> extends Array<DomArg<T>> {
|
|
8
12
|
}
|
|
9
|
-
export
|
|
13
|
+
export type DomElementArg = DomArg<HTMLElement>;
|
|
10
14
|
/**
|
|
11
15
|
* dom('tag#id.class1.class2', ...args)
|
|
12
16
|
* The first argument is a string consisting of a tag name, with optional #foo suffix
|
|
13
17
|
* to add the ID 'foo', and zero or more .bar suffixes to add a CSS class 'bar'.
|
|
14
18
|
*
|
|
15
19
|
* NOTE that better typings are available when a tag is used directly, e.g.
|
|
16
|
-
* dom('input', {id: 'foo'}, (elem) => ...)
|
|
17
|
-
* dom('input#foo', (elem) => ...)
|
|
20
|
+
* `dom('input', {id: 'foo'}, (elem) => ...)` -- elem has type HTMLInputElement
|
|
21
|
+
* `dom('input#foo', (elem) => ...)` -- elem has type HTMLElement
|
|
18
22
|
*
|
|
19
23
|
* The rest of the arguments are optional and may be:
|
|
20
24
|
*
|
|
21
25
|
* Nodes - which become children of the created element;
|
|
22
26
|
* strings - which become text node children;
|
|
23
|
-
* objects - of the form {attr: val} to set additional attributes on the element;
|
|
27
|
+
* objects - of the form `{attr: val}` to set additional attributes on the element;
|
|
24
28
|
* Arrays - which are flattened with each item processed recursively;
|
|
25
29
|
* functions - which are called with elem as the argument, for a chance to modify the
|
|
26
30
|
* element as it's being created. Return values are processed recursively.
|
|
@@ -28,8 +32,8 @@ export declare type DomElementArg = DomArg<HTMLElement>;
|
|
|
28
32
|
* are actually special cases of the "functions" category.
|
|
29
33
|
*/
|
|
30
34
|
export declare function dom<Tag extends TagName>(tagString: Tag, ...args: IDomArgs<TagElem<Tag>>): TagElem<Tag>;
|
|
31
|
-
export
|
|
32
|
-
export
|
|
35
|
+
export type TagName = keyof HTMLElementTagNameMap | string;
|
|
36
|
+
export type TagElem<T extends TagName> = T extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[T] : HTMLElement;
|
|
33
37
|
/**
|
|
34
38
|
* svg('tag#id.class1.class2', ...args)
|
|
35
39
|
* Same as dom(...), but creates an SVG element.
|
|
@@ -40,7 +44,26 @@ export declare function svg(tagString: string, ...args: IDomArgs<SVGElement>): S
|
|
|
40
44
|
*/
|
|
41
45
|
export declare function update<T extends Node, Args extends IDomArgs<T>>(elem: T, ...args: Args): T;
|
|
42
46
|
/**
|
|
43
|
-
* Creates a DocumentFragment processing arguments the same way as the dom() function.
|
|
47
|
+
* Creates a `DocumentFragment`, processing arguments in the same way as the `dom()` function.
|
|
48
|
+
*
|
|
49
|
+
* It's rarely needed since an array of `dom()` arguments is treated the same as a
|
|
50
|
+
* `DocumentFragment` in most cases.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* dom.frag(dom('span', 'Hello'), ' good ', dom('div', 'world'))
|
|
55
|
+
* ```
|
|
56
|
+
* creates document fragment with `<span>Hello</span> good <div>world</div>`.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* These two examples are equivalent:
|
|
60
|
+
* ```ts
|
|
61
|
+
* const world1 = () => dom.frag(' good ', dom('div', 'world'));
|
|
62
|
+
* dom('div', 'Hello', world1);
|
|
63
|
+
*
|
|
64
|
+
* const world2 = () => [' good ', dom('div', 'world')];
|
|
65
|
+
* dom('div', 'Hello', world2);
|
|
66
|
+
* ```
|
|
44
67
|
*/
|
|
45
68
|
export declare function frag(...args: IDomArgs<DocumentFragment>): DocumentFragment;
|
|
46
69
|
/**
|
package/dist/cjs/lib/domImpl.js
CHANGED
|
@@ -20,14 +20,14 @@ const browserGlobals_1 = require("./browserGlobals");
|
|
|
20
20
|
* to add the ID 'foo', and zero or more .bar suffixes to add a CSS class 'bar'.
|
|
21
21
|
*
|
|
22
22
|
* NOTE that better typings are available when a tag is used directly, e.g.
|
|
23
|
-
* dom('input', {id: 'foo'}, (elem) => ...)
|
|
24
|
-
* dom('input#foo', (elem) => ...)
|
|
23
|
+
* `dom('input', {id: 'foo'}, (elem) => ...)` -- elem has type HTMLInputElement
|
|
24
|
+
* `dom('input#foo', (elem) => ...)` -- elem has type HTMLElement
|
|
25
25
|
*
|
|
26
26
|
* The rest of the arguments are optional and may be:
|
|
27
27
|
*
|
|
28
28
|
* Nodes - which become children of the created element;
|
|
29
29
|
* strings - which become text node children;
|
|
30
|
-
* objects - of the form {attr: val} to set additional attributes on the element;
|
|
30
|
+
* objects - of the form `{attr: val}` to set additional attributes on the element;
|
|
31
31
|
* Arrays - which are flattened with each item processed recursively;
|
|
32
32
|
* functions - which are called with elem as the argument, for a chance to modify the
|
|
33
33
|
* element as it's being created. Return values are processed recursively.
|
|
@@ -57,11 +57,11 @@ function _createElementSvg(tag) {
|
|
|
57
57
|
/**
|
|
58
58
|
* Internal helper to parse tagString, create an element using createFunc with the given tag, and
|
|
59
59
|
* set its id and classes from the tagString.
|
|
60
|
-
* @param
|
|
60
|
+
* @param createFunc(tag) - Function that should create an element given a tag name.
|
|
61
61
|
* It is passed in to allow creating elements in different namespaces (e.g. plain HTML vs SVG).
|
|
62
|
-
* @param
|
|
62
|
+
* @param tagString - String of the form "tag#id.class1.class2" where id and classes are
|
|
63
63
|
* optional.
|
|
64
|
-
* @
|
|
64
|
+
* @returns {Element} The result of createFunc(), possibly with id and class attributes also set.
|
|
65
65
|
*/
|
|
66
66
|
function _createFromTagString(createFunc, tagString) {
|
|
67
67
|
// We do careful hand-written parsing rather than use a regexp for speed. Using a regexp is
|
|
@@ -122,7 +122,7 @@ function _updateWithArgsOrDispose(elem, args) {
|
|
|
122
122
|
return _updateWithArgs(elem, args);
|
|
123
123
|
}
|
|
124
124
|
catch (e) {
|
|
125
|
-
domDispose_1.domDispose(elem);
|
|
125
|
+
(0, domDispose_1.domDispose)(elem);
|
|
126
126
|
throw e;
|
|
127
127
|
}
|
|
128
128
|
}
|
|
@@ -144,14 +144,33 @@ function _updateWithArg(elem, arg) {
|
|
|
144
144
|
elem.appendChild(arg);
|
|
145
145
|
}
|
|
146
146
|
else if (typeof arg === 'object') {
|
|
147
|
-
domMethods_1.attrsElem(elem, arg);
|
|
147
|
+
(0, domMethods_1.attrsElem)(elem, arg);
|
|
148
148
|
}
|
|
149
149
|
else {
|
|
150
150
|
elem.appendChild(browserGlobals_1.G.document.createTextNode(arg));
|
|
151
151
|
}
|
|
152
152
|
}
|
|
153
153
|
/**
|
|
154
|
-
* Creates a DocumentFragment processing arguments the same way as the dom() function.
|
|
154
|
+
* Creates a `DocumentFragment`, processing arguments in the same way as the `dom()` function.
|
|
155
|
+
*
|
|
156
|
+
* It's rarely needed since an array of `dom()` arguments is treated the same as a
|
|
157
|
+
* `DocumentFragment` in most cases.
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* dom.frag(dom('span', 'Hello'), ' good ', dom('div', 'world'))
|
|
162
|
+
* ```
|
|
163
|
+
* creates document fragment with `<span>Hello</span> good <div>world</div>`.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* These two examples are equivalent:
|
|
167
|
+
* ```ts
|
|
168
|
+
* const world1 = () => dom.frag(' good ', dom('div', 'world'));
|
|
169
|
+
* dom('div', 'Hello', world1);
|
|
170
|
+
*
|
|
171
|
+
* const world2 = () => [' good ', dom('div', 'world')];
|
|
172
|
+
* dom('div', 'Hello', world2);
|
|
173
|
+
* ```
|
|
155
174
|
*/
|
|
156
175
|
function frag(...args) {
|
|
157
176
|
const elem = browserGlobals_1.G.document.createDocumentFragment();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"domImpl.js","sourceRoot":"","sources":["../../../lib/domImpl.ts"],"names":[],"mappings":";;;AAAA,6CAAwC;AACxC,6CAAuC;AAEvC,mFAAmF;AACnF,qDAAmC;
|
|
1
|
+
{"version":3,"file":"domImpl.js","sourceRoot":"","sources":["../../../lib/domImpl.ts"],"names":[],"mappings":";;;AAAA,6CAAwC;AACxC,6CAAuC;AAEvC,mFAAmF;AACnF,qDAAmC;AAqCnC,kGAAkG;AAClG,8CAA8C;AAC9C,4CAA4C;AAC5C,2CAA2C;AAC3C,2CAA2C;AAC3C,2CAA2C;AAC3C,2CAA2C;AAC3C,gFAAgF;AAChF,gFAAgF;AAEhF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,GAAG,CAAsB,SAAc,EAAE,GAAG,IAA4B;IACtF,OAAO,wBAAwB,CAAC,oBAAoB,CAAC,kBAAkB,EAAE,SAAS,CAAiB,EAAE,IAAI,CAAC,CAAC;AAC7G,CAAC;AAFD,kBAEC;AAKD;;;GAGG;AACH,SAAgB,GAAG,CAAC,SAAiB,EAAE,GAAG,IAA0B;IAClE,OAAO,wBAAwB,CAAC,oBAAoB,CAAC,iBAAiB,EAAE,SAAS,CAAC,EAAE,IAAI,CAAC,CAAC;AAC5F,CAAC;AAFD,kBAEC;AAED,gDAAgD;AAChD,SAAS,kBAAkB,CAAC,GAAW;IACrC,OAAO,kBAAC,CAAC,QAAQ,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;AACvC,CAAC;AAED,+CAA+C;AAC/C,SAAS,iBAAiB,CAAC,GAAW;IACpC,OAAO,kBAAC,CA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,IAAW,EAAE,GAAG,CAAC,CAAC;KAC7B;SAAM;QACL,IAAI,CAAC,WAAW,CAAC,kBAAC,CAAC,QAAQ,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,CAAC;KAClD;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,SAAgB,IAAI,CAAC,GAAG,IAAgC;IACtD,MAAM,IAAI,GAAG,kBAAC,CAAC,QAAQ,CAAC,sBAAsB,EAAE,CAAC;IACjD,OAAO,wBAAwB,CAAmB,IAAI,EAAE,IAAI,CAAC,CAAC;AAChE,CAAC;AAHD,oBAGC;AAED;;GAEG;AACH,SAAgB,IAAI,CAAC,QAAgB,IAAI,OAAO,kBAAC,CAAC,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AAArF,oBAAqF;AAErF;;GAEG;AACH,SAAgB,OAAO,CAAC,QAAgB,IAAI,OAAO,kBAAC,CAAC,QAAQ,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AAA3F,0BAA2F"}
|
|
@@ -1,74 +1,109 @@
|
|
|
1
1
|
import { BindableValue } from './binding';
|
|
2
2
|
import { DomElementMethod, DomMethod, IAttrObj } from './domImpl';
|
|
3
3
|
/**
|
|
4
|
-
* Sets multiple attributes of a DOM element.
|
|
4
|
+
* Sets multiple attributes of a DOM element.
|
|
5
5
|
* Null and undefined values are omitted, and booleans are either omitted or set to empty string.
|
|
6
|
-
* @param
|
|
6
|
+
* @param attrsObj - Object mapping attribute names to attribute values.
|
|
7
7
|
*/
|
|
8
8
|
export declare function attrsElem(elem: Element, attrsObj: IAttrObj): void;
|
|
9
|
+
/**
|
|
10
|
+
* Sets multiple attributes of a DOM element. Null and undefined values are omitted, and booleans
|
|
11
|
+
* are either omitted or set to empty string.
|
|
12
|
+
*/
|
|
9
13
|
export declare function attrs(attrsObj: IAttrObj): DomElementMethod;
|
|
10
14
|
/**
|
|
11
15
|
* Sets an attribute of a DOM element to the given value. Removes the attribute when the value is
|
|
12
|
-
* null or undefined.
|
|
13
|
-
*
|
|
14
|
-
* @param
|
|
15
|
-
* @param
|
|
16
|
-
* @param {String|null} attrValue: The string value or null to remove the attribute.
|
|
16
|
+
* null or undefined.
|
|
17
|
+
* @param elem - The element to update.
|
|
18
|
+
* @param attrName - The name of the attribute to bind, e.g. 'href'.
|
|
19
|
+
* @param attrValue - The string value, or null or undefined to remove the attribute.
|
|
17
20
|
*/
|
|
18
|
-
export declare function attrElem(elem: Element, attrName: string, attrValue: string | null): void;
|
|
19
|
-
|
|
21
|
+
export declare function attrElem(elem: Element, attrName: string, attrValue: string | null | undefined): void;
|
|
22
|
+
/**
|
|
23
|
+
* Sets an attribute of a DOM element to the given value. Removes the attribute when the value is
|
|
24
|
+
* null or undefined.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* dom('a', dom.attr('href', urlObs))
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare function attr(attrName: string, attrValueObs: BindableValue<string | null | undefined>): DomElementMethod;
|
|
20
32
|
/**
|
|
21
33
|
* Sets or removes a boolean attribute of a DOM element. According to the spec, empty string is a
|
|
22
34
|
* valid true value for the attribute, and the false value is indicated by the attribute's absence.
|
|
23
|
-
*
|
|
24
|
-
* @param
|
|
25
|
-
* @param
|
|
26
|
-
* @param {Boolean} boolValue: Boolean value whether to set or unset the attribute.
|
|
35
|
+
* @param elem - The element to update.
|
|
36
|
+
* @param attrName - The name of the attribute to bind, e.g. 'checked'.
|
|
37
|
+
* @param boolValue - Boolean value whether to set or unset the attribute.
|
|
27
38
|
*/
|
|
28
39
|
export declare function boolAttrElem(elem: Element, attrName: string, boolValue: boolean): void;
|
|
40
|
+
/**
|
|
41
|
+
* Dom-method that sets or removes a boolean attribute of a DOM element.
|
|
42
|
+
* @param attrName - The name of the attribute to bind, e.g. 'checked'.
|
|
43
|
+
* @param boolValueObs - Value, observable, or function for a whether to set or unset the attribute.
|
|
44
|
+
*/
|
|
29
45
|
export declare function boolAttr(attrName: string, boolValueObs: BindableValue<boolean>): DomElementMethod;
|
|
30
46
|
/**
|
|
31
|
-
* Adds a text node to the element.
|
|
32
|
-
*
|
|
33
|
-
* @param
|
|
34
|
-
* @param {String} value: The text value to add.
|
|
47
|
+
* Adds a text node to the element.
|
|
48
|
+
* @param elem - The element to update.
|
|
49
|
+
* @param value - The text value to add.
|
|
35
50
|
*/
|
|
36
51
|
export declare function textElem(elem: Node, value: string): void;
|
|
52
|
+
/**
|
|
53
|
+
* Sets text content of a DOM element to a value that may be an observable or a function.
|
|
54
|
+
*/
|
|
37
55
|
export declare function text(valueObs: BindableValue<string>): DomMethod;
|
|
38
56
|
/**
|
|
39
|
-
* Sets a style property of a DOM element to the given value.
|
|
40
|
-
*
|
|
41
|
-
* @param
|
|
42
|
-
* @param
|
|
43
|
-
* @param {String} value: The value for the property.
|
|
57
|
+
* Sets a style property of a DOM element to the given value.
|
|
58
|
+
* @param elem - The element to update.
|
|
59
|
+
* @param property - The name of the style property to update, e.g. 'fontWeight'.
|
|
60
|
+
* @param value - The value for the property.
|
|
44
61
|
*/
|
|
45
62
|
export declare function styleElem(elem: Element, property: string, value: string): void;
|
|
63
|
+
/**
|
|
64
|
+
* Sets a style property of a DOM element to the given value, which may be an observable or a
|
|
65
|
+
* function.
|
|
66
|
+
* @param property - The name of the style property to update, e.g. 'fontWeight'.
|
|
67
|
+
* @param value - The value for the property.
|
|
68
|
+
*/
|
|
46
69
|
export declare function style(property: string, valueObs: BindableValue<string>): DomElementMethod;
|
|
47
70
|
/**
|
|
48
71
|
* Sets the property of a DOM element to the given value.
|
|
49
|
-
*
|
|
50
|
-
* @param
|
|
51
|
-
* @param
|
|
52
|
-
* @param {Object} value: The value for the property.
|
|
72
|
+
* @param elem - The element to update.
|
|
73
|
+
* @param property - The name of the property to update, e.g. 'disabled'.
|
|
74
|
+
* @param value - The value for the property.
|
|
53
75
|
*/
|
|
54
76
|
export declare function propElem<T>(elem: Node, property: string, value: T): void;
|
|
77
|
+
/**
|
|
78
|
+
* Sets the property of a DOM element to the given value, which may be an observable or a
|
|
79
|
+
* function.
|
|
80
|
+
* @param property - The name of the property to update, e.g. 'disabled'.
|
|
81
|
+
* @param value - The value for the property.
|
|
82
|
+
*/
|
|
55
83
|
export declare function prop<T>(property: string, valueObs: BindableValue<T>): DomMethod;
|
|
56
84
|
/**
|
|
57
85
|
* Shows or hides the element depending on a boolean value. Note that the element must be visible
|
|
58
86
|
* initially (i.e. unsetting style.display should show it).
|
|
59
|
-
*
|
|
60
|
-
* @param
|
|
61
|
-
* @param {Boolean} boolValue: True to show the element, false to hide it.
|
|
87
|
+
* @param elem - The element to update.
|
|
88
|
+
* @param boolValue - True to show the element, false to hide it.
|
|
62
89
|
*/
|
|
63
90
|
export declare function showElem(elem: HTMLElement, boolValue: boolean): void;
|
|
91
|
+
/**
|
|
92
|
+
* Shows or hides the element depending on a boolean value, which may be an observable or a function.
|
|
93
|
+
* Note that the element must be visible by default (i.e. unsetting `style.display` should show it).
|
|
94
|
+
*/
|
|
64
95
|
export declare function show(boolValueObs: BindableValue<boolean>): DomElementMethod;
|
|
65
96
|
/**
|
|
66
97
|
* The opposite of show, hiding the element when boolValue is true.
|
|
67
|
-
*
|
|
68
|
-
* @param
|
|
69
|
-
* @param {Boolean} boolValue: True to hide the element, false to show it.
|
|
98
|
+
* @param elem - The element to update.
|
|
99
|
+
* @param boolValue - True to hide the element, false to show it.
|
|
70
100
|
*/
|
|
71
101
|
export declare function hideElem(elem: HTMLElement, boolValue: boolean): void;
|
|
102
|
+
/**
|
|
103
|
+
* The opposite of show, hiding the element when boolValue is true. `boolValueObs` may be an
|
|
104
|
+
* observable or a function.
|
|
105
|
+
* Note that the element must be visible by default (i.e. unsetting `style.display` should show it).
|
|
106
|
+
*/
|
|
72
107
|
export declare function hide(boolValueObs: BindableValue<boolean>): DomElementMethod;
|
|
73
108
|
/**
|
|
74
109
|
* Sets or toggles the given css class className.
|
|
@@ -78,12 +113,13 @@ export declare function clsElem(elem: Element, className: string, boolValue?: bo
|
|
|
78
113
|
* Sets or toggles a css class className. If className is an observable, it will be replaced when
|
|
79
114
|
* the observable changes. If a plain string, then an optional second boolean observable may be
|
|
80
115
|
* given, which will toggle it.
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
116
|
+
* ```ts
|
|
117
|
+
* dom.cls('foo') // Sets className 'foo'
|
|
118
|
+
* dom.cls('foo', isFoo); // Toggles 'foo' className according to observable.
|
|
119
|
+
* dom.cls('foo', (use) => use(isFoo)); // Toggles 'foo' className according to observable.
|
|
120
|
+
* dom.cls(fooClass); // Sets className to the value of fooClass observable
|
|
121
|
+
* dom.cls((use) => `prefix-${use(fooClass)}`); // Sets className to prefix- plus fooClass observable.
|
|
122
|
+
* ```
|
|
87
123
|
*/
|
|
88
124
|
export declare function cls(className: string, boolValue?: BindableValue<boolean>): DomElementMethod;
|
|
89
125
|
export declare function cls(className: BindableValue<string>): DomElementMethod;
|
|
@@ -93,19 +129,26 @@ export declare function cls(className: BindableValue<string>): DomElementMethod;
|
|
|
93
129
|
export declare function clsPrefix(prefix: string, className: string, boolValue?: BindableValue<boolean>): DomElementMethod;
|
|
94
130
|
export declare function clsPrefix(prefix: string, className: BindableValue<string>): DomElementMethod;
|
|
95
131
|
/**
|
|
96
|
-
* Associate arbitrary data with a DOM element.
|
|
97
|
-
*
|
|
98
|
-
* @param
|
|
99
|
-
* @param
|
|
100
|
-
* @param {Object} value: Arbitrary value to associate with elem.
|
|
132
|
+
* Associate arbitrary data with a DOM element.
|
|
133
|
+
* @param elem - The element with which to associate data.
|
|
134
|
+
* @param key - Key to identify this piece of data among others attached to elem.
|
|
135
|
+
* @param value - Arbitrary value to associate with elem.
|
|
101
136
|
*/
|
|
102
137
|
export declare function dataElem(elem: Node, key: string, value: any): void;
|
|
138
|
+
/**
|
|
139
|
+
* Associate arbitrary data with a DOM element: `value` may be an observable or a function.
|
|
140
|
+
* @param key - Key to identify this piece of data among others attached to elem.
|
|
141
|
+
* @param value - Arbitrary value to associate with elem.
|
|
142
|
+
*/
|
|
103
143
|
export declare function data(key: string, valueObs: BindableValue<any>): DomMethod;
|
|
144
|
+
/**
|
|
145
|
+
* Retrieve data associated with a DOM element using `data()` or `dataElem()`.
|
|
146
|
+
*/
|
|
104
147
|
export declare function getData(elem: Node, key: string): any;
|
|
105
148
|
/**
|
|
106
149
|
* A very simple setup to identify DOM elements for testing purposes. Here's the recommended
|
|
107
150
|
* usage.
|
|
108
|
-
*
|
|
151
|
+
* ```ts
|
|
109
152
|
* // In the component to be tested.
|
|
110
153
|
* import {noTestId, TestId} from 'grainjs';
|
|
111
154
|
*
|
|
@@ -114,22 +157,25 @@ export declare function getData(elem: Node, key: string): any;
|
|
|
114
157
|
* dom(..., testId("another-name"), ...),
|
|
115
158
|
* );
|
|
116
159
|
* }
|
|
160
|
+
* ```
|
|
117
161
|
*
|
|
118
162
|
* In the fixture code using this component:
|
|
119
|
-
*
|
|
163
|
+
* ```ts
|
|
120
164
|
* import {makeTestId} from 'grainjs';
|
|
121
165
|
*
|
|
122
166
|
* dom(..., myComponent(myArgs, makeTestId('test-mycomp-'), ...)
|
|
167
|
+
* ```
|
|
123
168
|
*
|
|
124
169
|
* In the webdriver test code:
|
|
125
|
-
*
|
|
170
|
+
* ```ts
|
|
126
171
|
* driver.find('.test-my-comp-some-name')
|
|
127
172
|
* driver.find('.test-my-comp-another-name')
|
|
173
|
+
* ```
|
|
128
174
|
*
|
|
129
175
|
* When myComponent() is created with testId argument omitted, the testId() calls are no-ops. When
|
|
130
176
|
* makeTestId('test-foo-') is passed in, testId() calls simply add a css class with that prefix.
|
|
131
177
|
*/
|
|
132
|
-
export
|
|
178
|
+
export type TestId = (name: string) => DomElementMethod | null;
|
|
133
179
|
/**
|
|
134
180
|
* See documentation for TestId above.
|
|
135
181
|
*/
|