-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamesPanel.js
135 lines (113 loc) · 3.56 KB
/
gamesPanel.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
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
//todo: allow user to choose not to display games that the entire group can't play
//todo: instead, allow user to set tolerance for how many people can't play
//todo: allow user to ignore games forever (like single player games)
//todo: select random game
/*
An aggregate for the list of owned games.
Does not actually render itself - all necessary HTML is already under this.ref.
Should not be instantiated until the document is ready.
*/
function gamesPanel() {
var ref = document.getElementById("games");
//the model will be a manager of gameModels
aggregate.call(this, ref.parentNode, new manager(), new manager());
this.ref = ref;
this.content = this.ref.getElementsByClassName("content")[0];
this.fitAncestor = document.body.getElementsByTagName("div")[0].nextElementSibling;
}
gamesPanel.prototype = {
maxVisibleGames: -1,
/*
Compare by how many selected users own this game.
*/
compareSelectedUsersLength: function(view1, view2) {
return view2.model.selectedUsers.employees.length -
view1.model.selectedUsers.employees.length;
},
/*
Compare by the name of the game.
*/
compareName: function(view1, view2) {
return view1.model.name.localeCompare(view2.model.name);
},
/*
Compare by selectedUsersLength, then by name
*/
defaultCompare: function(view1, view2) {
var cmp = this.compareSelectedUsersLength(view1, view2);
if(cmp != 0) return cmp;
return this.compareName(view1, view2);
},
_visibleGames: 0,
_nonVisibleGames: 0,
get visibleGames() {
return this._visibleGames;
},
set visibleGames(n) {
this.content.setAttribute("visibleGames", this._visibleGames = n);
},
get nonVisibleGames() {
return this._nonVisibleGames;
},
set nonVisibleGames(n) {
this.content.setAttribute("nonVisibleGames", this._nonVisibleGames = n);
},
//Override
/*
Commits all children up to this.maxVisibleGames.
*/
commit: function(){
var employees = this.children.employees;
var considerMaxVisibleGames = this.maxVisibleGames > 0;
for(var i=0, len=employees.length; i<len && (!considerMaxVisibleGames || i<this.maxVisibleGames); i++) {
employees[i].commit();
}
this.visibleGames = i;
this.nonVisibleGames = len - i;
},
//todo: let user choose how to sort
/*
Sort the gamePanel's children using this.defaultCompare.
*/
sort: function() {
this.uncommit();
var that = this;
this.children.sort(function(view1, view2){
return that.defaultCompare(view1, view2);
});
this.commit();
},
//Override
onModelAdd: function(source, model, ignore) {
var view = this.children.add(gameViewManager.add(new gameView(this.content, model)));
//since this game is new, we know that there is only one person who owns it
//for all other people who have their games downloaded, add this game to lacking
var soleOwner = model.selectedUsers.employees[0];
var users = selectedPanel.model.employees;
for(var i=0, len=users.length; i<len; i++) {
if(users[i] != soleOwner && !users[i].fetchingGames && users[i].games.employees.length!=0) {
model.selectedUsersLacking.add(users[i]);
}
}
if(!ignore) {
this.sort();
}
},
onModelAddMany: function(source, arr) {
this.sort();
},
//Override
onModelRemove: function(source, model, ignore) {
var view = this.children.remove(gameViewManager.remove(this.findChildByModel(model)));
//clear selectedUsersLacking (otherwise adding the game later may show the old lacking count)
model.selectedUsersLacking.clear();
view.uncommit();
if(!ignore) {
this.sort();
}
},
onModelRemoveMany: function(source, arr) {
this.onModelAddMany();
}
}
extend(gamesPanel, aggregate);