@mobsolutions/ngx-dadata
Version:
[](https://badge.fury.io/js/%40kolkov%2Fngx-dadata) [](https://travis-ci.org/kolkov/ngx-dadata) [![Coverage Stat
485 lines (475 loc) • 18.3 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/common/http'), require('rxjs'), require('rxjs/operators'), require('@angular/core'), require('@angular/common'), require('@angular/forms')) :
typeof define === 'function' && define.amd ? define('@mobsolutions/ngx-dadata', ['exports', '@angular/common/http', 'rxjs', 'rxjs/operators', '@angular/core', '@angular/common', '@angular/forms'], factory) :
(factory((global.mobsolutions = global.mobsolutions || {}, global.mobsolutions['ngx-dadata'] = {}),global.ng.common.http,global.rxjs,global.rxjs.operators,global.ng.core,global.ng.common,global.ng.forms));
}(this, (function (exports,i1,rxjs,operators,i0,common,forms) { 'use strict';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @enum {string} */
var DadataType = {
fio: 'fio',
address: 'address',
party: 'party',
bank: 'bank',
email: 'email',
};
var NgxDadataService = /** @class */ (function () {
function NgxDadataService(http) {
this.http = http;
this.apiKey = '';
}
/**
* @param {?} key
* @return {?}
*/
NgxDadataService.prototype.setApiKey = /**
* @param {?} key
* @return {?}
*/
function (key) {
this.apiKey = key;
};
/**
* @param {?} value
* @param {?=} type
* @param {?=} count
* @return {?}
*/
NgxDadataService.prototype.getData = /**
* @param {?} value
* @param {?=} type
* @param {?=} count
* @return {?}
*/
function (value, type, count) {
if (type === void 0) {
type = DadataType.address;
}
if (count === void 0) {
count = 10;
}
/** @type {?} */
var httpOptions = {
headers: new i1.HttpHeaders({
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: 'Token ' + this.apiKey,
})
};
/** @type {?} */
var body = { query: value, count: count };
return this.http.post('https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/' + type, body, httpOptions);
};
NgxDadataService.decorators = [
{ type: i0.Injectable, args: [{
providedIn: 'root'
},] }
];
/** @nocollapse */
NgxDadataService.ctorParameters = function () {
return [
{ type: i1.HttpClient }
];
};
/** @nocollapse */ NgxDadataService.ngInjectableDef = i0.defineInjectable({ factory: function NgxDadataService_Factory() { return new NgxDadataService(i0.inject(i1.HttpClient)); }, token: NgxDadataService, providedIn: "root" });
return NgxDadataService;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
var DadataConfigDefault = {
apiKey: '',
type: DadataType.address,
delay: 500,
limit: 10,
width: 'auto',
minWidth: '0',
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
var NGX_DADATA_VALUE_ACCESSOR = {
provide: forms.NG_VALUE_ACCESSOR,
useExisting: i0.forwardRef(( /**
* @return {?}
*/function () { return NgxDadataComponent; })),
multi: true
};
/*const NGX_DADATA_VALIDATOR = {
provide: NG_VALIDATORS,
useExisting: forwardRef(() => NgxDadataComponent),
multi: true,
};*/
/**
* @param {?} value
* @return {?}
*/
function createDaDataValidator(value) {
return ( /**
* @param {?} c
* @return {?}
*/function (c) {
/** @type {?} */
var err = {
rangeError: {
given: c.value,
expected: value,
}
};
return (c.value !== value) ? err : null;
});
}
var NgxDadataComponent = /** @class */ (function () {
function NgxDadataComponent(dataService, _r) {
this.dataService = dataService;
this._r = _r;
this.v = '';
this.currentFocus = -1;
this.data = [];
this.config = DadataConfigDefault;
this.disabled = null;
this.type = DadataType.address;
this.limit = DadataConfigDefault.limit;
this.placeholder = '';
this.selected = new i0.EventEmitter();
this.inputString$ = new rxjs.Subject();
// onSuggestionSelected = (value: string) => {};
this.onTouched = ( /**
* @return {?}
*/function () { });
this.propagateChange = ( /**
* @return {?}
*/function () { });
this.validateFn = ( /**
* @return {?}
*/function () { });
}
Object.defineProperty(NgxDadataComponent.prototype, "value", {
get: /**
* @return {?}
*/ function () {
return this.v;
},
set: /**
* @param {?} v
* @return {?}
*/ function (v) {
if (v !== this.v) {
this.v = v;
this.propagateChange(v);
}
},
enumerable: true,
configurable: true
});
/**
* @return {?}
*/
NgxDadataComponent.prototype.ngOnInit = /**
* @return {?}
*/
function () {
var _this = this;
/*this.validateFn = createDaDataValidator(this._value);
this.propagateChange(this._value);*/
this.type = this.config.type;
this.dataService.setApiKey(this.apiKey ? this.apiKey : this.config.apiKey);
this.inputString$.pipe(operators.debounce(( /**
* @return {?}
*/function () { return rxjs.timer(_this.config.delay ? _this.config.delay : 500); }))).subscribe(( /**
* @param {?} x
* @return {?}
*/function (x) {
_this.dataService.getData(x, _this.type, _this.limit).subscribe(( /**
* @param {?} y
* @return {?}
*/function (y) {
_this.data = y.suggestions;
}));
}));
};
/**
* @param {?} changes
* @return {?}
*/
NgxDadataComponent.prototype.ngOnChanges = /**
* @param {?} changes
* @return {?}
*/
function (changes) {
if (changes.value) ;
};
/**
* @param {?} value
* @return {?}
*/
NgxDadataComponent.prototype.getData = /**
* @param {?} value
* @return {?}
*/
function (value) {
this.inputString$.next(value);
this.currentFocus = -1;
};
/**
* @param {?} e
* @param {?} item
* @return {?}
*/
NgxDadataComponent.prototype.onClick = /**
* @param {?} e
* @param {?} item
* @return {?}
*/
function (e, item) {
// e.preventDefault();
this.inputValue.nativeElement.value = item.value;
this.propagateChange(item.value);
this.inputValue.nativeElement.focus();
this.selectedSuggestion = item;
this.data = [];
this.currentFocus = -1;
// this.writeValue(item.value);
this.selected.emit(item);
// this.selectedData.emit(item.data);
// this.selectedString.emit(item.value);
};
/**
* @return {?}
*/
NgxDadataComponent.prototype.onOutsideClick = /**
* @return {?}
*/
function () {
this.data = [];
};
/**
* @return {?}
*/
NgxDadataComponent.prototype.onArrowDown = /**
* @return {?}
*/
function () {
this.removeFocus(this.currentFocus);
if (this.currentFocus >= this.data.length - 1) {
this.currentFocus = 0;
}
else {
this.currentFocus++;
}
this.setFocus(this.currentFocus);
};
/**
* @return {?}
*/
NgxDadataComponent.prototype.onArrowUp = /**
* @return {?}
*/
function () {
this.removeFocus(this.currentFocus);
if (this.currentFocus === 0) {
this.currentFocus = this.data.length - 1;
}
else {
this.currentFocus--;
}
this.setFocus(this.currentFocus);
};
/**
* @return {?}
*/
NgxDadataComponent.prototype.onEnter = /**
* @return {?}
*/
function () {
this.selectedSuggestion = this.data[this.currentFocus];
this.inputValue.nativeElement.value = this.selectedSuggestion.value;
this.data = [];
this.currentFocus = -1;
this.propagateChange(this.selectedSuggestion.value);
// this.writeValue(this.selectedSuggestion.value);
this.selected.emit(this.selectedSuggestion);
// this.selectedData.emit(this.selectedSuggestion.data);
// this.selectedString.emit(this.selectedSuggestion.value);
};
/**
* @param {?} id
* @return {?}
*/
NgxDadataComponent.prototype.setFocus = /**
* @param {?} id
* @return {?}
*/
function (id) {
/** @type {?} */
var activeEl = document.getElementById(id + 'item');
this._r.addClass(activeEl, 'active');
};
/**
* @param {?} id
* @return {?}
*/
NgxDadataComponent.prototype.removeFocus = /**
* @param {?} id
* @return {?}
*/
function (id) {
if (id !== -1) {
/** @type {?} */
var activeEl = document.getElementById(id + 'item');
this._r.removeClass(activeEl, 'active');
}
};
/**
* @param {?} value
* @return {?}
*/
NgxDadataComponent.prototype.writeValue = /**
* @param {?} value
* @return {?}
*/
function (value) {
if (value !== undefined) {
this.v = value;
}
// this.onSuggestionSelected(value);
};
/**
* Set the function to be called
* when the control receives a change event.
*
* @param fn a function
*/
/**
* Set the function to be called
* when the control receives a change event.
*
* @param {?} fn a function
* @return {?}
*/
NgxDadataComponent.prototype.registerOnChange = /**
* Set the function to be called
* when the control receives a change event.
*
* @param {?} fn a function
* @return {?}
*/
function (fn) {
// this.onSuggestionSelected = fn;
this.propagateChange = fn;
};
/**
* Set the function to be called
* when the control receives a touch event.
*
* @param fn a function
*/
/**
* Set the function to be called
* when the control receives a touch event.
*
* @param {?} fn a function
* @return {?}
*/
NgxDadataComponent.prototype.registerOnTouched = /**
* Set the function to be called
* when the control receives a touch event.
*
* @param {?} fn a function
* @return {?}
*/
function (fn) {
this.onTouched = fn;
};
/**
* Implements disabled state for this element
*
* @param isDisabled
*/
/**
* Implements disabled state for this element
*
* @param {?} isDisabled
* @return {?}
*/
NgxDadataComponent.prototype.setDisabledState = /**
* Implements disabled state for this element
*
* @param {?} isDisabled
* @return {?}
*/
function (isDisabled) {
alert('disabled!');
this.disabled = isDisabled;
};
NgxDadataComponent.decorators = [
{ type: i0.Component, args: [{
selector: 'ngx-dadata',
template: "<div class=\"autocomplete\">\r\n <label for=\"address_input\"></label>\r\n <input [disabled]=\"disabled ? true : null\" type=\"text\" class=\"primary-input\" #inputValue (input)=\"getData(inputValue.value)\"\r\n [placeholder]=\"placeholder\" (keyup.ArrowDown)=\"onArrowDown()\" (keyup.ArrowUp)=\"onArrowUp()\"\r\n (keyup.Enter)=\"onEnter()\" spellcheck=\"false\" [(ngModel)]=\"value\" autocomplete=\"new-password\" />\r\n <div *ngIf=\"data.length\">\r\n <div class=\"autocomplete-items\">\r\n <div class=\"autocomplele-item\" *ngFor=\"let item of data;let i = index\" (click)=\"onClick($event, item)\" [id]=\"i+'item'\">\r\n {{item.value}}\r\n <ng-template [ngIf]=\"type==='party'\">\r\n <br/>\r\n <span>{{item.data?.inn}} {{item.data?.address?.value}}</span>\r\n </ng-template>\r\n </div>\r\n </div>\r\n </div>\r\n</div>\r\n",
providers: [NGX_DADATA_VALUE_ACCESSOR,],
styles: [".autocomplete{position:relative}.autocomplete-items{position:absolute;border:1px solid #d4d4d4;border-bottom:none;border-top:none;z-index:99;top:100%;left:0;right:0}.autocomplete-items .autocomplele-item{padding:5px 10px;cursor:pointer;background-color:#fff;border-bottom:1px solid #d4d4d4}.autocomplete-items .autocomplele-item:hover{background-color:#e9e9e9}.autocomplete-items .autocomplele-item.active{background-color:#1e90ff!important;color:#fff}.autocomplete-items .autocomplele-item span{color:#555;font-size:80%}"]
}] }
];
/** @nocollapse */
NgxDadataComponent.ctorParameters = function () {
return [
{ type: NgxDadataService },
{ type: i0.Renderer2 }
];
};
NgxDadataComponent.propDecorators = {
config: [{ type: i0.Input }],
apiKey: [{ type: i0.Input }],
disabled: [{ type: i0.Input }],
type: [{ type: i0.Input }],
limit: [{ type: i0.Input }],
placeholder: [{ type: i0.Input }],
selectedSuggestion: [{ type: i0.Output }],
selected: [{ type: i0.Output }],
inputValue: [{ type: i0.ViewChild, args: ['inputValue' /*, { static: true }*/,] }],
onOutsideClick: [{ type: i0.HostListener, args: ['document:click',] }]
};
return NgxDadataComponent;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var NgxDadataModule = /** @class */ (function () {
function NgxDadataModule() {
}
NgxDadataModule.decorators = [
{ type: i0.NgModule, args: [{
imports: [
common.CommonModule,
forms.FormsModule
],
declarations: [NgxDadataComponent],
exports: [NgxDadataComponent]
},] }
];
return NgxDadataModule;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
exports.DadataType = DadataType;
exports.NgxDadataService = NgxDadataService;
exports.createDaDataValidator = createDaDataValidator;
exports.NgxDadataComponent = NgxDadataComponent;
exports.NgxDadataModule = NgxDadataModule;
exports.DadataConfigDefault = DadataConfigDefault;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=mobsolutions-ngx-dadata.umd.js.map