@bedlamhotel/attrmanager 0.2.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/LICENSE +338 -0
- package/README.md +336 -0
- package/dist/js/attr-manager.cjs +138 -0
- package/dist/js/attr-manager.d.ts +100 -0
- package/dist/js/attr-manager.esm.js +136 -0
- package/dist/js/attr-manager.esm.min.js +2 -0
- package/dist/js/attr-manager.js +144 -0
- package/dist/js/attr-manager.min.cjs +2 -0
- package/dist/js/attr-manager.min.js +2 -0
- package/dist/js/bh-attrmanager.cjs +2 -0
- package/dist/js/bh-attrmanager.d.ts +100 -0
- package/dist/js/bh-attrmanager.esm.js +2 -0
- package/dist/js/bh-attrmanager.js +143 -0
- package/dist/js/bh-attrmanager.min.js +2 -0
- package/package.json +66 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.BhAttrManager = factory());
|
|
5
|
+
})(this, (function () { 'use strict';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A class used to create and/or enforce HTMLElement attribute values.
|
|
9
|
+
*/
|
|
10
|
+
class BhAttrManager {
|
|
11
|
+
el;
|
|
12
|
+
/** Unique ID for self-removal from `restorers` array. */
|
|
13
|
+
id;
|
|
14
|
+
/** An array of the attributes under management in BhNormalizedAttr form. */
|
|
15
|
+
items = [];
|
|
16
|
+
/** An array of the attribute values (used to restore initial DOM state). */
|
|
17
|
+
original = [];
|
|
18
|
+
/** A var used to track the state of the attributes under management. */
|
|
19
|
+
currentState = false;
|
|
20
|
+
/** Optional reference to the restorer array for self-removal. */
|
|
21
|
+
restorers;
|
|
22
|
+
/**
|
|
23
|
+
* Generates a unique ID for this instance.
|
|
24
|
+
*/
|
|
25
|
+
static createId() {
|
|
26
|
+
return Math.random().toString(36).slice(2, 7);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Constructs a new BhAttrManager instance.
|
|
30
|
+
*
|
|
31
|
+
* @param el
|
|
32
|
+
* The HTMLElement to manage.
|
|
33
|
+
* @param config
|
|
34
|
+
* The attribute configuration.
|
|
35
|
+
* @param restorers
|
|
36
|
+
* Optional restorer array for lifecycle management.
|
|
37
|
+
*/
|
|
38
|
+
constructor(el, config, restorers) {
|
|
39
|
+
this.el = el;
|
|
40
|
+
let hasExplicitInitial = false;
|
|
41
|
+
// Generate unique ID for self-removal from restorers
|
|
42
|
+
this.id = BhAttrManager.createId();
|
|
43
|
+
for (const [name, { whenTrue: trueValue, whenFalse, initial, fixed },] of Object.entries(config)) {
|
|
44
|
+
if (trueValue === undefined) {
|
|
45
|
+
throw new Error("BhAttrManager: each attribute must supply a `whenTrue` value.");
|
|
46
|
+
}
|
|
47
|
+
if (initial !== undefined &&
|
|
48
|
+
!["whenTrue", "whenFalse", null].includes(initial)) {
|
|
49
|
+
throw new Error("BhAttrManager: if attributes supply an `initial` value, it must be one of `whenTrue`, `whenFalse`, or `null`.");
|
|
50
|
+
}
|
|
51
|
+
const originalVal = el.getAttribute(name);
|
|
52
|
+
this.original.push([name, originalVal]);
|
|
53
|
+
const falseValue = whenFalse !== undefined ? whenFalse : originalVal;
|
|
54
|
+
this.items.push({ name, trueValue, falseValue, fixed: !!fixed });
|
|
55
|
+
if (initial !== undefined) {
|
|
56
|
+
this.applyValue(name, initial === "whenTrue" ? trueValue : falseValue);
|
|
57
|
+
if (!hasExplicitInitial) {
|
|
58
|
+
this.currentState = initial === "whenTrue";
|
|
59
|
+
hasExplicitInitial = true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// Register restorer with ID for self-removal
|
|
64
|
+
if (restorers) {
|
|
65
|
+
this.restorers = restorers;
|
|
66
|
+
restorers.push({
|
|
67
|
+
id: this.id,
|
|
68
|
+
restore: () => this.restore(),
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Toggles state of attributes managed by this instance.
|
|
74
|
+
*
|
|
75
|
+
* @param condition
|
|
76
|
+
* The condition to set. If omitted, toggles the current state.
|
|
77
|
+
*/
|
|
78
|
+
toggle(condition) {
|
|
79
|
+
this.currentState = condition ?? !this.currentState;
|
|
80
|
+
for (const { name, trueValue, falseValue, fixed } of this.items) {
|
|
81
|
+
if (fixed) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
this.applyValue(name, this.currentState ? trueValue : falseValue);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Restores original state of attributes.
|
|
89
|
+
*
|
|
90
|
+
* @param teardown
|
|
91
|
+
* If `true` (default), also clears internal state and self-removes from
|
|
92
|
+
* `restorers`.
|
|
93
|
+
*/
|
|
94
|
+
restore(teardown = false) {
|
|
95
|
+
for (const [name, value] of this.original) {
|
|
96
|
+
this.applyValue(name, value);
|
|
97
|
+
}
|
|
98
|
+
if (teardown) {
|
|
99
|
+
this.destroy();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Clears internal state and self-removes from `restorers`.
|
|
104
|
+
*
|
|
105
|
+
* Use this to clean up state without affecting the DOM.
|
|
106
|
+
*/
|
|
107
|
+
destroy() {
|
|
108
|
+
// Remove self from restorers if registered
|
|
109
|
+
if (this.restorers) {
|
|
110
|
+
const index = this.restorers.findIndex((r) => r.id === this.id);
|
|
111
|
+
if (index > -1) {
|
|
112
|
+
this.restorers.splice(index, 1);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// Clear all state
|
|
116
|
+
this.items = [];
|
|
117
|
+
this.original = [];
|
|
118
|
+
this.currentState = false;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Handles actual DOM attribute manipulation for toggle(), restore().
|
|
122
|
+
*
|
|
123
|
+
* @param name
|
|
124
|
+
* The attribute name.
|
|
125
|
+
* @param value
|
|
126
|
+
* The value to set. Pass `null` to remove the attribute.
|
|
127
|
+
*/
|
|
128
|
+
applyValue(name, value) {
|
|
129
|
+
if (this.el.getAttribute(name) === value) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (value === null) {
|
|
133
|
+
this.el.removeAttribute(name);
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
this.el.setAttribute(name, value);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return BhAttrManager;
|
|
142
|
+
|
|
143
|
+
}));
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/*! bh-attrmanager 0.0.0 — © Christopher Torgalson */
|
|
2
|
+
var global,factory;global=this,factory=function(){class BhAttrManager{el;id;items=[];original=[];currentState=!1;restorers;static createId(){return Math.random().toString(36).slice(2,7)}constructor(t,e,r){this.el=t;let i=!1;this.id=BhAttrManager.createId();for(const[r,{whenTrue:s,whenFalse:a,initial:o,fixed:n}]of Object.entries(e)){if(void 0===s)throw new Error("BhAttrManager: each attribute must supply a `whenTrue` value.");if(void 0!==o&&!["whenTrue","whenFalse",null].includes(o))throw new Error("BhAttrManager: if attributes supply an `initial` value, it must be one of `whenTrue`, `whenFalse`, or `null`.");const e=t.getAttribute(r);this.original.push([r,e]);const l=void 0!==a?a:e;this.items.push({name:r,trueValue:s,falseValue:l,fixed:!!n}),void 0!==o&&(this.applyValue(r,"whenTrue"===o?s:l),i||(this.currentState="whenTrue"===o,i=!0))}r&&(this.restorers=r,r.push({id:this.id,restore:()=>this.restore()}))}toggle(t){this.currentState=t??!this.currentState;for(const{name:t,trueValue:e,falseValue:r,fixed:i}of this.items)i||this.applyValue(t,this.currentState?e:r)}restore(t=!1){for(const[t,e]of this.original)this.applyValue(t,e);t&&this.destroy()}destroy(){if(this.restorers){const t=this.restorers.findIndex(t=>t.id===this.id);t>-1&&this.restorers.splice(t,1)}this.items=[],this.original=[],this.currentState=!1}applyValue(t,e){this.el.getAttribute(t)!==e&&(null===e?this.el.removeAttribute(t):this.el.setAttribute(t,e))}}return BhAttrManager},"object"==typeof exports&&"undefined"!=typeof module?module.exports=factory():"function"==typeof define&&define.amd?define(factory):(global="undefined"!=typeof globalThis?globalThis:global||self).BhAttrManager=factory();
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bedlamhotel/attrmanager",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.2.0",
|
|
5
|
+
"description": "Utility library for toggling HTML attributes and optionally restoring them to their original values.",
|
|
6
|
+
"homepage": "https://github.com/ctorgalson/bh-attrmanager",
|
|
7
|
+
"license": "GPL-2.0-or-later",
|
|
8
|
+
"main": "dist/js/attr-manager.js",
|
|
9
|
+
"module": "dist/js/attr-manager.esm.js",
|
|
10
|
+
"types": "dist/js/attr-manager.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "dist/js/attr-manager.esm.js",
|
|
14
|
+
"browser": "dist/js/attr-manager.js",
|
|
15
|
+
"require": "dist/js/attr-manager.cjs",
|
|
16
|
+
"types": "dist/js/attr-manager.d.ts"
|
|
17
|
+
},
|
|
18
|
+
"./*": "./*"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"html",
|
|
22
|
+
"DOM"
|
|
23
|
+
],
|
|
24
|
+
"author": "Christopher Torgalson",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/ctorgalson/bh-attrmanager.git"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist/*"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"dev": "vite",
|
|
34
|
+
"preview": "vite preview",
|
|
35
|
+
"build": "concurrently --raw npm:build:*",
|
|
36
|
+
"build:docs": "typedoc --logLevel Verbose --coverageLabel 'Docs' --coverageOutputType json && node scripts/typedoc-coverage.cjs",
|
|
37
|
+
"build:ts": "rollup --config",
|
|
38
|
+
"start": "npm run build ; npm run watch",
|
|
39
|
+
"test": "vitest run",
|
|
40
|
+
"test:coverage": "vitest run --coverage && node scripts/vitest-coverage.cjs",
|
|
41
|
+
"test:watch": "vitest",
|
|
42
|
+
"coverage:badges": "node scripts/typedoc-coverage.cjs && node scripts/vitest-coverage.cjs",
|
|
43
|
+
"watch": "concurrently --kill-others --raw npm:watch:*",
|
|
44
|
+
"watch:md": "onchange 'README.md' -- npm run build:docs",
|
|
45
|
+
"watch:ts": "onchange 'src/**/*.ts' -- concurrently --raw npm:build:ts npm:build:docs"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@rollup/plugin-terser": "^1.0.0",
|
|
49
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
50
|
+
"@vitest/coverage-v8": "^4.1.11",
|
|
51
|
+
"badge-maker": "^6.0.0",
|
|
52
|
+
"concurrently": "^10.0.5",
|
|
53
|
+
"jsdom": "^30.0.1",
|
|
54
|
+
"onchange": "^7.1.0",
|
|
55
|
+
"prettier": "^3.9.6",
|
|
56
|
+
"rollup": "^4.63.0",
|
|
57
|
+
"typedoc-plugin-coverage": "^4.0.3",
|
|
58
|
+
"typedoc-plugin-markdown": "^4.13.0",
|
|
59
|
+
"typedoc-plugin-mdn-links": "^5.1.1",
|
|
60
|
+
"typedoc-plugin-rename-defaults": "^0.7.3",
|
|
61
|
+
"typescript": "~6.0.2",
|
|
62
|
+
"vite": "^8.2.2",
|
|
63
|
+
"vitest": "^4.1.11",
|
|
64
|
+
"@rollup/plugin-node-resolve": "^16.0.3"
|
|
65
|
+
}
|
|
66
|
+
}
|