Commit 189a3b3af84ff6b7063ea39ecfdce10657559552
1 parent
d4e595559f
Exists in
master
Codigo identado.
Showing
1 changed file
with
2 additions
and
2 deletions
Show diff stats
src/app/components/amb-imagenes/amb-imagenes.component.ts
| 1 | import { Component, OnInit } from '@angular/core'; | 1 | import { Component, OnInit } from '@angular/core'; |
| 2 | import { appSettings } from 'src/etc/AppSettings'; | 2 | import { appSettings } from 'src/etc/AppSettings'; |
| 3 | import { ProductoService } from 'src/app/services/producto.service'; | 3 | import { ProductoService } from 'src/app/services/producto.service'; |
| 4 | import { Producto } from 'src/app/wrappers/producto'; | 4 | import { Producto } from 'src/app/wrappers/producto'; |
| 5 | import { HttpClient } from '@angular/common/http'; | 5 | import { HttpClient } from '@angular/common/http'; |
| 6 | 6 | ||
| 7 | @Component({ | 7 | @Component({ |
| 8 | selector: 'app-amb-imagenes', | 8 | selector: 'app-amb-imagenes', |
| 9 | templateUrl: './amb-imagenes.component.html', | 9 | templateUrl: './amb-imagenes.component.html', |
| 10 | styleUrls: ['./amb-imagenes.component.scss'] | 10 | styleUrls: ['./amb-imagenes.component.scss'] |
| 11 | }) | 11 | }) |
| 12 | 12 | ||
| 13 | export class AmbImagenesComponent implements OnInit { | 13 | export class AmbImagenesComponent implements OnInit { |
| 14 | 14 | ||
| 15 | apiUrl = appSettings.apiUrl; | 15 | apiUrl = appSettings.apiUrl; |
| 16 | articulos: Producto[] = []; | 16 | articulos: Producto[] = []; |
| 17 | private auxProductos: Producto[] = []; | 17 | private auxProductos: Producto[] = []; |
| 18 | private searchTerm: string = ''; | 18 | private searchTerm: string = ''; |
| 19 | 19 | ||
| 20 | constructor(private productoService: ProductoService, private http: HttpClient) {} | 20 | constructor(private productoService: ProductoService, private http: HttpClient) { } |
| 21 | 21 | ||
| 22 | ngOnInit() { | 22 | ngOnInit() { |
| 23 | 23 | ||
| 24 | this.productoService.getAll().subscribe((productos: Producto[]) => { | 24 | this.productoService.getAll().subscribe((productos: Producto[]) => { |
| 25 | this.articulos = productos; | 25 | this.articulos = productos; |
| 26 | this.filterItems(); | 26 | this.filterItems(); |
| 27 | }); | 27 | }); |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | this.articulos = productos; | 30 | this.articulos = productos; |
| 31 | this.filterItems(); | 31 | this.filterItems(); |
| 32 | }, error => console.error(error)); | 32 | }, error => console.error(error)); |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | onFileSelected(event, articulo: Producto) { | 35 | onFileSelected(event, articulo: Producto) { |
| 36 | 36 | ||
| 37 | let auxFiles: FileList = event.target.files; | 37 | let auxFiles: FileList = event.target.files; |
| 38 | Array.from(auxFiles).forEach(file => { | 38 | Array.from(auxFiles).forEach(file => { |
| 39 | 39 | ||
| 40 | this.onLoad(file) | 40 | this.onLoad(file) |
| 41 | .then(result => { | 41 | .then(result => { |
| 42 | 42 | ||
| 43 | articulo.imagenes.push({ | 43 | articulo.imagenes.push({ |
| 44 | name: file.name, | 44 | name: file.name, |
| 45 | fromGallery: true, | 45 | fromGallery: true, |
| 46 | imagen: result, | 46 | imagen: result, |
| 47 | id_articulo: articulo.id | 47 | id_articulo: articulo.id |
| 48 | }); | 48 | }); |
| 49 | 49 | ||
| 50 | this.saveInBase({ | 50 | this.saveInBase({ |
| 51 | name: file.name, | 51 | name: file.name, |
| 52 | base64: result, | 52 | base64: result, |
| 53 | codigo: articulo.CodArt, | 53 | codigo: articulo.CodArt, |
| 54 | sector: articulo.CodSec | 54 | sector: articulo.CodSec |
| 55 | }); | 55 | }); |
| 56 | }); | 56 | }); |
| 57 | }) | 57 | }) |
| 58 | } | 58 | } |
| 59 | 59 | ||
| 60 | filterItems() { | 60 | filterItems() { |
| 61 | 61 | ||
| 62 | this.auxProductos = this.articulos.filter(x => { | 62 | this.auxProductos = this.articulos.filter(x => { |
| 63 | return x.DET_LAR.toLowerCase().includes(this.searchTerm.toLowerCase()) | 63 | return x.DET_LAR.toLowerCase().includes(this.searchTerm.toLowerCase()) |
| 64 | }); | 64 | }); |
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | 67 | ||
| 68 | saveInBase(img) { | 68 | saveInBase(img) { |
| 69 | 69 | ||
| 70 | this.productoService.saveInBase(img) | 70 | this.productoService.saveInBase(img) |
| 71 | .subscribe(data => { | 71 | .subscribe(data => { |
| 72 | 72 | ||
| 73 | }, error => console.error(error)); | 73 | }, error => console.error(error)); |
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | onLoad(file) { | 76 | onLoad(file) { |
| 77 | 77 | ||
| 78 | return new Promise((resolve, reject) => { | 78 | return new Promise((resolve, reject) => { |
| 79 | 79 | ||
| 80 | var fr = new FileReader(); | 80 | var fr = new FileReader(); |
| 81 | 81 | ||
| 82 | fr.onload = function() { | 82 | fr.onload = function () { |
| 83 | 83 | ||
| 84 | resolve(fr.result); | 84 | resolve(fr.result); |
| 85 | }; | 85 | }; |
| 86 | 86 | ||
| 87 | fr.readAsDataURL(file); | 87 | fr.readAsDataURL(file); |
| 88 | }); | 88 | }); |
| 89 | 89 | ||
| 90 | } | 90 | } |
| 91 | 91 | ||
| 92 | deleteImage(imagenes, index: number) { | 92 | deleteImage(imagenes, index: number) { |
| 93 | 93 | ||
| 94 | if (!imagenes[index].name) { | 94 | if (!imagenes[index].name) { |
| 95 | imagenes[index].name = imagenes[index].imagen; | 95 | imagenes[index].name = imagenes[index].imagen; |
| 96 | } | 96 | } |
| 97 | 97 | ||
| 98 | this.productoService.deleteImage(imagenes[index]) | 98 | this.productoService.deleteImage(imagenes[index]) |
| 99 | .subscribe(res => { | 99 | .subscribe(res => { |
| 100 | 100 | ||
| 101 | if (res) { | 101 | if (res) { |
| 102 | imagenes.splice(index, 1); | 102 | imagenes.splice(index, 1); |
| 103 | } | 103 | } |
| 104 | }, error => console.error(error)); | 104 | }, error => console.error(error)); |
| 105 | } | 105 | } |
| 106 | 106 | ||
| 107 | } | 107 | } |
| 108 | 108 |