@kizmann/pico-js 2.0.6 → 2.0.7

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kizmann/pico-js",
3
- "version": "2.0.6",
3
+ "version": "2.0.7",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "private": false,
@@ -130,6 +130,28 @@ export class PicoDomAttributeInstance
130
130
  value = { ...styles, ...value };
131
131
  }
132
132
 
133
+ if ( value.translate ) {
134
+ value.transform = `translate3d(${value.translate.join(',')}, 0)`;
135
+ }
136
+
137
+ delete value.translate;
138
+
139
+ if ( Mix.isNum(value.top) ) {
140
+ value.top += 'px';
141
+ }
142
+
143
+ if ( Mix.isNum(value.left) ) {
144
+ value.left += 'px';
145
+ }
146
+
147
+ if ( Mix.isNum(value.width) ) {
148
+ value.width += 'px';
149
+ }
150
+
151
+ if ( Mix.isNum(value.height) ) {
152
+ value.height += 'px';
153
+ }
154
+
133
155
  this.attr('style', value);
134
156
 
135
157
  return this;
@@ -100,9 +100,10 @@ export class PicoDomEventInstance
100
100
  options = { id: options };
101
101
  }
102
102
 
103
- if ( Mix.isArr(event) ) {
104
- return (Arr.each(event, (e) => this.on(e, ...arguments)), this);
105
- }
103
+ // geht so nicht
104
+ // if ( Mix.isArr(event) ) {
105
+ // return (Arr.each(event, (e) => this.on(e, cb, options, pause, selector)), this);
106
+ // }
106
107
 
107
108
  let fn = (e) => {
108
109
  cb.call(e.target, e, e.target);
@@ -127,10 +128,15 @@ export class PicoDomEventInstance
127
128
  */
128
129
  off(event, selector = null, options = {})
129
130
  {
130
- if ( Mix.isArr(event) ) {
131
- return (Arr.each(event, (e) => this.off(e, ...arguments)), this);
131
+ if ( Mix.isPrim(options) ) {
132
+ options = { id: options };
132
133
  }
133
134
 
135
+ // geht so nicht
136
+ // if ( Mix.isArr(event) ) {
137
+ // return (Arr.each(event, (e) => this.off(e, ...arguments)), this);
138
+ // }
139
+
134
140
  if ( Mix.isObj(selector) ) {
135
141
  (options = selector, selector = null);
136
142
  }
@@ -220,11 +226,12 @@ export class PicoDomEventInstance
220
226
  * @example Dom.find("div").fire("click")
221
227
  *
222
228
  * @param {string} event Event name
229
+ * @param {object} [detail] Event detail
223
230
  * @returns {PicoDom} Current instance
224
231
  */
225
- fire(event)
232
+ fire(event, detail = {})
226
233
  {
227
- let callback = new Event(event);
234
+ let callback = new CustomEvent(event, { detail });
228
235
 
229
236
  this.each((el) => {
230
237
  el.dispatchEvent(callback);
@@ -443,6 +443,13 @@ export class PicoDomFinderInstance
443
443
  return null;
444
444
  }
445
445
 
446
+ upnode(selector)
447
+ {
448
+ return Dom.find(...[
449
+ this.closest(selector)
450
+ ]);
451
+ }
452
+
446
453
  /**
447
454
  * Get previous element
448
455
  *
@@ -610,7 +617,7 @@ export class PicoDomFinderInstance
610
617
  contains(selector)
611
618
  {
612
619
  // Has child of selector
613
- return this.find(selector) != null;
620
+ return this.el.contains(selector);
614
621
  }
615
622
 
616
623
  }
@@ -15,28 +15,7 @@ export class PicoDomObserverStatic
15
15
  */
16
16
  export class PicoDomObserverInstance
17
17
  {
18
-
19
- /**
20
- * Get or set element value
21
- *
22
- * @example Dom.find("input").value("hello")
23
- *
24
- * @param {any} [value] New value
25
- * @returns {any|PicoDom} Value or instance
26
- */
27
- value(value = undefined)
28
- {
29
- if ( value === undefined ) {
30
- return this.el.value;
31
- }
32
-
33
- this.each((el) => {
34
- el.value = value;
35
- });
36
-
37
- return this;
38
- }
39
-
18
+ //
40
19
  }
41
20
 
42
21
  /**
@@ -0,0 +1,264 @@
1
+ import { Arr, Mix, Obj, Dom } from "../index.esm.js";
2
+ import { PicoDom } from "../utils/Dom.js";
3
+
4
+ /**
5
+ * @memberof PicoDom
6
+ */
7
+ export class PicoDomPopoverStatic
8
+ {
9
+ //
10
+ }
11
+
12
+ /**
13
+ * @memberof PicoDom
14
+ * @extends {PicoDom}
15
+ */
16
+ export class PicoDomPopoverInstance
17
+ {
18
+
19
+ popover(target, position = 'botttom-center', options = {})
20
+ {
21
+ if ( /^(top|bottom)-/.test(position) ) {
22
+ return this.popoverY(target, position, options);
23
+ }
24
+
25
+ if ( /^(left|right)-/.test(position) ) {
26
+ return this.popoverX(target, position, options);
27
+ }
28
+ }
29
+
30
+ popoverY(target, position = 'bottom-center', options = {})
31
+ {
32
+ target = Dom.find(target);
33
+
34
+ let [rect, self] = [
35
+ target.rect(), this.rect()
36
+ ];
37
+
38
+ if ( options.x ) {
39
+ rect = {...rect, left: options.x, width: 1 };
40
+ }
41
+
42
+ if ( options.y ) {
43
+ rect = {...rect, top: options.y, height: 1 };
44
+ }
45
+
46
+ if ( ! options.width ) {
47
+ // self.width = rect.width;
48
+ }
49
+
50
+ let [offset, recto] = [
51
+ { x: 0, y: 0 }, rect.width - self.width
52
+ ];
53
+
54
+ if ( /^top-/.test(position) ) {
55
+ offset.y = rect.top - self.height;
56
+ }
57
+
58
+ if ( /^bottom-/.test(position) ) {
59
+ offset.y = rect.top + rect.height;
60
+ }
61
+
62
+ if ( /-start$/.test(position) ) {
63
+ offset.x = rect.left;
64
+ }
65
+
66
+ if ( /-center$/.test(position) ) {
67
+ offset.x = rect.left + (recto * 0.5);
68
+ }
69
+
70
+ if ( /-end$/.test(position) ) {
71
+ offset.x = rect.left + recto;
72
+ }
73
+
74
+ let inverse = position;
75
+
76
+ if ( /^top-/.test(position) ) {
77
+ inverse = inverse.replace(/^top-/, 'bottom-');
78
+ }
79
+
80
+ if ( /^bottom-/.test(position) ) {
81
+ inverse = inverse.replace(/^bottom-/, 'top-');
82
+ }
83
+
84
+ let win = {
85
+ x: window.innerWidth, y: window.innerHeight
86
+ };
87
+
88
+ let broken = offset.y + self.height > win.y || offset.y < 0;
89
+
90
+ if ( offset.y < 0 ) {
91
+ broken = true;
92
+ }
93
+
94
+ const rebound = {
95
+ ...options, offset
96
+ };
97
+
98
+ if ( broken && ! options.offset ) {
99
+ return this.popoverY(target, inverse, rebound);
100
+ }
101
+
102
+ if ( broken && options.offset ) {
103
+ offset = options.offset;
104
+ }
105
+
106
+ if ( offset.y < 0 ) {
107
+ offset.y = 0;
108
+ }
109
+
110
+ if ( offset.y + self.height > win.height ) {
111
+ offset.y = win.height - self.height;
112
+ }
113
+
114
+ let result = this.popoverNormalize(...[
115
+ offset, self, rect, win
116
+ ]);
117
+
118
+ if ( broken ) {
119
+ position = 'auto';
120
+ }
121
+
122
+ return { ...result, position };
123
+ }
124
+
125
+ popoverX(target, position = 'left-center', options = {})
126
+ {
127
+ target = Dom.find(target);
128
+
129
+ let [rect, self] = [
130
+ target.rect(), this.rect()
131
+ ];
132
+
133
+ if ( ! options.height ) {
134
+ // self.height = rect.height;
135
+ }
136
+
137
+ let [offset, recto] = [
138
+ { x: 0, y: 0 }, rect.height - self.height
139
+ ];
140
+
141
+ if ( /^left-/.test(position) ) {
142
+ offset.x = rect.left - self.width;
143
+ }
144
+
145
+ if ( /^right-/.test(position) ) {
146
+ offset.x = rect.left + rect.width;
147
+ }
148
+
149
+ if ( /-start$/.test(position) ) {
150
+ offset.y = rect.top;
151
+ }
152
+
153
+ if ( /-center/.test(position) ) {
154
+ offset.y = rect.top + (recto * 0.5);
155
+ }
156
+
157
+ if ( /-end/.test(position) ) {
158
+ offset.y = rect.top + recto;
159
+ }
160
+
161
+ let inverse = position;
162
+
163
+ if ( /^left-/.test(position) ) {
164
+ inverse = inverse.replace(/^left-/, 'right-');
165
+ }
166
+
167
+ if ( /^right-/.test(position) ) {
168
+ inverse = inverse.replace(/^right-/, 'left-');
169
+ }
170
+
171
+ let win = {
172
+ x: window.innerWidth, y: window.innerHeight
173
+ };
174
+
175
+ let broken = offset.y + self.height > win.y;
176
+
177
+ if ( offset.y < 0 ) {
178
+ broken = true;
179
+ }
180
+
181
+ const rebound = {
182
+ ...options, offset
183
+ };
184
+
185
+ if ( broken && ! options.offset ) {
186
+ return this.popoverX(target, inverse, rebound);
187
+ }
188
+
189
+ if ( broken && options.offset ) {
190
+ offset = options.offset;
191
+ }
192
+
193
+ let result = this.popoverNormalize(...[
194
+ offset, self, rect, win
195
+ ]);
196
+
197
+ if ( broken ) {
198
+ position = 'auto';
199
+ }
200
+
201
+ return { ...result, position };
202
+ }
203
+
204
+ popoverNormalize(offset, self, rect, win)
205
+ {
206
+ const scroll = Dom.find(document.body).scroll();
207
+
208
+ if ( offset.y < 0 ) {
209
+ offset.y = 0;
210
+ }
211
+
212
+ if ( offset.y + self.height > win.y ) {
213
+ offset.y = win.y - self.height;
214
+ }
215
+
216
+ if ( offset.x < 0 ) {
217
+ offset.x = 0;
218
+ }
219
+
220
+ const dw = document.body.clientWidth;
221
+
222
+ if ( offset.x + self.width > win.x ) {
223
+ offset.x = win.x - self.width - (win.x - dw);
224
+ }
225
+
226
+ offset = {
227
+ x: offset.x + scroll.left, y: offset.y + scroll.top
228
+ }
229
+
230
+ Obj.each(offset, (v, k) => {
231
+ offset[k] = Math.round(v);
232
+ });
233
+
234
+ offset.self = {
235
+ width: self.width, height: self.height
236
+ };
237
+
238
+ offset.rect = {
239
+ width: rect.width, height: rect.height
240
+ };
241
+
242
+ return offset;
243
+ }
244
+
245
+ }
246
+
247
+ /**
248
+ * @param {typeof PicoDom} self
249
+ * @returns {typeof PicoDom}
250
+ */
251
+ export const PicoDomPopoverPlugin = function (self) {
252
+
253
+ Obj.each(Mix.class(PicoDomPopoverStatic), (fn, id) => {
254
+ self[id] = fn;
255
+ });
256
+
257
+ Obj.each(Mix.proto(PicoDomPopoverInstance), (fn, id) => {
258
+ self.prototype[id] = fn;
259
+ });
260
+
261
+ // self.init.push(PicoDomPopoverInstance.constructor);
262
+
263
+ return self;
264
+ }
@@ -30,6 +30,17 @@ export class PicoDomRectangleStatic
30
30
  */
31
31
  export class PicoDomRectangleInstance
32
32
  {
33
+ rect(fallback = {})
34
+ {
35
+ const rect = this.el.getBoundingClientRect();
36
+
37
+ if ( rect == null ) {
38
+ return fallback;
39
+ }
40
+
41
+ return rect.toJSON();
42
+ }
43
+
33
44
  /**
34
45
  * Get margin values
35
46
  *
@@ -30,7 +30,7 @@ export class PicoFormatOptionStatic
30
30
  return this.castOption(key, val, null, space);
31
31
  });
32
32
 
33
- return result.join(';') + ';';
33
+ return result.join(space ? '; ' : ';') + ';';
34
34
  }
35
35
 
36
36
  /**
@@ -57,10 +57,10 @@ export class PicoFormatOptionStatic
57
57
  }
58
58
 
59
59
  let result = Arr.map(value, (v, k) => {
60
- return this.castOption(k, v, key);
60
+ return this.castOption(k, v, key, space);
61
61
  });
62
62
 
63
- return result.join(';');
63
+ return result.join(space ? '; ' : ';');
64
64
  }
65
65
 
66
66
  /**
@@ -27,4 +27,10 @@ import * as pi from "./index.esm.js";
27
27
  /**
28
28
  * @type {PicoLibrary}
29
29
  */
30
- globalThis.pi = pi;
30
+ globalThis.pi = pi;
31
+
32
+ globalThis.addEventListener && globalThis.addEventListener('beforeunload', (e) => {
33
+ pi.Arr.map(pi.Dom.$events, ({ el, cb, event }) => {
34
+ return (el.removeEventListener(event, cb), null);
35
+ });
36
+ });
package/src/index.esm.js CHANGED
@@ -5,7 +5,7 @@ import { PicoArray, default as Arr } from "./utils/Array.js";
5
5
  import { PicoObject, default as Obj } from "./utils/Object.js";
6
6
  import { PicoMixed, default as Mix } from "./utils/Mixed.js";
7
7
  import { PicoHash, default as Hash } from "./utils/Hash.js";
8
- import { PicoEvent, default as Event } from "./utils/Event.js";
8
+ import { PicoSignal, default as Signal } from "./utils/Signal.js";
9
9
  import { PicoLocale, default as Locale } from "./utils/Locale.js";
10
10
  import { PicoCookie, default as Cookie } from "./utils/Cookie.js";
11
11
 
@@ -25,7 +25,7 @@ const Now = NowBuilder();
25
25
  const For = ForBuilder();
26
26
 
27
27
  export {
28
- Dom, Now, For, Run, Str, Num, Arr, Obj, Mix, Hash, Event, Locale, Cookie
28
+ Dom, Now, For, Run, Str, Num, Arr, Obj, Mix, Hash, Signal, Locale, Cookie
29
29
  }
30
30
 
31
31
  import { PicoDom, default as DomBuilder } from "./utils/Dom.js";
@@ -61,6 +61,16 @@ export const Any = new Proxy({}, {
61
61
  }
62
62
  });
63
63
 
64
+ /**
65
+ * @type {typeof PicoMixed}
66
+ */
67
+ export const Event = new Proxy({}, {
68
+ get: function (target, prop) {
69
+ console.warn(`Event.${prop} is deprecated, use Signal.${prop}() instead.`);
70
+ return (...args) => Signal[prop](...args);
71
+ }
72
+ });
73
+
64
74
  /**
65
75
  * @returns {string}
66
76
  */
@@ -5,7 +5,10 @@ export const NOW_FORMAT = {
5
5
  'L': 'DD/MM/YYYY',
6
6
  'LL': 'MMMM DD, YYYY',
7
7
  'LLL': 'MMMM DD, YYYY HH:mm',
8
- 'LLLL': 'dddd, MMMM DD, YYYY HH:mm'
8
+ 'LLLL': 'dddd, MMMM DD, YYYY HH:mm',
9
+ 'LT': 'HH:mm',
10
+ 'LTS': 'HH:mm:ss',
11
+ 'LTSD': 'DD/MM/YYYY HH:mm:ss',
9
12
  }
10
13
 
11
14
  export const NOW_PARSE = {
@@ -107,7 +110,7 @@ export class PicoNowFormatInstance
107
110
  format(format = 'YYYY-MM-DD HH:mm:ss')
108
111
  {
109
112
  if ( Obj.has(NOW_FORMAT, format) ) {
110
- format = Locale.trans(NOW_FORMAT[format]);
113
+ format = Locale.$text[format] ?? NOW_FORMAT[format];
111
114
  }
112
115
 
113
116
  format = format.replace('dddd', () => {
@@ -111,10 +111,14 @@ export class PicoArray
111
111
  return this.findIndex(value, search) !== - 1;
112
112
  }
113
113
 
114
- if ( !Mix.isArr(value) ) {
114
+ if ( !Mix.isRef(value) ) {
115
115
  value = [value];
116
116
  }
117
117
 
118
+ if ( !Mix.isArr(value) ) {
119
+ value = Mix.vals(value);
120
+ }
121
+
118
122
  let index = value.findIndex((val) => {
119
123
  return val == search;
120
124
  });
@@ -331,6 +335,10 @@ export class PicoArray
331
335
  ...this.clone(cascade), value
332
336
  ];
333
337
 
338
+ if ( value == null ) {
339
+ return value;
340
+ }
341
+
334
342
  if ( Mix.isObj(value[key]) ) {
335
343
  value[key] = fn(cascade)(value[key]);
336
344
  }
package/src/utils/Dom.js CHANGED
@@ -9,6 +9,7 @@ import { PicoDomAttributePlugin } from "../dom/DomAttribute.js";
9
9
  import { PicoDomInviewPlugin } from "../dom/DomInview.js";
10
10
  import { PicoDomMetaPlugin } from "../dom/DomMeta.js";
11
11
  import { PicoDomObserverPlugin } from "../dom/DomObserver.js";
12
+ import { PicoDomPopoverPlugin } from "../dom/DomPopover.js";
12
13
 
13
14
  export const PicoDomPlugins = [
14
15
  PicoDomFinderPlugin,
@@ -21,6 +22,7 @@ export const PicoDomPlugins = [
21
22
  PicoDomInviewPlugin,
22
23
  PicoDomMetaPlugin,
23
24
  PicoDomObserverPlugin,
25
+ PicoDomPopoverPlugin,
24
26
  ];
25
27
 
26
28
  /**
@@ -43,6 +45,8 @@ export const PicoDomPlugins = [
43
45
  * @typedef {import('../dom/DomMeta.js').PicoDomMetaInstance} PicoDomMetaInstance
44
46
  * @typedef {import('../dom/DomObserver.js').PicoDomObserverStatic} PicoDomObserverStatic
45
47
  * @typedef {import('../dom/DomObserver.js').PicoDomObserverInstance} PicoDomObserverInstance
48
+ * @typedef {import('../dom/DomPopover.js').PicoDomPopoverStatic} PicoDomPopoverStatic
49
+ * @typedef {import('../dom/DomPopover.js').PicoDomPopoverInstance} PicoDomPopoverInstance
46
50
  *
47
51
  * @mixes PicoDomFinderStatic
48
52
  * @mixes PicoDomGlobalStatic
@@ -54,6 +58,7 @@ export const PicoDomPlugins = [
54
58
  * @mixes PicoDomInviewStatic
55
59
  * @mixes PicoDomMetaStatic
56
60
  * @mixes PicoDomObserverStatic
61
+ * @mixes PicoDomPopoverStatic
57
62
  *
58
63
  * @extends PicoDomFinderInstance
59
64
  * @extends PicoDomGlobalInstance
@@ -65,6 +70,7 @@ export const PicoDomPlugins = [
65
70
  * @extends PicoDomInviewInstance
66
71
  * @extends PicoDomMetaInstance
67
72
  * @extends PicoDomObserverInstance
73
+ * @extends PicoDomPopoverInstance
68
74
  */
69
75
  export class PicoDom {
70
76
 
@@ -454,6 +454,10 @@ export class PicoMixed
454
454
  return [];
455
455
  }
456
456
 
457
+ if ( this.isFunc(value.toJSON) ) {
458
+ value = value.toJSON();
459
+ }
460
+
457
461
  if ( this.isIter(value) ) {
458
462
  value = this.iter(value);
459
463
  }
@@ -476,6 +480,10 @@ export class PicoMixed
476
480
  return [];
477
481
  }
478
482
 
483
+ if ( this.isFunc(value.toJSON) ) {
484
+ value = value.toJSON();
485
+ }
486
+
479
487
  if ( this.isIter(value) ) {
480
488
  value = this.iter(value);
481
489
  }
@@ -244,17 +244,17 @@ export class PicoRunner
244
244
 
245
245
  Arr.add(this.$buffer, item);
246
246
 
247
- let fn = () => {
248
- this.runFramebuffer();
249
- };
250
-
251
247
  return (e, ...args) => {
252
248
 
249
+ if ( /^drag/.test(e.type) ) {
250
+ e.preventDefault();
251
+ }
252
+
253
253
  Obj.assign(item, {
254
254
  args: [e, ...args], active: true
255
255
  });
256
256
 
257
- (e.preventDefault(), fn());
257
+ this.runFramebuffer();
258
258
  };
259
259
  }
260
260
 
@@ -272,7 +272,6 @@ export class PicoRunner
272
272
  }
273
273
 
274
274
  this.$idler = setTimeout(() => {
275
- console.log('delayed runFramebuffer')
276
275
  this.runFramebuffer();
277
276
  }, 50);
278
277
 
@@ -1,5 +1,6 @@
1
1
  import { Str, Event, For, Mix, Arr } from "../index.esm.js";
2
- export class PicoEvent
2
+
3
+ export class PicoSignal
3
4
  {
4
5
  static $events = [];
5
6
 
@@ -13,7 +14,7 @@ export class PicoEvent
13
14
  * @param {function} cb Event callback
14
15
  * @param {any} [options] Listener options
15
16
  * @param {boolean} [paused] Start paused
16
- * @returns {typeof PicoEvent} Event class
17
+ * @returns {typeof PicoSignal} Event class
17
18
  */
18
19
  static bind(event, cb, options = {}, paused = false)
19
20
  {
@@ -40,7 +41,7 @@ export class PicoEvent
40
41
  *
41
42
  * @param {any} event Event name(s)
42
43
  * @param {any} [options] Listener options
43
- * @returns {typeof PicoEvent} Event class
44
+ * @returns {typeof PicoSignal} Event class
44
45
  */
45
46
  static unbind(event, options = {})
46
47
  {
@@ -66,7 +67,7 @@ export class PicoEvent
66
67
  *
67
68
  * @param {string} event Event name
68
69
  * @param {...any} args Event args
69
- * @returns {typeof PicoEvent} Event class
70
+ * @returns {typeof PicoSignal} Event class
70
71
  */
71
72
  static fire(event, ...args)
72
73
  {
@@ -90,7 +91,7 @@ export class PicoEvent
90
91
  *
91
92
  * @param {any} event Event name(s)
92
93
  * @param {any} [options] Listener options
93
- * @returns {typeof PicoEvent} Event class
94
+ * @returns {typeof PicoSignal} Event class
94
95
  */
95
96
  static pause(event, options = {})
96
97
  {
@@ -116,7 +117,7 @@ export class PicoEvent
116
117
  *
117
118
  * @param {any} event Event name(s)
118
119
  * @param {any} [options] Listener options
119
- * @returns {typeof PicoEvent} Event class
120
+ * @returns {typeof PicoSignal} Event class
120
121
  */
121
122
  static unpause(event, options = {})
122
123
  {
@@ -137,4 +138,4 @@ export class PicoEvent
137
138
 
138
139
  }
139
140
 
140
- export default PicoEvent;
141
+ export default PicoSignal;