@txstate-mws/svelte-components 1.1.3 → 1.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.
@@ -0,0 +1,21 @@
1
+ import type { WritableSubject } from '@txstate-mws/svelte-store';
2
+ declare class ElementQueries {
3
+ #private;
4
+ refresh(): void;
5
+ fullresync(): void;
6
+ activate(): void;
7
+ subscribe(el: HTMLElement, store?: WritableSubject<{
8
+ width: number;
9
+ }>): () => void;
10
+ }
11
+ export declare const elementqueries: ElementQueries;
12
+ interface EqOpts {
13
+ store: WritableSubject<{
14
+ width: number;
15
+ }>;
16
+ }
17
+ export declare function eq(el: HTMLElement, opts?: EqOpts): {
18
+ update(opts?: EqOpts): void;
19
+ destroy(): void;
20
+ };
21
+ export {};
@@ -0,0 +1,156 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
6
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
7
+ if (kind === "m") throw new TypeError("Private method is not writable");
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
+ };
12
+ var _ElementQueries_instances, _ElementQueries_watchlist, _ElementQueries_busy, _ElementQueries_hasInit, _ElementQueries_mutationobserver, _ElementQueries_boundRefresh, _ElementQueries_lastw, _ElementQueries_subscribers, _ElementQueries_stores, _ElementQueries_processwidths, _ElementQueries_finddepth, _ElementQueries_collecttargets, _ElementQueries_gotoanchor, _ElementQueries_activate, _ElementQueries_activateRaw, _ElementQueries_deactivate;
13
+ class ElementQueries {
14
+ constructor() {
15
+ _ElementQueries_instances.add(this);
16
+ _ElementQueries_watchlist.set(this, []);
17
+ _ElementQueries_busy.set(this, false);
18
+ _ElementQueries_hasInit.set(this, false);
19
+ _ElementQueries_mutationobserver.set(this, void 0);
20
+ _ElementQueries_boundRefresh.set(this, this.refresh.bind(this));
21
+ _ElementQueries_lastw.set(this, void 0);
22
+ _ElementQueries_subscribers.set(this, 0);
23
+ _ElementQueries_stores.set(this, new Map());
24
+ }
25
+ refresh() {
26
+ if (!__classPrivateFieldGet(this, _ElementQueries_busy, "f") && __classPrivateFieldGet(this, _ElementQueries_watchlist, "f").length > 0) {
27
+ __classPrivateFieldSet(this, _ElementQueries_busy, true, "f");
28
+ requestAnimationFrame(__classPrivateFieldGet(this, _ElementQueries_instances, "m", _ElementQueries_processwidths).bind(this));
29
+ }
30
+ }
31
+ fullresync() {
32
+ __classPrivateFieldGet(this, _ElementQueries_instances, "m", _ElementQueries_collecttargets).call(this);
33
+ this.refresh();
34
+ }
35
+ // if someone uses the library without svelte, they may just activate it once
36
+ // instead of faithfully subscribing and unsubscribing, so we'll add a subscriber that can't be
37
+ // decremented and it will stay activated forever
38
+ activate() {
39
+ var _a;
40
+ __classPrivateFieldSet(this, _ElementQueries_subscribers, (_a = __classPrivateFieldGet(this, _ElementQueries_subscribers, "f"), _a++, _a), "f");
41
+ __classPrivateFieldGet(this, _ElementQueries_instances, "m", _ElementQueries_activateRaw).call(this);
42
+ }
43
+ subscribe(el, store) {
44
+ var _a;
45
+ __classPrivateFieldSet(this, _ElementQueries_subscribers, (_a = __classPrivateFieldGet(this, _ElementQueries_subscribers, "f"), _a++, _a), "f");
46
+ if (store)
47
+ __classPrivateFieldGet(this, _ElementQueries_stores, "f").set(el, store);
48
+ else
49
+ __classPrivateFieldGet(this, _ElementQueries_stores, "f").delete(el);
50
+ if (!__classPrivateFieldGet(this, _ElementQueries_hasInit, "f")) {
51
+ __classPrivateFieldSet(this, _ElementQueries_hasInit, true, "f");
52
+ __classPrivateFieldGet(this, _ElementQueries_instances, "m", _ElementQueries_activateRaw).call(this);
53
+ }
54
+ return () => {
55
+ var _a;
56
+ if (__classPrivateFieldSet(this, _ElementQueries_subscribers, (_a = __classPrivateFieldGet(this, _ElementQueries_subscribers, "f"), --_a), "f") === 0)
57
+ __classPrivateFieldGet(this, _ElementQueries_instances, "m", _ElementQueries_deactivate).call(this);
58
+ };
59
+ }
60
+ }
61
+ _ElementQueries_watchlist = new WeakMap(), _ElementQueries_busy = new WeakMap(), _ElementQueries_hasInit = new WeakMap(), _ElementQueries_mutationobserver = new WeakMap(), _ElementQueries_boundRefresh = new WeakMap(), _ElementQueries_lastw = new WeakMap(), _ElementQueries_subscribers = new WeakMap(), _ElementQueries_stores = new WeakMap(), _ElementQueries_instances = new WeakSet(), _ElementQueries_processwidths = function _ElementQueries_processwidths() {
62
+ for (let i = 0; i < __classPrivateFieldGet(this, _ElementQueries_watchlist, "f").length; i++) {
63
+ const widths = [];
64
+ const attrs = [];
65
+ for (let j = 0; j < __classPrivateFieldGet(this, _ElementQueries_watchlist, "f")[i].length; j++) {
66
+ widths.push(__classPrivateFieldGet(this, _ElementQueries_watchlist, "f")[i][j].offsetWidth);
67
+ attrs.push(__classPrivateFieldGet(this, _ElementQueries_watchlist, "f")[i][j].getAttribute('data-eq'));
68
+ }
69
+ for (let j = 0; j < __classPrivateFieldGet(this, _ElementQueries_watchlist, "f")[i].length; j++) {
70
+ const el = __classPrivateFieldGet(this, _ElementQueries_watchlist, "f")[i][j];
71
+ const w = widths[j];
72
+ let attrstr = '';
73
+ let finalw = 1600;
74
+ for (let k = 1600; k >= w && k > 700; k -= 100) {
75
+ attrstr += `${k}px `;
76
+ finalw = k;
77
+ }
78
+ for (let k = 700; k >= w; k -= 50) {
79
+ attrstr += `${k}px `;
80
+ finalw = k;
81
+ }
82
+ if (attrstr !== attrs[j])
83
+ el.setAttribute('data-eq', attrstr);
84
+ if (__classPrivateFieldGet(this, _ElementQueries_lastw, "f") !== finalw && __classPrivateFieldGet(this, _ElementQueries_stores, "f").has(el)) {
85
+ __classPrivateFieldGet(this, _ElementQueries_stores, "f").get(el).update(v => ({ ...v, width: finalw }));
86
+ __classPrivateFieldSet(this, _ElementQueries_lastw, finalw, "f");
87
+ }
88
+ }
89
+ }
90
+ __classPrivateFieldSet(this, _ElementQueries_busy, false, "f");
91
+ }, _ElementQueries_finddepth = function _ElementQueries_finddepth(target, currentdepth = 0) {
92
+ const closest = target.parentElement?.closest('.eq-parent');
93
+ if (!closest)
94
+ return currentdepth;
95
+ return __classPrivateFieldGet(this, _ElementQueries_instances, "m", _ElementQueries_finddepth).call(this, closest, currentdepth + 1);
96
+ }, _ElementQueries_collecttargets = function _ElementQueries_collecttargets() {
97
+ __classPrivateFieldSet(this, _ElementQueries_watchlist, [], "f");
98
+ const targets = Array.from(document.querySelectorAll('.eq-parent'));
99
+ for (const target of targets) {
100
+ const depth = __classPrivateFieldGet(this, _ElementQueries_instances, "m", _ElementQueries_finddepth).call(this, target);
101
+ if (!__classPrivateFieldGet(this, _ElementQueries_watchlist, "f")[depth])
102
+ __classPrivateFieldGet(this, _ElementQueries_watchlist, "f")[depth] = [target];
103
+ else
104
+ __classPrivateFieldGet(this, _ElementQueries_watchlist, "f")[depth].push(target);
105
+ }
106
+ }, _ElementQueries_gotoanchor = function _ElementQueries_gotoanchor() {
107
+ // since we are dramatically resizing, we need to scroll to the proper place for an
108
+ // anchor tag
109
+ const m = document.location.hash.match(/^#([a-z][\w:.-]*)$/i);
110
+ if (m) {
111
+ const id = m[1];
112
+ requestAnimationFrame(() => document.getElementById(id) || document.querySelector(`[name="${id}"]`).scrollIntoView(true));
113
+ }
114
+ }, _ElementQueries_activate = function _ElementQueries_activate() {
115
+ this.fullresync();
116
+ window.addEventListener('resize', __classPrivateFieldGet(this, _ElementQueries_boundRefresh, "f"));
117
+ __classPrivateFieldSet(this, _ElementQueries_mutationobserver, new MutationObserver(() => this.fullresync()), "f");
118
+ __classPrivateFieldGet(this, _ElementQueries_mutationobserver, "f").observe(document.body, {
119
+ subtree: true,
120
+ childList: true,
121
+ attributeFilter: ['class', 'style']
122
+ });
123
+ }, _ElementQueries_activateRaw = function _ElementQueries_activateRaw() {
124
+ if (typeof window === 'undefined' || __classPrivateFieldGet(this, _ElementQueries_hasInit, "f"))
125
+ return;
126
+ __classPrivateFieldSet(this, _ElementQueries_hasInit, true, "f");
127
+ if (['interactive', 'complete'].includes(document.readyState)) {
128
+ __classPrivateFieldGet(this, _ElementQueries_instances, "m", _ElementQueries_activate).call(this);
129
+ }
130
+ else {
131
+ window.addEventListener('DOMContentLoaded', () => {
132
+ __classPrivateFieldGet(this, _ElementQueries_instances, "m", _ElementQueries_activate).call(this);
133
+ __classPrivateFieldGet(this, _ElementQueries_instances, "m", _ElementQueries_gotoanchor).call(this);
134
+ });
135
+ }
136
+ }, _ElementQueries_deactivate = function _ElementQueries_deactivate() {
137
+ if (__classPrivateFieldGet(this, _ElementQueries_subscribers, "f") > 0 || typeof window === 'undefined')
138
+ return;
139
+ window.removeEventListener('resize', __classPrivateFieldGet(this, _ElementQueries_boundRefresh, "f"));
140
+ __classPrivateFieldGet(this, _ElementQueries_mutationobserver, "f")?.disconnect();
141
+ __classPrivateFieldSet(this, _ElementQueries_hasInit, false, "f");
142
+ };
143
+ export const elementqueries = new ElementQueries();
144
+ export function eq(el, opts) {
145
+ el.classList.add('eq-parent');
146
+ let unsubscribe = elementqueries.subscribe(el, opts?.store);
147
+ return {
148
+ update(opts) {
149
+ unsubscribe();
150
+ unsubscribe = elementqueries.subscribe(el, opts?.store);
151
+ },
152
+ destroy() {
153
+ unsubscribe();
154
+ }
155
+ };
156
+ }
@@ -1,4 +1,5 @@
1
1
  export * from './buttonify.js';
2
+ export * from './elementqueries.js';
2
3
  export * from './glue.js';
3
4
  export * from './offset.js';
4
5
  export * from './portal.js';
package/actions/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './buttonify.js';
2
+ export * from './elementqueries.js';
2
3
  export * from './glue.js';
3
4
  export * from './offset.js';
4
5
  export * from './portal.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@txstate-mws/svelte-components",
3
- "version": "1.1.3",
3
+ "version": "1.2.0",
4
4
  "description": "Svelte components that are generically useful.",
5
5
  "dependencies": {
6
6
  "@txstate-mws/svelte-store": "^1.0.3",
@@ -31,6 +31,7 @@
31
31
  "exports": {
32
32
  "./package.json": "./package.json",
33
33
  "./actions/buttonify": "./actions/buttonify.js",
34
+ "./actions/elementqueries": "./actions/elementqueries.js",
34
35
  "./actions/glue": "./actions/glue.js",
35
36
  "./actions": "./actions/index.js",
36
37
  "./actions/offset": "./actions/offset.js",