abm-imagenes.component.ts
3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { Component, OnInit } from '@angular/core';
import { appSettings } from 'src/etc/AppSettings';
import { ImagenesService } from 'src/app/services/imagenes.service';
import { Producto } from 'src/app/wrappers/producto';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-amb-imagenes',
templateUrl: './abm-imagenes.component.html',
styleUrls: ['./abm-imagenes.component.scss']
})
export class AbmImagenesComponent implements OnInit {
apiImagenes = appSettings.apiImagenes;
articulos: Producto[] = [];
private auxProductos: Producto[] = [];
private searchTerm: string = '';
private paginationData: any;
private disabledPaginador: boolean = false;
constructor(private imagenService: ImagenesService, private http: HttpClient) { }
ngOnInit() {
this.imagenService.getAllWithPaginator()
.subscribe((res) => {
this.articulos = res.data;
this.paginationData = res.pagination;
this.filterItems();
}, error => console.error(error));
}
onFileSelected(event, articulo: Producto) {
let auxFiles: FileList = event.target.files;
Array.from(auxFiles).forEach(file => {
this.onLoad(file)
.then(result => {
// articulo.imagenes.push({
// name: file.name + articulo.CodArt + articulo.CodSec,
// fromGallery: true,
// imagen: result,
// id_articulo: articulo.id
// });
let imagenAguardar = {
imagen: {
name: `${articulo.CodSec}${articulo.CodArt}${file.name}`,
base64: result,
codigo: articulo.CodArt,
sector: articulo.CodSec,
id_articulo: articulo.id
},
articulo: articulo
};
this.saveInBase(imagenAguardar);
});
})
}
filterItems() {
this.auxProductos = this.articulos.filter(x => {
return x.DET_LAR.toLowerCase().includes(this.searchTerm.toLowerCase()) ||
x.CodArt.toString().includes(this.searchTerm.toLowerCase()) ||
x.CodSec.toString().includes(this.searchTerm.toLowerCase());
});
}
saveInBase(imagenAguardar) {
this.imagenService.saveInBase(imagenAguardar.imagen)
.subscribe(res => {
imagenAguardar.imagen['id'] = res[0];
imagenAguardar.imagen['fromGallery'] = true;
imagenAguardar.articulo.imagenes.push(imagenAguardar.imagen);
}, error => console.error(error));
}
onLoad(file) {
return new Promise((resolve, reject) => {
var fr = new FileReader();
fr.onload = function () {
resolve(fr.result);
};
fr.readAsDataURL(file);
});
}
deleteImage(imagenes, index: number) {
if (!imagenes[index].name) {
imagenes[index].name = imagenes[index].imagen;
}
this.imagenService.deleteImage(imagenes[index])
.subscribe(res => {
if (res) {
imagenes.splice(index, 1);
}
}, error => console.error(error));
}
pageChanged(event: any): void {
this.disabledPaginador = true;
this.imagenService.getAllWithPaginator(event.page)
.subscribe((res) => {
this.disabledPaginador = false;
this.articulos = res.data;
this.paginationData = res.pagination;
this.filterItems();
}, error => console.error(error));
}
}