-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.jsx
169 lines (151 loc) · 4.42 KB
/
app.jsx
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
163
164
165
166
167
168
169
var React = require("react"),
domReady = require("domready"),
navigation = require("../../"),
SortMenu = {};
//Menu Sorting components
SortMenu.List = React.createClass({
mixins: [navigation.mixin],
render: function() {
var children = this.navInit(this.props.children);
return (
<div className="menu">
{children}
</div>
);
}
});
SortMenu.Item = React.createClass({
mixins: [navigation.mixin],
navHandleKey: function(key) {
if(key === "ENTER") {
this.props.trigger();
}
},
render: function() {
var className="";
this.navInit();
if(this.state.focused === true) {
className = "focused";
}
return (
<a className={className} onClick={this.props.trigger}>{this.props.title}</a>
);
}
});
//Item and List components hold our data
var Item = React.createClass({
mixins: [navigation.mixin],
navHandleKey: function(key) {
if(key === "ENTER") {
alert("Item " + this.props.name + " selected!");
}
},
render: function() {
this.navInit();
var className="item";
if(this.state.focused === true) {
className = className + " focused";
}
return (
<div className={className} onClick={this.handleClick}>
<h2>{this.props.name}</h2>
<p>ID: #{this.props.itemId}</p>
</div>
);
}
});
var List = React.createClass({
mixins: [navigation.mixin],
render: function() {
var elements = this.navInit(this.props.children);
return (
<div className="videos">
{elements}
</div>
);
}
});
//Grid is a main component that holds everything together.
var Grid = React.createClass({
//Navigation mixin
mixins: [navigation.mixin],
getInitialState: function() {
return {sortBy: "id", ascending: true};
},
sort: function(comp) {
if(this.state.sortBy === comp) {
this.setState({ascending: !this.state.ascending});
} else {
this.setState({sortBy: comp});
}
},
render: function() {
var col,
row,
cols = 6,
items,
comp = this.state.sortBy,
ascending = this.state.ascending,
data = this.props.data || [];
//NavInit registers component as a navigable item
this.navInit();
//Sorting videos according to comparator
data.sort(function(a, b) {
var order = 0;
if(a[comp] > b[comp]) {
order = 1;
} else if(a[comp] < b[comp]) {
order = -1;
}
//Do we want to reverse order?
if(!ascending) {
order = -1 * order;
}
return order;
});
//transforming data to components
items = data.map(function (video, i) {
return(
<Item name={video.name} key={video.id} itemId={video.id} />
);
});
return (
<div id="grid">
<SortMenu.List navParent={this.navSelf} navType="horizontal" navWrap="wrap">
<b>Sort By:</b>
<SortMenu.Item trigger={this.sort.bind(this,'name')} title="Name"/>
<SortMenu.Item trigger={this.sort.bind(this,'id')} title="Id"/>
</SortMenu.List>
<List navParent={this.navSelf} navType="grid" navWrap="wrapX" navGridCols="3">
{items}
</List>
</div>
);
}
});
//Sample data
var data = [
{id: 1, name: "React"},
{id: 2, name: "Backbone"},
{id: 3, name: "VanillaJS"},
{id: 4, name: "Backbone"},
{id: 5, name: "Ember"},
{id: 6, name: "Angular"},
{id: 7, name: "Prototype"},
{id: 8, name: "jQuery"}
];
//Get everything together
domReady(function() {
React.render(
<Grid data={data} />,
document.getElementById("content"),
function() {
navigation.controller.init(document, 0);
//Adding key logger for debug purposes:
document.addEventListener('keydown', function(ev) {
var keyName = navigation.controller.keyMap[ev.keyCode];
document.querySelector("#debug b").innerText = keyName;
});
}
);
});