-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectionManager.js
52 lines (41 loc) · 1.29 KB
/
SelectionManager.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
function SelectionManager() {}
SelectionManager.prototype.selectedItems = {};
SelectionManager.prototype.callbacks = [];
SelectionManager.prototype.items;
SelectionManager.prototype.isSelected = function (item) {
// Return false if item has never been selected.
return !!this.selectedItems[item];
}
// Setting list of displayed products.
// Takes array of objects that contain the key 'symbol'
SelectionManager.prototype.setItems = function (items) {
if (typeof this.items != 'undefined') {
throw "You can only set items once.";
}
var newArray = []
for (var i = 0; i < items.length; i++){
newArray[i] = items[i]['symbol']
}
this.items = newArray;
}
// Changing selection status on chart
SelectionManager.prototype.setSelected = function (item, selected) {
// If item is not a legal item.
if (this.items.indexOf(item) == -1){
// Do nothing.
throw "Not a legal item"
}
// If the item is already selected.
if (this.selectedItems[item] == selected) {
// Do nothing.
return
}
this.selectedItems[item] = selected;
// Notify listeners of the selection change.
for (var i = 0; i < this.callbacks.length ; i++) {
this.callbacks[i](item, selected);
}
}
SelectionManager.prototype.addListener = function (callback) {
this.callbacks.push(callback);
}