@acanto/core-helpers
Advanced tools
Comparing version 0.0.34 to 0.0.35
@@ -1,2 +0,2 @@ | ||
import { $ } from "@acanto/core-dom"; | ||
import { $, getDataAttr } from "@acanto/core-dom"; | ||
import { isFunction, isString } from "./type"; | ||
@@ -59,1 +59,25 @@ | ||
} | ||
/** | ||
* Get data JSON encoded data attribute on given HTML element | ||
*/ | ||
export function getJSONdataAttr<T extends {}>( | ||
$element: HTMLElement, | ||
attr: string | ||
) { | ||
const attrValue = getDataAttr($element, attr); | ||
let data = {}; | ||
try { | ||
data = JSON.parse(attrValue); | ||
} catch (e) { | ||
if (__DEV__) { | ||
console.warn( | ||
`[@acanto/core-helpers/interface] Malformed 'data-${attr}' attribute on DOM element`, | ||
$element | ||
); | ||
} | ||
} | ||
return data as T; | ||
} |
64
item.ts
@@ -25,3 +25,3 @@ /** | ||
import { Emitter } from "./Emitter"; | ||
import { getScopedRoot, Rooter } from "./interface"; | ||
import { getScopedRoot, getJSONdataAttr, Rooter } from "./interface"; | ||
import { getQueryParams, mergeUrlParams, updateLinkParams } from "./location"; | ||
@@ -43,2 +43,6 @@ import { isUndefined } from "./type"; | ||
/** | ||
* Whether the properties should not be editable | ||
*/ | ||
readonlyProperties?: boolean; | ||
/** | ||
* A quantity of this item to put in the cart or other usages | ||
@@ -85,2 +89,14 @@ */ | ||
/** | ||
* Loop through each Item marked with the `data-item-id=""` attribute and run the | ||
* given callback. | ||
*/ | ||
export function eachItem(callback: ($item: HTMLElement) => any) { | ||
const $elements = (toArray( | ||
$$(`[data-${ATTR_ITEM_ID}]`) | ||
) as unknown) as HTMLElement[]; | ||
$elements.forEach(callback); | ||
return $elements; | ||
} | ||
/** | ||
* Loop through each Item with the given id, marked with the `data-item-id=""` | ||
@@ -91,3 +107,3 @@ * attribute and run the given callback. | ||
id: string, | ||
callback: ($btn: HTMLElement) => any | ||
callback: ($item: HTMLElement) => any | ||
) { | ||
@@ -110,18 +126,4 @@ const $elements = (toArray( | ||
): Item.Model & T => { | ||
const attrValue = getDataAttr($item, ATTR_ITEM_DATA); | ||
let data = {}; | ||
try { | ||
data = JSON.parse(attrValue); | ||
} catch (e) { | ||
if (__DEV__) { | ||
console.warn( | ||
`[@acanto/core-helpers/item] Malformed 'data-${ATTR_ITEM_DATA}' attribute on DOM element`, | ||
$item | ||
); | ||
} | ||
} | ||
return { | ||
...(data as Item.Model), | ||
...getJSONdataAttr<Item.Model>($item, ATTR_ITEM_DATA), | ||
...additionalData, | ||
@@ -157,2 +159,3 @@ }; | ||
const { syncUrl } = options || {}; | ||
const { readonlyProperties } = getItemData($root); | ||
const $properties: Record<string, Item.Property> = {}; | ||
@@ -174,3 +177,5 @@ let $quantity: Partial<Item.Property> = {}; | ||
// build maps and watch interactive item data | ||
initProperties(); | ||
if (!readonlyProperties) { | ||
initProperties(); | ||
} | ||
initQuantity(); | ||
@@ -285,9 +290,11 @@ | ||
const current = getItemData($root); | ||
const properties = getPropertiesValue(); | ||
const quantity = getQuantityValue(); | ||
const data = { ...current, properties }; | ||
const data = { ...current }; | ||
if (!isUndefined(quantity)) { | ||
data.quantity = quantity; | ||
} | ||
if (!readonlyProperties) { | ||
data.properties = getPropertiesValue(); | ||
} | ||
@@ -381,8 +388,10 @@ instance.data = data; | ||
eachPropertyElement(($input) => { | ||
const correspondingParam = params[$input.name]; | ||
if (!isUndefined(correspondingParam)) { | ||
setFormElementValue($input, correspondingParam); | ||
} | ||
}); | ||
if (!readonlyProperties) { | ||
eachPropertyElement(($input) => { | ||
const correspondingParam = params[$input.name]; | ||
if (!isUndefined(correspondingParam)) { | ||
setFormElementValue($input, correspondingParam); | ||
} | ||
}); | ||
} | ||
@@ -436,3 +445,3 @@ eachQuantityElement(($input) => { | ||
*/ | ||
function init(rooter?: Rooter, options?: Item.Options) { | ||
export function init(rooter?: Rooter, options?: Item.Options) { | ||
forEach($$(`[data-${ATTR_ITEM_ID}]`, getScopedRoot(rooter)), ($item) => { | ||
@@ -444,3 +453,4 @@ Item($item, options); | ||
export default { | ||
on, | ||
init, | ||
}; |
{ | ||
"name": "@acanto/core-helpers", | ||
"version": "0.0.34", | ||
"version": "0.0.35", | ||
"author": "Acanto <info@acanto.net> (https://acanto.agency/)", | ||
@@ -17,3 +17,3 @@ "license": "ISC", | ||
"@acanto/core-dom": "^0.0.18", | ||
"@acanto/core-forms": "^0.0.43", | ||
"@acanto/core-forms": "^0.0.44", | ||
"date-fns": "^2.16.1", | ||
@@ -24,3 +24,3 @@ "lodash.debounce": "^4.0.8", | ||
"sideEffects": false, | ||
"gitHead": "ce8a3a31c3c90fb08d496e6d33c0f3af7a59e377" | ||
"gitHead": "61258ff48e75b23d232f88dd3bfca320867b9853" | ||
} |
29711
11
1049
+ Added@acanto/core-forms@0.0.44(transitive)
- Removed@acanto/core-forms@0.0.43(transitive)
Updated@acanto/core-forms@^0.0.44