-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameView.js
162 lines (140 loc) · 4.47 KB
/
gameView.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
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
/*
Manages all gameViews.
*/
var gameViewManager = new manager();
/*
Renders the information contained by a gameModel.
*/
function gameView(parent, model) {
view.call(this, parent, model);
var showOwnership = false;
this.__defineGetter__("showOwnership", function() { return showOwnership });
this.__defineSetter__("showOwnership", function(b) {
if(b!=showOwnership) {
this.toggleShowOwnership();
showOwnership = !showOwnership;
}
});
}
gameView.prototype = {
//todo: clean up this ugly rendering stuff
setLogo: function(url) {
this.logo.src = url || null;
},
setId: function(id) {
this.id.innerText = id || null;
},
setName: function(name) {
this.name.innerText = name || null;
},
setStoreLink: function(url) {
this.storeLink.href = url || null;
},
setSharedAmong: function(num) {
this.sharedAmong.setAttribute("sharedamong", num);
this.ref.setAttribute("sharedamong", num);
},
setNotSharedAmong: function(num) {
this.sharedAmong.setAttribute("notsharedamong", num);
this.ref.setAttribute("notsharedamong", num);
},
setRecommend: function(id) {
this.recommend.href = "http://store.steampowered.com/recommended/recommendgame/" + id;
},
setPlay: function(id) {
this.play.href = "steam://run/" + id;
},
render: function() {
if(!this.ref) {
this.logo = document.createElement("img");
this.name = document.createElement("h4");
this.storeLink = document.createElement("a");
this.storeLink.innerText = "Store Page";
this.recommend = document.createElement("a");
this.recommend.innerText = "Recommend";
this.play = document.createElement("a");
this.play.innerText = "Play Now";
this.id = document.createElement("h6");
}
this.setLogo(this.model.logo);
this.setName(this.model.name);
this.setStoreLink(this.model.storeLink);
this.setRecommend(this.model.id);
this.setPlay(this.model.id);
this.setId(this.model.id);
if(!this.ref) {
this.ref = document.createElement("div");
addClass(this.ref, "game");
this.info = document.createElement("div");
this.clear = document.createElement("div");
addClass(this.clear, "clear");
this.sharedAmong = document.createElement("a");
this.sharedAmong.href = "";
this.ownership = document.createElement("div");
addClass(this.ownership, "ownership");
this.ref.appendChild(this.logo);
this.info.appendChild(this.name);
this.info.appendChild(this.storeLink);
this.info.appendChild(this.recommend);
this.info.appendChild(this.play);
this.info.appendChild(this.id);
this.info.appendChild(this.sharedAmong);
this.ref.appendChild(this.info);
this.ref.appendChild(this.ownership);
this.ref.appendChild(this.clear);
}
this.setSharedAmong(this.model.selectedUsersLength);
this.setNotSharedAmong(this.model.selectedUsersLackingLength);
},
//Override
onModelChange: function(source, changes) {
//only change if it has already been rendered
if(this.ref) {
if(changes.logo) this.setLogo(changes.logo);
if(changes.name) this.setName(changes.name);
if(changes.storeLink) this.setStoreLink(changes.storeLink);
if(changes.id) {
this.setId(changes.id);
this.setRecommend(changes.id);
this.setPlay(changes.id);
}
if(defined(changes.selectedUsersLength)) this.setSharedAmong(changes.selectedUsersLength);
if(defined(changes.selectedUsersLackingLength)) this.setNotSharedAmong(changes.selectedUsersLackingLength);
}
},
/*
Helper for the showOwnership getter.
*/
toggleShowOwnership: function() {
if(!this.ref) this.render();
if(!this.showOwnership) {
this.ownership.style.display = "block";
this.selectedUsers = new simpleUserAggregate(this.ownership, this.model.selectedUsers);
this.selectedUsersLacking = new simpleUserAggregate(this.ownership, this.model.selectedUsersLacking);
this.selectedUsers.commit();
this.selectedUsersLacking.commit();
}
else {
this.ownership.style.display = null;
this.selectedUsers.uncommit();
this.selectedUsersLacking.uncommit();
delete this.selectedUsers;
delete this.selectedUsersLacking;
}
}
}
extend(gameView, view);
document.addEventListener("click", function(e) {
var target = e.target;
if(target.tagName=="A") {
var viewRef = getParentByClassName(target, "game");
if(viewRef) {
gameViewManager.children = gameViewManager;
var view = aggregate.prototype.findChildByRef.call(gameViewManager, viewRef);
if(view && target == view.sharedAmong) {
e.preventDefault();
view.showOwnership = !view.showOwnership;
}
}
}
}, true);