@kizmann/pico-js 1.0.11 → 1.0.13

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": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "private": false,
package/src/index.js CHANGED
@@ -69,24 +69,22 @@ export const Pico = {
69
69
  Route: Route,
70
70
  }
71
71
 
72
- /**
73
- * @const window
74
- */
72
+ let win = Any.global();
75
73
 
76
- if ( typeof window.IE === 'undefined' && window.navigator ) {
77
- window.IE = !! window.navigator.userAgent.match(/Edge\/|Trident\/|MSIE /);
74
+ if ( typeof win.IE === 'undefined' && win.navigator ) {
75
+ win.IE = !! win.navigator.userAgent.match(/Edge\/|Trident\/|MSIE /);
78
76
  }
79
77
 
80
- if ( typeof window.WIN === 'undefined' && window.navigator ) {
81
- window.WIN = !! window.navigator.userAgent.match(/Windows/);
78
+ if ( typeof win.WIN === 'undefined' && win.navigator ) {
79
+ win.WIN = !! win.navigator.userAgent.match(/Windows/);
82
80
  }
83
81
 
84
- if ( typeof window.pi === 'undefined' && window.navigator ) {
85
- window.pi = Pico;
82
+ if ( typeof win.pi === 'undefined' && win.navigator ) {
83
+ win.pi = Pico;
86
84
  }
87
85
 
88
- if ( typeof window.pi !== 'undefined' && window.document ) {
89
- window.pi.Dom.ready(window.pi.Element.listen);
86
+ if ( typeof win.pi !== 'undefined' && win.document ) {
87
+ win.pi.Dom.ready(win.pi.Element.listen);
90
88
  }
91
89
 
92
90
  export default Pico;
@@ -2,7 +2,7 @@ import { Arr, Obj, Num, Any, Event } from "../index.js";
2
2
 
3
3
  export class Data
4
4
  {
5
- static data = Obj.get(window, '_data', {});
5
+ static data = Obj.get(Any.global(), '_data', {});
6
6
 
7
7
  static has(input)
8
8
  {
@@ -1,11 +1,11 @@
1
- import { Obj } from "../index.js";
1
+ import { Obj, Any } from "../index.js";
2
2
 
3
3
  export class Locale
4
4
  {
5
5
  /**
6
6
  * Get locales from window if present.
7
7
  */
8
- static locales = Obj.get(window, '_locales', {});
8
+ static locales = Obj.get(Any.global(), '_locales', {});
9
9
 
10
10
  static pickByCount(splits, count)
11
11
  {
@@ -2,7 +2,7 @@ import { Obj, Str, Any } from "../index.js";
2
2
 
3
3
  export default class Route
4
4
  {
5
- static routes = Obj.get(window, '_routes', {});
5
+ static routes = Obj.get(Any.global(), '_routes', {});
6
6
 
7
7
  static set (key, value)
8
8
  {
@@ -2,6 +2,19 @@ import { Arr, Obj, Now } from "../index.js";
2
2
 
3
3
  export class Any
4
4
  {
5
+ static global(fallback = {})
6
+ {
7
+ if ( window !== undefined ) {
8
+ return window;
9
+ }
10
+
11
+ if ( global !== undefined ) {
12
+ return global;
13
+ }
14
+
15
+ return fallback;
16
+ }
17
+
5
18
  static isEmpty(val)
6
19
  {
7
20
  if ( this.isNumber(val) ) {
@@ -133,11 +133,13 @@ export class Dom
133
133
  return this.get(0) && this.get(0).is(':visible');
134
134
  }
135
135
 
136
- inviewHeight()
136
+ inviewHeight(boundry = window)
137
137
  {
138
+ let parent = Dom.find(boundry);
139
+
138
140
  let viewport = {
139
- width: Dom.find(window).width(),
140
- height: Dom.find(window).height(),
141
+ width: parent.width(),
142
+ height: parent.height(),
141
143
  };
142
144
 
143
145
  let element = {
@@ -148,10 +150,12 @@ export class Dom
148
150
  let scroll = this.scroll(),
149
151
  offset = this.offset();
150
152
 
151
- let bottom = offset.top + element.height;
153
+ let [top, bottom] = [
154
+ scroll.top + parent.offset('top'), offset.top + element.height
155
+ ];
152
156
 
153
- return Math.max(0, Math.min(bottom, viewport.height + scroll.top) -
154
- Math.max(offset.top, scroll.top))
157
+ return Math.max(0, Math.min(bottom, viewport.height + top) -
158
+ Math.max(offset.top, top))
155
159
  }
156
160
 
157
161
  inviewX(ratio = 0)
@@ -212,10 +216,13 @@ export class Dom
212
216
  options = { el: options };
213
217
  }
214
218
 
219
+ let boundry = Dom.find(options.parent)
220
+ .closestScrollable(window);
221
+
215
222
  options = Obj.assign({}, defaults, options);
216
223
 
217
224
  let safeback = (item) => {
218
- return Math.min(Dom.find(item.el).height() * 0.5, Dom.find(window).height() * 0.2);
225
+ return Math.min(Dom.find(item.el).height() * 0.5, Dom.find(boundry).height() * 0.2);
219
226
  };
220
227
 
221
228
  let safezone = options.safezone;
@@ -236,7 +243,7 @@ export class Dom
236
243
 
237
244
  parent.each((el) => {
238
245
  items.push({
239
- el, attr: Dom.find(el).attr(attr), height: Dom.find(el).inviewHeight()
246
+ el, attr: Dom.find(el).attr(attr), height: Dom.find(el).inviewHeight(boundry)
240
247
  });
241
248
  });
242
249
 
@@ -245,7 +252,7 @@ export class Dom
245
252
  });
246
253
 
247
254
  results = Arr.sort(results, (a, b) => {
248
- return a.height > b.height ? -1 : 1;
255
+ return a.height >= b.height ? -1 : 1;
249
256
  });
250
257
 
251
258
  Arr.each(results, (item, index) => {
@@ -438,6 +445,10 @@ export class Dom
438
445
  return Dom.find(null);
439
446
  }
440
447
 
448
+ if ( el instanceof Window ) {
449
+ el = document;
450
+ }
451
+
441
452
  let nodes = el.querySelectorAll(selector);
442
453
 
443
454
  nodes = Array.prototype.slice.call(nodes);
@@ -7,7 +7,9 @@ export class Now
7
7
 
8
8
  constructor(date = null, format = 'YYYY-MM-DD HH:mm:ss')
9
9
  {
10
- if ( ! window.moment ) {
10
+ let win = Any.global();
11
+
12
+ if ( ! win.moment ) {
11
13
  throw new Error('Moment.js is required for pi.Now');
12
14
  }
13
15
 
@@ -18,14 +20,14 @@ export class Now
18
20
  this.initialDate = date;
19
21
 
20
22
  if ( ! Any.isString(date) ) {
21
- this.moment = window.moment(date || new Date, format);
23
+ this.moment = win.moment(date || new Date, format);
22
24
  }
23
25
 
24
26
  if ( this.moment !== null ) {
25
27
  return this;
26
28
  }
27
29
 
28
- this.moment = window.moment(date.match(/^now/) ?
30
+ this.moment = win.moment(date.match(/^now/) ?
29
31
  new Date : date, format);
30
32
 
31
33
  let second = this.initialDate.match(/(\+|-)([0-9]+)seconds?/);
@@ -92,7 +94,7 @@ export class Now
92
94
  valid()
93
95
  {
94
96
  return ! Any.isEmpty(this.initialDate) &&
95
- window.moment(this.initialDate).isValid();
97
+ Any.global().moment(this.initialDate).isValid();
96
98
  }
97
99
 
98
100
  clone()
@@ -362,7 +362,7 @@ export class Obj
362
362
  {
363
363
  let result = {};
364
364
 
365
- if ( window.FormData && obj instanceof FormData ) {
365
+ if ( FormData && obj instanceof FormData ) {
366
366
 
367
367
  for ( let [key, value] of obj.entries() ) {
368
368
  result[key] = callback(value, key);