UNPKG

22.6 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core')) :
3 typeof define === 'function' && define.amd ? define('ngx-bootstrap/utils', ['exports', '@angular/core'], factory) :
4 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global['ngx-bootstrap'] = global['ngx-bootstrap'] || {}, global['ngx-bootstrap'].utils = {}), global.ng.core));
5}(this, (function (exports, core) { 'use strict';
6
7 /**
8 * @copyright Valor Software
9 * @copyright Angular ng-bootstrap team
10 */
11 var Trigger = /** @class */ (function () {
12 function Trigger(open, close) {
13 this.open = open;
14 this.close = close || open;
15 }
16 Trigger.prototype.isManual = function () {
17 return this.open === 'manual' || this.close === 'manual';
18 };
19 return Trigger;
20 }());
21
22 var DEFAULT_ALIASES = {
23 hover: ['mouseover', 'mouseout'],
24 focus: ['focusin', 'focusout']
25 };
26 // eslint-disable-next-line @typescript-eslint/no-explicit-any
27 function parseTriggers(triggers, aliases) {
28 if (aliases === void 0) { aliases = DEFAULT_ALIASES; }
29 var trimmedTriggers = (triggers || '').trim();
30 if (trimmedTriggers.length === 0) {
31 return [];
32 }
33 var parsedTriggers = trimmedTriggers
34 .split(/\s+/)
35 .map(function (trigger) { return trigger.split(':'); })
36 .map(function (triggerPair) {
37 var alias = aliases[triggerPair[0]] || triggerPair;
38 return new Trigger(alias[0], alias[1]);
39 });
40 var manualTriggers = parsedTriggers.filter(function (triggerPair) { return triggerPair.isManual(); });
41 if (manualTriggers.length > 1) {
42 throw new Error('Triggers parse error: only one manual trigger is allowed');
43 }
44 if (manualTriggers.length === 1 && parsedTriggers.length > 1) {
45 throw new Error('Triggers parse error: manual trigger can\'t be mixed with other triggers');
46 }
47 return parsedTriggers;
48 }
49 function listenToTriggers(renderer,
50 // eslint-disable-next-line @typescript-eslint/no-explicit-any
51 target, triggers, showFn, hideFn, toggleFn) {
52 var parsedTriggers = parseTriggers(triggers);
53 var listeners = [];
54 if (parsedTriggers.length === 1 && parsedTriggers[0].isManual()) {
55 return Function.prototype;
56 }
57 parsedTriggers.forEach(function (trigger) {
58 if (trigger.open === trigger.close) {
59 listeners.push(renderer.listen(target, trigger.open, toggleFn));
60 return;
61 }
62 listeners.push(renderer.listen(target, trigger.open, showFn));
63 if (trigger.close) {
64 listeners.push(renderer.listen(target, trigger.close, hideFn));
65 }
66 });
67 return function () {
68 listeners.forEach(function (unsubscribeFn) { return unsubscribeFn(); });
69 };
70 }
71 function listenToTriggersV2(renderer, options) {
72 var parsedTriggers = parseTriggers(options.triggers);
73 var target = options.target;
74 // do nothing
75 if (parsedTriggers.length === 1 && parsedTriggers[0].isManual()) {
76 return Function.prototype;
77 }
78 // all listeners
79 var listeners = [];
80 // lazy listeners registration
81 var _registerHide = [];
82 var registerHide = function () {
83 // add hide listeners to unregister array
84 _registerHide.forEach(function (fn) { return listeners.push(fn()); });
85 // register hide events only once
86 _registerHide.length = 0;
87 };
88 // register open\close\toggle listeners
89 parsedTriggers.forEach(function (trigger) {
90 var useToggle = trigger.open === trigger.close;
91 var showFn = useToggle ? options.toggle : options.show;
92 if (!useToggle && trigger.close && options.hide) {
93 var _hide_1 = renderer.listen(target, trigger.close, options.hide);
94 _registerHide.push(function () { return _hide_1; });
95 }
96 if (showFn) {
97 listeners.push(renderer.listen(target, trigger.open, function () { return showFn(registerHide); }));
98 }
99 });
100 return function () {
101 listeners.forEach(function (unsubscribeFn) { return unsubscribeFn(); });
102 };
103 }
104 function registerOutsideClick(renderer, options) {
105 if (!options.outsideClick) {
106 return Function.prototype;
107 }
108 // eslint-disable-next-line @typescript-eslint/no-explicit-any
109 return renderer.listen('document', 'click', function (event) {
110 if (options.target && options.target.contains(event.target)) {
111 return;
112 }
113 if (options.targets &&
114 options.targets.some(function (target) { return target.contains(event.target); })) {
115 return;
116 }
117 if (options.hide) {
118 options.hide();
119 }
120 });
121 }
122 function registerEscClick(renderer, options) {
123 if (!options.outsideEsc) {
124 return Function.prototype;
125 }
126 return renderer.listen('document', 'keyup.esc', function (event) {
127 if (options.target && options.target.contains(event.target)) {
128 return;
129 }
130 if (options.targets &&
131 options.targets.some(function (target) { return target.contains(event.target); })) {
132 return;
133 }
134 if (options.hide) {
135 options.hide();
136 }
137 });
138 }
139
140 /**
141 * @license
142 * Copyright Google Inc. All Rights Reserved.
143 *
144 * Use of this source code is governed by an MIT-style license that can be
145 * found in the LICENSE file at https://angular.io/license
146 */
147 /**
148 * JS version of browser APIs. This library can only run in the browser.
149 */
150 // eslint-disable-next-line @typescript-eslint/no-explicit-any
151 var win = (typeof window !== 'undefined' && window) || {};
152 var document = win.document;
153 var location = win.location;
154 // eslint-disable-next-line @typescript-eslint/no-explicit-any
155 var gc = win.gc ? function () { return win.gc(); } : function () { return null; };
156 var performance = win.performance ? win.performance : null;
157 var Event = win.Event;
158 var MouseEvent = win.MouseEvent;
159 var KeyboardEvent = win.KeyboardEvent;
160 var EventTarget = win.EventTarget;
161 var History = win.History;
162 var Location = win.Location;
163 var EventListener = win.EventListener;
164
165 (function (BsVerions) {
166 BsVerions["isBs3"] = "bs3";
167 BsVerions["isBs4"] = "bs4";
168 BsVerions["isBs5"] = "bs5";
169 })(exports.BsVerions || (exports.BsVerions = {}));
170 var guessedVersion;
171 function _guessBsVersion() {
172 if (typeof win.document === 'undefined') {
173 return 'bs4';
174 }
175 var spanEl = win.document.createElement('span');
176 spanEl.innerText = 'testing bs version';
177 spanEl.classList.add('d-none');
178 spanEl.classList.add('visually-hidden');
179 win.document.head.appendChild(spanEl);
180 var rect = spanEl.getBoundingClientRect();
181 var overflowStyle = win.getComputedStyle(spanEl).overflow;
182 win.document.head.removeChild(spanEl);
183 if (!rect || (rect && rect.top !== 0)) {
184 return 'bs3';
185 }
186 if (overflowStyle && overflowStyle === 'hidden') {
187 return 'bs5';
188 }
189 return 'bs4';
190 }
191 function setTheme(theme) {
192 guessedVersion = theme;
193 }
194 // todo: in ngx-bootstrap, bs4 will became a default one
195 function isBs3() {
196 if (typeof win === 'undefined') {
197 return true;
198 }
199 if (typeof win.__theme === 'undefined') {
200 if (guessedVersion) {
201 return guessedVersion === 'bs3';
202 }
203 guessedVersion = _guessBsVersion();
204 return guessedVersion === 'bs3';
205 }
206 return win.__theme === 'bs3';
207 }
208 function isBs4() {
209 if (isBs3())
210 return false;
211 if (guessedVersion)
212 return guessedVersion === 'bs4';
213 guessedVersion = _guessBsVersion();
214 return guessedVersion === 'bs4';
215 }
216 function isBs5() {
217 if (isBs3() || isBs4())
218 return false;
219 if (guessedVersion)
220 return guessedVersion === 'bs5';
221 guessedVersion = _guessBsVersion();
222 return guessedVersion === 'bs5';
223 }
224 function getBsVer() {
225 return {
226 isBs3: isBs3(),
227 isBs4: isBs4(),
228 isBs5: isBs5()
229 };
230 }
231 function currentBsVersion() {
232 var bsVer = getBsVer();
233 var resVersion = Object.keys(bsVer).find(function (key) { return bsVer[key]; });
234 return exports.BsVerions[resVersion];
235 }
236
237 var LinkedList = /** @class */ (function () {
238 function LinkedList() {
239 this.length = 0;
240 this.asArray = [];
241 // Array methods overriding END
242 }
243 LinkedList.prototype.get = function (position) {
244 if (this.length === 0 || position < 0 || position >= this.length) {
245 return void 0;
246 }
247 var current = this.head;
248 for (var index = 0; index < position; index++) {
249 current = current === null || current === void 0 ? void 0 : current.next;
250 }
251 return current === null || current === void 0 ? void 0 : current.value;
252 };
253 LinkedList.prototype.add = function (value, position) {
254 if (position === void 0) { position = this.length; }
255 if (position < 0 || position > this.length) {
256 throw new Error('Position is out of the list');
257 }
258 var node = {
259 value: value,
260 next: undefined,
261 previous: undefined
262 };
263 if (this.length === 0) {
264 this.head = node;
265 this.tail = node;
266 this.current = node;
267 }
268 else {
269 if (position === 0 && this.head) {
270 // first node
271 node.next = this.head;
272 this.head.previous = node;
273 this.head = node;
274 }
275 else if (position === this.length && this.tail) {
276 // last node
277 this.tail.next = node;
278 node.previous = this.tail;
279 this.tail = node;
280 }
281 else {
282 // node in middle
283 var currentPreviousNode = this.getNode(position - 1);
284 var currentNextNode = currentPreviousNode === null || currentPreviousNode === void 0 ? void 0 : currentPreviousNode.next;
285 if (currentPreviousNode && currentNextNode) {
286 currentPreviousNode.next = node;
287 currentNextNode.previous = node;
288 node.previous = currentPreviousNode;
289 node.next = currentNextNode;
290 }
291 }
292 }
293 this.length++;
294 this.createInternalArrayRepresentation();
295 };
296 LinkedList.prototype.remove = function (position) {
297 if (position === void 0) { position = 0; }
298 var _a;
299 if (this.length === 0 || position < 0 || position >= this.length) {
300 throw new Error('Position is out of the list');
301 }
302 if (position === 0 && this.head) {
303 // first node
304 this.head = this.head.next;
305 if (this.head) {
306 // there is no second node
307 this.head.previous = undefined;
308 }
309 else {
310 // there is no second node
311 this.tail = undefined;
312 }
313 }
314 else if (position === this.length - 1 && ((_a = this.tail) === null || _a === void 0 ? void 0 : _a.previous)) {
315 // last node
316 this.tail = this.tail.previous;
317 this.tail.next = undefined;
318 }
319 else {
320 // middle node
321 var removedNode = this.getNode(position);
322 if ((removedNode === null || removedNode === void 0 ? void 0 : removedNode.next) && removedNode.previous) {
323 removedNode.next.previous = removedNode.previous;
324 removedNode.previous.next = removedNode.next;
325 }
326 }
327 this.length--;
328 this.createInternalArrayRepresentation();
329 };
330 LinkedList.prototype.set = function (position, value) {
331 if (this.length === 0 || position < 0 || position >= this.length) {
332 throw new Error('Position is out of the list');
333 }
334 var node = this.getNode(position);
335 if (node) {
336 node.value = value;
337 this.createInternalArrayRepresentation();
338 }
339 };
340 LinkedList.prototype.toArray = function () {
341 return this.asArray;
342 };
343 LinkedList.prototype.findAll = function (fn) {
344 var current = this.head;
345 var result = [];
346 if (!current) {
347 return result;
348 }
349 for (var index = 0; index < this.length; index++) {
350 if (!current) {
351 return result;
352 }
353 if (fn(current.value, index)) {
354 result.push({ index: index, value: current.value });
355 }
356 current = current.next;
357 }
358 return result;
359 };
360 // Array methods overriding start
361 LinkedList.prototype.push = function () {
362 var _this = this;
363 var args = [];
364 for (var _i = 0; _i < arguments.length; _i++) {
365 args[_i] = arguments[_i];
366 }
367 args.forEach(function (arg) {
368 _this.add(arg);
369 });
370 return this.length;
371 };
372 LinkedList.prototype.pop = function () {
373 if (this.length === 0) {
374 return;
375 }
376 var last = this.tail;
377 this.remove(this.length - 1);
378 return last === null || last === void 0 ? void 0 : last.value;
379 };
380 LinkedList.prototype.unshift = function () {
381 var _this = this;
382 var args = [];
383 for (var _i = 0; _i < arguments.length; _i++) {
384 args[_i] = arguments[_i];
385 }
386 args.reverse();
387 args.forEach(function (arg) {
388 _this.add(arg, 0);
389 });
390 return this.length;
391 };
392 LinkedList.prototype.shift = function () {
393 var _a;
394 if (this.length === 0) {
395 return undefined;
396 }
397 var lastItem = (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
398 this.remove();
399 return lastItem;
400 };
401 LinkedList.prototype.forEach = function (fn) {
402 var current = this.head;
403 for (var index = 0; index < this.length; index++) {
404 if (!current) {
405 return;
406 }
407 fn(current.value, index);
408 current = current.next;
409 }
410 };
411 LinkedList.prototype.indexOf = function (value) {
412 var current = this.head;
413 var position = -1;
414 for (var index = 0; index < this.length; index++) {
415 if (!current) {
416 return position;
417 }
418 if (current.value === value) {
419 position = index;
420 break;
421 }
422 current = current.next;
423 }
424 return position;
425 };
426 LinkedList.prototype.some = function (fn) {
427 var current = this.head;
428 var result = false;
429 while (current && !result) {
430 if (fn(current.value)) {
431 result = true;
432 break;
433 }
434 current = current.next;
435 }
436 return result;
437 };
438 LinkedList.prototype.every = function (fn) {
439 var current = this.head;
440 var result = true;
441 while (current && result) {
442 if (!fn(current.value)) {
443 result = false;
444 }
445 current = current.next;
446 }
447 return result;
448 };
449 LinkedList.prototype.toString = function () {
450 return '[Linked List]';
451 };
452 LinkedList.prototype.find = function (fn) {
453 var current = this.head;
454 for (var index = 0; index < this.length; index++) {
455 if (!current) {
456 return;
457 }
458 if (fn(current.value, index)) {
459 return current.value;
460 }
461 current = current.next;
462 }
463 };
464 LinkedList.prototype.findIndex = function (fn) {
465 var current = this.head;
466 for (var index = 0; index < this.length; index++) {
467 if (!current) {
468 return -1;
469 }
470 if (fn(current.value, index)) {
471 return index;
472 }
473 current = current.next;
474 }
475 return -1;
476 };
477 LinkedList.prototype.getNode = function (position) {
478 if (this.length === 0 || position < 0 || position >= this.length) {
479 throw new Error('Position is out of the list');
480 }
481 var current = this.head;
482 for (var index = 0; index < position; index++) {
483 current = current === null || current === void 0 ? void 0 : current.next;
484 }
485 return current;
486 };
487 LinkedList.prototype.createInternalArrayRepresentation = function () {
488 var outArray = [];
489 var current = this.head;
490 while (current) {
491 outArray.push(current.value);
492 current = current.next;
493 }
494 this.asArray = outArray;
495 };
496 return LinkedList;
497 }());
498
499 // eslint-disable-next-line @typescript-eslint/no-explicit-any
500 function OnChange() {
501 var sufix = 'Change';
502 // eslint-disable-next-line @typescript-eslint/no-explicit-any
503 return function OnChangeHandler(target, propertyKey) {
504 var _key = " __" + propertyKey + "Value";
505 Object.defineProperty(target, propertyKey, {
506 // eslint-disable-next-line @typescript-eslint/no-explicit-any
507 get: function () {
508 return this[_key];
509 },
510 // eslint-disable-next-line @typescript-eslint/no-explicit-any
511 set: function (value) {
512 var prevValue = this[_key];
513 this[_key] = value;
514 if (prevValue !== value && this[propertyKey + sufix]) {
515 this[propertyKey + sufix].emit(value);
516 }
517 }
518 });
519 };
520 }
521
522 var Utils = /** @class */ (function () {
523 function Utils() {
524 }
525 // eslint-disable-next-line @typescript-eslint/no-explicit-any
526 Utils.reflow = function (element) {
527 // eslint-disable-next-line @typescript-eslint/no-explicit-any
528 (function (bs) { return bs; })(element.offsetHeight);
529 };
530 // source: https://github.com/jquery/jquery/blob/master/src/css/var/getStyles.js
531 // eslint-disable-next-line @typescript-eslint/no-explicit-any
532 Utils.getStyles = function (elem) {
533 // Support: IE <=11 only, Firefox <=30 (#15098, #14150)
534 // IE throws on elements created in popups
535 // FF meanwhile throws on frame elements through "defaultView.getComputedStyle"
536 var view = elem.ownerDocument.defaultView;
537 if (!view || !view.opener) {
538 view = win;
539 }
540 return view.getComputedStyle(elem);
541 };
542 Utils.stackOverflowConfig = function () {
543 var bsVer = currentBsVersion();
544 return {
545 crossorigin: bsVer !== 'bs3' ? "anonymous" : undefined,
546 integrity: bsVer === 'bs5' ? 'sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We' : bsVer === 'bs4' ? 'sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2' : undefined,
547 cdnLink: bsVer === 'bs5' ? 'https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css' : bsVer === 'bs4' ? 'https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css' : 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css',
548 };
549 };
550 return Utils;
551 }());
552
553 var _messagesHash = {};
554 var _hideMsg = typeof console === 'undefined' || !('warn' in console);
555 function warnOnce(msg) {
556 if (!core.isDevMode() || _hideMsg || msg in _messagesHash) {
557 return;
558 }
559 _messagesHash[msg] = true;
560 console.warn(msg);
561 }
562
563 /**
564 * Generated bundle index. Do not edit.
565 */
566
567 exports.LinkedList = LinkedList;
568 exports.OnChange = OnChange;
569 exports.Trigger = Trigger;
570 exports.Utils = Utils;
571 exports.currentBsVersion = currentBsVersion;
572 exports.document = document;
573 exports.getBsVer = getBsVer;
574 exports.isBs3 = isBs3;
575 exports.listenToTriggers = listenToTriggers;
576 exports.listenToTriggersV2 = listenToTriggersV2;
577 exports.parseTriggers = parseTriggers;
578 exports.registerEscClick = registerEscClick;
579 exports.registerOutsideClick = registerOutsideClick;
580 exports.setTheme = setTheme;
581 exports.warnOnce = warnOnce;
582 exports.window = win;
583 exports.ɵa = isBs4;
584 exports.ɵb = isBs5;
585
586 Object.defineProperty(exports, '__esModule', { value: true });
587
588})));
589//# sourceMappingURL=ngx-bootstrap-utils.umd.js.map