ngx-monitorias-uniandes-lib
Version:
This library is used for Monitorias-Uniandes system.
171 lines • 8.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var forms_1 = require("@angular/forms");
var Observable_1 = require("rxjs/Observable");
var of_1 = require("rxjs/observable/of");
require("rxjs/add/operator/map");
require("rxjs/add/observable/empty");
require("rxjs/add/operator/mergeMap");
require("rxjs/add/operator/switchMap");
require("rxjs/add/operator/debounceTime");
require("rxjs/add/operator/distinctUntilChanged");
var search_combo_box_generic_local_model_1 = require("./search-combo-box-generic-local.model");
var ngx_bootstrap_1 = require("ngx-bootstrap");
var core_2 = require("@ngx-translate/core");
var SearchComboBoxGenericLocalComponent = /** @class */ (function () {
function SearchComboBoxGenericLocalComponent(cd, translate) {
var _this = this;
this.cd = cd;
this.translate = translate;
this.selectFirst = false;
this.disabled = false;
this.clearInput = false;
this.itemSelected = new core_1.EventEmitter();
this.noResults = new core_1.EventEmitter();
this.typeLoading = new core_1.EventEmitter();
this.dataSource = Observable_1.Observable.create(function (observer) {
observer.next(_this.cleanToken(_this.asyncDataSelected));
}).mergeMap(function (token) {
if (!_this.clearDataTypeObjectIfEmpty(token)) {
var dataObserver = _this.searchItems(_this.cleanToken(token), _this.componentModel);
return dataObserver;
}
return Observable_1.Observable.empty();
});
}
Object.defineProperty(SearchComboBoxGenericLocalComponent.prototype, "componentModelData", {
set: function (componentModelData) {
if (componentModelData) {
this.componentModel = componentModelData;
}
else {
this.componentModel = new search_combo_box_generic_local_model_1.SearchComboBoxGenericLocalModel([], null, "name", "search", 10, true);
}
},
enumerable: true,
configurable: true
});
SearchComboBoxGenericLocalComponent.prototype.ngOnInit = function () {
if (this.selectFirst) {
this.setInitialValue(null);
}
this.setPreSelectedValue();
};
SearchComboBoxGenericLocalComponent.prototype.ngOnChanges = function (changes) {
if (changes.clearInput && changes.clearInput.currentValue) {
this.asyncDataSelected = '';
}
if (changes.preSelectedValue && changes.preSelectedValue.previousValue !== undefined) {
this.preSelectedValue = changes.preSelectedValue.currentValue;
this.setPreSelectedValue();
}
};
SearchComboBoxGenericLocalComponent.prototype.searchItems = function (token, componentModel) {
var dataSourceLocal = componentModel.dataSource[componentModel.collectionName] ? componentModel.dataSource[componentModel.collectionName] : componentModel.dataSource;
return of_1.of(dataSourceLocal.filter(function (obj) {
var query = new RegExp(token, 'ig');
var value = obj[componentModel.optionField] ? obj[componentModel.optionField] : obj;
return query.test(value);
}));
};
SearchComboBoxGenericLocalComponent.prototype.selectItem = function (event) {
this.itemSelected.emit(event);
};
SearchComboBoxGenericLocalComponent.prototype.typeaheadNoResults = function (event) {
if (event) {
this.noResults.emit(event);
}
};
SearchComboBoxGenericLocalComponent.prototype.typeaheadLoading = function (event) {
this.typeLoading.emit(event);
};
SearchComboBoxGenericLocalComponent.prototype.setPreSelectedValue = function () {
var _this = this;
if (this.preSelectedValue !== undefined && this.preSelectedValue !== '') {
this.searchItems(this.preSelectedValue, this.componentModel).subscribe(function (values) {
if (values.length > 0) {
_this.asyncDataSelected = values[0][_this.componentModel.optionField];
_this.selectItem(new ngx_bootstrap_1.TypeaheadMatch(values[0], _this.asyncDataSelected, false));
}
else {
delete _this.asyncDataSelected;
_this.selectItem(new ngx_bootstrap_1.TypeaheadMatch(undefined, _this.asyncDataSelected, false));
}
});
}
};
SearchComboBoxGenericLocalComponent.prototype.setInitialValue = function (selectedAttribute) {
var _this = this;
this.searchItems("", this.componentModel).subscribe(function (values) {
if (selectedAttribute) {
values.forEach(function (element) {
if (element[selectedAttribute['name']] == selectedAttribute['value']) {
_this.asyncDataSelected = element[_this.componentModel.optionField];
}
});
}
else {
if (values.length > 0) {
_this.asyncDataSelected = values[0][_this.componentModel.optionField];
_this.selectItem(new ngx_bootstrap_1.TypeaheadMatch(values[0], _this.asyncDataSelected, false));
_this.cd.detectChanges();
}
}
});
};
SearchComboBoxGenericLocalComponent.prototype.cleanToken = function (token) {
var invalid = /[°"§%()\[\]{}=\\?´`'#<>|,;:+_]+/g;
if (token !== undefined && token !== null) {
if (typeof token === 'string') {
return token.replace(invalid, "");
}
else if (token.value !== undefined && token.value !== null) {
return token.value.replace(invalid, "");
}
else {
return "";
}
}
else
return undefined;
};
SearchComboBoxGenericLocalComponent.prototype.clearDataTypeObjectIfEmpty = function (token) {
if (token === '') {
delete this.asyncDataSelected;
this.selectItem(new ngx_bootstrap_1.TypeaheadMatch(undefined, this.asyncDataSelected, false));
return true;
}
else {
return false;
}
};
SearchComboBoxGenericLocalComponent.decorators = [
{ type: core_1.Component, args: [{
selector: 'app-search-combo-box-generic-local',
template: "\n <input #typeAhead [formControl]=\"itemControl\" [(ngModel)]=\"asyncDataSelected\" [typeahead]=\"dataSource\"\n [typeaheadOptionsLimit]=\"componentModel.optionsLimit\" [typeaheadMinLength]=\"0\"\n [typeaheadScrollable]=\"componentModel.isScrollable\" [typeaheadItemTemplate]=\"customTemplate\"\n (typeaheadNoResults)=\"typeaheadNoResults($event)\" (typeaheadLoading)=\"typeaheadLoading($event)\"\n (typeaheadOnSelect)=\"selectItem($event)\" [typeaheadOptionField]=\"componentModel.optionField\"\n placeholder=\"{{componentModel.placeholder|translate}}\" [typeaheadWaitMs]=\"300\" [disabled]=\"disabled\"\n class=\"form-control\" autocomplete=\"off\">\n ",
styles: ["\n\n "]
},] },
];
/** @nocollapse */
SearchComboBoxGenericLocalComponent.ctorParameters = function () { return [
{ type: core_1.ChangeDetectorRef, },
{ type: core_2.TranslateService, },
]; };
SearchComboBoxGenericLocalComponent.propDecorators = {
"componentModelData": [{ type: core_1.Input, args: ['componentModelData',] },],
"itemControl": [{ type: core_1.Input },],
"customTemplate": [{ type: core_1.Input },],
"preSelectedValue": [{ type: core_1.Input },],
"isScrollable": [{ type: core_1.Input },],
"selectFirst": [{ type: core_1.Input },],
"disabled": [{ type: core_1.Input },],
"clearInput": [{ type: core_1.Input },],
"itemSelected": [{ type: core_1.Output },],
"noResults": [{ type: core_1.Output },],
"typeLoading": [{ type: core_1.Output },],
};
return SearchComboBoxGenericLocalComponent;
}());
exports.SearchComboBoxGenericLocalComponent = SearchComboBoxGenericLocalComponent;
//# sourceMappingURL=search-combo-box-generic-local.component.js.map