@spscommerce/positioning 5.0.0-rc1 → 5.0.0

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/lib/index.cjs.js CHANGED
@@ -1,516 +1 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- require('intersection-observer');
6
- var utils = require('@spscommerce/utils');
7
- var eventemitter3 = require('eventemitter3');
8
-
9
- /** These are the possibilities for the `position` option of
10
- * `PositioningOptions` when using `relativeTo`. */
11
- exports.Position = void 0;
12
- (function (Position) {
13
- Position["TOP_LEFT"] = "top left";
14
- Position["TOP_MIDDLE"] = "top middle";
15
- Position["TOP_RIGHT"] = "top right";
16
- Position["RIGHT_TOP"] = "right top";
17
- Position["RIGHT_MIDDLE"] = "right middle";
18
- Position["RIGHT_BOTTOM"] = "right bottom";
19
- Position["BOTTOM_RIGHT"] = "bottom right";
20
- Position["BOTTOM_MIDDLE"] = "bottom middle";
21
- Position["BOTTOM_LEFT"] = "bottom left";
22
- Position["LEFT_BOTTOM"] = "left bottom";
23
- Position["LEFT_MIDDLE"] = "left middle";
24
- Position["LEFT_TOP"] = "left top";
25
- })(exports.Position || (exports.Position = {}));
26
-
27
- /** Indicates which point on a positioned element it should be positioned by. */
28
- exports.PositionAnchor = void 0;
29
- (function (PositionAnchor) {
30
- PositionAnchor["TOP_LEFT"] = "top left";
31
- PositionAnchor["TOP_RIGHT"] = "top right";
32
- PositionAnchor["BOTTOM_LEFT"] = "bottom left";
33
- PositionAnchor["BOTTOM_RIGHT"] = "bottom right";
34
- })(exports.PositionAnchor || (exports.PositionAnchor = {}));
35
-
36
- var DEFAULT_POSITIONING_OPTIONS = {
37
- anchor: exports.PositionAnchor.TOP_LEFT,
38
- offsets: [],
39
- position: exports.Position.TOP_LEFT,
40
- };
41
-
42
- /*! *****************************************************************************
43
- Copyright (c) Microsoft Corporation.
44
-
45
- Permission to use, copy, modify, and/or distribute this software for any
46
- purpose with or without fee is hereby granted.
47
-
48
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
49
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
50
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
51
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
52
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
53
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
54
- PERFORMANCE OF THIS SOFTWARE.
55
- ***************************************************************************** */
56
-
57
- var __assign = function() {
58
- __assign = Object.assign || function __assign(t) {
59
- for (var s, i = 1, n = arguments.length; i < n; i++) {
60
- s = arguments[i];
61
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
62
- }
63
- return t;
64
- };
65
- return __assign.apply(this, arguments);
66
- };
67
-
68
- function __decorate(decorators, target, key, desc) {
69
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
70
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
71
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
72
- return c > 3 && r && Object.defineProperty(target, key, r), r;
73
- }
74
-
75
- function __metadata(metadataKey, metadataValue) {
76
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
77
- }
78
-
79
- function __values(o) {
80
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
81
- if (m) return m.call(o);
82
- if (o && typeof o.length === "number") return {
83
- next: function () {
84
- if (o && i >= o.length) o = void 0;
85
- return { value: o && o[i++], done: !o };
86
- }
87
- };
88
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
89
- }
90
-
91
- function __read(o, n) {
92
- var m = typeof Symbol === "function" && o[Symbol.iterator];
93
- if (!m) return o;
94
- var i = m.call(o), r, ar = [], e;
95
- try {
96
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
97
- }
98
- catch (error) { e = { error: error }; }
99
- finally {
100
- try {
101
- if (r && !r.done && (m = i["return"])) m.call(i);
102
- }
103
- finally { if (e) throw e.error; }
104
- }
105
- return ar;
106
- }
107
-
108
- var NAVBAR_HEIGHT = 60;
109
- /** This class follows a singleton pattern where you can create an instance by calling
110
- * `new PositioningService()` if you wish, but all instance methods pass through to
111
- * static methods, so there's really only one set of elements being positioned that's
112
- * shared between all instances and static usages of PositioningService.
113
- */
114
- var PositioningService = /** @class */ (function () {
115
- function PositioningService() {
116
- }
117
- PositioningService.on = function (eventName, handler) {
118
- PositioningService.events.on(eventName, handler);
119
- };
120
- PositioningService.off = function (eventName, handler) {
121
- PositioningService.events.off(eventName, handler);
122
- };
123
- PositioningService.once = function (eventName, handler) {
124
- PositioningService.events.once(eventName, handler);
125
- };
126
- PositioningService.onViewportIntersection = function (entries) {
127
- var e_1, _a;
128
- var _loop_1 = function (entry) {
129
- // This is how you make a copy of an IntersectionObserverEntry that
130
- // you can modify; it's read-only and incompatible with simply using
131
- // destructuring or Object.assign. Before sending it out we make the
132
- // copy read-only by passing it to Object.freeze
133
- var entryCopy = Object.keys(IntersectionObserverEntry.prototype).reduce(function (copy, key) {
134
- var _a;
135
- return (__assign(__assign({}, copy), (_a = {}, _a[key] = entry[key], _a)));
136
- }, {});
137
- entryCopy.rootBounds = entryCopy.rootBounds || PositioningService.getRootBounds();
138
- PositioningService.events.emit('viewportIntersection', Object.freeze(entryCopy));
139
- };
140
- try {
141
- for (var entries_1 = __values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) {
142
- var entry = entries_1_1.value;
143
- _loop_1(entry);
144
- }
145
- }
146
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
147
- finally {
148
- try {
149
- if (entries_1_1 && !entries_1_1.done && (_a = entries_1.return)) _a.call(entries_1);
150
- }
151
- finally { if (e_1) throw e_1.error; }
152
- }
153
- };
154
- PositioningService.onElementIntersection = function (target, intersectingWith) {
155
- PositioningService.events.emit('elementIntersection', { target: target, intersectingWith: intersectingWith });
156
- };
157
- PositioningService.getRootBounds = function () {
158
- var rootWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
159
- var rootHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
160
- return {
161
- x: 0,
162
- y: NAVBAR_HEIGHT,
163
- left: 0,
164
- top: NAVBAR_HEIGHT,
165
- right: rootWidth,
166
- bottom: rootHeight,
167
- height: rootHeight - NAVBAR_HEIGHT,
168
- width: rootWidth,
169
- };
170
- };
171
- PositioningService.clearStyles = function (element) {
172
- Object.assign(element.style, {
173
- position: '',
174
- width: '',
175
- top: '',
176
- left: '',
177
- right: '',
178
- bottom: '',
179
- visibility: '',
180
- zIndex: '',
181
- });
182
- };
183
- PositioningService.fixElementPosition = function (element, options) {
184
- if (!this.elements.has(element)) {
185
- return;
186
- }
187
- var opts = options || this.elements.get(element);
188
- var rootBounds = this.getRootBounds();
189
- var positionedElementBounds = element.getBoundingClientRect();
190
- var relativeTargetBounds = opts.relativeTo.getBoundingClientRect();
191
- var width = (opts.useRelativeTargetWidth
192
- ? relativeTargetBounds
193
- : positionedElementBounds).width;
194
- Object.assign(element.style, {
195
- minWidth: width + "px",
196
- position: 'fixed',
197
- zIndex: opts.zIndex || '',
198
- });
199
- var top;
200
- var left;
201
- var positionRelativeToTarget = opts.position.split(' ');
202
- var offsetA = opts.offsets[0] || 0;
203
- var offsetB = opts.offsets[1] || 0;
204
- top = 0;
205
- left = 0;
206
- switch (positionRelativeToTarget[0]) {
207
- case 'top':
208
- top = relativeTargetBounds.top - positionedElementBounds.height - offsetA;
209
- break;
210
- case 'left':
211
- left = relativeTargetBounds.left - width - offsetA;
212
- break;
213
- case 'right':
214
- left = relativeTargetBounds.right + offsetA;
215
- break;
216
- case 'bottom':
217
- top = relativeTargetBounds.bottom + offsetA;
218
- break;
219
- default:
220
- throw new Error(opts.position + " is not a valid position");
221
- }
222
- switch (positionRelativeToTarget[1]) {
223
- case 'left':
224
- left = relativeTargetBounds.left - offsetB;
225
- break;
226
- case 'top':
227
- top = relativeTargetBounds.top - offsetB;
228
- break;
229
- case 'middle':
230
- if (positionRelativeToTarget[0] === 'top'
231
- || positionRelativeToTarget[0] === 'bottom') {
232
- left = relativeTargetBounds.left
233
- + relativeTargetBounds.width / 2
234
- - width / 2
235
- + offsetB;
236
- }
237
- else if (positionRelativeToTarget[0] === 'left'
238
- || positionRelativeToTarget[0] === 'right') {
239
- top = relativeTargetBounds.top
240
- + relativeTargetBounds.height / 2
241
- - positionedElementBounds.height / 2
242
- + offsetB;
243
- }
244
- break;
245
- case 'bottom':
246
- top = relativeTargetBounds.bottom - positionedElementBounds.height + offsetB;
247
- break;
248
- case 'right':
249
- left = relativeTargetBounds.right - width + offsetB;
250
- break;
251
- default:
252
- throw new Error(opts.position + " is not a valid position");
253
- }
254
- var bottom = top + positionedElementBounds.height;
255
- var right = left + positionedElementBounds.width;
256
- var topPx = Math.round(top) + "px";
257
- var bottomPx = Math.round(rootBounds.bottom - bottom) + "px";
258
- var leftPx = Math.round(left) + "px";
259
- var rightPx = Math.round(rootBounds.right - right) + "px";
260
- switch (opts.anchor) {
261
- case exports.PositionAnchor.TOP_LEFT:
262
- Object.assign(element.style, {
263
- top: topPx,
264
- bottom: 'auto',
265
- left: leftPx,
266
- right: 'auto',
267
- });
268
- break;
269
- case exports.PositionAnchor.TOP_RIGHT:
270
- Object.assign(element.style, {
271
- top: topPx,
272
- bottom: 'auto',
273
- left: 'auto',
274
- right: rightPx,
275
- });
276
- break;
277
- case exports.PositionAnchor.BOTTOM_LEFT:
278
- Object.assign(element.style, {
279
- top: 'auto',
280
- bottom: bottomPx,
281
- left: leftPx,
282
- right: 'auto',
283
- });
284
- break;
285
- case exports.PositionAnchor.BOTTOM_RIGHT:
286
- Object.assign(element.style, {
287
- top: 'auto',
288
- bottom: bottomPx,
289
- left: 'auto',
290
- right: rightPx,
291
- });
292
- break;
293
- default:
294
- throw new Error(opts.anchor + " is not a valid positioning anchor");
295
- }
296
- };
297
- PositioningService.update = function (event) {
298
- var e_2, _a;
299
- var elements = PositioningService.elements.entries();
300
- var updatedElements = [];
301
- try {
302
- for (var elements_1 = __values(elements), elements_1_1 = elements_1.next(); !elements_1_1.done; elements_1_1 = elements_1.next()) {
303
- var _b = __read(elements_1_1.value, 2), element = _b[0], options = _b[1];
304
- if (!event
305
- || event.target === document
306
- || event.target === options.scrollParent
307
- || event.target === window) {
308
- PositioningService.fixElementPosition(element, options);
309
- PositioningService.checkCollisions(element, updatedElements);
310
- updatedElements.push(element);
311
- }
312
- }
313
- }
314
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
315
- finally {
316
- try {
317
- if (elements_1_1 && !elements_1_1.done && (_a = elements_1.return)) _a.call(elements_1);
318
- }
319
- finally { if (e_2) throw e_2.error; }
320
- }
321
- };
322
- PositioningService.checkCollisions = function (element, otherElements) {
323
- var e_3, _a;
324
- var otherElementsList = otherElements || Array.from(PositioningService.elements.keys());
325
- var bounds = element.getBoundingClientRect();
326
- var collisions = [];
327
- try {
328
- for (var otherElementsList_1 = __values(otherElementsList), otherElementsList_1_1 = otherElementsList_1.next(); !otherElementsList_1_1.done; otherElementsList_1_1 = otherElementsList_1.next()) {
329
- var otherElement = otherElementsList_1_1.value;
330
- if (otherElement !== element) {
331
- var otherBounds = otherElement.getBoundingClientRect();
332
- if (otherBounds.left <= bounds.right
333
- && otherBounds.right >= bounds.left
334
- && otherBounds.top <= bounds.bottom
335
- && otherBounds.bottom >= bounds.top) {
336
- collisions.push(otherElement);
337
- }
338
- }
339
- }
340
- }
341
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
342
- finally {
343
- try {
344
- if (otherElementsList_1_1 && !otherElementsList_1_1.done && (_a = otherElementsList_1.return)) _a.call(otherElementsList_1);
345
- }
346
- finally { if (e_3) throw e_3.error; }
347
- }
348
- if (collisions.length) {
349
- PositioningService.onElementIntersection(element, collisions);
350
- }
351
- };
352
- /** Returns `true` if the element is currently being positioned by the service
353
- * and `false` otherwise. */
354
- PositioningService.isPositioned = function (element) {
355
- return PositioningService.elements.has(element);
356
- };
357
- /** Returns the options currently beinng used for positioning the element,
358
- * if it is indeed being positioned by the service. */
359
- PositioningService.getPositioningOptions = function (element) {
360
- if (PositioningService.elements.has(element)) {
361
- return PositioningService.elements.get(element);
362
- }
363
- return null;
364
- };
365
- /** PositioningService will start controlling the position of the given element. */
366
- PositioningService.position = function (element, options) {
367
- if (options === void 0) { options = {}; }
368
- var opts = __assign(__assign({}, DEFAULT_POSITIONING_OPTIONS), options);
369
- var e = element;
370
- while (e !== document.body) {
371
- e = e.parentElement;
372
- if (e.classList.contains('sps-focused-task')) {
373
- opts.scrollParent = e;
374
- if (!PositioningService.registeredScrollParents.has(e)) {
375
- PositioningService.registeredScrollParents.add(e);
376
- e.addEventListener('scroll', PositioningService.update);
377
- }
378
- break;
379
- }
380
- }
381
- // eslint-disable-next-line no-param-reassign
382
- element.style.visibility = 'hidden';
383
- if (PositioningService.elements.size === 0) {
384
- window.addEventListener('resize', PositioningService.update);
385
- window.addEventListener('scroll', PositioningService.update);
386
- }
387
- if (!options.relativeTo) {
388
- throw new Error('You must provide an element for the relativeTo option to position an element.');
389
- }
390
- PositioningService.elements.set(element, options);
391
- utils.onNextTick(function () {
392
- PositioningService.fixElementPosition(element);
393
- PositioningService.viewportObserver.observe(element);
394
- // eslint-disable-next-line no-param-reassign
395
- element.style.visibility = '';
396
- PositioningService.checkCollisions(element);
397
- });
398
- };
399
- /** PositioningService will stop controlling the position of the given element. */
400
- PositioningService.release = function (element) {
401
- if (PositioningService.elements.has(element)) {
402
- PositioningService.clearStyles(element);
403
- var options = PositioningService.elements.get(element);
404
- if (options.scrollParent) {
405
- options.scrollParent.removeEventListener('scroll', PositioningService.update);
406
- PositioningService.registeredScrollParents.delete(options.scrollParent);
407
- }
408
- PositioningService.elements.delete(element);
409
- if (PositioningService.elements.size === 0) {
410
- window.removeEventListener('resize', PositioningService.update);
411
- window.removeEventListener('scroll', PositioningService.update);
412
- }
413
- PositioningService.viewportObserver.unobserve(element);
414
- }
415
- };
416
- PositioningService.releaseAll = function () {
417
- var e_4, _a;
418
- try {
419
- for (var _b = __values(PositioningService.elements.keys()), _c = _b.next(); !_c.done; _c = _b.next()) {
420
- var element = _c.value;
421
- PositioningService.release(element);
422
- }
423
- }
424
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
425
- finally {
426
- try {
427
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
428
- }
429
- finally { if (e_4) throw e_4.error; }
430
- }
431
- };
432
- /** PositioningService will refresh the positioning of the given element */
433
- PositioningService.refresh = function (element) {
434
- if (PositioningService.elements.has(element)) {
435
- PositioningService.clearStyles(element);
436
- PositioningService.fixElementPosition(element);
437
- PositioningService.checkCollisions(element);
438
- }
439
- };
440
- PositioningService.refreshAll = function () {
441
- var e_5, _a;
442
- try {
443
- for (var _b = __values(PositioningService.elements.keys()), _c = _b.next(); !_c.done; _c = _b.next()) {
444
- var element = _c.value;
445
- PositioningService.refresh(element);
446
- }
447
- }
448
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
449
- finally {
450
- try {
451
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
452
- }
453
- finally { if (e_5) throw e_5.error; }
454
- }
455
- };
456
- /** Update the positioning options of a currently positioned element */
457
- PositioningService.reposition = function (element, newOptions) {
458
- if (PositioningService.elements.has(element)) {
459
- var options = PositioningService.elements.get(element);
460
- PositioningService.elements.set(element, Object.assign(options, newOptions));
461
- PositioningService.refresh(element);
462
- }
463
- };
464
- /* eslint class-methods-use-this: "off" */
465
- PositioningService.prototype.on = function (eventName, handler) {
466
- PositioningService.on(eventName, handler);
467
- };
468
- PositioningService.prototype.off = function (eventName, handler) {
469
- PositioningService.off(eventName, handler);
470
- };
471
- PositioningService.prototype.once = function (eventName, handler) {
472
- PositioningService.once(eventName, handler);
473
- };
474
- PositioningService.prototype.isPositioned = function (element) {
475
- return PositioningService.isPositioned(element);
476
- };
477
- PositioningService.prototype.getPositioningOptions = function (element) {
478
- return PositioningService.getPositioningOptions(element);
479
- };
480
- PositioningService.prototype.position = function (element, options) {
481
- if (options === void 0) { options = {}; }
482
- PositioningService.position(element, options);
483
- };
484
- PositioningService.prototype.release = function (element) {
485
- PositioningService.release(element);
486
- };
487
- PositioningService.prototype.releaseAll = function () {
488
- PositioningService.releaseAll();
489
- };
490
- PositioningService.prototype.refresh = function (element) {
491
- PositioningService.refresh(element);
492
- };
493
- PositioningService.prototype.refreshAll = function () {
494
- PositioningService.refreshAll();
495
- };
496
- PositioningService.prototype.reposition = function (element, newOptions) {
497
- PositioningService.reposition(element, newOptions);
498
- };
499
- PositioningService.elements = new Map();
500
- PositioningService.registeredScrollParents = new Set();
501
- PositioningService.events = new eventemitter3.EventEmitter();
502
- PositioningService.viewportObserver = new IntersectionObserver(PositioningService.onViewportIntersection, {
503
- rootMargin: "-" + NAVBAR_HEIGHT + "px 0px 0px",
504
- threshold: 1,
505
- });
506
- __decorate([
507
- utils.lockedToAnimationFrames,
508
- __metadata("design:type", Function),
509
- __metadata("design:paramtypes", [Event]),
510
- __metadata("design:returntype", void 0)
511
- ], PositioningService, "update", null);
512
- return PositioningService;
513
- }());
514
-
515
- exports.DEFAULT_POSITIONING_OPTIONS = DEFAULT_POSITIONING_OPTIONS;
516
- exports.PositioningService = PositioningService;
1
+ "use strict";var j=Object.defineProperty,V=Object.defineProperties;var G=Object.getOwnPropertyDescriptors;var C=Object.getOwnPropertySymbols;var z=Object.prototype.hasOwnProperty,W=Object.prototype.propertyIsEnumerable;var S=(e,i,r)=>i in e?j(e,i,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[i]=r,D=(e,i)=>{for(var r in i||(i={}))z.call(i,r)&&S(e,r,i[r]);if(C)for(var r of C(i))W.call(i,r)&&S(e,r,i[r]);return e},F=(e,i)=>V(e,G(i));Object.defineProperty(exports,"__esModule",{value:!0});exports[Symbol.toStringTag]="Module";var N=require("@spscommerce/utils");exports.Position=void 0;(function(e){e.TOP_LEFT="top left",e.TOP_MIDDLE="top middle",e.TOP_RIGHT="top right",e.RIGHT_TOP="right top",e.RIGHT_MIDDLE="right middle",e.RIGHT_BOTTOM="right bottom",e.BOTTOM_RIGHT="bottom right",e.BOTTOM_MIDDLE="bottom middle",e.BOTTOM_LEFT="bottom left",e.LEFT_BOTTOM="left bottom",e.LEFT_MIDDLE="left middle",e.LEFT_TOP="left top"})(exports.Position||(exports.Position={}));exports.PositionAnchor=void 0;(function(e){e.TOP_LEFT="top left",e.TOP_RIGHT="top right",e.BOTTOM_LEFT="bottom left",e.BOTTOM_RIGHT="bottom right"})(exports.PositionAnchor||(exports.PositionAnchor={}));const H={anchor:exports.PositionAnchor.TOP_LEFT,offsets:[],position:exports.Position.TOP_LEFT};(function(){if(typeof window!="object")return;if("IntersectionObserver"in window&&"IntersectionObserverEntry"in window&&"intersectionRatio"in window.IntersectionObserverEntry.prototype){"isIntersecting"in window.IntersectionObserverEntry.prototype||Object.defineProperty(window.IntersectionObserverEntry.prototype,"isIntersecting",{get:function(){return this.intersectionRatio>0}});return}function e(t){try{return t.defaultView&&t.defaultView.frameElement||null}catch{return null}}var i=function(t){for(var o=t,n=e(o);n;)o=n.ownerDocument,n=e(o);return o}(window.document),r=[],f=null,g=null;function b(t){this.time=t.time,this.target=t.target,this.rootBounds=m(t.rootBounds),this.boundingClientRect=m(t.boundingClientRect),this.intersectionRect=m(t.intersectionRect||w()),this.isIntersecting=!!t.intersectionRect;var o=this.boundingClientRect,n=o.width*o.height,c=this.intersectionRect,a=c.width*c.height;n?this.intersectionRatio=Number((a/n).toFixed(4)):this.intersectionRatio=this.isIntersecting?1:0}function l(t,o){var n=o||{};if(typeof t!="function")throw new Error("callback must be a function");if(n.root&&n.root.nodeType!=1&&n.root.nodeType!=9)throw new Error("root must be a Document or Element");this._checkForIntersections=d(this._checkForIntersections.bind(this),this.THROTTLE_TIMEOUT),this._callback=t,this._observationTargets=[],this._queuedEntries=[],this._rootMarginValues=this._parseRootMargin(n.rootMargin),this.thresholds=this._initThresholds(n.threshold),this.root=n.root||null,this.rootMargin=this._rootMarginValues.map(function(c){return c.value+c.unit}).join(" "),this._monitoringDocuments=[],this._monitoringUnsubscribes=[]}l.prototype.THROTTLE_TIMEOUT=100,l.prototype.POLL_INTERVAL=null,l.prototype.USE_MUTATION_OBSERVER=!0,l._setupCrossOriginUpdater=function(){return f||(f=function(t,o){!t||!o?g=w():g=x(t,o),r.forEach(function(n){n._checkForIntersections()})}),f},l._resetCrossOriginUpdater=function(){f=null,g=null},l.prototype.observe=function(t){var o=this._observationTargets.some(function(n){return n.element==t});if(!o){if(!(t&&t.nodeType==1))throw new Error("target must be an Element");this._registerInstance(),this._observationTargets.push({element:t,entry:null}),this._monitorIntersections(t.ownerDocument),this._checkForIntersections()}},l.prototype.unobserve=function(t){this._observationTargets=this._observationTargets.filter(function(o){return o.element!=t}),this._unmonitorIntersections(t.ownerDocument),this._observationTargets.length==0&&this._unregisterInstance()},l.prototype.disconnect=function(){this._observationTargets=[],this._unmonitorAllIntersections(),this._unregisterInstance()},l.prototype.takeRecords=function(){var t=this._queuedEntries.slice();return this._queuedEntries=[],t},l.prototype._initThresholds=function(t){var o=t||[0];return Array.isArray(o)||(o=[o]),o.sort().filter(function(n,c,a){if(typeof n!="number"||isNaN(n)||n<0||n>1)throw new Error("threshold must be a number between 0 and 1 inclusively");return n!==a[c-1]})},l.prototype._parseRootMargin=function(t){var o=t||"0px",n=o.split(/\s+/).map(function(c){var a=/^(-?\d*\.?\d+)(px|%)$/.exec(c);if(!a)throw new Error("rootMargin must be specified in pixels or percent");return{value:parseFloat(a[1]),unit:a[2]}});return n[1]=n[1]||n[0],n[2]=n[2]||n[0],n[3]=n[3]||n[1],n},l.prototype._monitorIntersections=function(t){var o=t.defaultView;if(!!o&&this._monitoringDocuments.indexOf(t)==-1){var n=this._checkForIntersections,c=null,a=null;this.POLL_INTERVAL?c=o.setInterval(n,this.POLL_INTERVAL):(u(o,"resize",n,!0),u(t,"scroll",n,!0),this.USE_MUTATION_OBSERVER&&"MutationObserver"in o&&(a=new o.MutationObserver(n),a.observe(t,{attributes:!0,childList:!0,characterData:!0,subtree:!0}))),this._monitoringDocuments.push(t),this._monitoringUnsubscribes.push(function(){var O=t.defaultView;O&&(c&&O.clearInterval(c),p(O,"resize",n,!0)),p(t,"scroll",n,!0),a&&a.disconnect()});var E=this.root&&(this.root.ownerDocument||this.root)||i;if(t!=E){var y=e(t);y&&this._monitorIntersections(y.ownerDocument)}}},l.prototype._unmonitorIntersections=function(t){var o=this._monitoringDocuments.indexOf(t);if(o!=-1){var n=this.root&&(this.root.ownerDocument||this.root)||i,c=this._observationTargets.some(function(y){var O=y.element.ownerDocument;if(O==t)return!0;for(;O&&O!=n;){var R=e(O);if(O=R&&R.ownerDocument,O==t)return!0}return!1});if(!c){var a=this._monitoringUnsubscribes[o];if(this._monitoringDocuments.splice(o,1),this._monitoringUnsubscribes.splice(o,1),a(),t!=n){var E=e(t);E&&this._unmonitorIntersections(E.ownerDocument)}}}},l.prototype._unmonitorAllIntersections=function(){var t=this._monitoringUnsubscribes.slice(0);this._monitoringDocuments.length=0,this._monitoringUnsubscribes.length=0;for(var o=0;o<t.length;o++)t[o]()},l.prototype._checkForIntersections=function(){if(!(!this.root&&f&&!g)){var t=this._rootIsInDom(),o=t?this._getRootRect():w();this._observationTargets.forEach(function(n){var c=n.element,a=T(c),E=this._rootContainsTarget(c),y=n.entry,O=t&&E&&this._computeTargetAndRootIntersection(c,a,o),R=null;this._rootContainsTarget(c)?(!f||this.root)&&(R=o):R=w();var L=n.entry=new b({time:_(),target:c,boundingClientRect:a,rootBounds:R,intersectionRect:O});y?t&&E?this._hasCrossedThreshold(y,L)&&this._queuedEntries.push(L):y&&y.isIntersecting&&this._queuedEntries.push(L):this._queuedEntries.push(L)},this),this._queuedEntries.length&&this._callback(this.takeRecords(),this)}},l.prototype._computeTargetAndRootIntersection=function(t,o,n){if(window.getComputedStyle(t).display!="none"){for(var c=o,a=I(t),E=!1;!E&&a;){var y=null,O=a.nodeType==1?window.getComputedStyle(a):{};if(O.display=="none")return null;if(a==this.root||a.nodeType==9)if(E=!0,a==this.root||a==i)f&&!this.root?!g||g.width==0&&g.height==0?(a=null,y=null,c=null):y=g:y=n;else{var R=I(a),L=R&&T(R),k=R&&this._computeTargetAndRootIntersection(R,L,n);L&&k?(a=R,y=x(L,k)):(a=null,c=null)}else{var B=a.ownerDocument;a!=B.body&&a!=B.documentElement&&O.overflow!="visible"&&(y=T(a))}if(y&&(c=v(y,c)),!c)break;a=a&&I(a)}return c}},l.prototype._getRootRect=function(){var t;if(this.root&&!P(this.root))t=T(this.root);else{var o=P(this.root)?this.root:i,n=o.documentElement,c=o.body;t={top:0,left:0,right:n.clientWidth||c.clientWidth,width:n.clientWidth||c.clientWidth,bottom:n.clientHeight||c.clientHeight,height:n.clientHeight||c.clientHeight}}return this._expandRectByRootMargin(t)},l.prototype._expandRectByRootMargin=function(t){var o=this._rootMarginValues.map(function(c,a){return c.unit=="px"?c.value:c.value*(a%2?t.width:t.height)/100}),n={top:t.top-o[0],right:t.right+o[1],bottom:t.bottom+o[2],left:t.left-o[3]};return n.width=n.right-n.left,n.height=n.bottom-n.top,n},l.prototype._hasCrossedThreshold=function(t,o){var n=t&&t.isIntersecting?t.intersectionRatio||0:-1,c=o.isIntersecting?o.intersectionRatio||0:-1;if(n!==c)for(var a=0;a<this.thresholds.length;a++){var E=this.thresholds[a];if(E==n||E==c||E<n!=E<c)return!0}},l.prototype._rootIsInDom=function(){return!this.root||h(i,this.root)},l.prototype._rootContainsTarget=function(t){var o=this.root&&(this.root.ownerDocument||this.root)||i;return h(o,t)&&(!this.root||o==t.ownerDocument)},l.prototype._registerInstance=function(){r.indexOf(this)<0&&r.push(this)},l.prototype._unregisterInstance=function(){var t=r.indexOf(this);t!=-1&&r.splice(t,1)};function _(){return window.performance&&performance.now&&performance.now()}function d(t,o){var n=null;return function(){n||(n=setTimeout(function(){t(),n=null},o))}}function u(t,o,n,c){typeof t.addEventListener=="function"?t.addEventListener(o,n,c||!1):typeof t.attachEvent=="function"&&t.attachEvent("on"+o,n)}function p(t,o,n,c){typeof t.removeEventListener=="function"?t.removeEventListener(o,n,c||!1):typeof t.detatchEvent=="function"&&t.detatchEvent("on"+o,n)}function v(t,o){var n=Math.max(t.top,o.top),c=Math.min(t.bottom,o.bottom),a=Math.max(t.left,o.left),E=Math.min(t.right,o.right),y=E-a,O=c-n;return y>=0&&O>=0&&{top:n,bottom:c,left:a,right:E,width:y,height:O}||null}function T(t){var o;try{o=t.getBoundingClientRect()}catch{}return o?(o.width&&o.height||(o={top:o.top,right:o.right,bottom:o.bottom,left:o.left,width:o.right-o.left,height:o.bottom-o.top}),o):w()}function w(){return{top:0,bottom:0,left:0,right:0,width:0,height:0}}function m(t){return!t||"x"in t?t:{top:t.top,y:t.top,bottom:t.bottom,left:t.left,x:t.left,right:t.right,width:t.width,height:t.height}}function x(t,o){var n=o.top-t.top,c=o.left-t.left;return{top:n,left:c,height:o.height,width:o.width,bottom:n+o.height,right:c+o.width}}function h(t,o){for(var n=o;n;){if(n==t)return!0;n=I(n)}return!1}function I(t){var o=t.parentNode;return t.nodeType==9&&t!=i?e(t):(o&&o.assignedSlot&&(o=o.assignedSlot.parentNode),o&&o.nodeType==11&&o.host?o.host:o)}function P(t){return t&&t.nodeType===9}window.IntersectionObserver=l,window.IntersectionObserverEntry=b})();var U={exports:{}};(function(e){var i=Object.prototype.hasOwnProperty,r="~";function f(){}Object.create&&(f.prototype=Object.create(null),new f().__proto__||(r=!1));function g(d,u,p){this.fn=d,this.context=u,this.once=p||!1}function b(d,u,p,v,T){if(typeof p!="function")throw new TypeError("The listener must be a function");var w=new g(p,v||d,T),m=r?r+u:u;return d._events[m]?d._events[m].fn?d._events[m]=[d._events[m],w]:d._events[m].push(w):(d._events[m]=w,d._eventsCount++),d}function l(d,u){--d._eventsCount==0?d._events=new f:delete d._events[u]}function _(){this._events=new f,this._eventsCount=0}_.prototype.eventNames=function(){var u=[],p,v;if(this._eventsCount===0)return u;for(v in p=this._events)i.call(p,v)&&u.push(r?v.slice(1):v);return Object.getOwnPropertySymbols?u.concat(Object.getOwnPropertySymbols(p)):u},_.prototype.listeners=function(u){var p=r?r+u:u,v=this._events[p];if(!v)return[];if(v.fn)return[v.fn];for(var T=0,w=v.length,m=new Array(w);T<w;T++)m[T]=v[T].fn;return m},_.prototype.listenerCount=function(u){var p=r?r+u:u,v=this._events[p];return v?v.fn?1:v.length:0},_.prototype.emit=function(u,p,v,T,w,m){var x=r?r+u:u;if(!this._events[x])return!1;var h=this._events[x],I=arguments.length,P,t;if(h.fn){switch(h.once&&this.removeListener(u,h.fn,void 0,!0),I){case 1:return h.fn.call(h.context),!0;case 2:return h.fn.call(h.context,p),!0;case 3:return h.fn.call(h.context,p,v),!0;case 4:return h.fn.call(h.context,p,v,T),!0;case 5:return h.fn.call(h.context,p,v,T,w),!0;case 6:return h.fn.call(h.context,p,v,T,w,m),!0}for(t=1,P=new Array(I-1);t<I;t++)P[t-1]=arguments[t];h.fn.apply(h.context,P)}else{var o=h.length,n;for(t=0;t<o;t++)switch(h[t].once&&this.removeListener(u,h[t].fn,void 0,!0),I){case 1:h[t].fn.call(h[t].context);break;case 2:h[t].fn.call(h[t].context,p);break;case 3:h[t].fn.call(h[t].context,p,v);break;case 4:h[t].fn.call(h[t].context,p,v,T);break;default:if(!P)for(n=1,P=new Array(I-1);n<I;n++)P[n-1]=arguments[n];h[t].fn.apply(h[t].context,P)}}return!0},_.prototype.on=function(u,p,v){return b(this,u,p,v,!1)},_.prototype.once=function(u,p,v){return b(this,u,p,v,!0)},_.prototype.removeListener=function(u,p,v,T){var w=r?r+u:u;if(!this._events[w])return this;if(!p)return l(this,w),this;var m=this._events[w];if(m.fn)m.fn===p&&(!T||m.once)&&(!v||m.context===v)&&l(this,w);else{for(var x=0,h=[],I=m.length;x<I;x++)(m[x].fn!==p||T&&!m[x].once||v&&m[x].context!==v)&&h.push(m[x]);h.length?this._events[w]=h.length===1?h[0]:h:l(this,w)}return this},_.prototype.removeAllListeners=function(u){var p;return u?(p=r?r+u:u,this._events[p]&&l(this,p)):(this._events=new f,this._eventsCount=0),this},_.prototype.off=_.prototype.removeListener,_.prototype.addListener=_.prototype.on,_.prefixed=r,_.EventEmitter=_,e.exports=_})(U);var $=Object.defineProperty,q=Object.getOwnPropertyDescriptor,Y=(e,i,r,f)=>{for(var g=f>1?void 0:f?q(i,r):i,b=e.length-1,l;b>=0;b--)(l=e[b])&&(g=(f?l(i,r,g):l(g))||g);return f&&g&&$(i,r,g),g};const A=60,s=class{static on(e,i){s.events.on(e,i)}static off(e,i){s.events.off(e,i)}static once(e,i){s.events.once(e,i)}static checkCollisions(e,i){const r=i||Array.from(s.elements.keys()),f=e.getBoundingClientRect(),g=[];for(const b of r)if(b!==e){const l=b.getBoundingClientRect();l.left<=f.right&&l.right>=f.left&&l.top<=f.bottom&&l.bottom>=f.top&&g.push(b)}g.length&&s.onElementIntersection(e,g)}static isPositioned(e){return s.elements.has(e)}static getPositioningOptions(e){return s.elements.has(e)?s.elements.get(e):null}static position(e,i={}){const r=D(D({},H),i);let f=e;for(;f!==document.body;)if(f=f.parentElement,f.classList.contains("sps-focused-task")){r.scrollParent=f,s.registeredScrollParents.has(f)||(s.registeredScrollParents.add(f),f.addEventListener("scroll",s.update));break}if(e.style.visibility="hidden",s.elements.size===0&&(window.addEventListener("resize",s.update),window.addEventListener("scroll",s.update)),!r.relativeTo)throw new Error("You must provide an element for the relativeTo option to position an element.");s.elements.set(e,r),N.onNextTick(()=>{s.fixElementPosition(e),s.viewportObserver.observe(e),e.style.visibility="",s.checkCollisions(e)})}static release(e){if(s.elements.has(e)){s.clearStyles(e);const i=s.elements.get(e);i.scrollParent&&(i.scrollParent.removeEventListener("scroll",s.update),s.registeredScrollParents.delete(i.scrollParent)),s.elements.delete(e),s.elements.size===0&&(window.removeEventListener("resize",s.update),window.removeEventListener("scroll",s.update)),s.viewportObserver.unobserve(e)}}static releaseAll(){for(const e of s.elements.keys())s.release(e)}static refresh(e){s.elements.has(e)&&(s.clearStyles(e),s.fixElementPosition(e),s.checkCollisions(e))}static refreshAll(){for(const e of s.elements.keys())s.refresh(e)}static reposition(e,i){if(s.elements.has(e)){const r=s.elements.get(e);s.elements.set(e,Object.assign(r,i)),s.refresh(e)}}static onViewportIntersection(e){for(const i of e){const r=Object.keys(IntersectionObserverEntry.prototype).reduce((f,g)=>F(D({},f),{[g]:i[g]}),{});r.rootBounds=r.rootBounds||s.getRootBounds(),s.events.emit("viewportIntersection",Object.freeze(r))}}static onElementIntersection(e,i){s.events.emit("elementIntersection",{target:e,intersectingWith:i})}static getRootBounds(){const e=Math.max(document.documentElement.clientWidth,window.innerWidth||0),i=Math.max(document.documentElement.clientHeight,window.innerHeight||0);return{x:0,y:A,left:0,top:A,right:e,bottom:i,height:i-A,width:e}}static clearStyles(e){Object.assign(e.style,{position:"",width:"",top:"",left:"",right:"",bottom:"",visibility:"",zIndex:""})}static fixElementPosition(e,i){if(!this.elements.has(e))return;const r=i||this.elements.get(e),f=this.getRootBounds(),g=e.getBoundingClientRect(),b=r.relativeTo.getBoundingClientRect(),{width:l}=r.useRelativeTargetWidth?b:g;Object.assign(e.style,{minWidth:`${l}px`,position:"fixed",zIndex:r.zIndex||""});let _,d;const u=r.position.split(" "),p=r.offsets[0]||0,v=r.offsets[1]||0;switch(_=0,d=0,u[0]){case"top":_=b.top-g.height-p;break;case"left":d=b.left-l-p;break;case"right":d=b.right+p;break;case"bottom":_=b.bottom+p;break;default:throw new Error(`${r.position} is not a valid position`)}switch(u[1]){case"left":d=b.left-v;break;case"top":_=b.top-v;break;case"middle":u[0]==="top"||u[0]==="bottom"?d=b.left+b.width/2-l/2+v:(u[0]==="left"||u[0]==="right")&&(_=b.top+b.height/2-g.height/2+v);break;case"bottom":_=b.bottom-g.height+v;break;case"right":d=b.right-l+v;break;default:throw new Error(`${r.position} is not a valid position`)}const T=_+g.height,w=d+g.width,m=`${Math.round(_)}px`,x=`${Math.round(f.bottom-T)}px`,h=`${Math.round(d)}px`,I=`${Math.round(f.right-w)}px`;switch(r.anchor){case exports.PositionAnchor.TOP_LEFT:Object.assign(e.style,{top:m,bottom:"auto",left:h,right:"auto"});break;case exports.PositionAnchor.TOP_RIGHT:Object.assign(e.style,{top:m,bottom:"auto",left:"auto",right:I});break;case exports.PositionAnchor.BOTTOM_LEFT:Object.assign(e.style,{top:"auto",bottom:x,left:h,right:"auto"});break;case exports.PositionAnchor.BOTTOM_RIGHT:Object.assign(e.style,{top:"auto",bottom:x,left:"auto",right:I});break}}static update(e){const i=s.elements.entries(),r=[];for(const[f,g]of i)(!e||e.target===document||e.target===g.scrollParent||e.target===window)&&(s.fixElementPosition(f,g),s.checkCollisions(f,r),r.push(f))}on(e,i){s.on(e,i)}off(e,i){s.off(e,i)}once(e,i){s.once(e,i)}isPositioned(e){return s.isPositioned(e)}getPositioningOptions(e){return s.getPositioningOptions(e)}position(e,i={}){s.position(e,i)}release(e){s.release(e)}releaseAll(){s.releaseAll()}refresh(e){s.refresh(e)}refreshAll(){s.refreshAll()}reposition(e,i){s.reposition(e,i)}};let M=s;M.elements=new Map;M.registeredScrollParents=new Set;M.events=new U.exports.EventEmitter;M.viewportObserver=new IntersectionObserver(s.onViewportIntersection,{rootMargin:`-${A}px 0px 0px`,threshold:1});Y([N.lockedToAnimationFrames],M,"update",1);exports.DEFAULT_POSITIONING_OPTIONS=H;exports.PositioningService=M;
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from './position.enum';
2
- export * from './position-anchor.enum';
3
- export * from './positioning-options.interface';
4
- export * from './positioning.service';
1
+ export * from "./position.enum";
2
+ export * from "./position-anchor.enum";
3
+ export * from "./positioning-options.interface";
4
+ export * from "./positioning.service";