frontend/src/app/components/editor/database/columns/columns.component.ts

97 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-02-09 05:09:46 +00:00
import {Component, Input, OnInit} from '@angular/core';
import {AlertController, ModalController} from '@ionic/angular';
import {uuid_v4} from '../../../../utility';
2020-02-09 05:09:46 +00:00
@Component({
selector: 'editor-database-columns',
templateUrl: './columns.component.html',
styleUrls: ['./columns.component.scss'],
})
export class ColumnsComponent implements OnInit {
2020-02-18 17:19:05 +00:00
@Input() columnSets: Array<{headerName: string, field: string, Type: string, additionalData: any}> = [];
2020-02-09 05:09:46 +00:00
constructor(
protected modals: ModalController,
protected alerts: AlertController,
2020-02-09 05:09:46 +00:00
) { }
ngOnInit() {}
onAddColumnClick() {
this.columnSets.push({headerName: '', field: uuid_v4(), Type: '', additionalData: {}});
2020-02-18 17:19:05 +00:00
}
onAddOption(i) {
const set = this.columnSets[i];
if ( !Array.isArray(set.additionalData.options) ) {
set.additionalData.options = [];
}
set.additionalData.options.push({value: ''});
}
onDeleteOptionClick(i, n) {
const set = this.columnSets[i];
set.additionalData.options = set.additionalData.options.filter((x, index) => index !== n);
2020-02-09 05:09:46 +00:00
}
dismissModal(doSave = true) {
if ( doSave ) {
this.columnSets = this.columnSets.map(x => {
if ( !x.field ) {
x.field = uuid_v4();
}
2020-02-09 05:09:46 +00:00
return x;
});
2020-02-09 05:09:46 +00:00
this.modals.dismiss(this.columnSets);
} else {
this.modals.dismiss();
}
}
onDeleteClick(i) {
this.alerts.create({
header: 'Delete column?',
message: 'Are you sure you want to delete this column? Its data will be lost.',
buttons: [
{ text: 'Keep It', role: 'cancel' },
{
text: 'Delete It',
handler: () => {
this.columnSets = this.columnSets.filter((x, index) => {
return index !== i;
});
},
}
],
}).then(alert => alert.present());
2020-02-09 05:09:46 +00:00
}
onUpArrow(i) {
if ( this.columnSets[i - 1] ) {
const temp = this.columnSets[i];
this.columnSets[i] = this.columnSets[i - 1];
this.columnSets[i - 1] = temp;
}
}
onDownArrow(i) {
if ( this.columnSets[i + 1] ) {
const temp = this.columnSets[i];
this.columnSets[i] = this.columnSets[i + 1];
this.columnSets[i + 1] = temp;
}
}
iteratePin(i) {
if ( !this.columnSets[i].additionalData.pinned ) {
this.columnSets[i].additionalData.pinned = 'left';
} else if ( this.columnSets[i].additionalData.pinned === 'left' ) {
this.columnSets[i].additionalData.pinned = 'right';
} else {
delete this.columnSets[i].additionalData.pinned;
}
}
2020-02-09 05:09:46 +00:00
}