@kizmann/pico-js 1.0.10 → 1.0.11

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.11",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "private": false,
@@ -202,33 +202,58 @@ export class Dom
202
202
  return top <= scroll.top && scroll.top <= bottom;
203
203
  }
204
204
 
205
- static inviewMaxY(selector, callback = null, context = null)
205
+ static inviewMaxY(options, callback = null)
206
206
  {
207
- let [items, attr] = [
208
- [], selector.replace(/^\[([^="]+)]$/, '$1')
209
- ];
207
+ let defaults = {
208
+ el: '[data-inview]', parent: document.body, multiple: false, safezone: null,
209
+ };
210
+
211
+ if ( Any.isString(options) ) {
212
+ options = { el: options };
213
+ }
210
214
 
211
- let parent = Dom.find(selector);
215
+ options = Obj.assign({}, defaults, options);
216
+
217
+ let safeback = (item) => {
218
+ return Math.min(Dom.find(item.el).height() * 0.5, Dom.find(window).height() * 0.2);
219
+ };
220
+
221
+ let safezone = options.safezone;
222
+
223
+ if ( Any.isNull(safezone) ) {
224
+ safezone = safeback;
225
+ }
212
226
 
213
- if ( ! Any.isNull(context) ) {
214
- parent = Dom.find(context).find(selector);
227
+ if ( ! Any.isFunction(safezone) ) {
228
+ safezone = () => options.safezone;
215
229
  }
216
230
 
231
+ let [items, attr] = [
232
+ [], options.el.replace(/^\[([^="]+)]$/, '$1')
233
+ ];
234
+
235
+ let parent = Dom.find(options.parent).find(options.el);
236
+
217
237
  parent.each((el) => {
218
- items.push({ el, height: Dom.find(el).inviewHeight() });
238
+ items.push({
239
+ el, attr: Dom.find(el).attr(attr), height: Dom.find(el).inviewHeight()
240
+ });
219
241
  });
220
242
 
221
- let heights = Arr.extract(items, 'height');
243
+ let results = Arr.filter(items, (item) => {
244
+ return Math.max(0, item.height - safezone(item)) !== 0;
245
+ });
222
246
 
223
- let el = Arr.find(items, (item) => {
224
- return item.height === Math.max(...heights);
247
+ results = Arr.sort(results, (a, b) => {
248
+ return a.height > b.height ? -1 : 1;
225
249
  });
226
250
 
227
- if ( ! Any.isEmpty(el) && Any.isFunction(callback) ) {
228
- callback.call({}, el.el, el.el.getAttribute(attr));
229
- }
251
+ Arr.each(results, (item, index) => {
252
+ Any.isFunction(callback) && callback.call({}, item, index);
253
+ });
230
254
 
231
- return el.el;
255
+ return options.multiple ? Arr.extract(results, 'el') :
256
+ Obj.get(results, '0.el');
232
257
  }
233
258
 
234
259
  is(selector)
@@ -332,7 +357,6 @@ export class Dom
332
357
  return this.find(selector).get(0) !== null;
333
358
  }
334
359
 
335
-
336
360
  if ( selector instanceof Element === false ) {
337
361
  return false;
338
362
  }