UNPKG

ngx-bootstrap

Version:
866 lines (854 loc) 22.7 kB
import { isDevMode } from '@angular/core'; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @copyright Valor Software * @copyright Angular ng-bootstrap team */ class Trigger { /** * @param {?} open * @param {?=} close */ constructor(open, close) { this.open = open; this.close = close || open; } /** * @return {?} */ isManual() { return this.open === 'manual' || this.close === 'manual'; } } if (false) { /** @type {?} */ Trigger.prototype.open; /** @type {?} */ Trigger.prototype.close; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @record */ function ListenOptions() { } if (false) { /** @type {?|undefined} */ ListenOptions.prototype.target; /** @type {?|undefined} */ ListenOptions.prototype.targets; /** @type {?|undefined} */ ListenOptions.prototype.triggers; /** @type {?|undefined} */ ListenOptions.prototype.outsideClick; /** @type {?|undefined} */ ListenOptions.prototype.outsideEsc; /** @type {?|undefined} */ ListenOptions.prototype.show; /** @type {?|undefined} */ ListenOptions.prototype.hide; /** @type {?|undefined} */ ListenOptions.prototype.toggle; } /** @type {?} */ const DEFAULT_ALIASES = { hover: ['mouseover', 'mouseout'], focus: ['focusin', 'focusout'] }; /* tslint:disable-next-line: no-any */ /** * @param {?} triggers * @param {?=} aliases * @return {?} */ function parseTriggers(triggers, aliases = DEFAULT_ALIASES) { /** @type {?} */ const trimmedTriggers = (triggers || '').trim(); if (trimmedTriggers.length === 0) { return []; } /** @type {?} */ const parsedTriggers = trimmedTriggers .split(/\s+/) .map((/** * @param {?} trigger * @return {?} */ (trigger) => trigger.split(':'))) .map((/** * @param {?} triggerPair * @return {?} */ (triggerPair) => { /** @type {?} */ const alias = aliases[triggerPair[0]] || triggerPair; return new Trigger(alias[0], alias[1]); })); /** @type {?} */ const manualTriggers = parsedTriggers.filter((/** * @param {?} triggerPair * @return {?} */ (triggerPair) => triggerPair.isManual())); if (manualTriggers.length > 1) { throw new Error('Triggers parse error: only one manual trigger is allowed'); } if (manualTriggers.length === 1 && parsedTriggers.length > 1) { throw new Error('Triggers parse error: manual trigger can\'t be mixed with other triggers'); } return parsedTriggers; } /** * @param {?} renderer * @param {?} target * @param {?} triggers * @param {?} showFn * @param {?} hideFn * @param {?} toggleFn * @return {?} */ function listenToTriggers(renderer, /* tslint:disable-next-line: no-any */ target, triggers, showFn, hideFn, toggleFn) { /** @type {?} */ const parsedTriggers = parseTriggers(triggers); /* tslint:disable-next-line: no-any */ /** @type {?} */ const listeners = []; if (parsedTriggers.length === 1 && parsedTriggers[0].isManual()) { return Function.prototype; } parsedTriggers.forEach((/** * @param {?} trigger * @return {?} */ (trigger) => { if (trigger.open === trigger.close) { listeners.push(renderer.listen(target, trigger.open, toggleFn)); return; } listeners.push(renderer.listen(target, trigger.open, showFn), renderer.listen(target, trigger.close, hideFn)); })); return (/** * @return {?} */ () => { listeners.forEach((/** * @param {?} unsubscribeFn * @return {?} */ (unsubscribeFn) => unsubscribeFn())); }); } /** * @param {?} renderer * @param {?} options * @return {?} */ function listenToTriggersV2(renderer, options) { /** @type {?} */ const parsedTriggers = parseTriggers(options.triggers); /** @type {?} */ const target = options.target; // do nothing if (parsedTriggers.length === 1 && parsedTriggers[0].isManual()) { return Function.prototype; } // all listeners /* tslint:disable-next-line: no-any */ /** @type {?} */ const listeners = []; // lazy listeners registration /** @type {?} */ const _registerHide = []; /** @type {?} */ const registerHide = (/** * @return {?} */ () => { // add hide listeners to unregister array _registerHide.forEach((/** * @param {?} fn * @return {?} */ (fn) => listeners.push(fn()))); // register hide events only once _registerHide.length = 0; }); // register open\close\toggle listeners parsedTriggers.forEach((/** * @param {?} trigger * @return {?} */ (trigger) => { /** @type {?} */ const useToggle = trigger.open === trigger.close; /** @type {?} */ const showFn = useToggle ? options.toggle : options.show; if (!useToggle) { _registerHide.push((/** * @return {?} */ () => renderer.listen(target, trigger.close, options.hide))); } listeners.push(renderer.listen(target, trigger.open, (/** * @return {?} */ () => showFn(registerHide)))); })); return (/** * @return {?} */ () => { listeners.forEach((/** * @param {?} unsubscribeFn * @return {?} */ (unsubscribeFn) => unsubscribeFn())); }); } /** * @param {?} renderer * @param {?} options * @return {?} */ function registerOutsideClick(renderer, options) { if (!options.outsideClick) { return Function.prototype; } /* tslint:disable-next-line: no-any */ return renderer.listen('document', 'click', (/** * @param {?} event * @return {?} */ (event) => { if (options.target && options.target.contains(event.target)) { return undefined; } if (options.targets && options.targets.some((/** * @param {?} target * @return {?} */ target => target.contains(event.target)))) { return undefined; } options.hide(); })); } /** * @param {?} renderer * @param {?} options * @return {?} */ function registerEscClick(renderer, options) { if (!options.outsideEsc) { return Function.prototype; } return renderer.listen('document', 'keyup.esc', (/** * @param {?} event * @return {?} */ (event) => { if (options.target && options.target.contains(event.target)) { return undefined; } if (options.targets && options.targets.some((/** * @param {?} target * @return {?} */ target => target.contains(event.target)))) { return undefined; } options.hide(); })); } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @license * Copyright Google Inc. All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ /** * JS version of browser APIs. This library can only run in the browser. * @type {?} */ const win = (typeof window !== 'undefined' && window) || (/** @type {?} */ ({})); /** @type {?} */ const document$1 = win.document; /** @type {?} */ const location = win.location; /** @type {?} */ const gc = win.gc ? (/** * @return {?} */ () => win.gc()) : (/** * @return {?} */ () => null); /** @type {?} */ const performance = win.performance ? win.performance : null; /** @type {?} */ const Event = win.Event; /** @type {?} */ const MouseEvent = win.MouseEvent; /** @type {?} */ const KeyboardEvent = win.KeyboardEvent; /** @type {?} */ const EventTarget = win.EventTarget; /** @type {?} */ const History = win.History; /** @type {?} */ const Location = win.Location; /** @type {?} */ const EventListener = win.EventListener; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ let guessedVersion; /** * @return {?} */ function _guessBsVersion() { if (typeof document === 'undefined') { return null; } /** @type {?} */ const spanEl = document.createElement('span'); spanEl.innerText = 'test bs version'; document.body.appendChild(spanEl); spanEl.classList.add('d-none'); /** @type {?} */ const rect = spanEl.getBoundingClientRect(); document.body.removeChild(spanEl); if (!rect) { return 'bs3'; } return rect.top === 0 ? 'bs4' : 'bs3'; } /** * @param {?} theme * @return {?} */ function setTheme(theme) { guessedVersion = theme; } // todo: in ngx-bootstrap, bs4 will became a default one /** * @return {?} */ function isBs3() { if (typeof win === 'undefined') { return true; } if (typeof win.__theme === 'undefined') { if (guessedVersion) { return guessedVersion === 'bs3'; } guessedVersion = _guessBsVersion(); return guessedVersion === 'bs3'; } return win.__theme !== 'bs4'; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @template T */ class LinkedList { constructor() { this.length = 0; this.asArray = []; // Array methods overriding END } /** * @param {?} position * @return {?} */ get(position) { if (this.length === 0 || position < 0 || position >= this.length) { return void 0; } /** @type {?} */ let current = this.head; for (let index = 0; index < position; index++) { current = current.next; } return current.value; } /** * @param {?} value * @param {?=} position * @return {?} */ add(value, position = this.length) { if (position < 0 || position > this.length) { throw new Error('Position is out of the list'); } /* tslint:disable-next-line: no-any*/ /** @type {?} */ const node = { value, next: undefined, previous: undefined }; if (this.length === 0) { this.head = node; this.tail = node; this.current = node; } else { if (position === 0) { // first node node.next = this.head; this.head.previous = node; this.head = node; } else if (position === this.length) { // last node this.tail.next = node; node.previous = this.tail; this.tail = node; } else { // node in middle /** @type {?} */ const currentPreviousNode = this.getNode(position - 1); /** @type {?} */ const currentNextNode = currentPreviousNode.next; currentPreviousNode.next = node; currentNextNode.previous = node; node.previous = currentPreviousNode; node.next = currentNextNode; } } this.length++; this.createInternalArrayRepresentation(); } /** * @param {?=} position * @return {?} */ remove(position = 0) { if (this.length === 0 || position < 0 || position >= this.length) { throw new Error('Position is out of the list'); } if (position === 0) { // first node this.head = this.head.next; if (this.head) { // there is no second node this.head.previous = undefined; } else { // there is no second node this.tail = undefined; } } else if (position === this.length - 1) { // last node this.tail = this.tail.previous; this.tail.next = undefined; } else { // middle node /** @type {?} */ const removedNode = this.getNode(position); removedNode.next.previous = removedNode.previous; removedNode.previous.next = removedNode.next; } this.length--; this.createInternalArrayRepresentation(); } /** * @param {?} position * @param {?} value * @return {?} */ set(position, value) { if (this.length === 0 || position < 0 || position >= this.length) { throw new Error('Position is out of the list'); } /** @type {?} */ const node = this.getNode(position); node.value = value; this.createInternalArrayRepresentation(); } /** * @return {?} */ toArray() { return this.asArray; } /* tslint:disable-next-line: no-any*/ /** * @param {?} fn * @return {?} */ findAll(fn) { /** @type {?} */ let current = this.head; /* tslint:disable-next-line: no-any*/ /** @type {?} */ const result = []; for (let index = 0; index < this.length; index++) { if (fn(current.value, index)) { result.push({ index, value: current.value }); } current = current.next; } return result; } // Array methods overriding start /** * @param {...?} args * @return {?} */ push(...args) { /* tslint:disable-next-line: no-any*/ args.forEach((/** * @param {?} arg * @return {?} */ (arg) => { this.add(arg); })); return this.length; } /** * @return {?} */ pop() { if (this.length === 0) { return undefined; } /** @type {?} */ const last = this.tail; this.remove(this.length - 1); return last.value; } /** * @param {...?} args * @return {?} */ unshift(...args) { args.reverse(); /* tslint:disable-next-line: no-any*/ args.forEach((/** * @param {?} arg * @return {?} */ (arg) => { this.add(arg, 0); })); return this.length; } /** * @return {?} */ shift() { if (this.length === 0) { return undefined; } /** @type {?} */ const lastItem = this.head.value; this.remove(); return lastItem; } /* tslint:disable-next-line: no-any*/ /** * @param {?} fn * @return {?} */ forEach(fn) { /** @type {?} */ let current = this.head; for (let index = 0; index < this.length; index++) { fn(current.value, index); current = current.next; } } /** * @param {?} value * @return {?} */ indexOf(value) { /** @type {?} */ let current = this.head; /** @type {?} */ let position = 0; for (let index = 0; index < this.length; index++) { if (current.value === value) { position = index; break; } current = current.next; } return position; } /* tslint:disable-next-line: no-any*/ /** * @param {?} fn * @return {?} */ some(fn) { /** @type {?} */ let current = this.head; /** @type {?} */ let result = false; while (current && !result) { if (fn(current.value)) { result = true; break; } current = current.next; } return result; } /* tslint:disable-next-line: no-any*/ /** * @param {?} fn * @return {?} */ every(fn) { /** @type {?} */ let current = this.head; /** @type {?} */ let result = true; while (current && result) { if (!fn(current.value)) { result = false; } current = current.next; } return result; } /** * @return {?} */ toString() { return '[Linked List]'; } /* tslint:disable-next-line: no-any*/ /** * @param {?} fn * @return {?} */ find(fn) { /** @type {?} */ let current = this.head; /** @type {?} */ let result; for (let index = 0; index < this.length; index++) { if (fn(current.value, index)) { result = current.value; break; } current = current.next; } return result; } /* tslint:disable-next-line: no-any*/ /** * @param {?} fn * @return {?} */ findIndex(fn) { /** @type {?} */ let current = this.head; /** @type {?} */ let result; for (let index = 0; index < this.length; index++) { if (fn(current.value, index)) { result = index; break; } current = current.next; } return result; } /* tslint:disable-next-line: no-any*/ /** * @protected * @param {?} position * @return {?} */ getNode(position) { if (this.length === 0 || position < 0 || position >= this.length) { throw new Error('Position is out of the list'); } /** @type {?} */ let current = this.head; for (let index = 0; index < position; index++) { current = current.next; } return current; } /** * @protected * @return {?} */ createInternalArrayRepresentation() { /* tslint:disable-next-line: no-any*/ /** @type {?} */ const outArray = []; /** @type {?} */ let current = this.head; while (current) { outArray.push(current.value); current = current.next; } this.asArray = outArray; } } if (false) { /** @type {?} */ LinkedList.prototype.length; /** * @type {?} * @protected */ LinkedList.prototype.head; /** * @type {?} * @protected */ LinkedList.prototype.tail; /** * @type {?} * @protected */ LinkedList.prototype.current; /** * @type {?} * @protected */ LinkedList.prototype.asArray; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /*tslint:disable:no-invalid-this */ /* tslint:disable-next-line: no-any */ /** * @param {?=} defaultValue * @return {?} */ function OnChange(defaultValue) { /** @type {?} */ const sufix = 'Change'; /* tslint:disable-next-line: no-any */ return (/** * @param {?} target * @param {?} propertyKey * @return {?} */ function OnChangeHandler(target, propertyKey) { /** @type {?} */ const _key = ` __${propertyKey}Value`; Object.defineProperty(target, propertyKey, { /* tslint:disable-next-line: no-any */ /** * @return {?} */ get() { return this[_key]; }, /* tslint:disable-next-line: no-any */ /** * @param {?} value * @return {?} */ set(value) { /** @type {?} */ const prevValue = this[_key]; this[_key] = value; if (prevValue !== value && this[propertyKey + sufix]) { this[propertyKey + sufix].emit(value); } } }); }); } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class Utils { /* tslint:disable-next-line: no-any */ /** * @param {?} element * @return {?} */ static reflow(element) { /* tslint:disable-next-line: no-any */ ((/** * @param {?} bs * @return {?} */ (bs) => bs))(element.offsetHeight); } // source: https://github.com/jquery/jquery/blob/master/src/css/var/getStyles.js /* tslint:disable-next-line: no-any */ /** * @param {?} elem * @return {?} */ static getStyles(elem) { // Support: IE <=11 only, Firefox <=30 (#15098, #14150) // IE throws on elements created in popups // FF meanwhile throws on frame elements through "defaultView.getComputedStyle" /** @type {?} */ let view = elem.ownerDocument.defaultView; if (!view || !view.opener) { view = win; } return view.getComputedStyle(elem); } } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ const _messagesHash = {}; /** @type {?} */ const _hideMsg = typeof console === 'undefined' || !('warn' in console); /** * @param {?} msg * @return {?} */ function warnOnce(msg) { if (!isDevMode() || _hideMsg || msg in _messagesHash) { return; } _messagesHash[msg] = true; /*tslint:disable-next-line*/ console.warn(msg); } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ export { LinkedList, OnChange, Trigger, Utils, document$1 as document, isBs3, listenToTriggers, listenToTriggersV2, parseTriggers, registerEscClick, registerOutsideClick, setTheme, warnOnce, win as window }; //# sourceMappingURL=ngx-bootstrap-utils.js.map