UNPKG

@douglas-serena/ng-inputs

Version:
1,054 lines (1,042 loc) 94.2 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common'), require('imask'), require('simple-mask-money'), require('@douglas-serena/calc.js'), require('@angular/forms'), require('@angular/common/http'), require('dayjs')) : typeof define === 'function' && define.amd ? define('@douglas-serena/ng-inputs', ['exports', '@angular/core', '@angular/common', 'imask', 'simple-mask-money', '@douglas-serena/calc.js', '@angular/forms', '@angular/common/http', 'dayjs'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global['douglas-serena'] = global['douglas-serena'] || {}, global['douglas-serena']['ng-inputs'] = {}), global.ng.core, global.ng.common, global.IMask, global.SimpleMaskMoney, global.calcJs, global.ng.forms, global.ng.common.http, global.dayjs)); }(this, (function (exports, i0, i2, IMask, SimpleMaskMoney, calcJs, forms, http, dayjs) { 'use strict'; function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } 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 i0__namespace = /*#__PURE__*/_interopNamespace(i0); var i2__namespace = /*#__PURE__*/_interopNamespace(i2); var IMask__default = /*#__PURE__*/_interopDefaultLegacy(IMask); var SimpleMaskMoney__default = /*#__PURE__*/_interopDefaultLegacy(SimpleMaskMoney); var calcJs__default = /*#__PURE__*/_interopDefaultLegacy(calcJs); var dayjs__namespace = /*#__PURE__*/_interopNamespace(dayjs); var NgInputConfigService = /** @class */ (function () { function NgInputConfigService() { this._field = { type: 'group', alignIcons: 'left', icons: { password: { align: 'right', clickable: true, }, }, }; this._theme = 'bootstrap'; this._currency = { allowNegative: true, negativeSignAfter: false, prefix: '', suffix: '', fixed: true, fractionDigits: 2, decimalSeparator: ',', thousandsSeparator: '.', align: 'right', cursor: 'end', }; this._percent = { allowNegative: true, negativeSignAfter: false, prefix: '', suffix: '%', fixed: true, fractionDigits: 2, decimalSeparator: '.', thousandsSeparator: '', align: 'right', cursor: 'end', }; this._environments = { debug: false, url: 'http://localhost:4200', }; } Object.defineProperty(NgInputConfigService.prototype, "field", { get: function () { return this._field; }, set: function (value) { this._field = Object.assign(Object.assign(Object.assign({}, this._field), value), { icons: Object.assign(Object.assign({}, this._field.icons), value.icons) }); }, enumerable: false, configurable: true }); Object.defineProperty(NgInputConfigService.prototype, "theme", { get: function () { return this._theme; }, set: function (value) { this._theme = value; }, enumerable: false, configurable: true }); Object.defineProperty(NgInputConfigService.prototype, "currency", { get: function () { return this._currency; }, set: function (value) { var _a, _b; this._currency = Object.assign(Object.assign(Object.assign({}, this._currency), value), { suffix: (_a = value.suffix) === null || _a === void 0 ? void 0 : _a.trim(), prefix: (_b = value.prefix) === null || _b === void 0 ? void 0 : _b.trim() }); }, enumerable: false, configurable: true }); Object.defineProperty(NgInputConfigService.prototype, "percent", { get: function () { return this._percent; }, set: function (value) { var _a, _b; this._percent = Object.assign(Object.assign(Object.assign({}, this._percent), value), { suffix: (_a = value.suffix) === null || _a === void 0 ? void 0 : _a.trim(), prefix: (_b = value.prefix) === null || _b === void 0 ? void 0 : _b.trim() }); }, enumerable: false, configurable: true }); Object.defineProperty(NgInputConfigService.prototype, "environments", { get: function () { return this._environments; }, set: function (value) { this._environments = Object.assign(Object.assign({}, this._environments), value); }, enumerable: false, configurable: true }); return NgInputConfigService; }()); NgInputConfigService.ɵprov = i0__namespace.ɵɵdefineInjectable({ factory: function NgInputConfigService_Factory() { return new NgInputConfigService(); }, token: NgInputConfigService, providedIn: "root" }); NgInputConfigService.decorators = [ { type: i0.Injectable, args: [{ providedIn: 'root', },] } ]; var TEL = { mask: [{ mask: '(00) 0000-0000' }, { mask: '(00) 0 0000-0000' }], }; var CPF = { mask: '000.000.000-00', }; var CNPJ = { mask: '00.000.000/0000-00', }; var CPF_CNPJ = { mask: [CPF, CNPJ], }; var RG = { mask: '00.000.000-0', }; var ESTADUAL = { mask: '00.0.000.0000000-0', }; var RG_ESTADUAL = { mask: [RG, ESTADUAL], }; var ZIPCODE = { mask: '00000-000', }; var typesCustom = [ 'ZIPCODE', 'TEL', 'RG', 'RG_ESTADUAL', 'CPF_CNPJ', 'CNPJ', 'CPF', 'ESTADUAL', ]; var MASKS = { ZIPCODE: ZIPCODE, TEL: TEL, RG: RG, RG_ESTADUAL: RG_ESTADUAL, CPF_CNPJ: CPF_CNPJ, CNPJ: CNPJ, CPF: CPF, ESTADUAL: ESTADUAL, typesCustom: typesCustom, }; var NgInputMasksService = /** @class */ (function () { function NgInputMasksService(configService, datePipe) { this.configService = configService; this.datePipe = datePipe; } NgInputMasksService.prototype.format = function (value, masksType, options) { masksType = masksType.toUpperCase(); if (masksType === 'DATE') { return this.datePipe.transform(value, (options === null || options === void 0 ? void 0 : options.mask) === undefined ? 'dd/MM/yyyy' : options.mask); } if (masksType === 'CURRENCY' || masksType === 'PERCENT') { var config = this.configService[masksType.toLowerCase()]; if ((options === null || options === void 0 ? void 0 : options.allowNegative) !== undefined) config.allowNegative = options.allowNegative; value = calcJs__default['default'](("" + value).replace(/[,.]/g, '.')) .toString() .replace('.', config.decimalSeparator) .replace(config.decimalSeparator + '00', ''); return SimpleMaskMoney__default['default'].formatToCurrency(value, config); } if (masksType === 'AMOUNT') { value = "" + value; if (/^0/g.test(value)) { value = value.replace(/0+/, ''); } var result = Number(value); if (Number.isNaN(result)) { result = 0; } return result.toFixed(3); } var mask = null; if (masksType) mask = MASKS[masksType]; if (options === null || options === void 0 ? void 0 : options.mask) { mask = { mask: options === null || options === void 0 ? void 0 : options.mask.split('|').map(function (mask) { return ({ mask: mask, }); }), }; } return IMask__default['default'].pipe(value, mask); }; NgInputMasksService.prototype.set = function (element, masksType, options) { masksType = masksType.toUpperCase(); if (masksType === 'CURRENCY' || masksType === 'PERCENT') { var config = this.configService[masksType.toLowerCase()]; if ((options === null || options === void 0 ? void 0 : options.allowNegative) !== undefined) config.allowNegative = options.allowNegative; return SimpleMaskMoney__default['default'].setMask(element, config); } var mask = null; if (masksType) { mask = MASKS[masksType]; } if (options === null || options === void 0 ? void 0 : options.mask) { mask = { mask: options === null || options === void 0 ? void 0 : options.mask.split('|').map(function (mask) { return ({ mask: mask, }); }), }; } return IMask__default['default'](element, mask); }; return NgInputMasksService; }()); NgInputMasksService.ɵprov = i0__namespace.ɵɵdefineInjectable({ factory: function NgInputMasksService_Factory() { return new NgInputMasksService(i0__namespace.ɵɵinject(NgInputConfigService), i0__namespace.ɵɵinject(i2__namespace.DatePipe)); }, token: NgInputMasksService, providedIn: "root" }); NgInputMasksService.decorators = [ { type: i0.Injectable, args: [{ providedIn: 'root', },] } ]; NgInputMasksService.ctorParameters = function () { return [ { type: NgInputConfigService }, { type: i2.DatePipe } ]; }; /*! ***************************************************************************** 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; } var SelectCustomControlValueAccessor = /** @class */ (function (_super) { __extends(SelectCustomControlValueAccessor, _super); function SelectCustomControlValueAccessor(_controlContainer, elementRef, renderer, _configService, _changeDetectorRef) { var _this = _super.call(this, renderer, elementRef) || this; _this._controlContainer = _controlContainer; _this.elementRef = elementRef; _this.renderer = renderer; _this._configService = _configService; _this._changeDetectorRef = _changeDetectorRef; _this.formControlName = ''; _this.disabled = false; _this.change = new i0.EventEmitter(); _this.blur = new i0.EventEmitter(); _this.focus = new i0.EventEmitter(); _this.placeholder = ''; _this.label = ''; _this._cols = { default: 12, sm: 12, }; _this._field = null; _this.readonly = false; _this.errors = {}; _this.time = 0; _this.readonly = false; return _this; } Object.defineProperty(SelectCustomControlValueAccessor.prototype, "_placeholder", { get: function () { return this.field === 'floating' && !!this.label && this.placeholder.length === 0 ? false : this.placeholder; }, enumerable: false, configurable: true }); Object.defineProperty(SelectCustomControlValueAccessor.prototype, "cols", { set: function (cols) { this._cols = Object.assign(Object.assign({}, this._cols), cols); }, enumerable: false, configurable: true }); Object.defineProperty(SelectCustomControlValueAccessor.prototype, "field", { get: function () { return this._field ? this._field : this._configService.field.type; }, set: function (value) { this._field = value; }, enumerable: false, configurable: true }); Object.defineProperty(SelectCustomControlValueAccessor.prototype, "control", { get: function () { var _a, _b; return (this.formControl || ((_b = (_a = this._controlContainer) === null || _a === void 0 ? void 0 : _a.control) === null || _b === void 0 ? void 0 : _b.get(this.formControlName))); }, enumerable: false, configurable: true }); Object.defineProperty(SelectCustomControlValueAccessor.prototype, "classCols", { get: function () { var className = this._configService.theme === 'bootstrap' ? "col-" + this._cols.default : "col"; if (this._cols.lg) className += this._configService.theme === 'bootstrap' ? " col-lg-" + this._cols.lg : " l" + this._cols.lg; if (this._cols.md) className += this._configService.theme === 'bootstrap' ? " col-md-" + this._cols.md : " m" + this._cols.md; if (this._cols.sm) className += this._configService.theme === 'bootstrap' ? " col-sm-" + this._cols.sm : " s" + this._cols.sm; return className; }, enumerable: false, configurable: true }); SelectCustomControlValueAccessor.prototype.ngOnInit = function () { this.ngOnInitSuper(); if (this.disabled) this.control.disable(); else this.control.enable(); }; SelectCustomControlValueAccessor.prototype.ngOnInitSuper = function () { if (this.name === undefined) this.name = this.formControlName; this.validRequired(); }; SelectCustomControlValueAccessor.prototype.getMultiLabels = function (labels, label) { var lastLabel = label.concat([]); var rest = label.splice(1, label.length - 1); return lastLabel.length === 1 ? labels === null || labels === void 0 ? void 0 : labels[lastLabel[0]] : this.getMultiLabels(labels === null || labels === void 0 ? void 0 : labels[lastLabel[0]], rest); }; SelectCustomControlValueAccessor.prototype.getKeys = function (errors) { return Object.keys(errors); }; SelectCustomControlValueAccessor.prototype.getError = function (key) { var _a, _b, _c; return ((_b = (_a = this.control) === null || _a === void 0 ? void 0 : _a.errors) === null || _b === void 0 ? void 0 : _b[key]) && ((_c = this.control) === null || _c === void 0 ? void 0 : _c.touched); }; SelectCustomControlValueAccessor.prototype.validRequired = function () { var _a, _b, _c; var value = this.control.value; (_a = this === null || this === void 0 ? void 0 : this.onWrite) === null || _a === void 0 ? void 0 : _a.call(this, null); this._changeDetectorRef.detectChanges(); if (this.required === undefined) { this.required = (_b = this.control.errors) === null || _b === void 0 ? void 0 : _b.required; } (_c = this === null || this === void 0 ? void 0 : this.onWrite) === null || _c === void 0 ? void 0 : _c.call(this, value); this._changeDetectorRef.detectChanges(); }; SelectCustomControlValueAccessor.prototype.registerOnTouched = function (fn) { var _a; (_a = this.formControlDirective.valueAccessor) === null || _a === void 0 ? void 0 : _a.registerOnTouched(fn); }; SelectCustomControlValueAccessor.prototype.registerOnChange = function (fn) { this.onWrite = fn; }; SelectCustomControlValueAccessor.prototype.writeValue = function (obj) { var _this = this; clearTimeout(this.time); this.time = setTimeout(function () { _this.onWrite(obj); }); }; SelectCustomControlValueAccessor.prototype.setDisabledState = function (isDisabled) { this.disabled = isDisabled; }; return SelectCustomControlValueAccessor; }(forms.SelectControlValueAccessor)); SelectCustomControlValueAccessor.decorators = [ { type: i0.Component, args: [{ selector: '', template: '' },] } ]; SelectCustomControlValueAccessor.ctorParameters = function () { return [ { type: forms.ControlContainer }, { type: i0.ElementRef }, { type: i0.Renderer2 }, { type: NgInputConfigService }, { type: i0.ChangeDetectorRef } ]; }; SelectCustomControlValueAccessor.propDecorators = { formControlDirective: [{ type: i0.ViewChild, args: [forms.FormControlDirective, { static: true },] }], formControl: [{ type: i0.Input }], formControlName: [{ type: i0.Input }], name: [{ type: i0.Input }], disabled: [{ type: i0.Input }], help: [{ type: i0.Input }], change: [{ type: i0.Output }], blur: [{ type: i0.Output }], focus: [{ type: i0.Output }], placeholder: [{ type: i0.Input }], label: [{ type: i0.Input }], cols: [{ type: i0.Input }], field: [{ type: i0.Input }], readonly: [{ type: i0.Input }], required: [{ type: i0.Input }], errors: [{ type: i0.Input }], classCols: [{ type: i0.HostBinding, args: ['class',] }] }; var NgSearchComponent = /** @class */ (function (_super) { __extends(NgSearchComponent, _super); function NgSearchComponent(controlContainer, elementRef, renderer, httpClient, configService, changeDetectorRef) { var _this = _super.call(this, controlContainer, elementRef, renderer, configService, changeDetectorRef) || this; _this.controlContainer = controlContainer; _this.elementRef = elementRef; _this.renderer = renderer; _this.httpClient = httpClient; _this.configService = configService; _this.notFound = 'Sem resultado.'; _this.pathLabel = 'label'; _this.value = null; _this.options = []; _this.uri = null; _this.responseData = null; _this.return = null; _this.loading = false; _this.focused = false; _this.timeInput = 0; _this.timeBlur = 0; _this.blur = new i0.EventEmitter(); _this.timeFocus = 0; _this.focus = new i0.EventEmitter(); return _this; } NgSearchComponent.prototype.onInput = function (_b) { var _this = this; var target = _b.target; clearTimeout(this.timeInput); this.timeInput = setTimeout(function () { return __awaiter(_this, void 0, void 0, function () { var value, uri, response, error_1; return __generator(this, function (_b) { switch (_b.label) { case 0: value = target.value; if (value.length === 0) return [2 /*return*/]; if (!this.focused) this.focused = true; if (!this.uri) return [3 /*break*/, 5]; uri = this.createUrl(this.uri); uri = uri === null || uri === void 0 ? void 0 : uri.replace('{value}', value); this.loading = true; _b.label = 1; case 1: _b.trys.push([1, 3, , 4]); return [4 /*yield*/, this.httpClient.get(uri).toPromise()]; case 2: response = _b.sent(); this.options = this.responseData ? this.getMultiLabels(response, this.responseData.split('.')) : response; this.format(); return [3 /*break*/, 4]; case 3: error_1 = _b.sent(); if (!this.configService.environments.debug) { console.log(error_1); } return [3 /*break*/, 4]; case 4: this.loading = false; _b.label = 5; case 5: return [2 /*return*/]; } }); }); }, 300); }; NgSearchComponent.prototype.createUrl = function (uri) { var _this = this; var variables = uri.match(/(\{[\w\_]+\})+/g); variables === null || variables === void 0 ? void 0 : variables.forEach(function (variable) { var key = variable.replace(/([\{\}])+/g, ''); var env = _this.configService.environments[key]; if (env) uri = uri === null || uri === void 0 ? void 0 : uri.replace(variable, env); }); return uri; }; NgSearchComponent.prototype.handleKeyDown = function (event) { var _this = this; var key = event.key; var keys = { ArrowUp: function () { event.preventDefault(); if (!_this.focused) { _this.focused = true; return; } var index = _this.options.findIndex(function (option) { return option.dssSelect; }); if (index === -1) { _this.options[_this.options.length - 1].dssSelect = true; return; } _this.options[index].dssSelect = false; if (index === 0) { _this.options[_this.options.length - 1].dssSelect = true; return; } _this.options[index - 1].dssSelect = true; }, ArrowDown: function () { event.preventDefault(); if (!_this.focused) { _this.focused = true; return; } var index = _this.options.findIndex(function (option) { return option.dssSelect; }); if (index === -1) { _this.options[0].dssSelect = true; } _this.options[index].dssSelect = false; if (index === _this.options.length - 1) { _this.options[0].dssSelect = true; return; } _this.options[index + 1].dssSelect = true; }, Enter: function () { event.preventDefault(); if (!_this.focused) { _this.focused = true; return; } var index = _this.options.findIndex(function (option) { return option.dssSelect; }); if (index !== -1) { _this.focused = false; _this.itemSelect = index; _this.inputChange(_this.options[index]); } }, }; try { keys[key](); } catch (_a) { } }; NgSearchComponent.prototype.ngOnInit = function () { var _a; this.required = (_a = this.control.errors) === null || _a === void 0 ? void 0 : _a.required; if (!this.required) this.required = Object.keys(this.errors).includes('required'); this.format(); }; NgSearchComponent.prototype.onBlur = function (event) { var _this = this; clearTimeout(this.timeBlur); this.timeBlur = setTimeout(function () { _this.focused = false; _this.blur.emit(event); }, 300); }; NgSearchComponent.prototype.onFocus = function (event) { var _this = this; this.onInput(event); clearTimeout(this.timeFocus); this.timeFocus = setTimeout(function () { _this.focused = true; _this.focus.emit(event); }, 100); }; NgSearchComponent.prototype.ngOnChanges = function (_b) { var options = _b.options, value = _b.value; if (!!options && !!options.currentValue) { this.format(); } if (!!value && !!value.currentValue) { this.inputChange(this.value); } }; NgSearchComponent.prototype.inputChange = function (value) { var _this = this; this.control.setValue(value instanceof Object ? this.getMultiLabels(value, this.pathLabel.split('.')) : value); if (typeof value === 'string') { this.onWrite(value); } else { var newValue_1 = Object.assign({}, value); delete newValue_1.dssLabel; delete newValue_1.dssSelect; setTimeout(function () { _this.onWrite(!!_this.return ? _this.getMultiLabels(newValue_1, _this.return.split('.')) : newValue_1); }, 250); } }; NgSearchComponent.prototype.format = function () { var _this = this; this.options.map(function (option, index) { option.dssLabel = _this.getMultiLabels(option, _this.pathLabel.split('.')); option.dssSelect = _this.itemSelect === index; }); }; return NgSearchComponent; }(SelectCustomControlValueAccessor)); NgSearchComponent.decorators = [ { type: i0.Component, args: [{ selector: 'dss-search', template: "<div\r\n class=\"form-group form-control-search\"\r\n [ngClass]=\"{\r\n invalid: control.invalid && control.touched,\r\n valid: control.valid && control.touched,\r\n 'form-group-label': !!label,\r\n active: !!control?.value\r\n }\"\r\n>\r\n <div class=\"form-content\">\r\n <i\r\n class=\"form-icon-floating icon-background left\"\r\n [ngClass]=\"{ search: !loading, loading: loading }\"\r\n ></i>\r\n\r\n <input\r\n #inputRef\r\n type=\"search\"\r\n class=\"form-control browser-default\"\r\n [placeholder]=\"_placeholder\"\r\n [id]=\"name\"\r\n [formControl]=\"control\"\r\n [readonly]=\"readonly\"\r\n autocomplete=\"off\"\r\n autocapitalize=\"off\"\r\n (blur)=\"onBlur($event)\"\r\n (focus)=\"onFocus($event)\"\r\n [ngClass]=\"{\r\n readonly: readonly,\r\n 'is-invalid': !readonly && control.invalid && control.touched\r\n }\"\r\n />\r\n\r\n <!-- BORDER -->\r\n <span class=\"focus-border\">\r\n <i></i>\r\n </span>\r\n\r\n <!-- LABEL -->\r\n <label *ngIf=\"!!label\" [for]=\"name\">\r\n {{ label }}<span *ngIf=\"required\" class=\"required\">*</span>\r\n </label>\r\n\r\n <ul\r\n class=\"options-list hidden\"\r\n [class.hidden]=\"control.value?.length === 0 || loading || !focused\"\r\n >\r\n <ng-content *ngIf=\"options.length === 0\"> </ng-content>\r\n <li\r\n *ngIf=\"options.length === 0; else template_options\"\r\n class=\"not-found option-item\"\r\n >\r\n {{ notFound }}\r\n </li>\r\n\r\n <ng-template #template_options>\r\n <li\r\n *ngFor=\"let option of options; let index = index\"\r\n role=\"listitem\"\r\n class=\"option-item\"\r\n [class.hover]=\"option.dssSelect\"\r\n [class.select]=\"itemSelect === index\"\r\n (click)=\"inputChange(option); itemSelect = index\"\r\n >\r\n {{ option.dssLabel }}\r\n </li>\r\n </ng-template>\r\n </ul>\r\n </div>\r\n\r\n <ng-container *ngFor=\"let error of getKeys(errors)\">\r\n <!-- MESSAGE ERROR -->\r\n <div *ngIf=\"getError(error)\" class=\"message error\">\r\n {{ errors[error] }}\r\n </div>\r\n </ng-container>\r\n\r\n <!-- MESSAGE HELp -->\r\n <div *ngIf=\"!!help\" class=\"message\">\r\n {{ help }}\r\n </div>\r\n</div>\r\n", providers: [ { provide: forms.NG_VALUE_ACCESSOR, multi: true, useExisting: i0.forwardRef(function () { return NgSearchComponent; }), }, ] },] } ]; NgSearchComponent.ctorParameters = function () { return [ { type: forms.ControlContainer }, { type: i0.ElementRef }, { type: i0.Renderer2 }, { type: http.HttpClient }, { type: NgInputConfigService }, { type: i0.ChangeDetectorRef } ]; }; NgSearchComponent.propDecorators = { inputRef: [{ type: i0.ViewChild, args: ['inputRef',] }], notFound: [{ type: i0.Input }], pathLabel: [{ type: i0.Input }], value: [{ type: i0.Input }], options: [{ type: i0.Input }], uri: [{ type: i0.Input }], responseData: [{ type: i0.Input }], return: [{ type: i0.Input }], onInput: [{ type: i0.HostListener, args: ['input', ['$event'],] }], handleKeyDown: [{ type: i0.HostListener, args: ['keydown', ['$event'],] }], blur: [{ type: i0.Output }], focus: [{ type: i0.Output }] }; var NgSelectComponent = /** @class */ (function (_super) { __extends(NgSelectComponent, _super); function NgSelectComponent(controlContainer, elementRef, renderer, configService, changeDetectorRef) { var _this = _super.call(this, controlContainer, elementRef, renderer, configService, changeDetectorRef) || this; _this.controlContainer = controlContainer; _this.elementRef = elementRef; _this.renderer = renderer; _this.configService = configService; _this.optionDefault = { label: 'Selecione uma opção', value: '', hide: true, }; _this.options = []; _this.formatOptions(); return _this; } NgSelectComponent.prototype.ngOnInit = function () { this.ngOnInitSuper(); }; NgSelectComponent.prototype.ngOnChanges = function (params) { if (!!params.options && !!params.options.currentValue) { this.formatOptions(); } }; NgSelectComponent.prototype.formatOptions = function () { var _this = this; var _a, _b; if (this.options.length > 0) { var option_1 = []; option_1.push(this.optionDefault); var optionRef = this.options[0]; if (typeof (optionRef === null || optionRef === void 0 ? void 0 : optionRef.label) === 'string' && typeof (optionRef === null || optionRef === void 0 ? void 0 : optionRef.value) === 'string') { (_a = this.options) === null || _a === void 0 ? void 0 : _a.forEach(function (opt) { return option_1 === null || option_1 === void 0 ? void 0 : option_1.push(opt); }); } else { if (this.path) { var key_1 = Object.keys(this.path)[0]; var value_1 = this.path[key_1]; (_b = this.options) === null || _b === void 0 ? void 0 : _b.forEach(function (opt) { return option_1 === null || option_1 === void 0 ? void 0 : option_1.push({ label: _this.getMultiLabels(opt, key_1.split('.')), value: _this.getMultiLabels(opt, value_1.split('.')), hide: false, }); }); } } this.options = option_1; } }; Object.defineProperty(NgSelectComponent.prototype, "className", { get: function () { var validField = this.control.valid && this.control.touched; var invalidField = this.control.invalid && this.control.touched; return { readonly: this.readonly, 'is-invalid': !this.readonly && invalidField, 'is-valid': validField, }; }, enumerable: false, configurable: true }); return NgSelectComp