@spartacus/storefront
Version:
Spartacus Storefront is a package that you can include in your application, which allows you to add default storefront features.
992 lines (974 loc) • 1.55 MB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/common'), require('@angular/core'), require('@spartacus/core'), require('@angular/router'), require('rxjs/operators'), require('rxjs'), require('@angular/platform-browser'), require('@angular/forms'), require('@ng-bootstrap/ng-bootstrap'), require('@ng-select/ng-select'), require('@ngrx/effects'), require('@ngrx/store'), require('@angular/service-worker'), require('ngx-infinite-scroll'), require('@angular/common/http'), require('@ngrx/router-store')) :
typeof define === 'function' && define.amd ? define('@spartacus/storefront', ['exports', '@angular/common', '@angular/core', '@spartacus/core', '@angular/router', 'rxjs/operators', 'rxjs', '@angular/platform-browser', '@angular/forms', '@ng-bootstrap/ng-bootstrap', '@ng-select/ng-select', '@ngrx/effects', '@ngrx/store', '@angular/service-worker', 'ngx-infinite-scroll', '@angular/common/http', '@ngrx/router-store'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.spartacus = global.spartacus || {}, global.spartacus.storefront = {}), global.ng.common, global.ng.core, global.core, global.ng.router, global.rxjs.operators, global.rxjs, global.ng.platformBrowser, global.ng.forms, global.ngBootstrap, global.ngSelect, global.effects, global.store, global.ng.serviceWorker, global.ngxInfiniteScroll, global.ng.common.http, global.fromNgrxRouter));
}(this, (function (exports, i3, i0, i1, i1$1, operators, rxjs, i1$2, i3$1, i1$3, i1$4, effects, i1$5, i1$6, i6, http, routerStore) { 'use strict';
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () {
return e[k];
}
});
}
});
}
n['default'] = e;
return Object.freeze(n);
}
var i3__namespace = /*#__PURE__*/_interopNamespace(i3);
var i0__namespace = /*#__PURE__*/_interopNamespace(i0);
var i1__namespace = /*#__PURE__*/_interopNamespace(i1);
var i1__namespace$1 = /*#__PURE__*/_interopNamespace(i1$1);
var i1__namespace$2 = /*#__PURE__*/_interopNamespace(i1$2);
var i3__namespace$1 = /*#__PURE__*/_interopNamespace(i3$1);
var i1__namespace$3 = /*#__PURE__*/_interopNamespace(i1$3);
var i1__namespace$4 = /*#__PURE__*/_interopNamespace(i1$4);
var i1__namespace$5 = /*#__PURE__*/_interopNamespace(i1$5);
var i1__namespace$6 = /*#__PURE__*/_interopNamespace(i1$6);
var i6__namespace = /*#__PURE__*/_interopNamespace(i6);
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise */
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b)
if (Object.prototype.hasOwnProperty.call(b, p))
d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
var __assign = function () {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s)
if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __rest(s, e) {
var t = {};
for (var p in s)
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
}
function __decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function __param(paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); };
}
function __metadata(metadataKey, metadataValue) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
return Reflect.metadata(metadataKey, metadataValue);
}
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try {
step(generator.next(value));
}
catch (e) {
reject(e);
} }
function rejected(value) { try {
step(generator["throw"](value));
}
catch (e) {
reject(e);
} }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __generator(thisArg, body) {
var _ = { label: 0, sent: function () { if (t[0] & 1)
throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function () { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f)
throw new TypeError("Generator is already executing.");
while (_)
try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done)
return t;
if (y = 0, t)
op = [op[0] & 2, t.value];
switch (op[0]) {
case 0:
case 1:
t = op;
break;
case 4:
_.label++;
return { value: op[1], done: false };
case 5:
_.label++;
y = op[1];
op = [0];
continue;
case 7:
op = _.ops.pop();
_.trys.pop();
continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
_ = 0;
continue;
}
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
_.label = op[1];
break;
}
if (op[0] === 6 && _.label < t[1]) {
_.label = t[1];
t = op;
break;
}
if (t && _.label < t[2]) {
_.label = t[2];
_.ops.push(op);
break;
}
if (t[2])
_.ops.pop();
_.trys.pop();
continue;
}
op = body.call(thisArg, _);
}
catch (e) {
op = [6, e];
y = 0;
}
finally {
f = t = 0;
}
if (op[0] & 5)
throw op[1];
return { value: op[0] ? op[1] : void 0, done: true };
}
}
var __createBinding = Object.create ? (function (o, m, k, k2) {
if (k2 === undefined)
k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function () { return m[k]; } });
}) : (function (o, m, k, k2) {
if (k2 === undefined)
k2 = k;
o[k2] = m[k];
});
function __exportStar(m, o) {
for (var p in m)
if (p !== "default" && !Object.prototype.hasOwnProperty.call(o, p))
__createBinding(o, m, p);
}
function __values(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m)
return m.call(o);
if (o && typeof o.length === "number")
return {
next: function () {
if (o && i >= o.length)
o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
}
function __read(o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m)
return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
ar.push(r.value);
}
catch (error) {
e = { error: error };
}
finally {
try {
if (r && !r.done && (m = i["return"]))
m.call(i);
}
finally {
if (e)
throw e.error;
}
}
return ar;
}
/** @deprecated */
function __spread() {
for (var ar = [], i = 0; i < arguments.length; i++)
ar = ar.concat(__read(arguments[i]));
return ar;
}
/** @deprecated */
function __spreadArrays() {
for (var s = 0, i = 0, il = arguments.length; i < il; i++)
s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
}
function __spreadArray(to, from, pack) {
if (pack || arguments.length === 2)
for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar)
ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || from);
}
function __await(v) {
return this instanceof __await ? (this.v = v, this) : new __await(v);
}
function __asyncGenerator(thisArg, _arguments, generator) {
if (!Symbol.asyncIterator)
throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n])
i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try {
step(g[n](v));
}
catch (e) {
settle(q[0][3], e);
} }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length)
resume(q[0][0], q[0][1]); }
}
function __asyncDelegator(o) {
var i, p;
return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; }
}
function __asyncValues(o) {
if (!Symbol.asyncIterator)
throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function (v) { resolve({ value: v, done: d }); }, reject); }
}
function __makeTemplateObject(cooked, raw) {
if (Object.defineProperty) {
Object.defineProperty(cooked, "raw", { value: raw });
}
else {
cooked.raw = raw;
}
return cooked;
}
;
var __setModuleDefault = Object.create ? (function (o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function (o, v) {
o["default"] = v;
};
function __importStar(mod) {
if (mod && mod.__esModule)
return mod;
var result = {};
if (mod != null)
for (var k in mod)
if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
__createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
}
function __importDefault(mod) {
return (mod && mod.__esModule) ? mod : { default: mod };
}
function __classPrivateFieldGet(receiver, state, kind, f) {
if (kind === "a" && !f)
throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
}
function __classPrivateFieldSet(receiver, state, value, kind, f) {
if (kind === "m")
throw new TypeError("Private method is not writable");
if (kind === "a" && !f)
throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
}
/** The element attribute used to store the focus state */
var FOCUS_ATTR = 'data-cx-focus';
/** The element attribute used to store the focus group state */
var FOCUS_GROUP_ATTR = 'data-cx-focus-group';
exports.TrapFocus = void 0;
(function (TrapFocus) {
/**
* Will trap the focus at the start of the focus group.
*/
TrapFocus["start"] = "start";
/**
* Will trap the focus only at the end of the focus group.
*/
TrapFocus["end"] = "end";
/**
* Will not trap the focus in both directions. This is actually not are
* a great example of focus trap, but it will give the benefit of keyboard
* tabbing by arrows.
*/
TrapFocus["both"] = "both";
})(exports.TrapFocus || (exports.TrapFocus = {}));
var BaseFocusService = /** @class */ (function () {
function BaseFocusService() {
}
return BaseFocusService;
}());
BaseFocusService.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: BaseFocusService, deps: [], target: i0__namespace.ɵɵFactoryTarget.Injectable });
BaseFocusService.ɵprov = i0__namespace.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: BaseFocusService, providedIn: 'root' });
i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: BaseFocusService, decorators: [{
type: i0.Injectable,
args: [{
providedIn: 'root',
}]
}] });
/**
* Abstract directive that provides a common interface for all focus directives:
* - Block Focus
* - Persist Focus
* - Escape Focus
* - Auto Focus
* - Tab Focus
* - Trap Focus
* - Lock Focus
*/
var BaseFocusDirective = /** @class */ (function () {
function BaseFocusDirective(elementRef, service) {
this.elementRef = elementRef;
this.service = service;
/**
* A default config can be provided for each directive if a specific focus directive
* is used directly. i.e. `<div cxAutoFocus></div>`
*/
this.defaultConfig = {};
}
BaseFocusDirective.prototype.ngOnInit = function () {
this.setDefaultConfiguration();
this.requiredTabindex = -1;
};
// empty, but sub classes might have an implementation
BaseFocusDirective.prototype.ngOnChanges = function (_changes) { };
/**
* Override the (input) config if it undefined or an empty string, with the
* `defaultConfig`. The `defaultConfig` might be specified for each directive
* differently. If a specific directive is used (i.e. `cxAutoFocus`), the
* specific (inherited) defaultConfig will be used.
*/
BaseFocusDirective.prototype.setDefaultConfiguration = function () {
if ((!this.config || this.config === '') && this.defaultConfig) {
this.config = this.defaultConfig;
}
};
Object.defineProperty(BaseFocusDirective.prototype, "host", {
/**
* Helper method to return the host element for the directive
* given by the `elementRef`.
*/
get: function () {
return this.elementRef.nativeElement;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BaseFocusDirective.prototype, "requiredTabindex", {
/**
* Force a tabindex on the host element if it is _required_ to make the element
* focusable. If the element is focusable by nature or by a given tabindex, the
* `tabindex` is not applied.
*
* Buttons, active links, etc. do no need an explicit tabindex to receive focus.
*/
set: function (tabindex) {
if (this.requiresExplicitTabIndex) {
this.tabindex = tabindex;
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(BaseFocusDirective.prototype, "requiresExplicitTabIndex", {
/**
* Returns true if the host element does not have a tabindex defined
* and it also doesn't get focus by browsers nature (i.e. button or
* active link).
*/
get: function () {
return (this.tabindex === undefined &&
['button', 'input', 'select', 'textarea'].indexOf(this.host.tagName.toLowerCase()) === -1 &&
!(this.host.tagName === 'A' &&
(this.host.hasAttribute('href') ||
this.host.hasAttribute('routerlink') ||
this.host.getAttribute('ng-reflect-router-link'))));
},
enumerable: false,
configurable: true
});
return BaseFocusDirective;
}());
BaseFocusDirective.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: BaseFocusDirective, deps: [{ token: i0__namespace.ElementRef }, { token: BaseFocusService }], target: i0__namespace.ɵɵFactoryTarget.Directive });
BaseFocusDirective.ɵdir = i0__namespace.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "12.0.5", type: BaseFocusDirective, inputs: { tabindex: "tabindex" }, host: { properties: { "attr.tabindex": "this.tabindex" } }, usesOnChanges: true, ngImport: i0__namespace });
i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: BaseFocusDirective, decorators: [{
type: i0.Directive
}], ctorParameters: function () { return [{ type: i0__namespace.ElementRef }, { type: BaseFocusService }]; }, propDecorators: { tabindex: [{
type: i0.Input
}, {
type: i0.HostBinding,
args: ['attr.tabindex']
}] } });
/**
* Directive implementation that adds a CSS class to the host element
* when the moused is used to focus an element. As soon as the keyboard
* is used, the class is removed.
*
* This feature must be explicitly enabled with the `disableMouseFocus` config.
*
* The appearance of the visual focus depends on the CSS implementation to
* begin with. Spartacus styles add a blue border around each focusable element.
* This can be considered annoying by keyboard users, as they won't need such a
* strong indication of the selected element.
*/
var VisibleFocusDirective = /** @class */ (function (_super) {
__extends(VisibleFocusDirective, _super);
function VisibleFocusDirective() {
var _this = _super.apply(this, __spreadArray([], __read(arguments))) || this;
_this.defaultConfig = {
disableMouseFocus: true,
};
/** Controls a css class to hide focus visible CSS rules */
_this.mouseFocus = false;
return _this;
}
VisibleFocusDirective.prototype.handleMousedown = function () {
if (this.shouldFocusVisible) {
this.mouseFocus = true;
}
};
VisibleFocusDirective.prototype.handleKeydown = function (event) {
if (this.shouldFocusVisible) {
this.mouseFocus = !this.isNavigating(event);
}
};
Object.defineProperty(VisibleFocusDirective.prototype, "shouldFocusVisible", {
/**
* Indicates whether the configurations setup to disable visual focus.
*/
get: function () {
var _a;
return (_a = this.config) === null || _a === void 0 ? void 0 : _a.disableMouseFocus;
},
enumerable: false,
configurable: true
});
/**
* Indicates whether the event is used to navigate the storefront. Some keyboard events
* are used by mouse users to fill a form or interact with the OS or browser.
*/
VisibleFocusDirective.prototype.isNavigating = function (event) {
// when the cmd or ctrl keys are used, the user doesn't navigate the storefront
if (event.metaKey) {
return false;
}
// when the tab key is used, users are for navigating away from the current (form) element
if (event.code === 'Tab') {
return true;
}
// If the user fill in a form, we don't considering it part of storefront navigation.
if (['INPUT', 'TEXTAREA'].includes(event.target.tagName)) {
return false;
}
return true;
};
return VisibleFocusDirective;
}(BaseFocusDirective));
VisibleFocusDirective.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: VisibleFocusDirective, deps: null, target: i0__namespace.ɵɵFactoryTarget.Directive });
VisibleFocusDirective.ɵdir = i0__namespace.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "12.0.5", type: VisibleFocusDirective, host: { listeners: { "mousedown": "handleMousedown()", "keydown": "handleKeydown($event)" }, properties: { "class.mouse-focus": "this.mouseFocus" } }, usesInheritance: true, ngImport: i0__namespace });
i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: VisibleFocusDirective, decorators: [{
type: i0.Directive
}], propDecorators: { mouseFocus: [{
type: i0.HostBinding,
args: ['class.mouse-focus']
}], handleMousedown: [{
type: i0.HostListener,
args: ['mousedown']
}], handleKeydown: [{
type: i0.HostListener,
args: ['keydown', ['$event']]
}] } });
// { selector: '[cxBlockFocus]' }
var BlockFocusDirective = /** @class */ (function (_super) {
__extends(BlockFocusDirective, _super);
function BlockFocusDirective(elementRef, service) {
var _this = _super.call(this, elementRef, service) || this;
_this.elementRef = elementRef;
_this.service = service;
_this.defaultConfig = { block: true };
// @Input('cxBlockFocus')
_this.config = {};
return _this;
}
BlockFocusDirective.prototype.ngOnInit = function () {
_super.prototype.ngOnInit.call(this);
if (this.config.block) {
this.tabindex = -1;
}
};
return BlockFocusDirective;
}(VisibleFocusDirective));
BlockFocusDirective.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: BlockFocusDirective, deps: [{ token: i0__namespace.ElementRef }, { token: BaseFocusService }], target: i0__namespace.ɵɵFactoryTarget.Directive });
BlockFocusDirective.ɵdir = i0__namespace.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "12.0.5", type: BlockFocusDirective, usesInheritance: true, ngImport: i0__namespace });
i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: BlockFocusDirective, decorators: [{
type: i0.Directive
}], ctorParameters: function () { return [{ type: i0__namespace.ElementRef }, { type: BaseFocusService }]; } });
var GLOBAL_GROUP = '_g_';
/**
* Shared service to persist the focus for an element or a group
* of elements. The persisted element focus can be used to persist
* the focus for a DOM tree, so that the focus remains after a repaint
* or reoccurs when a DOM tree is "unlocked".
*/
var PersistFocusService = /** @class */ (function (_super) {
__extends(PersistFocusService, _super);
function PersistFocusService() {
var _this = _super.apply(this, __spreadArray([], __read(arguments))) || this;
// this is going to fail as we have sub services. They will al have their own map.
// We must bring this to a singleton map.
_this.focus = new Map();
return _this;
}
PersistFocusService.prototype.get = function (group) {
return this.focus.get(group || GLOBAL_GROUP);
};
/**
* Persist the keyboard focus state for the given key. The focus is stored globally
* or for the given group.
*/
PersistFocusService.prototype.set = function (key, group) {
if (key) {
this.focus.set(group || GLOBAL_GROUP, key);
}
};
/**
* Clears the persisted keyboard focus state globally or for the given group.
*/
PersistFocusService.prototype.clear = function (group) {
this.focus.delete(group || GLOBAL_GROUP);
};
/**
* Returns the group for the host element based on the configured group or
* by the `data-cx-focus-group` attribute stored on the host.
*/
PersistFocusService.prototype.getPersistenceGroup = function (host, config) {
return (config === null || config === void 0 ? void 0 : config.group) ? config.group : host.getAttribute(FOCUS_GROUP_ATTR);
};
return PersistFocusService;
}(BaseFocusService));
PersistFocusService.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: PersistFocusService, deps: null, target: i0__namespace.ɵɵFactoryTarget.Injectable });
PersistFocusService.ɵprov = i0__namespace.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: PersistFocusService, providedIn: 'root' });
i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: PersistFocusService, decorators: [{
type: i0.Injectable,
args: [{
providedIn: 'root',
}]
}] });
/**
* Directive that provides persistence of the focused state. This is useful
* when a group of focusable elements got refocused or even recreated. That
* happens often when the DOM is constructed with an `*ngIf` or `*ngFor`.
*
* The focus state is based on a configured _key_, which can be passed in the
* config input, either by using a string primitive or `PersistFocusConfig.key`:
*
* ```html
* <button cxPersistFocus="myKey"></button>
* <button cxFocus="myKey"></button>
* <button [cxFocus]="{{key:'myKey'}"></button>
* ```
*
* The focus state can be part of a focus _group_, so that the state is shared
* and remember for the given group. In order to detect the persistence for a
* given element, we store the persistence key as a data attribute (`data-cx-focus`):
*
* ```html
* <button data-cx-focus="myKey"></button>
* ```
*
* Other keyboard focus directives can read the key to understand whether the element
* should retrieve focus.
*
*/
var PersistFocusDirective = /** @class */ (function (_super) {
__extends(PersistFocusDirective, _super);
function PersistFocusDirective(elementRef, service) {
var _this = _super.call(this, elementRef, service) || this;
_this.elementRef = elementRef;
_this.service = service;
_this.defaultConfig = {};
/**
* The persistence key can be passed directly or through the `FocusConfig.key`.
* While this could be considered a global key, the likeliness of conflicts
* is very small since the key is cleared when the focus is changed.
*/
// @Input('cxPersistFocus')
_this.config = {};
return _this;
}
PersistFocusDirective.prototype.handleFocus = function (event) {
this.service.set(this.key, this.group);
event === null || event === void 0 ? void 0 : event.preventDefault();
event === null || event === void 0 ? void 0 : event.stopPropagation();
};
PersistFocusDirective.prototype.ngOnInit = function () {
_super.prototype.ngOnInit.call(this);
this.attr = this.key ? this.key : undefined;
};
PersistFocusDirective.prototype.setDefaultConfiguration = function () {
if (typeof this.config === 'string' && this.config !== '') {
this.config = { key: this.config };
}
_super.prototype.setDefaultConfiguration.call(this);
};
/**
* Focus the element explicitly if it was focused before.
*/
PersistFocusDirective.prototype.ngAfterViewInit = function () {
if (this.isPersisted) {
this.host.focus({ preventScroll: true });
}
};
Object.defineProperty(PersistFocusDirective.prototype, "isPersisted", {
get: function () {
return !!this.key && this.service.get(this.group) === this.key;
},
enumerable: false,
configurable: true
});
Object.defineProperty(PersistFocusDirective.prototype, "key", {
/**
* Returns the key for the host element, which is used to persist the
* focus state. This is useful in cases where the DOM is rebuild.
*/
get: function () {
var _a;
return (_a = this.config) === null || _a === void 0 ? void 0 : _a.key;
},
enumerable: false,
configurable: true
});
Object.defineProperty(PersistFocusDirective.prototype, "group", {
/**
* returns the persistence group (if any) for the focusable elements.
*/
get: function () {
return this.service.getPersistenceGroup(this.host, this.config);
},
enumerable: false,
configurable: true
});
return PersistFocusDirective;
}(BlockFocusDirective));
PersistFocusDirective.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: PersistFocusDirective, deps: [{ token: i0__namespace.ElementRef }, { token: PersistFocusService }], target: i0__namespace.ɵɵFactoryTarget.Directive });
PersistFocusDirective.ɵdir = i0__namespace.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "12.0.5", type: PersistFocusDirective, host: { listeners: { "focus": "handleFocus($event)" }, properties: { "attr.data-cx-focus": "this.attr" } }, usesInheritance: true, ngImport: i0__namespace });
i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: PersistFocusDirective, decorators: [{
type: i0.Directive
}], ctorParameters: function () { return [{ type: i0__namespace.ElementRef }, { type: PersistFocusService }]; }, propDecorators: { attr: [{
type: i0.HostBinding,
args: ["attr." + FOCUS_ATTR]
}], handleFocus: [{
type: i0.HostListener,
args: ['focus', ['$event']]
}] } });
var SelectFocusUtility = /** @class */ (function () {
function SelectFocusUtility() {
/**
* Query selectors used to query focusable child elements of the host element.
* The selectors are supplemented with `:not([disabled])` and `:not([hidden])`.
*/
this.focusableSelectors = [
'a[href]',
'button',
'[tabindex]',
'input',
'select',
'textarea',
];
// like to leave out the following as we don't use it, and make this list exensible.
// `[contentEditable=true]`, // very unlikely to suport as we're not a business tool
// `iframe`, // we really don't like iframes...
// `area[href]`, // very debatable!
this.focusableSelectorSuffix = ':not([disabled]):not([hidden])';
}
SelectFocusUtility.prototype.query = function (host, selector) {
if (!selector || selector === '') {
return [];
}
return Array.from(host.querySelectorAll(selector));
};
SelectFocusUtility.prototype.findFirstFocusable = function (host, config) {
var _this = this;
if (config === void 0) { config = { autofocus: true }; }
var selector = typeof (config === null || config === void 0 ? void 0 : config.autofocus) === 'string' ? config.autofocus : '[autofocus]';
// fallback to first focusable
return (this.query(host, selector).find(function (el) { return !_this.isHidden(el); }) ||
this.findFocusable(host).find(function (el) { return Boolean(el); }));
};
/**
* returns all focusable child elements of the host element. The element selectors
* are build from the `focusableSelectors`.
*
* @param host the `HTMLElement` used to query focusable elements
* @param locked indicates whether inactive (`tabindex="-1"`) focusable elements should be returned
* @param invisible indicates whether hidden focusable elements should be returned
*/
SelectFocusUtility.prototype.findFocusable = function (host, locked, invisible) {
var _this = this;
if (locked === void 0) { locked = false; }
if (invisible === void 0) { invisible = false; }
var suffix = this.focusableSelectorSuffix;
if (!locked) {
suffix += ":not([tabindex='-1'])";
}
var selector = this.focusableSelectors
.map(function (s) { return (s += suffix); })
.join(',');
return this.query(host, selector).filter(function (el) { return !invisible ? !_this.isHidden(el) : Boolean(el); });
};
/**
* Indicates whether the element is hidden by CSS. There are various CSS rules and
* HTML structures which can lead to an hidden or invisible element. An `offsetParent`
* of null indicates that the element or any of it's decendants is hidden (`display:none`).
*
* Oother techniques use the visibility (`visibility: hidden`), opacity (`opacity`) or
* phyisical location on the element itself or any of it's anchestor elements. Those
* technique require to work with the _computed styles_, which will cause a performance
* downgrade. We don't do this in the standard implementaton.
*/
SelectFocusUtility.prototype.isHidden = function (el) {
return el.offsetParent === null;
};
return SelectFocusUtility;
}());
SelectFocusUtility.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: SelectFocusUtility, deps: [], target: i0__namespace.ɵɵFactoryTarget.Injectable });
SelectFocusUtility.ɵprov = i0__namespace.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: SelectFocusUtility, providedIn: 'root' });
i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: SelectFocusUtility, decorators: [{
type: i0.Injectable,
args: [{
providedIn: 'root',
}]
}] });
var EscapeFocusService = /** @class */ (function (_super) {
__extends(EscapeFocusService, _super);
function EscapeFocusService(selectFocusUtil) {
var _this = _super.call(this) || this;
_this.selectFocusUtil = selectFocusUtil;
return _this;
}
EscapeFocusService.prototype.shouldFocus = function (config) {
return !!(config === null || config === void 0 ? void 0 : config.focusOnEscape);
};
EscapeFocusService.prototype.handleEscape = function (host, config, event) {
var _a;
if (this.shouldFocus(config)) {
if (host !== event.target) {
host.focus({ preventScroll: true });
event.preventDefault();
event.stopPropagation();
}
else {
if (config === null || config === void 0 ? void 0 : config.focusOnDoubleEscape) {
(_a = this.selectFocusUtil
.findFirstFocusable(host, { autofocus: true })) === null || _a === void 0 ? void 0 : _a.focus();
}
}
}
};
return EscapeFocusService;
}(PersistFocusService));
EscapeFocusService.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: EscapeFocusService, deps: [{ token: SelectFocusUtility }], target: i0__namespace.ɵɵFactoryTarget.Injectable });
EscapeFocusService.ɵprov = i0__namespace.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: EscapeFocusService, providedIn: 'root' });
i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: EscapeFocusService, decorators: [{
type: i0.Injectable,
args: [{
providedIn: 'root',
}]
}], ctorParameters: function () { return [{ type: SelectFocusUtility }]; } });
/**
* Directive to focus the host element whenever the `escape` key is captured.
* UiEvents bubble up by nature, which is why the `cxEscGroup` can be used
* on a tree of elements. Each time the escape key is used, the focus will
* move up in the DOM tree.
*
*/
var EscapeFocusDirective = /** @class */ (function (_super) {
__extends(EscapeFocusDirective, _super);
function EscapeFocusDirective(elementRef, service) {
var _this = _super.call(this, elementRef, service) || this;
_this.elementRef = elementRef;
_this.service = service;
_this.defaultConfig = { focusOnEscape: true };
_this.esc = new i0.EventEmitter();
return _this;
}
/**
* Handles the escape key event.
* @param event the native keyboard event which contains the escape keydown event
*/
EscapeFocusDirective.prototype.handleEscape = function (event) {
if (this.service.shouldFocus(this.config)) {
this.service.handleEscape(this.host, this.config, event);
}
this.esc.emit(this.service.shouldFocus(this.config));
};
EscapeFocusDirective.prototype.ngOnInit = function () {
if (this.service.shouldFocus(this.config)) {
this.requiredTabindex = -1;
}
_super.prototype.ngOnInit.call(this);
};
return EscapeFocusDirective;
}(PersistFocusDirective));
EscapeFocusDirective.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: EscapeFocusDirective, deps: [{ token: i0__namespace.ElementRef }, { token: EscapeFocusService }], target: i0__namespace.ɵɵFactoryTarget.Directive });
EscapeFocusDirective.ɵdir = i0__namespace.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "12.0.5", type: EscapeFocusDirective, outputs: { esc: "esc" }, host: { listeners: { "keydown.escape": "handleEscape($event)" } }, usesInheritance: true, ngImport: i0__namespace });
i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: EscapeFocusDirective, decorators: [{
type: i0.Directive
}], ctorParameters: function () { return [{ type: i0__namespace.ElementRef }, { type: EscapeFocusService }]; }, propDecorators: { esc: [{
type: i0.Output
}], handleEscape: [{
type: i0.HostListener,
args: ['keydown.escape', ['$event']]
}] } });
var AutoFocusService = /** @class */ (function (_super) {
__extends(AutoFocusService, _super);
function AutoFocusService() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Returns the first focusable child element of the host element.
*/
AutoFocusService.prototype.findFirstFocusable = function (host, config) {
if (config === void 0) { config = { autofocus: true }; }
if ((config === null || config === void 0 ? void 0 : config.autofocus) === ':host') {
return host;
}
else if (this.hasPersistedFocus(host, config)) {
return this.getPersisted(host, this.getPersistenceGroup(host, config));
}
else {
return this.selectFocusUtil.findFirstFocusable(host, config) || host;
}
};
/**
* Indicates whether any of the focusable child elements is focused.
*/
AutoFocusService.prototype.hasPersistedFocus = function (host, config) {
return !!this.getPersisted(host, this.getPersistenceGroup(host, config));
};
/**
* Returns the element that has a persisted focus state.
*
* @param host the `HTMLElement` used to query for focusable children
* @param group the optional group for the persistent state, to separate different focus
* groups and remain the persistence
*/
AutoFocusService.prototype.getPersisted = function (host, group) {
if (!this.get(group)) {
return;
}
var focussed = Array.from(host.querySelectorAll("[" + FOCUS_ATTR + "='" + this.get(group) + "']"));
return focussed.length > 0 ? focussed[0] : null;
};
return AutoFocusService;
}(EscapeFocusService));
AutoFocusService.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: AutoFocusService, deps: null, target: i0__namespace.ɵɵFactoryTarget.Injectable });
AutoFocusService.ɵprov = i0__namespace.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: AutoFocusService, providedIn: 'root' });
i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: AutoFocusService, decorators: [{
type: i0.Injectable,
args: [{
providedIn: 'root',
}]
}] });
/**
* Directive that focus the first nested _focusable_ element based on state and configuration:
*
* 1. focusable element that was left in a focused state (aka _persisted_ focus)
* 2. focusable element selected by configured CSS selector (i.e. 'button[type=submit]')
* 3. focusable element marked with the native HTML5 `autofocus` attribute
* 4. first focusable element
* 5. the host element, in case the configured CSS selector is `:host`.
*
* Example configurations:
*
* `<div cxAutoFocus>[...]</div>`
*
* `<div [cxAutoFocus]="{autofocus: false}">[...]</div>`
*
* `<div [cxAutoFocus]="{autofocus: 'button.active'}">[...]</div>`
*
* `<div [cxAutoFocus]="{autofocus: ':host'}">[...]</div>`
*
* When your element is added dynamically (ie. by using an *ngIf or after a DOM change), the
* focus can be refreshed by using the refreshFocus configuration.
*/
var AutoFocusDirective = /** @class */ (function (_super) {
__extends(AutoFocusDirective, _super);
function AutoFocusDirective(elementRef, service) {
var _this = _super.call(this, elementRef, service) || this;