-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeline.js
More file actions
170 lines (156 loc) · 6.72 KB
/
Copy pathtimeline.js
File metadata and controls
170 lines (156 loc) · 6.72 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
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
(function (root, factory) {
var chronology = typeof module === 'object' && module.exports ? require('./chronology.js') : root.ParallelWorldsChronology;
var api = factory(chronology);
if (typeof module === 'object' && module.exports) module.exports = api;
root.ParallelTimeline = api;
}(typeof self !== 'undefined' ? self : this, function (chronology) {
'use strict';
function normalize(value) {
return String(value || '').toLocaleLowerCase('ru-RU').trim();
}
function searchableText(track) {
return [track.name, track.summary]
.concat(track.periods.map(function (period) { return period.name + ' ' + (period.note || ''); }))
.concat(track.events.map(function (event) { return event.title + ' ' + (event.note || ''); }))
.join(' ');
}
function filterTracks(tracks, filters) {
var query = normalize(filters.query);
return tracks.filter(function (track) {
if (filters.region && filters.region !== 'all' && track.region !== filters.region) return false;
if (filters.type === 'society' && track.type === 'tradition') return false;
if (filters.type === 'reviewed' && track.reviewStatus !== 'reviewed') return false;
if (filters.type === 'legacy' && track.reviewStatus !== 'legacy') return false;
if (filters.type && filters.type !== 'all' && filters.type !== 'society' && filters.type !== 'reviewed' && filters.type !== 'legacy' && track.type !== filters.type) return false;
return !query || normalize(searchableText(track)).indexOf(query) !== -1;
});
}
function clamp(value, min, max) {
return Math.min(max, Math.max(min, value));
}
function yearToPercent(year, start, end) {
if (start && typeof start === 'object') return chronology.projectYear(year, start);
if (end === start) return 0;
return clamp(((year - start) / (end - start)) * 100, 0, 100);
}
function formatYear(year, locale) {
var lang = String(locale || 'ru').toLowerCase();
if (lang.indexOf('zh') === 0) return year < 0 ? '公元前' + Math.abs(year) + '年' : '公元' + (year === 0 ? 1 : year) + '年';
var english = lang.indexOf('en') === 0;
if (year < 0) return Math.abs(year) + (english ? ' BCE' : ' до н. э.');
return (year === 0 ? 1 : year) + (english ? ' CE' : ' н. э.');
}
function activeTracks(tracks, year) {
return tracks.filter(function (track) {
return track.periods.some(function (period) {
return year >= period.start && year <= period.end;
});
});
}
function numericParam(params, name) {
if (!params.has(name)) return undefined;
var value = Number(params.get(name));
return Number.isFinite(value) ? value : undefined;
}
function visiblePeriods(track, start, end) {
return track.periods.filter(function (period) {
return period.end >= start && period.start <= end;
}).map(function (period) {
return Object.assign({}, period, {
clippedStart: Math.max(period.start, start),
clippedEnd: Math.min(period.end, end)
});
});
}
function periodDensity(width) {
width = Number(width);
if (width >= 112) return 'wide';
if (width >= 64) return 'medium';
if (width >= 32) return 'compact';
return 'node';
}
function periodTooltipRecord(period) {
var dating = period.dating || {};
return {
id: period.id || '',
name: period.name,
start: period.start,
end: period.end,
precision: dating.precision || '',
basis: dating.basis || ''
};
}
function tooltipPosition(target, box, viewport) {
var margin = 8;
var gap = 10;
var maxLeft = Math.max(margin, Number(viewport.width) - Number(box.width) - margin);
var maxTop = Math.max(margin, Number(viewport.height) - Number(box.height) - margin);
var left = Number(target.left) + Number(target.width) / 2 - Number(box.width) / 2;
var top = Number(target.top) - Number(box.height) - gap;
if (top < margin) top = Math.min(maxTop, Number(target.bottom) + gap);
return {
left: Math.round(clamp(left, margin, maxLeft)),
top: Math.round(clamp(top, margin, maxTop))
};
}
function buildCsv(tracks, options) {
options = options || {};
var headers = options.headers || ['Линия', 'Тип', 'Регион', 'Период', 'Начало', 'Конец', 'Примечание'];
var includeEvidence = Boolean(options.includeEvidence || headers.length > 7);
var includeExtendedEvidence = Boolean(options.includeExtendedEvidence || headers.length > 12);
var rows = [headers];
tracks.forEach(function (track) {
track.periods.forEach(function (period) {
var row = [
track.name,
options.typeNames && options.typeNames[track.type] || track.type,
options.regionNames && options.regionNames[track.region] || track.region,
period.name, period.start, period.end, period.note || ''
];
if (includeEvidence) {
var dating = period.dating || {};
var sourceIds = period.sourceIds && period.sourceIds.length ? period.sourceIds : track.sources || [];
var sourceUrls = sourceIds.map(function (sourceId) {
var source = options.sources && options.sources[sourceId];
return source && source.url ? source.url : '';
}).filter(Boolean);
row.push(
options.precisionNames && options.precisionNames[dating.precision] || dating.precision || '',
options.basisNames && options.basisNames[dating.basis] || dating.basis || '',
dating.original || '',
options.reviewNames && options.reviewNames[track.reviewStatus] || track.reviewStatus || '',
sourceUrls.join(' | ')
);
if (includeExtendedEvidence) {
row.push(
options.confidenceNames && options.confidenceNames[dating.confidence] || dating.confidence || '',
dating.model || '',
dating.calibrationCurve || '',
(dating.alternatives || []).map(function (alternative) {
return (alternative.label || alternative.id) + ': ' + alternative.start + '–' + alternative.end;
}).join(' | '),
dating.disputeNote || ''
);
}
}
rows.push(row);
});
});
return rows.map(function (row) {
return row.map(function (cell) { return '"' + String(cell).replace(/"/g, '""') + '"'; }).join(',');
}).join('\n');
}
return {
activeTracks: activeTracks,
buildCsv: buildCsv,
clamp: clamp,
filterTracks: filterTracks,
formatYear: formatYear,
numericParam: numericParam,
periodDensity: periodDensity,
periodTooltipRecord: periodTooltipRecord,
tooltipPosition: tooltipPosition,
visiblePeriods: visiblePeriods,
yearToPercent: yearToPercent
};
}));