src/assets/primeng/components/autocomplete/autocomplete.js
"use strict";
var __decorate = (this && this.__decorate) || function (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;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var common_1 = require("@angular/common");
var inputtext_1 = require("../inputtext/inputtext");
var button_1 = require("../button/button");
var shared_1 = require("../common/shared");
var domhandler_1 = require("../dom/domhandler");
var objectutils_1 = require("../utils/objectutils");
var forms_1 = require("@angular/forms");
exports.AUTOCOMPLETE_VALUE_ACCESSOR = {
provide: forms_1.NG_VALUE_ACCESSOR,
useExisting: core_1.forwardRef(function () { return AutoComplete; }),
multi: true
};
var AutoComplete = (function () {
function AutoComplete(el, domHandler, renderer, objectUtils, cd, differs) {
this.el = el;
this.domHandler = domHandler;
this.renderer = renderer;
this.objectUtils = objectUtils;
this.cd = cd;
this.differs = differs;
this.minLength = 1;
this.delay = 300;
this.type = 'text';
this.completeMethod = new core_1.EventEmitter();
this.onSelect = new core_1.EventEmitter();
this.onUnselect = new core_1.EventEmitter();
this.onFocus = new core_1.EventEmitter();
this.onBlur = new core_1.EventEmitter();
this.onDropdownClick = new core_1.EventEmitter();
this.onClear = new core_1.EventEmitter();
this.onKeyUp = new core_1.EventEmitter();
this.scrollHeight = '200px';
this.dropdownMode = 'blank';
this.immutable = true;
this.onModelChange = function () { };
this.onModelTouched = function () { };
this.panelVisible = false;
this.focus = false;
this.inputFieldValue = null;
this.differ = differs.find([]).create(null);
}
Object.defineProperty(AutoComplete.prototype, "suggestions", {
get: function () {
return this._suggestions;
},
set: function (val) {
this._suggestions = val;
if (this.immutable) {
this.handleSuggestionsChange();
}
},
enumerable: true,
configurable: true
});
AutoComplete.prototype.ngDoCheck = function () {
if (!this.immutable) {
var changes = this.differ.diff(this.suggestions);
if (changes) {
this.handleSuggestionsChange();
}
}
};
AutoComplete.prototype.handleSuggestionsChange = function () {
if (this.panelEL && this.panelEL.nativeElement && this.loading) {
this.highlightOption = null;
if (this._suggestions && this._suggestions.length) {
this.noResults = false;
this.show();
this.suggestionsUpdated = true;
if (this.autoHighlight) {
this.highlightOption = this._suggestions[0];
}
}
else {
this.noResults = true;
if (this.emptyMessage) {
this.show();
this.suggestionsUpdated = true;
}
else {
this.hide();
}
}
}
this.loading = false;
};
AutoComplete.prototype.ngAfterContentInit = function () {
var _this = this;
this.templates.forEach(function (item) {
switch (item.getType()) {
case 'item':
_this.itemTemplate = item.template;
break;
case 'selectedItem':
_this.selectedItemTemplate = item.template;
break;
default:
_this.itemTemplate = item.template;
break;
}
});
};
AutoComplete.prototype.ngAfterViewInit = function () {
if (this.appendTo) {
if (this.appendTo === 'body')
document.body.appendChild(this.panelEL.nativeElement);
else
this.domHandler.appendChild(this.panelEL.nativeElement, this.appendTo);
}
};
AutoComplete.prototype.ngAfterViewChecked = function () {
var _this = this;
//Use timeouts as since Angular 4.2, AfterViewChecked is broken and not called after panel is updated
if (this.suggestionsUpdated && this.panelEL.nativeElement && this.panelEL.nativeElement.offsetParent) {
setTimeout(function () { return _this.align(); }, 1);
this.suggestionsUpdated = false;
}
if (this.highlightOptionChanged) {
setTimeout(function () {
var listItem = _this.domHandler.findSingle(_this.panelEL.nativeElement, 'li.ui-state-highlight');
if (listItem) {
_this.domHandler.scrollInView(_this.panelEL.nativeElement, listItem);
}
}, 1);
this.highlightOptionChanged = false;
}
};
AutoComplete.prototype.writeValue = function (value) {
this.value = value;
this.filled = this.value && this.value != '';
this.updateInputField();
};
AutoComplete.prototype.registerOnChange = function (fn) {
this.onModelChange = fn;
};
AutoComplete.prototype.registerOnTouched = function (fn) {
this.onModelTouched = fn;
};
AutoComplete.prototype.setDisabledState = function (val) {
this.disabled = val;
};
AutoComplete.prototype.onInput = function (event) {
var _this = this;
if (!this.inputKeyDown) {
return;
}
if (this.timeout) {
clearTimeout(this.timeout);
}
var value = event.target.value;
if (!this.multiple) {
this.onModelChange(value);
}
if (value.length === 0) {
this.hide();
this.onClear.emit(event);
}
if (value.length >= this.minLength) {
this.timeout = setTimeout(function () {
_this.search(event, value);
}, this.delay);
}
else {
this.suggestions = null;
this.hide();
}
this.updateFilledState();
this.inputKeyDown = false;
};
AutoComplete.prototype.onInputClick = function (event) {
if (this.documentClickListener) {
this.inputClick = true;
}
};
AutoComplete.prototype.search = function (event, query) {
//allow empty string but not undefined or null
if (query === undefined || query === null) {
return;
}
this.loading = true;
this.completeMethod.emit({
originalEvent: event,
query: query
});
};
AutoComplete.prototype.selectItem = function (option) {
if (this.multiple) {
this.multiInputEL.nativeElement.value = '';
this.value = this.value || [];
if (!this.isSelected(option)) {
this.value = this.value.concat([option]);
this.onModelChange(this.value);
}
}
else {
this.inputEL.nativeElement.value = this.field ? this.objectUtils.resolveFieldData(option, this.field) || '' : option;
this.value = option;
this.onModelChange(this.value);
}
this.onSelect.emit(option);
this.focusInput();
};
AutoComplete.prototype.show = function () {
if (this.multiInputEL || this.inputEL) {
var hasFocus = this.multiple ? document.activeElement == this.multiInputEL.nativeElement : document.activeElement == this.inputEL.nativeElement;
if (!this.panelVisible && hasFocus) {
this.panelVisible = true;
this.panelEL.nativeElement.style.zIndex = ++domhandler_1.DomHandler.zindex;
this.domHandler.fadeIn(this.panelEL.nativeElement, 200);
this.bindDocumentClickListener();
}
}
};
AutoComplete.prototype.align = function () {
if (this.appendTo)
this.domHandler.absolutePosition(this.panelEL.nativeElement, (this.multiple ? this.multiContainerEL.nativeElement : this.inputEL.nativeElement));
else
this.domHandler.relativePosition(this.panelEL.nativeElement, (this.multiple ? this.multiContainerEL.nativeElement : this.inputEL.nativeElement));
};
AutoComplete.prototype.hide = function () {
this.panelVisible = false;
this.unbindDocumentClickListener();
};
AutoComplete.prototype.handleDropdownClick = function (event) {
this.focusInput();
this.dropdownClick = true;
var queryValue = this.multiple ? this.multiInputEL.nativeElement.value : this.inputEL.nativeElement.value;
if (this.dropdownMode === 'blank')
this.search(event, '');
else if (this.dropdownMode === 'current')
this.search(event, queryValue);
this.onDropdownClick.emit({
originalEvent: event,
query: queryValue
});
};
AutoComplete.prototype.focusInput = function () {
if (this.multiple)
this.multiInputEL.nativeElement.focus();
else
this.inputEL.nativeElement.focus();
};
AutoComplete.prototype.removeItem = function (item) {
var itemIndex = this.domHandler.index(item);
var removedValue = this.value[itemIndex];
this.value = this.value.filter(function (val, i) { return i != itemIndex; });
this.onUnselect.emit(removedValue);
this.onModelChange(this.value);
};
AutoComplete.prototype.onKeydown = function (event) {
if (this.panelVisible) {
var highlightItemIndex = this.findOptionIndex(this.highlightOption);
switch (event.which) {
//down
case 40:
if (highlightItemIndex != -1) {
var nextItemIndex = highlightItemIndex + 1;
if (nextItemIndex != (this.suggestions.length)) {
this.highlightOption = this.suggestions[nextItemIndex];
this.highlightOptionChanged = true;
}
}
else {
this.highlightOption = this.suggestions[0];
}
event.preventDefault();
break;
//up
case 38:
if (highlightItemIndex > 0) {
var prevItemIndex = highlightItemIndex - 1;
this.highlightOption = this.suggestions[prevItemIndex];
this.highlightOptionChanged = true;
}
event.preventDefault();
break;
//enter
case 13:
if (this.highlightOption) {
this.selectItem(this.highlightOption);
this.hide();
}
event.preventDefault();
break;
//escape
case 27:
this.hide();
event.preventDefault();
break;
//tab
case 9:
if (this.highlightOption) {
this.selectItem(this.highlightOption);
}
this.hide();
break;
}
}
else {
if (event.which === 40 && this.suggestions) {
this.search(event, event.target.value);
}
}
if (this.multiple) {
switch (event.which) {
//backspace
case 8:
if (this.value && this.value.length && !this.multiInputEL.nativeElement.value) {
this.value = this.value.slice();
var removedValue = this.value.pop();
this.onUnselect.emit(removedValue);
this.onModelChange(this.value);
}
break;
}
}
this.inputKeyDown = true;
};
AutoComplete.prototype.onKeyup = function (event) {
this.onKeyUp.emit(event);
};
AutoComplete.prototype.onInputFocus = function (event) {
this.focus = true;
this.onFocus.emit(event);
};
AutoComplete.prototype.onInputBlur = function (event) {
this.focus = false;
this.onModelTouched();
this.onBlur.emit(event);
if (this.forceSelection) {
var valid = false;
var inputValue = event.target.value.toLowerCase().trim();
if (this.suggestions) {
for (var _i = 0, _a = this.suggestions; _i < _a.length; _i++) {
var suggestion = _a[_i];
var itemValue = this.field ? this.objectUtils.resolveFieldData(suggestion, this.field) : suggestion;
if (itemValue && inputValue === itemValue.toLowerCase()) {
valid = true;
break;
}
}
}
if (!valid) {
if (this.multiple) {
this.multiInputEL.nativeElement.value = '';
}
else {
this.value = null;
this.inputEL.nativeElement.value = '';
}
this.onModelChange(this.value);
}
}
};
AutoComplete.prototype.isSelected = function (val) {
var selected = false;
if (this.value && this.value.length) {
for (var i = 0; i < this.value.length; i++) {
if (this.objectUtils.equals(this.value[i], val, this.dataKey)) {
selected = true;
break;
}
}
}
return selected;
};
AutoComplete.prototype.findOptionIndex = function (option) {
var index = -1;
if (this.suggestions) {
for (var i = 0; i < this.suggestions.length; i++) {
if (this.objectUtils.equals(option, this.suggestions[i])) {
index = i;
break;
}
}
}
return index;
};
AutoComplete.prototype.updateFilledState = function () {
if (this.multiple)
this.filled = (this.value && this.value.length) || (this.multiInputEL && this.multiInputEL.nativeElement && this.multiInputEL.nativeElement.value != '');
else
this.filled = this.inputFieldValue && this.inputFieldValue != '';
};
AutoComplete.prototype.updateInputField = function () {
var formattedValue = this.value ? (this.field ? this.objectUtils.resolveFieldData(this.value, this.field) || '' : this.value) : '';
this.inputFieldValue = formattedValue;
if (this.inputEL && this.inputEL.nativeElement) {
this.inputEL.nativeElement.value = formattedValue;
}
this.updateFilledState();
};
AutoComplete.prototype.bindDocumentClickListener = function () {
var _this = this;
if (!this.documentClickListener) {
this.documentClickListener = this.renderer.listen('document', 'click', function (event) {
if (event.which === 3) {
return;
}
if (!_this.inputClick && !_this.dropdownClick) {
_this.hide();
}
_this.inputClick = false;
_this.dropdownClick = false;
_this.cd.markForCheck();
});
}
};
AutoComplete.prototype.unbindDocumentClickListener = function () {
if (this.documentClickListener) {
this.documentClickListener();
this.documentClickListener = null;
}
};
AutoComplete.prototype.ngOnDestroy = function () {
this.unbindDocumentClickListener();
if (this.appendTo) {
this.el.nativeElement.appendChild(this.panelEL.nativeElement);
}
};
return AutoComplete;
}());
__decorate([
core_1.Input(),
__metadata("design:type", Number)
], AutoComplete.prototype, "minLength", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Number)
], AutoComplete.prototype, "delay", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Object)
], AutoComplete.prototype, "style", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", String)
], AutoComplete.prototype, "styleClass", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Object)
], AutoComplete.prototype, "inputStyle", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", String)
], AutoComplete.prototype, "inputId", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", String)
], AutoComplete.prototype, "inputStyleClass", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", String)
], AutoComplete.prototype, "placeholder", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Boolean)
], AutoComplete.prototype, "readonly", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Boolean)
], AutoComplete.prototype, "disabled", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Number)
], AutoComplete.prototype, "maxlength", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Boolean)
], AutoComplete.prototype, "required", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Number)
], AutoComplete.prototype, "size", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Object)
], AutoComplete.prototype, "appendTo", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Boolean)
], AutoComplete.prototype, "autoHighlight", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Boolean)
], AutoComplete.prototype, "forceSelection", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", String)
], AutoComplete.prototype, "type", void 0);
__decorate([
core_1.Output(),
__metadata("design:type", core_1.EventEmitter)
], AutoComplete.prototype, "completeMethod", void 0);
__decorate([
core_1.Output(),
__metadata("design:type", core_1.EventEmitter)
], AutoComplete.prototype, "onSelect", void 0);
__decorate([
core_1.Output(),
__metadata("design:type", core_1.EventEmitter)
], AutoComplete.prototype, "onUnselect", void 0);
__decorate([
core_1.Output(),
__metadata("design:type", core_1.EventEmitter)
], AutoComplete.prototype, "onFocus", void 0);
__decorate([
core_1.Output(),
__metadata("design:type", core_1.EventEmitter)
], AutoComplete.prototype, "onBlur", void 0);
__decorate([
core_1.Output(),
__metadata("design:type", core_1.EventEmitter)
], AutoComplete.prototype, "onDropdownClick", void 0);
__decorate([
core_1.Output(),
__metadata("design:type", core_1.EventEmitter)
], AutoComplete.prototype, "onClear", void 0);
__decorate([
core_1.Output(),
__metadata("design:type", core_1.EventEmitter)
], AutoComplete.prototype, "onKeyUp", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", String)
], AutoComplete.prototype, "field", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", String)
], AutoComplete.prototype, "scrollHeight", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Boolean)
], AutoComplete.prototype, "dropdown", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", String)
], AutoComplete.prototype, "dropdownMode", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Boolean)
], AutoComplete.prototype, "multiple", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Number)
], AutoComplete.prototype, "tabindex", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", String)
], AutoComplete.prototype, "dataKey", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", String)
], AutoComplete.prototype, "emptyMessage", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Boolean)
], AutoComplete.prototype, "immutable", void 0);
__decorate([
core_1.ViewChild('in'),
__metadata("design:type", core_1.ElementRef)
], AutoComplete.prototype, "inputEL", void 0);
__decorate([
core_1.ViewChild('multiIn'),
__metadata("design:type", core_1.ElementRef)
], AutoComplete.prototype, "multiInputEL", void 0);
__decorate([
core_1.ViewChild('panel'),
__metadata("design:type", core_1.ElementRef)
], AutoComplete.prototype, "panelEL", void 0);
__decorate([
core_1.ViewChild('multiContainer'),
__metadata("design:type", core_1.ElementRef)
], AutoComplete.prototype, "multiContainerEL", void 0);
__decorate([
core_1.ContentChildren(shared_1.PrimeTemplate),
__metadata("design:type", core_1.QueryList)
], AutoComplete.prototype, "templates", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Array),
__metadata("design:paramtypes", [Array])
], AutoComplete.prototype, "suggestions", null);
AutoComplete = __decorate([
core_1.Component({
selector: 'p-autoComplete',
template: "\n <span [ngClass]=\"{'ui-autocomplete ui-widget':true,'ui-autocomplete-dd':dropdown,'ui-autocomplete-multiple':multiple}\" [ngStyle]=\"style\" [class]=\"styleClass\">\n <input *ngIf=\"!multiple\" #in [attr.type]=\"type\" [attr.id]=\"inputId\" [ngStyle]=\"inputStyle\" [class]=\"inputStyleClass\" autocomplete=\"off\" [attr.required]=\"required\"\n [ngClass]=\"'ui-inputtext ui-widget ui-state-default ui-corner-all ui-autocomplete-input'\" [value]=\"inputFieldValue\"\n (click)=\"onInputClick($event)\" (input)=\"onInput($event)\" (keydown)=\"onKeydown($event)\" (keyup)=\"onKeyup($event)\" (focus)=\"onInputFocus($event)\" (blur)=\"onInputBlur($event)\"\n [attr.placeholder]=\"placeholder\" [attr.size]=\"size\" [attr.maxlength]=\"maxlength\" [attr.tabindex]=\"tabindex\" [readonly]=\"readonly\" [disabled]=\"disabled\"\n ><ul *ngIf=\"multiple\" #multiContainer class=\"ui-autocomplete-multiple-container ui-widget ui-inputtext ui-state-default ui-corner-all\" [ngClass]=\"{'ui-state-disabled':disabled,'ui-state-focus':focus}\" (click)=\"multiIn.focus()\">\n <li #token *ngFor=\"let val of value\" class=\"ui-autocomplete-token ui-state-highlight ui-corner-all\">\n <span class=\"ui-autocomplete-token-icon fa fa-fw fa-close\" (click)=\"removeItem(token)\" *ngIf=\"!disabled\"></span>\n <span *ngIf=\"!selectedItemTemplate\" class=\"ui-autocomplete-token-label\">{{field ? val[field] : val}}</span>\n <ng-template *ngIf=\"selectedItemTemplate\" [pTemplateWrapper]=\"selectedItemTemplate\" [item]=\"val\"></ng-template>\n </li>\n <li class=\"ui-autocomplete-input-token\">\n <input #multiIn [attr.type]=\"type\" [attr.id]=\"inputId\" [disabled]=\"disabled\" [attr.placeholder]=\"(value&&value.length ? null : placeholder)\" [attr.tabindex]=\"tabindex\" (input)=\"onInput($event)\" (click)=\"onInputClick($event)\"\n (keydown)=\"onKeydown($event)\" (keyup)=\"onKeyup($event)\" (focus)=\"onInputFocus($event)\" (blur)=\"onInputBlur($event)\" autocomplete=\"off\" [ngStyle]=\"inputStyle\" [class]=\"inputStyleClass\">\n </li>\n </ul\n ><i *ngIf=\"loading\" class=\"ui-autocomplete-loader fa fa-circle-o-notch fa-spin fa-fw\"></i><button type=\"button\" pButton icon=\"fa-fw fa-caret-down\" class=\"ui-autocomplete-dropdown\" [disabled]=\"disabled\"\n (click)=\"handleDropdownClick($event)\" *ngIf=\"dropdown\"></button>\n <div #panel class=\"ui-autocomplete-panel ui-widget-content ui-corner-all ui-shadow\" [style.display]=\"panelVisible ? 'block' : 'none'\" [style.width]=\"appendTo ? 'auto' : '100%'\" [style.max-height]=\"scrollHeight\">\n <ul class=\"ui-autocomplete-items ui-autocomplete-list ui-widget-content ui-widget ui-corner-all ui-helper-reset\" *ngIf=\"panelVisible\">\n <li *ngFor=\"let option of suggestions; let idx = index\" [ngClass]=\"{'ui-autocomplete-list-item ui-corner-all':true,'ui-state-highlight':(highlightOption==option)}\"\n (mouseenter)=\"highlightOption=option\" (mouseleave)=\"highlightOption=null\" (click)=\"selectItem(option)\">\n <span *ngIf=\"!itemTemplate\">{{field ? option[field] : option}}</span>\n <ng-template *ngIf=\"itemTemplate\" [pTemplateWrapper]=\"itemTemplate\" [item]=\"option\" [index]=\"idx\"></ng-template>\n </li>\n <li *ngIf=\"noResults && emptyMessage\" class=\"ui-autocomplete-list-item ui-corner-all\">{{emptyMessage}}</li>\n </ul>\n </div>\n </span>\n ",
host: {
'[class.ui-inputwrapper-filled]': 'filled',
'[class.ui-inputwrapper-focus]': 'focus'
},
providers: [domhandler_1.DomHandler, objectutils_1.ObjectUtils, exports.AUTOCOMPLETE_VALUE_ACCESSOR]
}),
__metadata("design:paramtypes", [core_1.ElementRef, domhandler_1.DomHandler, core_1.Renderer2, objectutils_1.ObjectUtils, core_1.ChangeDetectorRef, core_1.IterableDiffers])
], AutoComplete);
exports.AutoComplete = AutoComplete;
var AutoCompleteModule = (function () {
function AutoCompleteModule() {
}
return AutoCompleteModule;
}());
AutoCompleteModule = __decorate([
core_1.NgModule({
imports: [common_1.CommonModule, inputtext_1.InputTextModule, button_1.ButtonModule, shared_1.SharedModule],
exports: [AutoComplete, shared_1.SharedModule],
declarations: [AutoComplete]
})
], AutoCompleteModule);
exports.AutoCompleteModule = AutoCompleteModule;
//# sourceMappingURL=autocomplete.js.map