Repository navigation
Expand file tree
/
Copy pathstore.js
More file actions
111 lines (90 loc) · 2.52 KB
/
Copy pathstore.js
File metadata and controls
111 lines (90 loc) · 2.52 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
/* eslint-disable function-paren-newline */
import {
extendObservable,
computed,
} from 'mobx';
import uniqBy from 'lodash.uniqby';
import { getData } from './helpers';
class Store {
constructor() {
extendObservable(this, {
tags: [],
startDate: -Infinity,
endDate: +Infinity,
bounds: {
minLat: -180,
maxLat: 180,
minLng: -180,
maxLng: 180,
},
selectedTag: null,
});
}
async fetchTags() {
const { tags } = await getData();
this.setTags(tags);
}
get tagsInRange() {
const startTimestamp = +this.startDate;
const endTimestamp = +this.endDate;
// TODO this could just assume sorting and use filtering or `for`
return computed(
() => {
const tagsInRange = this.tags.reduce((list, tag) => {
const { timestamp } = tag;
const isInTimeRange = timestamp >= startTimestamp && timestamp < endTimestamp;
let isInRange;
if (!this.bounds) {
isInRange = isInTimeRange;
} else if (tag.geolocation) {
const {
longitude,
latitude,
} = tag.geolocation;
const {
minLat,
maxLat,
minLng,
maxLng,
} = this.bounds;
const isInRegion = longitude > minLng && longitude < maxLng
&& latitude > minLat && latitude < maxLat;
isInRange = isInTimeRange && isInRegion;
} else {
// there are bounds, but the track has no geolocation info. not in range.
isInRange = false;
}
return list.concat(isInRange ? tag : []);
}, []);
return uniqBy(tagsInRange, 'key');
} // eslint-disable-line comma-dangle
);
}
setBounds(bounds) {
this.bounds = bounds;
}
setSelectedTag(tagId) {
this.selectedTag = this.tags.find(({ tagid }) => tagid === tagId);
}
setDateRange(startDate, endDate) {
Object.assign(this, { startDate, endDate });
}
addTag(tag) {
this.tags.push(tag);
}
setTags(tags) {
this.tags = tags.reverse();
const timestamps = tags.map(({ timestamp }) => timestamp);
Object.assign(this, {
startDate: new Date(Math.min.apply(null, timestamps)),
endDate: new Date(Math.max.apply(null, timestamps)),
});
}
getDateRange() {
return [this.startDate, this.endDate];
}
findTagById(tagId) {
return this.tags.find(({ tagid }) => tagid === tagId);
}
}
export default new Store();