chnobli 0.1.2 → 0.1.4

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,46 @@
1
+ export default class ResponsiveEvent {
2
+ constructor() {
3
+ this._listeners = new Set;
4
+
5
+ window.addEventListener('resize', e => this._onResize(e));
6
+ }
7
+
8
+ static global() {
9
+ // allow to define a test ticker
10
+ if (typeof globalThis !== 'undefined' && globalThis.chnobliResponsive)
11
+ return globalThis.chnobliResponsive;
12
+
13
+ if (typeof window === 'undefined')
14
+ return null;
15
+
16
+ if (typeof window.chnobliResponsive === 'undefined')
17
+ window.chnobliResponsive = new ResponsiveEvent;
18
+ return window.chnobliResponsive;
19
+ }
20
+
21
+ // fn({ width, height }, { remove() })
22
+ add(fn) {
23
+ this._listeners.add(fn);
24
+
25
+ return {
26
+ remove: () => {
27
+ this.remove(fn);
28
+ }
29
+ };
30
+ }
31
+
32
+ remove(fn) {
33
+ this._listeners.delete(fn);
34
+ }
35
+
36
+ _onResize(_e) {
37
+ for (const fn of this._listeners) {
38
+ fn({
39
+ width: window.innerWidth,
40
+ height: window.innerHeight
41
+ }, {
42
+ remove: () => this.remove(fn)
43
+ });
44
+ }
45
+ }
46
+ }
@@ -0,0 +1,31 @@
1
+ export default class TestResponsiveEvent {
2
+ constructor() {
3
+ this._listeners = new Set;
4
+
5
+ globalThis.chnobliResponsive = this;
6
+ }
7
+
8
+ // fn({ width, height }, { remove() })
9
+ add(fn) {
10
+ this._listeners.add(fn);
11
+
12
+ return {
13
+ remove: () => {
14
+ this.remove(fn);
15
+ }
16
+ };
17
+ }
18
+
19
+ remove(fn) {
20
+ this._listeners.delete(fn);
21
+ }
22
+
23
+ resize(width, height) {
24
+ for (const fn of this._listeners) {
25
+ fn(
26
+ { width, height },
27
+ { remove: () => this.remove(fn) }
28
+ );
29
+ }
30
+ }
31
+ }
@@ -28,7 +28,7 @@ export default class ScrollEvent {
28
28
  this._listeners.delete(fn);
29
29
  }
30
30
 
31
- _onScroll(e) {
31
+ _onScroll(_e) {
32
32
  for (const fn of this._listeners) {
33
33
  fn({
34
34
  y: window.scrollY,
@@ -1,6 +1,7 @@
1
- // export class StaggerFunction {
2
-
3
- // }
1
+ /**
2
+ * Staggering allows to have a different value for every target
3
+ * allowing to easely offset each target by a few pixels
4
+ */
4
5
 
5
6
  function isStagger(value) {
6
7
  return typeof value === 'object' &&
@@ -22,11 +23,7 @@ export function staggerMap(value, fn) {
22
23
  return fn(value);
23
24
  }
24
25
 
25
- export function stagger(value) {
26
- return new Stagger(value);
27
- }
28
-
29
- class Stagger {
26
+ export default class Stagger {
30
27
  constructor(value) {
31
28
  if (typeof value === 'function') {
32
29
  this.fn = value;
@@ -0,0 +1,92 @@
1
+ import StyleValue from '../values/stylevalue.js';
2
+
3
+
4
+ export default class BaseTarget {
5
+ constructor(target) {
6
+ this.target = target;
7
+
8
+ // this might be an object, a dom node
9
+ // what about arrays?
10
+ }
11
+
12
+ type() {
13
+ return 'unknown';
14
+ }
15
+
16
+ getValue(name) {
17
+ throw new Error('cannot get value of ' + name);
18
+ }
19
+
20
+ setValue(name, _value) {
21
+ throw new Error('cannot set value of ' + name);
22
+ }
23
+
24
+ removeValue(name) {
25
+ throw new Error('cannot remove value of ' + name);
26
+ }
27
+
28
+ getTransformValue(name) {
29
+ return this.getValue(name);
30
+ }
31
+
32
+ setTransformValue(name, value) {
33
+ return this.setValue(name, value);
34
+ }
35
+
36
+ removeTransformValue(name) {
37
+ return this.removeValue(name);
38
+ }
39
+
40
+ /**
41
+ * returns nothing or a style value
42
+ */
43
+ getStyleValue(name) {
44
+ const val = this.getValue(name);
45
+ if (!val)
46
+ return val;
47
+ return StyleValue([val]);
48
+ }
49
+
50
+ /**
51
+ * Expects a style value
52
+ */
53
+ setStyleValue(name, value) {
54
+ return this.setValue(name, value);
55
+ }
56
+
57
+ removeStyleValue(name) {
58
+ return this.removeValue(name);
59
+ }
60
+
61
+ hasClass(name) {
62
+ throw new Error('cannot know if class exists ' + name);
63
+ }
64
+
65
+ addClass(name) {
66
+ throw new Error('cannot add class ' + name);
67
+ }
68
+
69
+ removeClass(name) {
70
+ throw new Error('cannot remove class ' + name);
71
+ }
72
+
73
+ /**
74
+ * Tries to unify both values so their units match
75
+ *
76
+ * a and b need to be a Value
77
+ */
78
+ unifyValues(name, a, b) {
79
+ if (!a.unit && b.unit)
80
+ a.unit = b.unit;
81
+
82
+ if (!b.unit && a.unit)
83
+ b.unit = a.unit;
84
+
85
+ if (a.unit !== b.unit)
86
+ throw new Error(`${name} cannot unify ${a.unit} and ${b.unit}`);
87
+
88
+ return [a, b];
89
+ }
90
+
91
+ apply() {}
92
+ }
@@ -0,0 +1,266 @@
1
+ import BaseTarget from './basetarget.js';
2
+ import Value from '../values/value.js';
3
+ import StyleValue from '../values/stylevalue.js';
4
+
5
+
6
+ export default class DomTarget extends BaseTarget {
7
+ /**
8
+ * target a dom node or svg
9
+ *
10
+ * extFns: {getComputedStyle, getRootElement}
11
+ */
12
+ constructor(target, extFns) {
13
+ super(target);
14
+
15
+ this._extFns = extFns;
16
+
17
+ // for the moment
18
+
19
+ this.transformValues = new Map;
20
+ this.styleValues = new Map;
21
+ this.values = new Map;
22
+ }
23
+
24
+ type() {
25
+ return 'dom';
26
+ }
27
+
28
+ getValue(name) {
29
+ return this.values.get(name);
30
+ }
31
+
32
+ setValue(name, value) {
33
+ this.values.set(name, value);
34
+ }
35
+
36
+ removeValue(name) {
37
+ this.values.delete(name);
38
+ }
39
+
40
+ getTransformValue(name) {
41
+ const [fn, idx] = TRANSFORM_PROPS[name] ?? [];
42
+ if (!fn)
43
+ return null;
44
+
45
+ const r = this.transformValues.get(fn);
46
+ return r?.[idx ?? 0] ?? null;
47
+ }
48
+
49
+ setTransformValue(name, value) {
50
+ const [fn, idx] = TRANSFORM_PROPS[name] ?? [];
51
+ if (!fn)
52
+ throw new Error('unknown transform ' + name);
53
+
54
+ let vals = this.transformValues.get(fn);
55
+ if (!vals)
56
+ vals = Array(TRANSFORM_FUNCTIONS[fn].length).fill(null);
57
+
58
+ if (idx === null)
59
+ vals = vals.map(() => value.clone());
60
+ else
61
+ vals[idx] = value;
62
+
63
+ this.transformValues.set(fn, vals);
64
+ }
65
+
66
+ removeTransformValue(name) {
67
+ const [fn, idx] = TRANSFORM_PROPS[name] ?? [];
68
+ if (!fn)
69
+ return;
70
+
71
+ if (idx === null) {
72
+ this.transformValues.delete(fn);
73
+ return;
74
+ }
75
+
76
+ let vals = this.transformValues.get(fn);
77
+ if (!vals)
78
+ return;
79
+
80
+ vals[idx] = null;
81
+
82
+ if (vals.every(v => v === null))
83
+ this.transformValues.delete(fn);
84
+ }
85
+
86
+ getStyleValue(name) {
87
+ const v = this.styleValues.get(name);
88
+
89
+ if (typeof v === 'undefined' || v === null) {
90
+ // let's try to get it from the dom
91
+ const style = this._extFns.getComputedStyle(this.target);
92
+ const styleV = style.getPropertyValue(name);
93
+
94
+ if (typeof styleV === 'undefined' || styleV === null)
95
+ return styleV;
96
+
97
+ try {
98
+ const value = StyleValue.parse(styleV);
99
+ this.styleValues.set(name, value);
100
+
101
+ return value;
102
+ } catch (e) {
103
+ console.log('could not parse value', styleV);
104
+ return v;
105
+ }
106
+ } else
107
+ return v;
108
+ }
109
+
110
+ setStyleValue(name, value) {
111
+ this.styleValues.set(name, value);
112
+ }
113
+
114
+ removeStyleValue(name) {
115
+ this.styleValues.set(name, undefined);
116
+ }
117
+
118
+ hasClass(name) {
119
+ return this.target.classList.contains(name);
120
+ }
121
+
122
+ addClass(name) {
123
+ return this.target.classList.add(name);
124
+ }
125
+
126
+ removeClass(name) {
127
+ return this.target.classList.remove(name);
128
+ }
129
+
130
+ _getRem() {
131
+ const rootEl = this._extFns.getRootElement();
132
+ const style = this._extFns.getComputedStyle(rootEl);
133
+ const fontSize = Value.parse(style.fontSize, 'px');
134
+ if (fontSize.unit !== 'px')
135
+ throw new Error('rem could not be determined');
136
+
137
+ return fontSize.num;
138
+ }
139
+
140
+ _convertToPx(name, value) {
141
+ if (value.num === 0)
142
+ return new Value(0, 'px');
143
+
144
+ switch (value.unit) {
145
+ case 'px':
146
+ return value;
147
+ case 'rem':
148
+ return new Value(value.num * this._getRem(), 'px');
149
+ default:
150
+ throw new Error(
151
+ `cannot convert ${name} from ${value.unit} to px`
152
+ );
153
+ }
154
+ }
155
+
156
+ // value needs to be a unit with pixels
157
+ _convertFromPx(name, value, unit) {
158
+ if (value.num === 0)
159
+ return new Value(0, unit);
160
+
161
+ let v;
162
+ switch (unit) {
163
+ case 'px':
164
+ return value;
165
+
166
+ case 'rem':
167
+ return new Value(value.num / this._getRem(), 'rem');
168
+
169
+ case '%':
170
+ v = this.getStyleValue(name).intoValue();
171
+ if (v.unit !== 'px')
172
+ throw new Error('cannot get style value of ' + name);
173
+
174
+ return new Value((value.num / v.num) * 100, '%');
175
+
176
+ default:
177
+ throw new Error(
178
+ `cannot convert ${name} from px to ${unit}`
179
+ );
180
+ }
181
+ }
182
+
183
+ unifyValues(name, a, b) {
184
+ if (a.unit === b.unit)
185
+ return [a, b];
186
+
187
+ if (!a.unit && b.unit)
188
+ a.unit = b.unit;
189
+
190
+ if (!b.unit && a.unit)
191
+ b.unit = a.unit;
192
+
193
+ const aPx = this._convertToPx(name, a);
194
+ const aBUnit = this._convertFromPx(name, aPx, b.unit);
195
+
196
+ return [aBUnit, b];
197
+ }
198
+
199
+ apply() {
200
+ this.target.style.transform = Array.from(this.transformValues.entries())
201
+ .map(([k, vals]) => {
202
+ // since we allow null in default Values we now need to make
203
+ // sure either a value or a default value are set for the
204
+ // transform function
205
+
206
+ const defVals = TRANSFORM_FUNCTIONS[k];
207
+ if (vals.length !== defVals.length)
208
+ throw new Error('transform values don\'t match');
209
+
210
+ const nArray = Array(vals.length);
211
+
212
+ for (let i = 0; i < vals.length; i++) {
213
+ let val = vals[i];
214
+
215
+ if (val === null)
216
+ val = new Value(defVals[i][0]);
217
+
218
+ nArray[i] = val.withDefaultUnit(defVals[i][1]);
219
+ }
220
+
221
+ return `${k}(${nArray.join(',')})`;
222
+ })
223
+ .join(' ');
224
+
225
+ // style
226
+ for (const [k, v] of this.styleValues.entries()) {
227
+ if (v === undefined) {
228
+ this.target.style.removeProperty(k);
229
+ // is this a problem?
230
+ this.styleValues.delete(k);
231
+ } else {
232
+ this.target.style.setProperty(k, v.toString());
233
+ }
234
+ }
235
+
236
+ // values
237
+ for (const [k, _v] of this.values.entries()) {
238
+ console.log('what todo with this ' + k);
239
+ }
240
+ }
241
+ }
242
+
243
+
244
+ const TRANSFORM_PROPS = {
245
+ 'x': ['translate3d', 0],
246
+ 'y': ['translate3d', 1],
247
+ 'z': ['translate3d', 2],
248
+ 'scale': ['scale3d', null],
249
+ 'scaleX': ['scale3d', 0],
250
+ 'scaleY': ['scale3d', 1],
251
+ 'scaleZ': ['scale3d', 2],
252
+ 'rotate': ['rotate3d', null],
253
+ 'rotateX': ['rotate3d', 0],
254
+ 'rotateY': ['rotate3d', 1],
255
+ 'rotateZ': ['rotate3d', 2],
256
+ 'skew': ['skew', 0],
257
+ 'skewX': ['skew', 0],
258
+ 'skewY': ['skew', 1]
259
+ };
260
+
261
+ const TRANSFORM_FUNCTIONS = {
262
+ 'translate3d': [[0, 'px'], [0, 'px'], [0, 'px']],
263
+ 'scale3d': [[1, null], [1, null], [1, null]],
264
+ 'rotate3d': [[0, 'deg'], [0, 'deg'], [0, 'deg']],
265
+ 'skew': [[0, 'deg'], [0, 'deg']]
266
+ };
@@ -0,0 +1,22 @@
1
+ import DomTarget from './domtarget.js';
2
+
3
+
4
+ export function newTarget(target) {
5
+ if (typeof window === 'undefined') {
6
+ if (typeof target.__simulatedDom__ === 'function') {
7
+ return new DomTarget(target, {
8
+ getComputedStyle: target.__getComputedStyleFn__(),
9
+ getRootElement: target.__getRootElementFn__()
10
+ });
11
+ }
12
+ } else {
13
+ if (target instanceof HTMLElement || target instanceof SVGElement) {
14
+ return new DomTarget(target, {
15
+ getComputedStyle: e => window.getComputedStyle(e),
16
+ getRootElement: () => document.documentElement
17
+ });
18
+ }
19
+ }
20
+
21
+ throw new Error('unknown target');
22
+ }
@@ -0,0 +1,61 @@
1
+ let ROOT_ELEMENT = null;
2
+
3
+ export default class TestDomNode {
4
+ constructor() {
5
+ this.computedStyle = {
6
+ getPropertyValue(k) {
7
+ return this[k];
8
+ }
9
+ };
10
+
11
+ this.style = {
12
+ setProperty: (k, v) => {
13
+ this.style[k] = v;
14
+ },
15
+ removeProperty: (k) => {
16
+ delete this.style[k];
17
+ }
18
+ };
19
+
20
+ this.classList = {
21
+ set: new Set,
22
+ contains(name) {
23
+ return this.set.has(name);
24
+ },
25
+ add(name) {
26
+ this.set.add(name);
27
+ },
28
+ remove(name) {
29
+ this.set.delete(name);
30
+ }
31
+ };
32
+ }
33
+
34
+ syncStyles() {
35
+ this.computedStyle = { ...this.style };
36
+ }
37
+
38
+ static getComputedStyle(el) {
39
+ return el.computedStyle;
40
+ }
41
+
42
+ __simulatedDom__() {}
43
+
44
+ __getComputedStyleFn__() {
45
+ return TestDomNode.getComputedStyle;
46
+ }
47
+
48
+ __getRootElementFn__() {
49
+ return getRootElement;
50
+ }
51
+ }
52
+
53
+ export function el() {
54
+ return new TestDomNode();
55
+ }
56
+
57
+ export function getRootElement() {
58
+ if (!ROOT_ELEMENT)
59
+ ROOT_ELEMENT = el();
60
+ return ROOT_ELEMENT;
61
+ }