-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
255 lines (218 loc) · 10.3 KB
/
index.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>React Playground</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="https://fb.me/react-0.14.7.min.js"></script>
<script src="https://fb.me/react-dom-0.14.7.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-push-3" id="example"></div>
</div>
<div class="row">
<div class="col-md-6 col-md-push-3" id="content"></div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script type="text/babel">
class Message extends React.Component {
constructor(props) {
super(props);
if(this.props.msg) {
var msg = this.props.msg;
} else {
var msg = "Radi ko urica : )";
}
this.state = { open: false, msg: msg };
}
componentDidMount() {
if(this.state.open == false) {
this.setState({ open: true}, function() {
$(this.refs.box).slideDown();
});
}
}
render() {
return (<div ref="box" style={{ display: 'none' }}><div className="alert alert-info">{this.state.msg}</div></div>);
}
};
var CommentBox = React.createClass({
getInitialState: function() {
return { comments: [], loading: true, visible: false, index: 1 };
},
componentDidUpdate: function() {
if(this.state.visible == false) {
$(this.refs.box).slideDown(function() {
this.setState({ visible: true });
});
};
},
componentDidMount: function() {
$.get(this.props.src, function(data) {
var items = data.map(function(item) {
item.open = true;
item.index = this.state.index++;
return item;
}.bind(this));
var callback = function() {
this.self.setState({ comments: this.items, loading: false });
}.bind({ self: this, items: items});
setTimeout(callback, 1000);
}.bind(this), 'json');
},
handleSubmitComment: function(comment) {
var comments = this.state.comments;
comment.index = this.state.index++;
comment.open = false;
comments.push(comment);
this.setState({ comments: comments });
},
handleRemoveComment: function(comment) {
var newComments = this.state.comments.filter(function(item) {
return item.index != comment.index;
});
this.setState({ comments: newComments });
},
render: function() {
if(this.state.loading == true) {
return (
<div className="commentBox">
<div className="panel panel-default panel-body">
<p style={{ textAlign: 'center' }}>Učitavam komentare</p>
<div className="progress" style={{ marginBottom: '0px' }}>
<div className="progress-bar progress-bar-striped active" style={{ width: '100%' }}></div>
</div>
</div>
</div>
);
} else {
var style = {};
if(this.state.visible == false) {
style.display = 'none';
}
return (
<div>
<div ref="box" className="commentBox" style={style}>
<CommentList data={this.state.comments} onRemoveComment={this.handleRemoveComment}/>
<CommentForm onSubmitComment={this.handleSubmitComment} />
</div>
</div>
);
}
}
});
var CommentList = React.createClass({
render: function() {
var comments = this.props.data.map(function(item) {
return (
<Comment comment={item} onRemoveComment={this.props.onRemoveComment} author={item.author}>{item.comment}</Comment>
);
}.bind(this));
return (
<div className='commentList'>
{comments}
</div>
);
}
});
var CommentForm = React.createClass({
getInitialState: function() {
return { author: '', comment: '', message: false };
},
handleAuthorChange: function(e) {
this.setState({ author: e.target.value, message: false });
},
handleCommentChange: function(e) {
this.setState({ comment: e.target.value, message: false });
},
handleSubmitComment: function() {
if(this.state.author.trim() == '') {
this.setState({ message: true });
$(this.refs.author).focus();
} else if(this.state.comment.trim() == '') {
this.setState({ message: true });
$(this.refs.comment).focus();
} else {
this.props.onSubmitComment({ author: this.state.author, comment: this.state.comment });
this.setState(this.getInitialState());
$(this.refs.author).focus();
}
},
componentDidUpdate: function() {
if(this.state.message == true) {
$(this.refs.message).slideDown('fast', function() {
var body = $('body');
body.animate({ scrollTop: body.height() - $(window).height() });
});
} else {
$(this.refs.message).slideUp('fast');
}
},
render: function() {
return (
<div className="commentForm panel panel-info">
<div className="panel-heading"><span className="panel-title">Novi komentar</span></div>
<div className="panel-body">
<div ref="message" className="alert alert-danger" style={{ display: 'none' }}><strong>Upozorenje</strong> Morate upisati naziv autora i komentar!</div>
<div className="form-group"><input className="form-control" ref="author" placeholder="Autor" type="text" value={this.state.author} onChange={this.handleAuthorChange} /></div>
<div className="form-group"><textarea style={{ height: '100px' }} className="form-control" ref="comment" placeholder="Komentar" value={this.state.comment} onChange={this.handleCommentChange} /></div>
<button onClick={this.handleSubmitComment} className="btn btn-primary btn-block">Objavi komentar</button>
</div>
</div>
);
}
});
var Comment = React.createClass({
componentDidMount: function() {
if(this.props.comment.open == false) {
$(this.refs.box).slideDown('fast', function() {
this.props.comment.open = true;
var body = $('body');
body.animate({ scrollTop: body.height() - $(window).height() });
}.bind(this));
}
},
handleClose: function(e) {
e.preventDefault();
$(this.refs.box).slideUp(function() {
this.props.onRemoveComment(this.props.comment);
}.bind(this));
},
render: function() {
var style = {}
if(this.props.comment.open == false) {
style.display = 'none';
}
return (
<div key={this.props.comment.index} ref="box" style={style}>
<div className="comment panel panel-default">
<div className="panel-heading">
<span className="panel-title">{this.props.author}</span>
<a href="#" onClick={this.handleClose}><span className="glyphicon glyphicon-remove pull-right" style={ { marginTop: '3px' } }></span></a>
</div>
<div className="panel-body">{this.props.children}</div>
</div>
<div className="clearfix"></div>
</div>
);
}
});
ReactDOM.render(
<h1>Comments</h1>,
document.getElementById('example')
);
ReactDOM.render(
<CommentBox src="/react-comments.json" />,
document.getElementById('content')
);
//# sourceURL=dynamics.js
</script>
</body>
</html>