-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
index.js
69 lines (55 loc) · 2.22 KB
/
index.js
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
import CompositeClosureHelper from 'paraviewweb/src/Common/Core/CompositeClosureHelper';
// ----------------------------------------------------------------------------
// Partition Provider
// ----------------------------------------------------------------------------
function scoresProvider(publicAPI, model) {
publicAPI.setScores = (scores) => {
model.scores = [].concat(scores);
const scoreMapByValue = {};
model.scores.forEach((score) => {
scoreMapByValue[score.value] = score;
});
model.scoreMapByValue = scoreMapByValue;
publicAPI.fireScoresChange(model.scores);
};
publicAPI.getScoreColor = (value) => {
const score = model.scoreMapByValue[value];
return score ? score.color : undefined;
};
publicAPI.getScoreName = (value) => {
const score = model.scoreMapByValue[value];
return score ? score.name : undefined;
};
publicAPI.getDefaultScore = () => {
if (model.scores) {
const index = model.scores.findIndex((score) => !!score.isDefault);
return index === -1 ? 0 : index;
}
return 0;
};
publicAPI.setDefaultScore = (value) => {
if (model.scores) {
model.scores[publicAPI.getDefaultScore()].isDefault = false;
model.scores[value].isDefault = true;
}
};
}
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
const DEFAULT_VALUES = {
// scores: null,
};
// ----------------------------------------------------------------------------
export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);
CompositeClosureHelper.destroy(publicAPI, model);
CompositeClosureHelper.isA(publicAPI, model, 'ScoresProvider');
CompositeClosureHelper.event(publicAPI, model, 'scoresChange', false);
CompositeClosureHelper.get(publicAPI, model, ['scores']);
scoresProvider(publicAPI, model);
}
// ----------------------------------------------------------------------------
export const newInstance = CompositeClosureHelper.newInstance(extend);
// ----------------------------------------------------------------------------
export default { newInstance, extend };