-
Notifications
You must be signed in to change notification settings - Fork 3
/
gridster-component.html
191 lines (167 loc) · 4.39 KB
/
gridster-component.html
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!DOCTYPE html>
<html>
<head>
<script src="build/react-with-addons.js"></script>
<script src="build/JSXTransformer.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="build/jquery.gridster.js"></script>
<style type="text/css">
.item {
background-color: pink;
display: block;
position: absolute;
}
body {
height: 900px;
}
</style>
</head>
<body>
<script type="text/jsx">
/** @jsx React.DOM */
var TestItem = React.createClass({
getInitialState: function() {
return {count: 0};
},
componentDidMount: function() {
this.interval = setInterval(this.handleTick, 1000);
},
componentWillUnmount: function() {
clearInterval(this.interval);
},
handleTick: function() {
this.setState({count: this.state.count + 1});
},
render: function() {
return <span>{this.props.children}{': '}{this.state.count}</span>;
}
});
var Grid = React.createClass({
render: function() {
return this.transferPropsTo(<ul />);
},
componentDidMount: function() {
// initialize gridster
var gridster = $(this.getDOMNode()).gridster({
widget_margins: [10, 10],
widget_base_dimensions: [140, 140]
}).data('gridster');
this.props.children.map(function(child, i) {
var attributes = child.props.attributes;
var key = "child" + child.props.key;
gridster.add_widget(
'<li class="item" id={key}></li>'.replace('{key}', key),
attributes.width,
attributes.height,
attributes.col,
attributes.row
);
React.renderComponent(child, document.getElementById(key));
});
this.setState({gridster: gridster});
},
componentDidUpdate: function(prevProps, prevState) {
var gridster = this.state.gridster;
var uniques = this.props.children.filter(function(newChild) {
for (var index in prevProps.children) {
var oldChild = prevProps.children[index];
if (oldChild.props.key === newChild.props.key) {
return false;
}
}
return true;
});
uniques.map(function(child) {
var attributes = child.props.attributes;
var key = "child" + child.props.key;
gridster.add_widget(
'<li class="item" id={key}></li>'.replace('{key}', key),
attributes.width,
attributes.height,
attributes.col,
attributes.row
);
React.renderComponent(child, document.getElementById(key));
});
}
});
var App = React.createClass({
getInitialState: function() {
return {
items: [{
key: 'test0',
width: 1,
height: 1,
col: 1,
row: 1
}, {
key: 'test1',
width: 1,
height: 1,
col: 2,
row: 1
}, {
key: 'test2',
width: 1,
height: 1,
col: 3,
row: 1
}],
nextCol: 4,
nextRow: 1,
nextKey: 3
};
},
handleAdd: function() {
var nextCol = this.state.nextCol;
var nextRow = this.state.nextRow;
var newItem = {
key: 'test' + this.state.nextKey,
width: 1,
height: 1,
col: nextCol,
row: nextRow
}
var newItems = React.addons.update(this.state.items, {
$push: [newItem]
});
if (nextCol % 6 === 0) {
nextCol = 1;
nextRow++;
} else {
nextCol++;
}
this.setState({
items: newItems,
nextKey: this.state.nextKey + 1,
nextCol: nextCol,
nextRow: nextRow
});
},
render: function() {
var items = this.state.items.map(function(item) {
return (
<div key={item.key}
attributes={item}>
<TestItem >{item.key}</TestItem>
</div>
);
});
return (
<div>
<h1>React + Gridster</h1>
<p>Next Col: {this.state.nextCol}</p>
<p>Next Row: {this.state.nextRow}</p>
<p>Items: {JSON.stringify(this.state.items)}</p>
<p><button onClick={this.handleAdd}>Add an item</button></p>
<Grid>
{items}
</Grid>
</div>
);
}
});
React.renderComponent(<App />, document.body);
</script>
</body>
</html>