@srfnstack/fntags 0.4.6 → 0.5.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/package.json +1 -1
- package/src/fnroute.d.mts +6 -6
- package/src/fnroute.d.mts.map +1 -1
- package/src/fnroute.mjs +3 -3
- package/src/fntags.d.mts +17 -19
- package/src/fntags.d.mts.map +1 -1
- package/src/fntags.mjs +48 -66
package/package.json
CHANGED
package/src/fnroute.d.mts
CHANGED
|
@@ -22,17 +22,17 @@
|
|
|
22
22
|
* )
|
|
23
23
|
* )
|
|
24
24
|
*
|
|
25
|
-
* @param {Object|Node} children The attributes and children of this element.
|
|
25
|
+
* @param {(Object|Node)[]} children The attributes and children of this element.
|
|
26
26
|
* @returns {HTMLDivElement} A div element that will only be displayed if the current route starts with the path attribute.
|
|
27
27
|
*/
|
|
28
|
-
export function route(...children: any | Node): HTMLDivElement;
|
|
28
|
+
export function route(...children: (any | Node)[]): HTMLDivElement;
|
|
29
29
|
/**
|
|
30
30
|
* An element that only renders the first route that matches and updates when the route is changed
|
|
31
31
|
* The primary purpose of this element is to provide catchall routes for not found pages and path variables
|
|
32
|
-
* @param {
|
|
32
|
+
* @param {(Object|Node)[]} children
|
|
33
33
|
* @returns {Node|(()=>Node)}
|
|
34
34
|
*/
|
|
35
|
-
export function routeSwitch(...children: any): Node | (() => Node);
|
|
35
|
+
export function routeSwitch(...children: (any | Node)[]): Node | (() => Node);
|
|
36
36
|
/**
|
|
37
37
|
* The main function of this library. It will load the route at the specified path and render it into the container element.
|
|
38
38
|
* @param {object} options
|
|
@@ -54,10 +54,10 @@ export function modRouter({ routePath, attrs, onerror, frame, sendRawPath, forma
|
|
|
54
54
|
}): HTMLElement;
|
|
55
55
|
/**
|
|
56
56
|
* A link element that is a link to another route in this single page app
|
|
57
|
-
* @param {
|
|
57
|
+
* @param {(Object|Node)[]} children The attributes of the anchor element and any children
|
|
58
58
|
* @returns {HTMLAnchorElement} An anchor element that will navigate to the specified route when clicked
|
|
59
59
|
*/
|
|
60
|
-
export function fnlink(...children: any): HTMLAnchorElement;
|
|
60
|
+
export function fnlink(...children: (any | Node)[]): HTMLAnchorElement;
|
|
61
61
|
/**
|
|
62
62
|
* A function to navigate to the specified route
|
|
63
63
|
* @param {string} route The route to navigate to
|
package/src/fnroute.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fnroute.d.mts","sourceRoot":"","sources":["fnroute.mjs"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,mCAHW,MAAO,IAAI,
|
|
1
|
+
{"version":3,"file":"fnroute.d.mts","sourceRoot":"","sources":["fnroute.mjs"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,mCAHW,CAAC,MAAO,IAAI,CAAC,EAAE,GACb,cAAc,CAoB1B;AAED;;;;;GAKG;AACH,yCAHW,CAAC,MAAO,IAAI,CAAC,EAAE,GACb,IAAI,GAAC,CAAC,MAAI,IAAI,CAAC,CAwB3B;AAeD;;;;;;;;;;GAUG;AACH;eARW,MAAM;WACN,MAAM;qBACE,KAAK,gBAAgB,MAAM,KAAG,IAAI,GAAC,IAAI;kBACxC,IAAI,UAAU,MAAM,KAAG,IAAI;iBAClC,OAAO;uBACA,MAAM,KAAG,MAAM;IACrB,WAAW,CA0DtB;AAwBD;;;;GAIG;AACH,oCAHW,CAAC,MAAO,IAAI,CAAC,EAAE,GACb,iBAAiB,CAuB7B;AAED;;;;;;GAMG;AACH,4BALW,MAAM,WACN,GAAG,YACH,OAAO,WACP,OAAO,QA6CjB;AA6DD;;;;;;GAMG;AACH,qDAFY,MAAI,IAAI,CAanB;AAED;;;GAGG;AACH,sCAFW,MAAM,QAOhB;AApFD;;;GAGG;AAEH;;;GAGG;AACH,6BAFU,OAAO,cAAc,EAAE,OAAO,CAAC,cAAc,CAAC,CAEf;AAEzC;;;GAGG;AAEH;;;GAGG;AACH,wBAFU,OAAO,cAAc,EAAE,OAAO,CAAC,SAAS,CAAC,CAO/C;AAEJ;;GAEG;AACH;;;GAGG;AACH,gCAFU,UAAU,CAEgC;AACpD;;;GAGG;AACH,+BAFU,UAAU,CAE8B;AAClD;;;GAGG;AACH,kCAFU,UAAU,CAEoC;;;;;;;;wBA/B3C;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,GAAG,CAAA;CAAC;yBAetD,MAAM"}
|
package/src/fnroute.mjs
CHANGED
|
@@ -28,7 +28,7 @@ import { fnstate, getAttrs, h, isAttrs, renderNode } from './fntags.mjs'
|
|
|
28
28
|
* )
|
|
29
29
|
* )
|
|
30
30
|
*
|
|
31
|
-
* @param {Object|Node} children The attributes and children of this element.
|
|
31
|
+
* @param {(Object|Node)[]} children The attributes and children of this element.
|
|
32
32
|
* @returns {HTMLDivElement} A div element that will only be displayed if the current route starts with the path attribute.
|
|
33
33
|
*/
|
|
34
34
|
export function route (...children) {
|
|
@@ -54,7 +54,7 @@ export function route (...children) {
|
|
|
54
54
|
/**
|
|
55
55
|
* An element that only renders the first route that matches and updates when the route is changed
|
|
56
56
|
* The primary purpose of this element is to provide catchall routes for not found pages and path variables
|
|
57
|
-
* @param {
|
|
57
|
+
* @param {(Object|Node)[]} children
|
|
58
58
|
* @returns {Node|(()=>Node)}
|
|
59
59
|
*/
|
|
60
60
|
export function routeSwitch (...children) {
|
|
@@ -187,7 +187,7 @@ function updatePathParameters () {
|
|
|
187
187
|
|
|
188
188
|
/**
|
|
189
189
|
* A link element that is a link to another route in this single page app
|
|
190
|
-
* @param {
|
|
190
|
+
* @param {(Object|Node)[]} children The attributes of the anchor element and any children
|
|
191
191
|
* @returns {HTMLAnchorElement} An anchor element that will navigate to the specified route when clicked
|
|
192
192
|
*/
|
|
193
193
|
export function fnlink (...children) {
|
package/src/fntags.d.mts
CHANGED
|
@@ -42,18 +42,17 @@ export function fntemplate(templateFn: (any: any) => Node): (any: any) => Node;
|
|
|
42
42
|
/**
|
|
43
43
|
* @template T The type of data stored in the state container
|
|
44
44
|
* @typedef FnStateObj A container for a state value that can be bound to.
|
|
45
|
-
* @property {(element
|
|
46
|
-
* If called with no parameters, the state's value will be rendered as an element.
|
|
47
|
-
*
|
|
48
|
-
* @property {(parent: Node|any|(()=>Node|any),element: Node|any|(()=>Node|any), update?: (Node)=>void)=> Node|(()=> Node)} bindChildren Bind the values of this state to the given element.
|
|
45
|
+
* @property {(element?: ()=>(Node|any))=>Node} bindAs Bind this state to the given element function. This causes the element to be replaced when state changes.
|
|
46
|
+
* If called with no parameters, the state's value will be rendered as an element.
|
|
47
|
+
* @property {(parent: ()=>(Node|any)|any|Node, element: (childState: FnState)=>(Node|any))=>Node} bindChildren Bind the values of this state to the given element.
|
|
49
48
|
* Values are items/elements of an array.
|
|
50
49
|
* If the current value is not an array, this will behave the same as bindAs.
|
|
51
|
-
* @property {(
|
|
50
|
+
* @property {(prop: string)=>Node} bindProp Bind to a property of an object stored in this state instead of the state itself.
|
|
52
51
|
* Shortcut for `mystate.bindAs((current)=> current[prop])`
|
|
53
|
-
* @property {(attribute
|
|
54
|
-
* @property {(style
|
|
55
|
-
* @property {(element
|
|
56
|
-
* @property {(attribute
|
|
52
|
+
* @property {(attribute?: ()=>(string|any))=>any} bindAttr Bind attribute values to state changes
|
|
53
|
+
* @property {(style?: ()=>string) => string} bindStyle Bind style values to state changes
|
|
54
|
+
* @property {(element?: ()=>(Node|any))=>Node} bindSelect Bind selected state to an element
|
|
55
|
+
* @property {(attribute?: ()=>(string|any))=>any} bindSelectAttr Bind selected state to an attribute
|
|
57
56
|
* @property {(key: any)=>void} select Mark the element with the given key as selected
|
|
58
57
|
* where the key is identified using the mapKey function passed on creation of the fnstate.
|
|
59
58
|
* This causes the bound select functions to be executed.
|
|
@@ -119,38 +118,37 @@ export function styled<T extends HTMLElement | SVGElement>(style: object | strin
|
|
|
119
118
|
*/
|
|
120
119
|
export type FnStateObj<T> = {
|
|
121
120
|
/**
|
|
122
|
-
* Bind this state to the given element. This causes the element to
|
|
123
|
-
* If called with no parameters, the state's value will be rendered as an element.
|
|
124
|
-
* the second parameter (the update function) must be provided and must be a function. This function receives the node the state is bound to.
|
|
121
|
+
* Bind this state to the given element function. This causes the element to be replaced when state changes.
|
|
122
|
+
* If called with no parameters, the state's value will be rendered as an element.
|
|
125
123
|
*/
|
|
126
|
-
bindAs: (element
|
|
124
|
+
bindAs: (element?: () => (Node | any)) => Node;
|
|
127
125
|
/**
|
|
128
126
|
* Bind the values of this state to the given element.
|
|
129
127
|
* Values are items/elements of an array.
|
|
130
128
|
* If the current value is not an array, this will behave the same as bindAs.
|
|
131
129
|
*/
|
|
132
|
-
bindChildren: (parent:
|
|
130
|
+
bindChildren: (parent: () => (Node | any) | any | Node, element: (childState: any) => (Node | any)) => Node;
|
|
133
131
|
/**
|
|
134
132
|
* Bind to a property of an object stored in this state instead of the state itself.
|
|
135
133
|
* Shortcut for `mystate.bindAs((current)=> current[prop])`
|
|
136
134
|
*/
|
|
137
|
-
bindProp: (
|
|
135
|
+
bindProp: (prop: string) => Node;
|
|
138
136
|
/**
|
|
139
137
|
* Bind attribute values to state changes
|
|
140
138
|
*/
|
|
141
|
-
bindAttr: (attribute
|
|
139
|
+
bindAttr: (attribute?: () => (string | any)) => any;
|
|
142
140
|
/**
|
|
143
141
|
* Bind style values to state changes
|
|
144
142
|
*/
|
|
145
|
-
bindStyle: (style
|
|
143
|
+
bindStyle: (style?: () => string) => string;
|
|
146
144
|
/**
|
|
147
145
|
* Bind selected state to an element
|
|
148
146
|
*/
|
|
149
|
-
bindSelect: (element
|
|
147
|
+
bindSelect: (element?: () => (Node | any)) => Node;
|
|
150
148
|
/**
|
|
151
149
|
* Bind selected state to an attribute
|
|
152
150
|
*/
|
|
153
|
-
bindSelectAttr: (attribute
|
|
151
|
+
bindSelectAttr: (attribute?: () => (string | any)) => any;
|
|
154
152
|
/**
|
|
155
153
|
* Mark the element with the given key as selected
|
|
156
154
|
* where the key is identified using the mapKey function passed on creation of the fnstate.
|
package/src/fntags.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fntags.d.mts","sourceRoot":"","sources":["fntags.mjs"],"names":[],"mappings":"AAAA;;GAEG;AACH;;;;;;;;;;;;;;;;;;;GAmBG;AACH,2DALW,MAAM,eACN,IAAI,MAAO,KA6CrB;AAUD;;;;;;;;;;;;;;;GAeG;AACH,qDAJkB,IAAI,iBACH,IAAI,CA0DtB;AAED
|
|
1
|
+
{"version":3,"file":"fntags.d.mts","sourceRoot":"","sources":["fntags.mjs"],"names":[],"mappings":"AAAA;;GAEG;AACH;;;;;;;;;;;;;;;;;;;GAmBG;AACH,2DALW,MAAM,eACN,IAAI,MAAO,KA6CrB;AAUD;;;;;;;;;;;;;;;GAeG;AACH,qDAJkB,IAAI,iBACH,IAAI,CA0DtB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH;;;GAGG;AAEH;;;;;;;;;;GAUG;AACH,iEAPgB,GAAG,cA+KlB;AAiRD;;;;GAIG;AACH,iCAHW,GAAG,GACD,IAAI,CAuBhB;AAwFD;;;;GAIG;AACH,6BAHW,GAAG,GACD,OAAO,CAInB;AAED;;;;GAIG;AACH,mCAHW,GAAG,GACF,MAAM,CAIjB;AAED;;;;;;;;;;GAUG;AACH,kEALW,MAAM,GAAC,MAAM,OACb,MAAM,YACN,MAAM,EAAE,GAAC,IAAI,EAAE,KAezB;;;;;;;;;uBA9nBwB,MAAI,CAAC,IAAI,GAAC,GAAG,CAAC,KAAG,IAAI;;;;;;2BAEvB,MAAI,CAAC,IAAI,GAAC,GAAG,CAAC,GAAC,GAAG,GAAC,IAAI,gCAAkC,CAAC,IAAI,GAAC,GAAG,CAAC,KAAG,IAAI;;;;;qBAG5E,MAAM,KAAG,IAAI;;;;2BAEP,MAAI,CAAC,MAAM,GAAC,GAAG,CAAC,KAAG,GAAG;;;;wBAC1B,MAAI,MAAM,KAAK,MAAM;;;;2BACnB,MAAI,CAAC,IAAI,GAAC,GAAG,CAAC,KAAG,IAAI;;;;iCACnB,MAAI,CAAC,MAAM,GAAC,GAAG,CAAC,KAAG,GAAG;;;;;;kBAC7B,GAAG,KAAG,IAAI;;;;cAGhB,MAAK,GAAG;;;;;qBACC,CAAC,KAAG,IAAI;;;;;;oBAEV,MAAM,KAAG,GAAG;;;;oBAGZ,MAAM,SAAS,GAAG,mBAAmB,OAAO,KAAG,IAAI;;;;uCAClC,CAAC,YAAY,CAAC,KAAG,IAAI,KAAK,IAAI;;;;oBAC7C,OAAO,KAAG,EAAE;;;;eACrB,OAAO;;;;;sDAKqB,CAAC,KAAG,CAAC"}
|
package/src/fntags.mjs
CHANGED
|
@@ -148,18 +148,17 @@ export function fntemplate (templateFn) {
|
|
|
148
148
|
/**
|
|
149
149
|
* @template T The type of data stored in the state container
|
|
150
150
|
* @typedef FnStateObj A container for a state value that can be bound to.
|
|
151
|
-
* @property {(element
|
|
152
|
-
* If called with no parameters, the state's value will be rendered as an element.
|
|
153
|
-
*
|
|
154
|
-
* @property {(parent: Node|any|(()=>Node|any),element: Node|any|(()=>Node|any), update?: (Node)=>void)=> Node|(()=> Node)} bindChildren Bind the values of this state to the given element.
|
|
151
|
+
* @property {(element?: ()=>(Node|any))=>Node} bindAs Bind this state to the given element function. This causes the element to be replaced when state changes.
|
|
152
|
+
* If called with no parameters, the state's value will be rendered as an element.
|
|
153
|
+
* @property {(parent: ()=>(Node|any)|any|Node, element: (childState: FnState)=>(Node|any))=>Node} bindChildren Bind the values of this state to the given element.
|
|
155
154
|
* Values are items/elements of an array.
|
|
156
155
|
* If the current value is not an array, this will behave the same as bindAs.
|
|
157
|
-
* @property {(
|
|
156
|
+
* @property {(prop: string)=>Node} bindProp Bind to a property of an object stored in this state instead of the state itself.
|
|
158
157
|
* Shortcut for `mystate.bindAs((current)=> current[prop])`
|
|
159
|
-
* @property {(attribute
|
|
160
|
-
* @property {(style
|
|
161
|
-
* @property {(element
|
|
162
|
-
* @property {(attribute
|
|
158
|
+
* @property {(attribute?: ()=>(string|any))=>any} bindAttr Bind attribute values to state changes
|
|
159
|
+
* @property {(style?: ()=>string) => string} bindStyle Bind style values to state changes
|
|
160
|
+
* @property {(element?: ()=>(Node|any))=>Node} bindSelect Bind selected state to an element
|
|
161
|
+
* @property {(attribute?: ()=>(string|any))=>any} bindSelectAttr Bind selected state to an attribute
|
|
163
162
|
* @property {(key: any)=>void} select Mark the element with the given key as selected
|
|
164
163
|
* where the key is identified using the mapKey function passed on creation of the fnstate.
|
|
165
164
|
* This causes the bound select functions to be executed.
|
|
@@ -216,22 +215,21 @@ export function fnstate (initialValue, mapKey) {
|
|
|
216
215
|
/**
|
|
217
216
|
* Bind this state to the given element
|
|
218
217
|
*
|
|
219
|
-
* @param [element] The element to bind to. If not a function, an update function must be passed. If not passed, defaults to the state's value
|
|
220
|
-
* @
|
|
221
|
-
* @returns {(HTMLDivElement|Text)[]|HTMLDivElement|Text}
|
|
218
|
+
* @param {((T)=>(Node|any))?} [element] The element to bind to. If not a function, an update function must be passed. If not passed, defaults to the state's value
|
|
219
|
+
* @returns {()=>Node}
|
|
222
220
|
*/
|
|
223
|
-
ctx.state.bindAs = (element
|
|
221
|
+
ctx.state.bindAs = (element) => doBindAs(ctx, element ?? ctx.state)
|
|
224
222
|
|
|
225
223
|
/**
|
|
226
224
|
* Bind the values of this state to the given element.
|
|
227
225
|
* Values are items/elements of an array.
|
|
228
226
|
* If the current value is not an array, this will behave the same as bindAs.
|
|
229
227
|
*
|
|
230
|
-
* @param parent The parent to bind the children to.
|
|
231
|
-
* @param
|
|
232
|
-
* @
|
|
228
|
+
* @param {(()=>(Node|any)) | Node | any} parent The parent to bind the children to.
|
|
229
|
+
* @param {(childState: FnState)=>(Node|any)} element A function that receives each element wrapped as a fnstate and produces an element
|
|
230
|
+
* @returns {Node}
|
|
233
231
|
*/
|
|
234
|
-
ctx.state.bindChildren = (parent, element
|
|
232
|
+
ctx.state.bindChildren = (parent, element) => doBindChildren(ctx, parent, element)
|
|
235
233
|
|
|
236
234
|
/**
|
|
237
235
|
* Bind to a property of an object stored in this state instead of the state itself.
|
|
@@ -239,35 +237,35 @@ export function fnstate (initialValue, mapKey) {
|
|
|
239
237
|
* Shortcut for `mystate.bindAs((current)=> current[prop])`
|
|
240
238
|
*
|
|
241
239
|
* @param {string} prop The object property to bind as
|
|
242
|
-
* @returns {(
|
|
240
|
+
* @returns {()=>Node}
|
|
243
241
|
*/
|
|
244
242
|
ctx.state.bindProp = (prop) => doBindAs(ctx, (st) => st[prop])
|
|
245
243
|
|
|
246
244
|
/**
|
|
247
245
|
* Bind attribute values to state changes
|
|
248
|
-
* @param [attribute] A function that returns an attribute value. If not passed, defaults to the state's value
|
|
249
|
-
* @returns {()=>
|
|
246
|
+
* @param {(()=>(string|any))?} [attribute] A function that returns an attribute value. If not passed, defaults to the state's value
|
|
247
|
+
* @returns {()=>(string|any)} A function that calls the passed function, with some extra metadata
|
|
250
248
|
*/
|
|
251
249
|
ctx.state.bindAttr = (attribute) => doBindAttr(ctx.state, attribute ?? ctx.state)
|
|
252
250
|
|
|
253
251
|
/**
|
|
254
252
|
* Bind style values to state changes
|
|
255
|
-
* @param [style] A function that returns a style's value. If not passed, defaults to the state's value
|
|
253
|
+
* @param {(()=>string)?} [style] A function that returns a style's value. If not passed, defaults to the state's value
|
|
256
254
|
* @returns {()=>Node} A function that calls the passed function, with some extra metadata
|
|
257
255
|
*/
|
|
258
256
|
ctx.state.bindStyle = (style) => doBindStyle(ctx.state, style ?? ctx.state)
|
|
259
257
|
|
|
260
258
|
/**
|
|
261
259
|
* Bind select and deselect to an element
|
|
262
|
-
* @param [element] The element to bind to. If not
|
|
263
|
-
* @
|
|
260
|
+
* @param {(()=>(Node|any))?} [element] The element to bind to. If not passed, defaults to the state's value
|
|
261
|
+
* @returns {()=>Node}
|
|
264
262
|
*/
|
|
265
|
-
ctx.state.bindSelect = (element
|
|
263
|
+
ctx.state.bindSelect = (element) => doBindSelect(ctx, element ?? ctx.state)
|
|
266
264
|
|
|
267
265
|
/**
|
|
268
266
|
* Bind select and deselect to an attribute
|
|
269
|
-
* @param [attribute] A function that returns an attribute value. If not passed, defaults to the state's value
|
|
270
|
-
* @returns {()=>any} A function that calls the passed function, with some extra metadata
|
|
267
|
+
* @param {(()=>(string|any))?} [attribute] A function that returns an attribute value. If not passed, defaults to the state's value
|
|
268
|
+
* @returns {()=>(string|any)} A function that calls the passed function, with some extra metadata
|
|
271
269
|
*/
|
|
272
270
|
ctx.state.bindSelectAttr = (attribute) => doBindSelectAttr(ctx, attribute ?? ctx.state)
|
|
273
271
|
|
|
@@ -286,6 +284,7 @@ export function fnstate (initialValue, mapKey) {
|
|
|
286
284
|
|
|
287
285
|
/**
|
|
288
286
|
* Perform an Object.assign() on the current state using the provided update
|
|
287
|
+
* @param {T} [update]
|
|
289
288
|
*/
|
|
290
289
|
ctx.state.assign = (update) => ctx.state(Object.assign(ctx.currentValue, update))
|
|
291
290
|
|
|
@@ -294,6 +293,7 @@ export function fnstate (initialValue, mapKey) {
|
|
|
294
293
|
*
|
|
295
294
|
* This returns a reference to the real current value. If you perform any modifications to the object, be sure to call setPath after you're done or the changes
|
|
296
295
|
* will not be reflected correctly.
|
|
296
|
+
* @param {string} [path] a json path type path that points to a property
|
|
297
297
|
*/
|
|
298
298
|
ctx.state.getPath = (path) => {
|
|
299
299
|
if (typeof path !== 'string') {
|
|
@@ -318,9 +318,9 @@ export function fnstate (initialValue, mapKey) {
|
|
|
318
318
|
|
|
319
319
|
/**
|
|
320
320
|
* Set a value at the given property path
|
|
321
|
-
* @param path The JSON path of the value to set
|
|
322
|
-
* @param value The value to set the path to
|
|
323
|
-
* @param fillWithObjects Whether to non object values with new empty objects.
|
|
321
|
+
* @param {string} path The JSON path of the value to set
|
|
322
|
+
* @param {any} value The value to set the path to
|
|
323
|
+
* @param {boolean} fillWithObjects Whether to non object values with new empty objects.
|
|
324
324
|
*/
|
|
325
325
|
ctx.state.setPath = (path, value, fillWithObjects = false) => {
|
|
326
326
|
const s = path.split('.')
|
|
@@ -346,12 +346,14 @@ export function fnstate (initialValue, mapKey) {
|
|
|
346
346
|
|
|
347
347
|
/**
|
|
348
348
|
* Register a callback that will be executed whenever the state is changed
|
|
349
|
-
* @
|
|
349
|
+
* @param {(newValue:T,oldValue:T)=>void} callback
|
|
350
|
+
* @return {()=>void} a function to stop the subscription
|
|
350
351
|
*/
|
|
351
352
|
ctx.state.subscribe = (callback) => doSubscribe(ctx, ctx.observers, callback)
|
|
352
353
|
|
|
353
354
|
/**
|
|
354
355
|
* Remove all the observers and optionally reset the value to it's initial value
|
|
356
|
+
* @param {boolean} reInit whether to reset the state to it's initial value
|
|
355
357
|
*/
|
|
356
358
|
ctx.state.reset = (reInit) => doReset(ctx, reInit, initialValue)
|
|
357
359
|
|
|
@@ -428,13 +430,13 @@ function doSelect (ctx, key) {
|
|
|
428
430
|
}
|
|
429
431
|
}
|
|
430
432
|
|
|
431
|
-
function doBindChildren (ctx, parent, element
|
|
433
|
+
function doBindChildren (ctx, parent, element) {
|
|
432
434
|
parent = renderNode(parent)
|
|
433
435
|
if (parent === undefined || parent.nodeType === undefined) {
|
|
434
436
|
throw new Error('You must provide a parent element to bind the children to. aka Need Bukkit.')
|
|
435
437
|
}
|
|
436
|
-
if (typeof element !== 'function'
|
|
437
|
-
throw new Error('You must pass
|
|
438
|
+
if (typeof element !== 'function') {
|
|
439
|
+
throw new Error('You must pass a function to produce child elements.')
|
|
438
440
|
}
|
|
439
441
|
if (typeof ctx.mapKey !== 'function') {
|
|
440
442
|
console.warn('Using value index as key, may not work correctly when moving items...')
|
|
@@ -445,8 +447,8 @@ function doBindChildren (ctx, parent, element, update) {
|
|
|
445
447
|
throw new Error('You can only use bindChildren with a state that contains an array. try myState([mystate]) before calling this function.')
|
|
446
448
|
}
|
|
447
449
|
ctx.currentValue = ctx.currentValue.map(v => v.isFnState ? v : fnstate(v))
|
|
448
|
-
ctx.bindContexts.push({ element,
|
|
449
|
-
ctx.state.subscribe((
|
|
450
|
+
ctx.bindContexts.push({ element, parent })
|
|
451
|
+
ctx.state.subscribe((_, oldState) => {
|
|
450
452
|
if (!Array.isArray(ctx.currentValue)) {
|
|
451
453
|
console.warn('A state used with bindChildren was updated to a non array value. This will be converted to an array of 1 and the state will be updated.')
|
|
452
454
|
new Promise((resolve) => {
|
|
@@ -467,19 +469,13 @@ function doBindChildren (ctx, parent, element, update) {
|
|
|
467
469
|
return parent
|
|
468
470
|
}
|
|
469
471
|
|
|
470
|
-
const doBind = function (ctx, element,
|
|
471
|
-
if (typeof element !== 'function'
|
|
472
|
-
throw new Error('You must pass
|
|
473
|
-
}
|
|
474
|
-
if (typeof update === 'function') {
|
|
475
|
-
const boundElement = renderNode(evaluateElement(element, ctx.currentValue))
|
|
476
|
-
handleUpdate(boundElement)
|
|
477
|
-
return boundElement
|
|
478
|
-
} else {
|
|
479
|
-
const elCtx = { current: renderNode(evaluateElement(element, ctx.currentValue)) }
|
|
480
|
-
handleReplace(elCtx)
|
|
481
|
-
return () => elCtx.current
|
|
472
|
+
const doBind = function (ctx, element, handleReplace) {
|
|
473
|
+
if (typeof element !== 'function') {
|
|
474
|
+
throw new Error('You must pass a function to bind with')
|
|
482
475
|
}
|
|
476
|
+
const elCtx = { current: renderNode(evaluateElement(element, ctx.currentValue)) }
|
|
477
|
+
handleReplace(elCtx)
|
|
478
|
+
return () => elCtx.current
|
|
483
479
|
}
|
|
484
480
|
|
|
485
481
|
const updateReplacer = (ctx, element, elCtx) => () => {
|
|
@@ -509,25 +505,11 @@ const updateReplacer = (ctx, element, elCtx) => () => {
|
|
|
509
505
|
}
|
|
510
506
|
}
|
|
511
507
|
|
|
512
|
-
const doBindSelect = (ctx, element
|
|
513
|
-
doBind(ctx, element,
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
subscribeSelect(
|
|
518
|
-
ctx,
|
|
519
|
-
updateReplacer(ctx, element, elCtx)
|
|
520
|
-
)
|
|
521
|
-
)
|
|
522
|
-
|
|
523
|
-
const doBindAs = (ctx, element, update) =>
|
|
524
|
-
doBind(ctx, element, update,
|
|
525
|
-
boundElement => {
|
|
526
|
-
ctx.state.subscribe(() => update(boundElement))
|
|
527
|
-
},
|
|
528
|
-
(elCtx) =>
|
|
529
|
-
ctx.state.subscribe(updateReplacer(ctx, element, elCtx))
|
|
530
|
-
)
|
|
508
|
+
const doBindSelect = (ctx, element) =>
|
|
509
|
+
doBind(ctx, element, (elCtx) => subscribeSelect(ctx, updateReplacer(ctx, element, elCtx)))
|
|
510
|
+
|
|
511
|
+
const doBindAs = (ctx, element) =>
|
|
512
|
+
doBind(ctx, element, (elCtx) => ctx.state.subscribe(updateReplacer(ctx, element, elCtx)))
|
|
531
513
|
|
|
532
514
|
/**
|
|
533
515
|
* Reconcile the state of the current array value with the state of the bound elements
|