UNPKG

@neocomplexx/ngx-neo-directives-mat

Version:
1,224 lines (1,207 loc) 47.3 kB
import { __decorate, __metadata, __awaiter } from 'tslib'; import { ElementRef, Input, HostListener, Directive, InjectionToken, Renderer2, NgModule } from '@angular/core'; import { BehaviorSubject, Subject, combineLatest, timer } from 'rxjs'; import { tap, map, filter, switchMap } from 'rxjs/operators'; import { NgModel } from '@angular/forms'; import { MatTabGroup } from '@angular/material/tabs'; var OnReturnDirective_1; let OnReturnDirective = OnReturnDirective_1 = class OnReturnDirective { constructor(_el) { this._el = _el; this.setEnabled = true; this.el = this._el; } onKeyDown(e) { if ((e.which === 13 || e.keyCode === 13)) { e.preventDefault(); OnReturnDirective_1.setNextFocus(this.onReturn, this.setEnabled); return; } } // tslint:disable-next-line:member-ordering static setNextFocus(onReturn, setEnabled = true) { if (onReturn instanceof Array) { let termine = false; let i = 0; while (!termine && i < onReturn.length) { let element = onReturn[i]; if (element) { if (element instanceof HTMLInputElement || element instanceof HTMLButtonElement || element instanceof HTMLSelectElement || element instanceof HTMLSelectElement) { if (!element.disabled) { element.focus(); termine = true; } } else { if (element && element.nativeElement instanceof HTMLInputElement || element.nativeElement instanceof HTMLButtonElement || element.nativeElement instanceof HTMLSelectElement) { if (!element.nativeElement.disabled) { element.nativeElement.focus(); termine = true; } } else { let input = element['ctrInput']; if (input) { input = input['nativeElement']; if (input && input instanceof HTMLInputElement) { if (!input.disabled) { input.focus(); termine = true; } } } else { element = document.getElementById(element); if (element) { if (element.disabled && setEnabled) { element.disabled = false; } if (!element.disabled) { element.focus(); termine = true; } } } } } } i++; } } else if (onReturn) { let element = onReturn; if (element instanceof HTMLInputElement || element instanceof HTMLButtonElement || element instanceof HTMLSelectElement) { if (element.disabled) { element.disabled = false; } element.focus(); } else { let input = element['ctrInput']; if (input) { input = input['nativeElement']; if (input && input instanceof HTMLInputElement) { if (input.disabled) { input.disabled = false; } input.focus(); } } else { element = document.getElementById(onReturn); if (element) { if (element.disabled && setEnabled) { element.disabled = false; } element.focus(); } } } } } }; OnReturnDirective.ctorParameters = () => [ { type: ElementRef } ]; __decorate([ Input(), __metadata("design:type", Object) ], OnReturnDirective.prototype, "onReturn", void 0); __decorate([ Input(), __metadata("design:type", Object) ], OnReturnDirective.prototype, "setEnabled", void 0); __decorate([ HostListener('keydown', ['$event']), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], OnReturnDirective.prototype, "onKeyDown", null); OnReturnDirective = OnReturnDirective_1 = __decorate([ Directive({ // tslint:disable-next-line:directive-selector selector: '[onReturn]' }), __metadata("design:paramtypes", [ElementRef]) ], OnReturnDirective); // tslint:disable:indent const COMMAND_CONFIG = new InjectionToken('COMMAND_CONFIG'); const COMMAND_DEFAULT_CONFIG = { executingCssClass: 'executing', }; /* export function provideConfig(config: CommandOptions): any { return Object.assign({}, COMMAND_DEFAULT_CONFIG, config); } */ /** * * ### Example with options * ```html * <button [command]='saveCmd' [commandOptions]='{executingCssClass: 'in-progress'}'>Save</button> * ``` */ let CommandDirective = class CommandDirective { constructor(renderer, element) { this.renderer = renderer; this.element = element; this.stopPropagation = false; // @HostBinding('disabled') isDisabled: boolean; this.setEnabled = true; this.config = COMMAND_DEFAULT_CONFIG; this.ownDisabledState = false; this.commandDisabledChanged = false; } ngOnInit() { // console.log('[commandDirective::init]'); this.commandOptions = Object.assign({}, this.config, this.commandOptions); if (!this.command) { throw new Error('[commandDirective] command should be defined!'); } else { this.command.verifyCommandExecutionPipe(); this.command.setNextFocus(this.commandNextFocus); this.command.setEnabledValue(this.setEnabled); } // if (this.element.nativeElement.localName === 'button') { // let thisAux = this; // Set up a new observer // var observer = new MutationObserver(function(mutations) { // mutations.forEach(function(mutation) { // // Check the modified attributeName is "disabled" // if(!thisAux.commandDisabledChanged) { // if (mutation.attributeName === "disabled") { // thisAux.ownDisabledState = thisAux.element.nativeElement.disabled; // } // } else { // thisAux.commandDisabledChanged = false; // } // }); // }); // // Configure to only listen to attribute changes // var config = { attributes: true }; // // Start observing myElem // observer.observe(this.element.nativeElement, config); // } this.canExecute$$ = this.command.canExecute$.pipe(tap(x => { if (this.element.nativeElement.localName === 'button') { this.commandDisabledChanged = true; if (this.commandValue == this.command.executingParam) { this.element.nativeElement.disabled = !x; // } else { // this.element.nativeElement.disabled = !(!this.ownDisabledState && x); } } })).subscribe(); this.isExecuting$$ = this.command.isExecuting$.pipe(tap(x => { // console.log('[commandDirective::isExecuting$]', x); x ? this.renderer.addClass(this.element.nativeElement, this.commandOptions.executingCssClass) : this.renderer.removeClass(this.element.nativeElement, this.commandOptions.executingCssClass); })).subscribe(); if (this.isMobileOperatingSystem()) { this.element.nativeElement.addEventListener('touchstart', (event) => __awaiter(this, void 0, void 0, function* () { event.preventDefault(); event.stopPropagation(); if (this.element.nativeElement.localName === 'input') { return; } this.executeCommand(); })); this.element.nativeElement.addEventListener('focusin', (event) => __awaiter(this, void 0, void 0, function* () { event.preventDefault(); event.stopPropagation(); this.executeCommand(); })); } else { this.element.nativeElement.addEventListener('keydown', (event) => __awaiter(this, void 0, void 0, function* () { if ((event.which === 13 || event.keyCode === 13)) { event.preventDefault(); event.stopPropagation(); this.executeCommand(); } })); this.element.nativeElement.addEventListener('click', (event) => __awaiter(this, void 0, void 0, function* () { if (this.element.nativeElement.localName === 'input') { return; } event.preventDefault(); if (this.stopPropagation) { event.stopPropagation(); } this.executeCommand(); })); } } executeCommand() { if (!this.element.nativeElement.disabled) this.command.canExecute = true; this.command.verifyCommandExecutionPipe(); this.command.execute(this.commandValue); } ngOnDestroy() { if (this.command) { this.command.destroy(); } if (this.canExecute$$) { this.canExecute$$.unsubscribe(); } if (this.isExecuting$$) { this.isExecuting$$.unsubscribe(); } } isMobileOperatingSystem() { let isMobile = false; const userAgent = navigator.userAgent || navigator.vendor; // Windows Phone must come first because its UA also contains "Android" if (/windows phone/i.test(userAgent)) { isMobile = true; // "Windows Phone"; } else if (/android/i.test(userAgent)) { isMobile = true; // "Android"; } else if (/iPad|iPhone|iPod/.test(userAgent)) { // iOS detection from: http://stackoverflow.com/a/9039885/177710 isMobile = true; // "iOS"; } isMobile = false; return isMobile; // "unknown"; } }; CommandDirective.ctorParameters = () => [ { type: Renderer2 }, { type: ElementRef } ]; __decorate([ Input(), __metadata("design:type", Object) ], CommandDirective.prototype, "command", void 0); __decorate([ Input(), __metadata("design:type", Object) ], CommandDirective.prototype, "commandOptions", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], CommandDirective.prototype, "commandCanExecute", void 0); __decorate([ Input(), __metadata("design:type", Object) ], CommandDirective.prototype, "commandValue", void 0); __decorate([ Input(), __metadata("design:type", Object) ], CommandDirective.prototype, "commandNextFocus", void 0); __decorate([ Input(), __metadata("design:type", Object) ], CommandDirective.prototype, "stopPropagation", void 0); __decorate([ Input(), __metadata("design:type", Object) ], CommandDirective.prototype, "setEnabled", void 0); CommandDirective = __decorate([ Directive({ // tslint:disable-next-line:directive-selector selector: '[command]' }), __metadata("design:paramtypes", [Renderer2, ElementRef]) ], CommandDirective); /** * Command object used to encapsulate information which is needed to perform an action. */ class Command { /** * Creates an instance of Command. */ constructor(executeParam, canExecute$, isAsync, delay) { this.executeParam = executeParam; this.isAsync = isAsync; this.delay = delay; this.isExecuting = false; this.isExecuting$ = new BehaviorSubject(false); this.canExecute = true; this.executionPipe$ = new Subject(); this.setEnabled = true; if (canExecute$) { this.canExecute$ = combineLatest(this.isExecuting$, canExecute$, (isExecuting, canExecuteResult) => { // console.log('[command::combineLatest$] update!', { isExecuting, canExecuteResult }); this.isExecuting = isExecuting; this.canExecute = !isExecuting && canExecuteResult; return this.canExecute; }); this.canExecute$$ = this.canExecute$.subscribe(); } else { this.canExecute$ = this.isExecuting$.pipe(map(x => { const canExecute = !x; this.canExecute = canExecute; return canExecute; })); this.isExecuting$$ = this.isExecuting$.pipe(tap(x => this.isExecuting = x)) .subscribe(); } this.buildExecutionPipe(executeParam, isAsync, delay); } verifyCommandExecutionPipe() { if (this.executionPipe$.observers.length === 0) { this.buildExecutionPipe(this.executeParam, this.isAsync, this.delay); } } execute(value) { this.executingParam = value; this.executionPipe$.next(value); } executeWithResult(value) { return __awaiter(this, void 0, void 0, function* () { this.executingParam = value; this.executionPipe$.next(value); return yield this.result; }); } destroy() { if (this.executionPipe$$) { this.executionPipe$$.unsubscribe(); } if (this.canExecute$$) { this.canExecute$$.unsubscribe(); } if (this.isExecuting$$) { this.isExecuting$$.unsubscribe(); } if (this.isExecuting$) { this.isExecuting$.complete(); } if (this.asyncAction != null) { this.asyncAction = null; this.resultAsyncAction = null; } } buildExecutionPipe(executeParm, isAsync, delay) { let pipe$ = this.executionPipe$.pipe(filter(() => this.canExecute), tap(() => { this.isExecuting$.next(true); if (isAsync && this.asyncAction != null) { this.resultAsyncAction = this.asyncAction(undefined); } })); pipe$ = isAsync ? pipe$.pipe(switchMap((value) => { if (delay && delay > 0) { if (this.delaySubscribe) { this.delaySubscribe.unsubscribe(); } const delayTimer = timer(delay); this.delaySubscribe = delayTimer.subscribe(t => { executeParm(value); }); return Promise.resolve(null); } else { const result = executeParm(value); return Promise.resolve(result); } })) : pipe$.pipe(tap((value) => { executeParm(value); })); pipe$ = pipe$.pipe(tap(() => { // console.log('[command::excutionPipe$] do#2 - set idle'); this.isExecuting$.next(false); this.executingParam = undefined; if (isAsync && this.asyncAction != null) { this.resultAsyncAction = this.asyncAction(this.resultAsyncAction); } OnReturnDirective.setNextFocus(this.elementNextFocus, this.setEnabled); }, (e) => { console.log('[command::excutionPipe$] do#2 error - set idle' + e.toString()); this.isExecuting$.next(false); this.executingParam = undefined; if (isAsync && this.asyncAction != null) { this.resultAsyncAction = this.asyncAction(this.resultAsyncAction); } this.buildExecutionPipe(executeParm, isAsync, delay); })); this.executionPipe$$ = pipe$.subscribe(); } setNextFocus(element) { this.elementNextFocus = element; } setEnabledValue(enabled) { this.setEnabled = enabled; } } let StringsDirective = class StringsDirective { constructor(_el) { this._el = _el; this.el = this._el; } onKeyDown(e) { if (e.key === 'Delete' || e.key === 'Tab' || e.key === 'Backspace' || e.key === 'ArrowLeft' || e.altKey || e.ctrlKey || e.key === 'Home' || e.key === 'End' || e.key === 'PageDown' || e.key === 'PageUp' || e.key === 'ArrowRight' || e.key === 'Control' || e.key === 'Alt' || e.key === 'Shift') { return; } const element = this._el.nativeElement.value; if (element && element.length >= this.maxLength) { e.preventDefault(); } } }; StringsDirective.ctorParameters = () => [ { type: ElementRef } ]; __decorate([ Input(), __metadata("design:type", Object) ], StringsDirective.prototype, "maxLength", void 0); __decorate([ HostListener('keydown', ['$event']), __metadata("design:type", Function), __metadata("design:paramtypes", [KeyboardEvent]), __metadata("design:returntype", void 0) ], StringsDirective.prototype, "onKeyDown", null); StringsDirective = __decorate([ Directive({ // tslint:disable-next-line:directive-selector selector: '[maxLength]' }), __metadata("design:paramtypes", [ElementRef]) ], StringsDirective); let NeoNumbersDirective = class NeoNumbersDirective { constructor(el, renderer, model) { this.el = el; this.renderer = renderer; this.model = model; this.allowNullValue = false; } ngOnInit() { if (this.allowNullValue === "") this.allowNullValue = true; this.regex = new RegExp('^\\d+$'); this.el.nativeElement.type = "number"; } blockPaste(e) { const clipboardData = e.clipboardData || window['clipboardData']; let pastedData = clipboardData.getData('text/plain').split(",").join("."); let posPoint = pastedData.lastIndexOf('.'); if (posPoint > -1) { pastedData = pastedData.substring(0, posPoint).split(".").join(""); } const onlyNumbers = Array.from(pastedData).map(x => this.isOnlyNumber(x)).reduce((prev, el) => prev && el, true); const isCorrect = this.regex.test(pastedData); if (isCorrect && this.el.nativeElement.max && pastedData.length > this.el.nativeElement.max.length) pastedData = pastedData.substr(0, this.el.nativeElement.max.length); setTimeout(() => { if (onlyNumbers && isCorrect) { this.el.nativeElement.value = pastedData; } else { this.el.nativeElement.value = '0'; } }, 10); } onfocus() { if (this.el.nativeElement.value == "0") { this.el.nativeElement.value = null; } } onBlur() { let current = this.el.nativeElement.value; if (current == "") { if (this.allowNullValue) { this.el.nativeElement.value = null; this.model.control.setValue(null); } else { this.el.nativeElement.value = "0"; this.model.control.setValue(0); } } else { this.el.nativeElement.type = "text"; this.el.nativeElement.type = "number"; if (this.el.nativeElement.max && this.el.nativeElement.valueAsNumber > +this.el.nativeElement.max) { this.el.nativeElement.value = this.el.nativeElement.max; this.model.control.setValue(this.el.nativeElement.valueAsNumber); } } } onKeyUp(event) { console.log(event); let current = this.el.nativeElement.value; current = current.split("-").join(""); let posPoint = current.indexOf('.'); if (posPoint > -1) { current = current.substring(0, posPoint); this.el.nativeElement.value = current; } if (this.isSpecial(event) || current == "") { if (event.keyCode == 8 || current != "") { this.prev = current; } return; } if (!this.isOnlyNumber(event)) { event.preventDefault(); event.stopPropagation(); return false; } if (this.el.nativeElement.max && this.el.nativeElement.valueAsNumber > +this.el.nativeElement.max) { this.el.nativeElement.value = current.substring(0, current.length - 1); } } onKeyDown(event) { if (this.isSpecial(event)) { return; } if (!this.isOnlyNumber(event)) { event.preventDefault(); event.stopPropagation(); return false; } let current = this.el.nativeElement.value; this.prev = current; } isSpecial(e) { return e.key === 'Delete' || (e.key === 'Tab' || e.keyCode == 9) || (e.key === 'Backspace' || e.keyCode == 8) || e.key === 'ArrowLeft' || e.altKey || e.ctrlKey || e.key === 'Home' || e.key === 'End' || e.key === 'PageDown' || e.key === 'PageUp' || e.key === 'ArrowDown' || e.key === 'ArrowUp' || e.key === 'ArrowRight' || e.key === 'Control' || e.key === 'Alt' || e.key === 'Shift' || (e.keyIdentifier == 'Down' || e.keyIdentifier == 'Up' || e.keyIdentifier == 'Left' || e.keyIdentifier == 'Right'); } isOnlyNumber(e) { if (e instanceof KeyboardEvent) { return ((e.key >= '0' && e.key <= '9') || (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)); } else { return (e >= '0' && e <= '9'); } } }; NeoNumbersDirective.ctorParameters = () => [ { type: ElementRef }, { type: Renderer2 }, { type: NgModel } ]; __decorate([ Input(), __metadata("design:type", Object) ], NeoNumbersDirective.prototype, "isNumber", void 0); __decorate([ Input('allowNullValue'), __metadata("design:type", Object) ], NeoNumbersDirective.prototype, "allowNullValue", void 0); __decorate([ HostListener('paste', ['$event']), __metadata("design:type", Function), __metadata("design:paramtypes", [ClipboardEvent]), __metadata("design:returntype", void 0) ], NeoNumbersDirective.prototype, "blockPaste", null); __decorate([ HostListener('focus'), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], NeoNumbersDirective.prototype, "onfocus", null); __decorate([ HostListener('blur'), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], NeoNumbersDirective.prototype, "onBlur", null); __decorate([ HostListener('keyup', ['$event']), __metadata("design:type", Function), __metadata("design:paramtypes", [KeyboardEvent]), __metadata("design:returntype", void 0) ], NeoNumbersDirective.prototype, "onKeyUp", null); __decorate([ HostListener('keydown', ['$event']), __metadata("design:type", Function), __metadata("design:paramtypes", [KeyboardEvent]), __metadata("design:returntype", void 0) ], NeoNumbersDirective.prototype, "onKeyDown", null); NeoNumbersDirective = __decorate([ Directive({ selector: '[isNumber]' }), __metadata("design:paramtypes", [ElementRef, Renderer2, NgModel]) ], NeoNumbersDirective); let NeoAutofocusDirective = class NeoAutofocusDirective { constructor(el) { this.el = el; } ngOnInit() { } ngAfterViewInit() { this.subscription = timer(100).subscribe(() => { if (this.neoAutofocus) { this.setFocus(); } this.subscription.unsubscribe(); }); } setFocus() { if (this.el) { if (this.el instanceof HTMLInputElement || this.el instanceof HTMLButtonElement || this.el instanceof HTMLSelectElement || this.el instanceof HTMLSelectElement) { if (!this.el.disabled) { this.el.focus(); } } else { if (this.el && this.el.nativeElement instanceof HTMLInputElement || this.el.nativeElement instanceof HTMLButtonElement || this.el.nativeElement instanceof HTMLSelectElement) { if (!this.el.nativeElement.disabled) { this.el.nativeElement.focus(); } } else { let input = this.el['ctrInput']; if (input) { input = input['nativeElement']; if (input && input instanceof HTMLInputElement) { if (!input.disabled) { input.focus(); } } } } } } } }; NeoAutofocusDirective.ctorParameters = () => [ { type: ElementRef } ]; __decorate([ Input('neoAutofocus'), __metadata("design:type", Boolean) ], NeoAutofocusDirective.prototype, "neoAutofocus", void 0); NeoAutofocusDirective = __decorate([ Directive({ selector: '[neoAutofocus]' }), __metadata("design:paramtypes", [ElementRef]) ], NeoAutofocusDirective); let NeoChangeCommandDirective = class NeoChangeCommandDirective { constructor(element) { this.element = element; } onInputChange(event) { const value = this.element.nativeElement.value; if (!this._lastValue || this._lastValue !== value) { this._lastValue = value; this.changeCommand.execute(value); } } }; NeoChangeCommandDirective.ctorParameters = () => [ { type: ElementRef } ]; __decorate([ Input(), __metadata("design:type", Object) ], NeoChangeCommandDirective.prototype, "changeCommand", void 0); __decorate([ HostListener('keyup', ['$event']), __metadata("design:type", Function), __metadata("design:paramtypes", [KeyboardEvent]), __metadata("design:returntype", void 0) ], NeoChangeCommandDirective.prototype, "onInputChange", null); NeoChangeCommandDirective = __decorate([ Directive({ // tslint:disable-next-line:directive-selector selector: '[changeCommand]' }), __metadata("design:paramtypes", [ElementRef]) ], NeoChangeCommandDirective); let NeoDecimalNumbersDirective = class NeoDecimalNumbersDirective { constructor(el, renderer, model) { this.el = el; this.renderer = renderer; this.model = model; this.allowNullValue = false; this.decimals = 2; this.haveDecimalPoint = false; } ngOnInit() { var _a, _b; if (this.options && !this.options.apply) { return; } this.decimals = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.decimals) !== null && _b !== void 0 ? _b : 2; if (this.decimals == "") this.decimals = 2; if (this.allowNullValue === "") this.allowNullValue = true; this.el.nativeElement.type = "number"; this.regex = new RegExp('^[0-9]+(\\.[0-9]{0,' + this.decimals + '})?$'); } blockPaste(e) { if (this.options && !this.options.apply) { return; } const clipboardData = e.clipboardData || window['clipboardData']; let pastedData = clipboardData.getData('text/plain').split(",").join("."); let posPoint = pastedData.lastIndexOf('.'); if (posPoint > -1) { pastedData = pastedData.substring(0, posPoint).split(".").join("") + pastedData.substring(posPoint); posPoint = pastedData.lastIndexOf('.'); if (posPoint + this.decimals < pastedData.length) { pastedData = pastedData.substring(0, posPoint + this.decimals + 1); } } const onlyNumbers = Array.from(pastedData).map(x => this.isNumber(x)).reduce((prev, el) => prev && el, true); const a = this.regex.test(pastedData); setTimeout(() => { if (onlyNumbers && a) { this.el.nativeElement.value = pastedData; } else { this.el.nativeElement.value = '0'; } }, 10); } onfocus() { if (this.options && !this.options.apply) { return; } if (this.el.nativeElement.value == "0") { this.el.nativeElement.value = null; } } onBlur() { if (this.options && !this.options.apply) { return; } let current = this.el.nativeElement.value; if (current == "") { if (this.allowNullValue) { this.el.nativeElement.value = null; this.model.control.setValue(null); } else { this.el.nativeElement.value = "0"; this.model.control.setValue(0); } } else { this.el.nativeElement.type = "text"; this.el.nativeElement.type = "number"; if (this.el.nativeElement.max && this.el.nativeElement.valueAsNumber > +this.el.nativeElement.max) { this.el.nativeElement.value = this.el.nativeElement.max; this.model.control.setValue(this.el.nativeElement.valueAsNumber); } } } onKeyUp(event) { if (this.options && !this.options.apply) { return; } let current = this.el.nativeElement.value; current = current.split("-").join(""); let posPoint = current.indexOf('.'); if (posPoint > -1) { this.el.nativeElement.value = current.substring(0, posPoint + 1) + current.substring(posPoint).split(".").join(""); } if (this.haveDecimalPoint && current == "" && this.prev > "") { this.haveDecimalPoint = false; this.el.nativeElement.value = this.prev + (this.prev.endsWith(".0") ? "" : ".0"); this.el.nativeElement.type = "text"; this.el.nativeElement.selectionStart = this.el.nativeElement.value.length - 1; this.el.nativeElement.selectionEnd = this.el.nativeElement.value.length; this.el.nativeElement.type = "number"; return; } if (this.isSpecial(event) || current == "") { if (event.keyCode == 8 || current != "") { this.prev = current; const dotIndex = current.toString().indexOf('.'); if (dotIndex >= 0) this.haveDecimalPoint = false; } return; } if (!this.isNumber(event)) { event.preventDefault(); event.stopPropagation(); return false; } if (current) { current = current.toString(); const dotIndex = current.indexOf('.'); if ((dotIndex >= 0) && ((current.length - 1) - dotIndex > this.decimals)) { this.renderer.setProperty(this.el.nativeElement, 'value', current.slice(0, dotIndex + this.decimals + 1)); } } else { this.renderer.setProperty(this.el.nativeElement, 'value', this.prev); } } onKeyDown(event) { if (this.options && !this.options.apply) { return; } if (this.isSpecial(event)) { return; } if (!this.isNumber(event)) { event.preventDefault(); event.stopPropagation(); return false; } let current = this.el.nativeElement.value; if (this.isDecimalPoint(event)) { this.haveDecimalPoint = true; if (!current.endsWith(".0")) { this.el.nativeElement.value = current + ".0"; this.el.nativeElement.type = "text"; this.el.nativeElement.selectionStart = this.el.nativeElement.value.length - 1; this.el.nativeElement.selectionEnd = this.el.nativeElement.value.length; this.el.nativeElement.type = "number"; } } this.prev = current; } isSpecial(e) { return e.key === 'Delete' || (e.key === 'Tab' || e.keyCode == 9) || (e.key === 'Backspace' || e.keyCode == 8) || e.key === 'ArrowLeft' || e.altKey || e.ctrlKey || e.key === 'Home' || e.key === 'End' || e.key === 'PageDown' || e.key === 'PageUp' || e.key === 'ArrowDown' || e.key === 'ArrowUp' || e.key === 'ArrowRight' || e.key === 'Control' || e.key === 'Alt' || e.key === 'Shift' || (e.keyIdentifier == 'Down' || e.keyIdentifier == 'Up'); } isNumber(e) { if (e instanceof KeyboardEvent) { return ((e.key >= '0' && e.key <= '9') || ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) || (e.key === '.' || e.key === ',') || (e.keyCode === 188 || e.keyCode === 190 || e.keyCode === 229)); } else { return ((e >= '0' && e <= '9') || (e === '.' || e === ',')); } } isDecimalPoint(e) { return (e.key === '.' || e.key === ',') || (e.keyCode === 188 || e.keyCode === 190 || e.keyCode === 229); } }; NeoDecimalNumbersDirective.ctorParameters = () => [ { type: ElementRef }, { type: Renderer2 }, { type: NgModel } ]; __decorate([ Input('isDecimal'), __metadata("design:type", Object) ], NeoDecimalNumbersDirective.prototype, "options", void 0); __decorate([ Input('allowNullValue'), __metadata("design:type", Object) ], NeoDecimalNumbersDirective.prototype, "allowNullValue", void 0); __decorate([ HostListener('paste', ['$event']), __metadata("design:type", Function), __metadata("design:paramtypes", [ClipboardEvent]), __metadata("design:returntype", void 0) ], NeoDecimalNumbersDirective.prototype, "blockPaste", null); __decorate([ HostListener('focus'), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], NeoDecimalNumbersDirective.prototype, "onfocus", null); __decorate([ HostListener('blur'), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], NeoDecimalNumbersDirective.prototype, "onBlur", null); __decorate([ HostListener('keyup', ['$event']), __metadata("design:type", Function), __metadata("design:paramtypes", [KeyboardEvent]), __metadata("design:returntype", void 0) ], NeoDecimalNumbersDirective.prototype, "onKeyUp", null); __decorate([ HostListener('keydown', ['$event']), __metadata("design:type", Function), __metadata("design:paramtypes", [KeyboardEvent]), __metadata("design:returntype", void 0) ], NeoDecimalNumbersDirective.prototype, "onKeyDown", null); NeoDecimalNumbersDirective = __decorate([ Directive({ selector: '[isDecimal]' }), __metadata("design:paramtypes", [ElementRef, Renderer2, NgModel]) ], NeoDecimalNumbersDirective); /** * Boolean function that check if the keyboard event is not a normal key but a command like key * Will return boolean if the key is Alt, Shift , Control, Delete, Tab, Backspace, Arrows, Home, End, PageUp and PageDown. * * @export * @param e */ function isCommandKey(e) { return e.key === 'Delete' || e.key === 'Tab' || e.key === 'Backspace' || e.key === 'ArrowLeft' || e.key === 'Left' || e.altKey || e.ctrlKey || e.key === 'Home' || e.key === 'End' || e.key === 'PageDown' || e.key === 'PageUp' || e.key === 'ArrowRight' || e.key === 'Right' || e.key === 'Control' || e.key === 'Alt' || e.key === 'Shift'; } let NeoCompositeInputDirective = class NeoCompositeInputDirective { constructor(el) { this.el = el; // Next input to move from the current input this.previousInput = null; // Previous input to move from the current input this.nextInput = null; } onKeyDown(e) { // If the element is selected and the user presses backspace we must clear the input if (e.key === 'Backspace' && this.elementIsSelected()) { return true; } // We must move to the previous input if the user presses backspace and he is in the first position of the input // or if he presses the left key if (((e.key === 'Backspace') && (this.el.nativeElement.selectionStart === 0) || ((e.key === 'ArrowLeft') || (e.key === 'Left')))) { if ((this.previousInput) && (this.previousInput instanceof HTMLInputElement) && (!this.previousInput.disabled)) { this.previousInput.focus(); return false; } } // If he presses the right arrow it must move to the next input if (e.key === 'ArrowRight' || e.key === 'Right') { if ((this.nextInput) && (this.nextInput instanceof HTMLInputElement) && (!this.nextInput.disabled)) { this.nextInput.focus(); return false; } } } /** * * Method called on keyup to check if the pressed key is allow, if it is not it stops the event. * If the element reached its maximum length on key pressed it must jump to the next input * @param e */ onKeyUp(e) { if (isCommandKey(e)) { return; } const value = this.el.nativeElement.value; if (this.nextInput && (value.length >= this.el.nativeElement.maxLength)) { this.nextInput.focus(); } } /** * On focus we select the entire value of the input */ onFocus() { this.el.nativeElement.select(); } /** * Method to check if the element is selected * */ elementIsSelected() { return this.el.nativeElement.selectionStart === 0 && this.el.nativeElement.selectionEnd !== 0; } }; NeoCompositeInputDirective.ctorParameters = () => [ { type: ElementRef } ]; __decorate([ Input(), __metadata("design:type", Object) ], NeoCompositeInputDirective.prototype, "previousInput", void 0); __decorate([ Input(), __metadata("design:type", Object) ], NeoCompositeInputDirective.prototype, "nextInput", void 0); __decorate([ HostListener('keydown', ['$event']), __metadata("design:type", Function), __metadata("design:paramtypes", [KeyboardEvent]), __metadata("design:returntype", void 0) ], NeoCompositeInputDirective.prototype, "onKeyDown", null); __decorate([ HostListener('keyup', ['$event']), __metadata("design:type", Function), __metadata("design:paramtypes", [KeyboardEvent]), __metadata("design:returntype", void 0) ], NeoCompositeInputDirective.prototype, "onKeyUp", null); __decorate([ HostListener('focus', []), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], NeoCompositeInputDirective.prototype, "onFocus", null); NeoCompositeInputDirective = __decorate([ Directive({ selector: '[compositeInput]' }), __metadata("design:paramtypes", [ElementRef]) ], NeoCompositeInputDirective); let NeoMatchHeightDirective = class NeoMatchHeightDirective { constructor(el) { this.el = el; } onResize() { this.matchHeight(this.el.nativeElement, this.matchHeightClassName); } ngAfterViewChecked() { this.matchHeight(this.el.nativeElement, this.matchHeightClassName); } matchHeight(parent, className) { if (!parent) { return; } // step 1: find all the child elements with the selected class name const children = parent.getElementsByClassName(className); if (!children) { return; } // step 1b: reset all children height Array.from(children).forEach((x) => { x.style.height = 'initial'; }); // step 2a: get all the child elements heights const itemHeights = Array.from(children) .map(x => x.getBoundingClientRect().height); // step 2b: find out the tallest const maxHeight = itemHeights.reduce((prev, curr) => { return curr > prev ? curr : prev; }, 0); // step 3: update all the child elements to the tallest height Array.from(children) .forEach((x) => x.style.height = `${maxHeight}px`); } }; NeoMatchHeightDirective.ctorParameters = () => [ { type: ElementRef } ]; __decorate([ Input('neoMatchHeight'), __metadata("design:type", String) ], NeoMatchHeightDirective.prototype, "matchHeightClassName", void 0); __decorate([ HostListener('window:resize'), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], NeoMatchHeightDirective.prototype, "onResize", null); NeoMatchHeightDirective = __decorate([ Directive({ // tslint:disable-next-line:directive-selector selector: '[neoMatchHeight]' }), __metadata("design:paramtypes", [ElementRef]) ], NeoMatchHeightDirective); /** * @author slarrieu */ let NgbTabSetDirective = class NgbTabSetDirective { constructor(_el) { this._el = _el; this.el = this._el; this.onTabChanging = false; } ngOnInit() { // this.el._handleClick = this.interceptTabChange.bind(this); if (this.tabSelectedId) { this.tabSelectedIdSubscribe = this.tabSelectedId.subscribe((tabSelectedId) => { if (tabSelectedId > -1) { // const previousValue = this.tabSelectedId.value; if (this.el.selectedIndex !== tabSelectedId) { this.el.selectedIndex = tabSelectedId; } } }); } } /* interceptTabChange(tab: MatTab, tabHeader: MatTabHeader, idx: number) { if (this.onTabChanging) { return true; } this.onTabChanging = true; let canChange = true; if (this.tabChangeController) { canChange = await this.tabChangeController.canChangeTab(); } this.onTabChanging = false; return canChange && MatTabGroup.prototype._handleClick.apply(this.el, arguments); } */ ngOnDestroy() { if (this.tabSelectedIdSubscribe) { this.tabSelectedIdSubscribe.unsubscribe(); } } cambiarTab($event, force = false) { const tabSelecting = $event.nextId; if (force || this.tabSelectedId.value !== tabSelecting) { this.tabSelectedId.next(tabSelecting); } } }; NgbTabSetDirective.ctorParameters = () => [ { type: MatTabGroup } ]; __decorate([ Input(), __metadata("design:type", BehaviorSubject) ], NgbTabSetDirective.prototype, "tabSelectedId", void 0); __decorate([ Input(), __metadata("design:type", Object) ], NgbTabSetDirective.prototype, "tabChangeController", void 0); NgbTabSetDirective = __decorate([ Directive({ // tslint:disable-next-line:directive-selector selector: '[tabSelectedId], [tabChangeController]', }), __metadata("design:paramtypes", [MatTabGroup]) ], NgbTabSetDirective); var NgxNeoDirectivesModule_1; let NgxNeoDirectivesModule = NgxNeoDirectivesModule_1 = class NgxNeoDirectivesModule { static forRoot(options = { executingCssClass: 'executing' }) { return { ngModule: NgxNeoDirectivesModule_1, providers: [ { provide: COMMAND_CONFIG, useValue: options }, ] }; } }; NgxNeoDirectivesModule = NgxNeoDirectivesModule_1 = __decorate([ NgModule({ imports: [], declarations: [ CommandDirective, OnReturnDirective, StringsDirective, NeoNumbersDirective, NeoAutofocusDirective, NeoChangeCommandDirective, NeoDecimalNumbersDirective, NeoCompositeInputDirective, NeoMatchHeightDirective, NgbTabSetDirective ], exports: [ CommandDirective, OnReturnDirective, StringsDirective, NeoNumbersDirective, NeoAutofocusDirective, NeoChangeCommandDirective, NeoDecimalNumbersDirective, NeoCompositeInputDirective, NeoMatchHeightDirective, NgbTabSetDirective ] }) ], NgxNeoDirectivesModule); /* * Public API Surface of ngx-neo-directives */ /** * Generated bundle index. Do not edit. */ export { COMMAND_CONFIG, COMMAND_DEFAULT_CONFIG, Command, CommandDirective, NeoAutofocusDirective, NeoChangeCommandDirective, NeoCompositeInputDirective, NeoDecimalNumbersDirective, NeoMatchHeightDirective, NeoNumbersDirective, NgxNeoDirectivesModule, OnReturnDirective, StringsDirective, isCommandKey, NeoCompositeInputDirective as ɵa, NgbTabSetDirective as ɵb }; //# sourceMappingURL=neocomplexx-ngx-neo-directives-mat.js.map