-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomAddNewNotes.html
More file actions
187 lines (147 loc) · 4.06 KB
/
randomAddNewNotes.html
File metadata and controls
187 lines (147 loc) · 4.06 KB
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
<html>
<head>
<script src="https://fb.me/react-15.2.1.js"></script>
<script src="https://fb.me/react-dom-15.2.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.js"></script>
<title> ES6 Stateless component!</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/1.0.0/fetch.js"> </script>
<script src="https://npmcdn.com/react-draggable"></script>
</head>
<body>
<div id="react-container">
</div>
<script type="text/babel">
var Note = React.createClass({
getInitialState(){
return {editing:false};
},
componentWillMount(){
this.style = {
right: this.randomBetween(0, window.innerWidth - 150, 'px'),
top: this.randomBetween(0, window.innerHeight - 150, 'px')
}
},
randomBetween(x, y, s){
var pos = (x + Math.ceil(Math.random() * (y - x)));
console.log(pos)
return pos + s;
},
save(){
this.props.onChange(this.refs.saveText.value, this.props.id);
this.setState({editing:false});
},
edit(){
this.setState({editing:true});
},
remove(){
this.props.onRemove(this.props.id);
this.setState({editing:false});
},
componentDidUpdate(){
if(this.state.editing){
this.refs.saveText.focus();
this.refs.saveText.select();
}
},
shouldComponentUpdate(nextProps, nextState){
return (this.props.children != nextProps.children ) || (this.state != nextState);
},
renderForm(){
return (
<div className="note"
style={this.style}>
<textarea ref="saveText" defaultValue={this.props.children}></textarea>
<button onClick={this.save}>SAVE</button>
</div>
)
},
renderNote(){
return ( <div className="note" style={this.style}>
<p>{this.props.children}</p>
<span>
<button onClick={this.edit}>EDIT</button>
<button onClick={this.remove}>X</button>
</span>
</div>
)
},
render(){
return (<ReactDraggable>
{
(this.state.editing) ? this.renderForm(): this.renderNote()
}
</ReactDraggable>
);
}
});
var Board = React.createClass({
propTypes:{
count: function(props, propName){
if(typeof props[propName] != "number"){
return Error("Count must be a number");
}
if(props[propName] > 100){
return new Error("cannot support that many notes");
}
}
},
componentWillMount(){
if(this.props.count){
var url = `http://baconipsum.com/api/?type=all-meat&sentences=${this.props.count}`
fetch(url).then(results => results.json())
.then(array => array[0])
.then(text => text.split('. '))
.then(array => array.forEach(
sentence => this.add(sentence)
))
.catch(function(err){
console.log('didnt connect to api', err);
});
}
},
getInitialState(){
return {
notes: [
]
}
},
nextId(){
this.uniqueId = this.uniqueId || 0;
return this.uniqueId++;
},
add(text){
if(!text)
text = "Add New note";
var notes = [...this.state.notes, {id: this.nextId(), note: text}]
this.setState({notes});
},
update(newText, id){
var notes = this.state.notes.map(
note => (note.id !== id) ? note: {
...note,
note: newText
}
)
this.setState({notes});
},
remove(id){
var notes = this.state.notes.filter(note => (note.id !== id));
this.setState({notes});
},
eachNote(note){
return (<Note key={note.id} id={note.id} onChange={this.update} onRemove={this.remove}> {note.note} </Note>
);
},
render(){
return (<div className="Board">
{this.state.notes.map(this.eachNote)}
<button onClick={() => this.add()}>+</button>
</div>)
}
});
ReactDOM.render(<Board count={10}/>,
document.getElementById('react-container'))
</script>
</body>
</html>xx