@kizmann/pico-js 1.0.10 → 1.0.12

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.10",
3
+ "version": "1.0.12",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "private": false,
@@ -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)
@@ -202,33 +206,61 @@ export class Dom
202
206
  return top <= scroll.top && scroll.top <= bottom;
203
207
  }
204
208
 
205
- static inviewMaxY(selector, callback = null, context = null)
209
+ static inviewMaxY(options, callback = null)
206
210
  {
207
- let [items, attr] = [
208
- [], selector.replace(/^\[([^="]+)]$/, '$1')
209
- ];
211
+ let defaults = {
212
+ el: '[data-inview]', parent: document.body, multiple: false, safezone: null,
213
+ };
214
+
215
+ if ( Any.isString(options) ) {
216
+ options = { el: options };
217
+ }
218
+
219
+ let boundry = Dom.find(options.parent)
220
+ .closestScrollable(window);
221
+
222
+ options = Obj.assign({}, defaults, options);
223
+
224
+ let safeback = (item) => {
225
+ return Math.min(Dom.find(item.el).height() * 0.5, Dom.find(boundry).height() * 0.2);
226
+ };
210
227
 
211
- let parent = Dom.find(selector);
228
+ let safezone = options.safezone;
212
229
 
213
- if ( ! Any.isNull(context) ) {
214
- parent = Dom.find(context).find(selector);
230
+ if ( Any.isNull(safezone) ) {
231
+ safezone = safeback;
215
232
  }
216
233
 
234
+ if ( ! Any.isFunction(safezone) ) {
235
+ safezone = () => options.safezone;
236
+ }
237
+
238
+ let [items, attr] = [
239
+ [], options.el.replace(/^\[([^="]+)]$/, '$1')
240
+ ];
241
+
242
+ let parent = Dom.find(options.parent).find(options.el);
243
+
217
244
  parent.each((el) => {
218
- items.push({ el, height: Dom.find(el).inviewHeight() });
245
+ items.push({
246
+ el, attr: Dom.find(el).attr(attr), height: Dom.find(el).inviewHeight(boundry)
247
+ });
219
248
  });
220
249
 
221
- let heights = Arr.extract(items, 'height');
250
+ let results = Arr.filter(items, (item) => {
251
+ return Math.max(0, item.height - safezone(item)) !== 0;
252
+ });
222
253
 
223
- let el = Arr.find(items, (item) => {
224
- return item.height === Math.max(...heights);
254
+ results = Arr.sort(results, (a, b) => {
255
+ return a.height >= b.height ? -1 : 1;
225
256
  });
226
257
 
227
- if ( ! Any.isEmpty(el) && Any.isFunction(callback) ) {
228
- callback.call({}, el.el, el.el.getAttribute(attr));
229
- }
258
+ Arr.each(results, (item, index) => {
259
+ Any.isFunction(callback) && callback.call({}, item, index);
260
+ });
230
261
 
231
- return el.el;
262
+ return options.multiple ? Arr.extract(results, 'el') :
263
+ Obj.get(results, '0.el');
232
264
  }
233
265
 
234
266
  is(selector)
@@ -332,7 +364,6 @@ export class Dom
332
364
  return this.find(selector).get(0) !== null;
333
365
  }
334
366
 
335
-
336
367
  if ( selector instanceof Element === false ) {
337
368
  return false;
338
369
  }
@@ -414,6 +445,10 @@ export class Dom
414
445
  return Dom.find(null);
415
446
  }
416
447
 
448
+ if ( el instanceof Window ) {
449
+ el = document;
450
+ }
451
+
417
452
  let nodes = el.querySelectorAll(selector);
418
453
 
419
454
  nodes = Array.prototype.slice.call(nodes);