-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyKoyomiModel.js
88 lines (88 loc) · 2.32 KB
/
MyKoyomiModel.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function MyKoyomiModel(aMyself) {
this._myself = aMyself;
aMyself.listener = this;
this._others = [];
this._listeners = [];
}
MyKoyomiModel.fromJSON = function (aJSON) {
var myself = MyKoyomiItem.fromJSON(aJSON.myself);
var model = new MyKoyomiModel(myself);
aJSON.others.forEach(function (aOther) {
model.add(MyKoyomiItem.fromJSON(aOther));
});
return model;
};
MyKoyomiModel.prototype = {
toJSON: function () {
var others = [];
this._others.forEach(function (aOther) {
others.push(aOther.toJSON())
});
return {
'myself': this._myself.toJSON(),
'others': others,
};
},
myself: function () {
return this._myself;
},
others: function () {
return this._others;
},
all: function () {
return [this._myself].concat(this._others);
},
visibleIndexOf: function (aItem) {
var i = 0;
var found = -1;
this.all().forEach(function (theItem) {
if (aItem == theItem) {
found = i;
}
if (theItem.isVisible()) {
i++;
}
});
return found;
},
visibleCount: function () {
var i = 1;
this._others.forEach(function (aOther) {
if (aOther.isVisible()) {
i++;
}
});
return i;
},
hslAt: function (aIndex) {
var COLORS = 7;
var hue = 360 * (aIndex % COLORS) / COLORS;
return 'hsl(' + hue + ', 100%, 85%)';
},
add: function (aItem) {
this._others.push(aItem);
aItem.listener = this;
this._changed(aItem, -1);
},
removeAt: function (aIndex) {
this._others.splice(aIndex, 1);
this._changed(null, aIndex);
},
addListener: function (aListener) {
this._listeners.push(aListener);
},
removeListener: function (aListner) {
var found = this._listeners.indexOf(aListner);
if (found > -1) {
this._listeners.splice(found, 1);
}
},
onChange: function () {
this._changed(null, -1);
},
_changed: function (aAdded, aRemovedIndex) {
this._listeners.forEach(function (aListener) {
aListener.onChange(aAdded, aRemovedIndex);
});
},
};