UNPKG

groupcenter-dropdown-moneda-frontend

Version:
283 lines (277 loc) 9.32 kB
import { Component, EventEmitter, Injectable, Input, NgModule, Output, Renderer, ViewChild, forwardRef } from '@angular/core'; import { CommonModule } from '@angular/common'; import { Observable as Observable$1 } from 'rxjs/Observable'; import { BaseService, ModelosBaseModule } from 'groupcenter-modelos-base-frontend'; import { Http, HttpModule } from '@angular/http'; import 'rxjs/add/observable/of'; import { FormsModule, NG_VALUE_ACCESSOR } from '@angular/forms'; var __extends = (undefined && undefined.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var filterOptions = [ { code: 'usd', description: '(Dólar Estadounidense)' }, { code: 'cop', description: '(Peso Colombiano)' } ]; var MonedaService = (function (_super) { __extends(MonedaService, _super); /** * @param {?} http */ function MonedaService(http$$1) { var _this = _super.call(this, http$$1, '/gc-producto') || this; _this.URL_MONEDA = '/reverse/catalog/policy/'; return _this; } /** * @param {?} config * @return {?} */ MonedaService.prototype.setCredentials = function (config) { this.user = config.USUARIOGW; this.token = config.PASSWORDGW; if (config.CONTEXT !== null && config.CONTEXT !== undefined) { _super.prototype.setContext.call(this, config.CONTEXT); } }; /** * @param {?} codigoPais * @return {?} */ MonedaService.prototype.getMonedas = function (codigoPais) { if (!codigoPais || codigoPais === '') { return Observable$1.of([]); } return this.doPost(this.URL_MONEDA, this.generateJson(codigoPais), this.user, this.token); }; /** * @param {?} response * @return {?} */ MonedaService.prototype.mapper = function (response) { var /** @type {?} */ resultados = response.json().result.typelist; var /** @type {?} */ monedas = resultados.map(function (resultado) { var /** @type {?} */ miMoneda = filterOptions.filter(function (currency) { return resultado.code.toLowerCase() === currency.code; }) .shift(); return ({ codigoMoneda: resultado.code, nombreMoneda: resultado.name.trim().toLowerCase().length === resultado.code.trim().toLowerCase().length ? resultado.name.toUpperCase() + ' - ' + miMoneda.description : resultado.name }); }); return monedas; }; /** * @param {?} codigoPais * @return {?} */ MonedaService.prototype.generateJson = function (codigoPais) { return { 'id': '2', 'method': 'getTypelist', 'params': [ { 'countryCode': codigoPais, 'typeListCode': 'Currency' } ], 'jsonrpc': '2.0' }; }; return MonedaService; }(BaseService)); MonedaService.decorators = [ { type: Injectable }, ]; /** * @nocollapse */ MonedaService.ctorParameters = function () { return [ { type: Http, }, ]; }; // import { Moneda } from 'groupcenter-modelos-base-frontend'; var noop = function () { }; var DropdownMonedaComponentValueAccessor = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(function () { return DropdownMonedaComponent; }), multi: true }; var DropdownMonedaComponent = (function () { /** * @param {?} MonedaService * @param {?} renderer */ function DropdownMonedaComponent(MonedaService$$1, renderer) { this.MonedaService = MonedaService$$1; this.renderer = renderer; this.monedas = []; this.monedasFiltradas = []; this._value = ''; this.dText = 'Seleccione una moneda'; this.dName = 'moneda'; this.objectSelected = new EventEmitter(); this._onTouchedCallback = noop; } Object.defineProperty(DropdownMonedaComponent.prototype, "value", { /** * @return {?} */ get: function () { return this._value; }, /** * @param {?} value * @return {?} */ set: function (value) { if (value !== this._value) { this._value = value; this.updateSelectedValue(); } }, enumerable: true, configurable: true }); /** * @param {?} value * @return {?} */ DropdownMonedaComponent.prototype.writeValue = function (value) { this._value = value; this.updateSelectedValue(); }; /** * @param {?} isDisabled * @return {?} */ DropdownMonedaComponent.prototype.setDisabledState = function (isDisabled) { this.renderer.setElementProperty(this.dropdown.nativeElement, 'disabled', isDisabled); }; /** * @return {?} */ DropdownMonedaComponent.prototype.updateSelectedValue = function () { var _this = this; this.selectedObject = this.monedas ? this.monedas.find(function (moneda) { return moneda.codigoMoneda === _this.value; }) : undefined; if (this.selectedObject) { this.objectSelected.emit(this.selectedObject); } }; /** * @param {?} changes * @return {?} */ DropdownMonedaComponent.prototype.ngOnChanges = function (changes) { var _this = this; if (this.codigoPais !== undefined) { this.MonedaService.setCredentials(this.config); this.MonedaService.getMonedas(this.codigoPais).subscribe(function (datos) { _this.monedas = datos.sort(function (a, b) { var /** @type {?} */ nameA = a.nombreMoneda.toLowerCase(), /** @type {?} */ nameB = b.nombreMoneda.toLowerCase(); if (nameA < nameB) { return -1; } if (nameA > nameB) { return 1; } return 0; }); _this.monedas.map(function (c) { c.codigoPais = _this.codigoPais; }); _this.monedasFiltradas = _this.monedas; }, function (errores) { return console.log(/** @type {?} */ (errores)); }); } }; /** * @return {?} */ DropdownMonedaComponent.prototype.ngOnInit = function () { console.log('Iniciando Dropdown Moneda'); }; /** * @param {?} fn * @return {?} */ DropdownMonedaComponent.prototype.registerOnChange = function (fn) { }; /** * @param {?} fn * @return {?} */ DropdownMonedaComponent.prototype.registerOnTouched = function (fn) { this.onTouched = fn; }; /** * @return {?} */ DropdownMonedaComponent.prototype.onTouched = function () { this._onTouchedCallback(null); }; return DropdownMonedaComponent; }()); DropdownMonedaComponent.decorators = [ { type: Component, args: [{ selector: 'dropdown-moneda', template: "<select #dropdown class=\"form-control\" name=\"{{ dName }}\" [(ngModel)]=\"value\" (ngModelChange)=\"updateSelectedValue()\"> <option disabled hidden [value]=\"'undefined'\">{{ dText }}</option> <option *ngFor=\"let moneda of monedasFiltradas\" [value]=\"moneda.codigoMoneda\"> {{ moneda.nombreMoneda }} </option> </select>", providers: [DropdownMonedaComponentValueAccessor] },] }, ]; /** * @nocollapse */ DropdownMonedaComponent.ctorParameters = function () { return [ { type: MonedaService, }, { type: Renderer, }, ]; }; DropdownMonedaComponent.propDecorators = { 'dropdown': [{ type: ViewChild, args: ['dropdown',] },], 'dText': [{ type: Input },], 'dName': [{ type: Input },], 'config': [{ type: Input },], 'codigoPais': [{ type: Input },], 'objectSelected': [{ type: Output },], }; var DropdownMonedaModule = (function () { function DropdownMonedaModule() { } /** * @return {?} */ DropdownMonedaModule.forRoot = function () { return { ngModule: DropdownMonedaModule, providers: [MonedaService] }; }; return DropdownMonedaModule; }()); DropdownMonedaModule.decorators = [ { type: NgModule, args: [{ imports: [ CommonModule, FormsModule, HttpModule, ModelosBaseModule.forRoot() ], declarations: [ DropdownMonedaComponent ], exports: [ DropdownMonedaComponent ] },] }, ]; /** * @nocollapse */ DropdownMonedaModule.ctorParameters = function () { return []; }; export { DropdownMonedaModule };