src/app/home/home.component.ts
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import {
Component,
OnInit, ViewChild, ElementRef
} from '@angular/core';
import { Http, Response } from '@angular/http';
import { Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { PersonData, RelationShipData, relationShipTypeEnum, TimeNetDataService } from './timenet.service';
import { Router } from '@angular/router';
export interface RowData {
level: number;
id: string;
tag: string;
value: string;
}
@Component({
// The selector is what angular internally uses
// for `document.querySelectorAll(selector)` in our index.html
// where, in this case, selector is the string 'home'
selector: 'home', // <home></home>
// We need to tell Angular's Dependency Injection which providers are in our app.
providers: [ ],
// Our list of styles in our component. We may add more to compose many styles together
styleUrls: [ './home.component.css' ],
// Every Angular template is first compiled by the browser before Angular runs it's compiler
templateUrl: './home.component.html'
})
export class HomeComponent implements OnInit {
private chartData: any[];
@ViewChild('fileLoader') fileInput: ElementRef;
private dragging: boolean;
// TypeScript public modifiers
constructor(
public timeNetDataService: TimeNetDataService,
public router: Router
) {}
private dragCounter: number = 0;
private isLoading: boolean = false;
private fileName: string = '';
public ngOnInit() { // this.title.getData().subscribe(data => this.data = data);
}
handleDragEnter() {
this.dragCounter++;
this.dragging = true;
}
handleDragLeave() {
this.dragCounter--;
if(this.dragCounter===0)
this.dragging = false;
}
handleDrop(e) {
e.preventDefault();
this.dragging = false;
this.handleInputChange(e);
}
handleInputChange(e) {
let file = e.dataTransfer ? e.dataTransfer.files[0] : e.target.files[0];
let pattern = '\w*(.ged)$';
let reader = new FileReader();
if (!file.name.match(pattern)) {
alert('invalid format');
return;
}
this.processFile(file);
/*this.loaded = false;
reader.onload = this._handleReaderLoaded.bind(this);
reader.readAsDataURL(file);*/
}
openFileLoader() {
let event = new MouseEvent('click', { bubbles: true });
this.fileInput.nativeElement.dispatchEvent(event);
}
/**
* Parses the input file
* @param event
*/
fileChange(event) {
let file: File = event.target.files[0];
this.processFile(file);
}
processFile(file: File)
{
this.isLoading = true;
this.fileName = file.name;
console.log(file);
console.log(this.fileName);
let reader = new FileReader();
let self = this;
reader.onload = function (progressEvent) {
// split into lines
//
let lines = this.result.split('\n');
self.parseGedFile(lines);
};
reader.readAsText(file);
}
/**
* Parses the file
* @param lines
*/
public parseGedFile(lines): void
{
let line = 0;
let end: boolean = false;
let currentRow: RowData;
while (line < lines.length && !end) {
currentRow = this.parseLine(lines[line]);
if (currentRow.level === 0) {
// reading meta data
if (currentRow.tag === 'HEAD') {
let name: string;
let gedVersion: string;
let form: string;
let charset: string;
currentRow = this.parseLine(lines[++line]);
// iterate until lvl 0 is reached again
while (currentRow.level !== 0)
{
if (currentRow.level === 2 && currentRow.tag === 'NAME')
name = currentRow.value;
if (currentRow.level === 2 && currentRow.tag === 'VERS')
gedVersion = currentRow.value;
if (currentRow.level === 2 && currentRow.tag === 'FORM')
form = currentRow.value;
if (currentRow.level === 1 && currentRow.tag === 'CHAR')
charset = currentRow.value;
currentRow = this.parseLine(lines[++line]);
}
// save the metadata in service
this.timeNetDataService.newTimeNetData(this.fileName, name, gedVersion, form, charset);
}
// reading a person
else if (currentRow.tag === 'INDI') {
let names: string[] = [];
let id: string = currentRow.id;
let dateOfBirth: string = '';
let dateOfDeath: string = '';
let sex: string = '';
currentRow = this.parseLine(lines[++line]);
// iterate until lvl 0 is reached again
while (currentRow.level !== 0) {
if (currentRow.level === 1 && currentRow.tag === 'SEX')
sex = currentRow.value;
if (currentRow.level === 1 && currentRow.tag === 'NAME')
names.push(currentRow.value);
if (currentRow.level === 1 && currentRow.tag === 'BIRT')
{
currentRow = this.parseLine(lines[++line]);
while (currentRow.level === 2)
{
if (currentRow.tag === 'DATE')
dateOfBirth += currentRow.value;
if (currentRow.tag === 'TYPE')
dateOfBirth += currentRow.value;
currentRow = this.parseLine(lines[++line]);
}
}
if (currentRow.level === 1 && currentRow.tag === 'DEAT') {
currentRow = this.parseLine(lines[++line]);
while (currentRow.level === 2) {
if (currentRow.tag === 'DATE')
dateOfDeath += currentRow.value;
if (currentRow.tag === 'TYPE')
dateOfDeath += currentRow.value;
currentRow = this.parseLine(lines[++line]);
}
}
if (currentRow.level === 1 && currentRow.tag === 'FAMS') {
/*this.timeNetDataService.addReleationShip(
id, currentRow.id, relationShipTypeEnum['Spouse-Of'], '', ''
);*/
this.timeNetDataService.addSpouseToFamily(currentRow.id,id);
}
if (currentRow.level === 1 && currentRow.tag === 'FAMC') {
/*this.timeNetDataService.addReleationShip(
currentRow.id, id, relationShipTypeEnum['Child-Of'], '', ''
);*/
this.timeNetDataService.addChildToFamily(currentRow.id,id);
}
currentRow = this.parseLine(lines[++line]);
}
// save the metadata in service
this.timeNetDataService.addPerson(id, names, sex, dateOfBirth, dateOfDeath);
}
else if (currentRow.tag === 'FAM')
{
let famId: string = currentRow.id;
// 0 - 1 husband or wife
let husband: string = '';
let wife: string = '';
let children: string[] = [];
currentRow = this.parseLine(lines[++line]);
while (currentRow.level !== 0) {
if (currentRow.tag === 'HUSB' || currentRow.tag === 'WIFE') {
this.timeNetDataService.addSpouseToFamily(famId, currentRow.id);
} else if (currentRow.tag === 'CHIL') {
this.timeNetDataService.addChildToFamily(famId, currentRow.id);
}
currentRow = this.parseLine(lines[++line]);
}
}
else if (currentRow.tag === 'TRLR') {
end = true;
}
else {
line++;
}
} else {
line++;
}
}
this.timeNetDataService.finishImport();
console.log(this.timeNetDataService.getTimeNetData());
console.log('loading finished - redirect to visualization');
this.router.navigate(['vis']);
}
/**
* Parses one single line of a gedcom file
* @param line
* src: https://github.com/dcapwell/gedcom.js/blob/master/lib/gedcom.js
*/
private parseLine(line: String): RowData {
let split = line.split(' '),
lvl = split.shift().trim(),
tmp = split.shift().trim(),
id = null,
tag = null,
value = null;
if (tmp.charAt(0) === '@') {
// line contains an id
id = tmp;
tmp = split.shift().trim();
}
tag = tmp;
if (split.length > 0) {
value = split.join(' ');
if (value.match(/@[^@]+@/)) {
// contains a reference...
// Family Tree Legends seems to put id in value some times, other times it will put it in id location...
id = value;
value = null;
}
}
return <RowData> { level: +lvl, id, tag, value };
}
}