-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
executable file
·175 lines (157 loc) · 3.84 KB
/
scripts.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
163
164
165
166
167
168
169
170
171
172
173
174
175
var DragDropMixin = ReactDND.DragDropMixin;
const ItemTypes = {
TASK: 'task'
};
var Task = React.createClass({
mixins: [DragDropMixin],
statics: {
configureDragDrop: function(register) {
register(ItemTypes.TASK, {
dragSource: {
beginDrag: function(component) {
return {
item: {
text: component.props.text,
deleteTask: component.props.deleteTask
}
};
}
}
});
}
},
render: function() {
return (
<li className="task"
{...this.dragSourceFor(ItemTypes.TASK)}>
{this.props.text}
<span className="delete"
onClick={this.props.deleteTask} />
</li>
);
}
});
var AddTask = React.createClass({
getInitialState: function() {
return { text: "" };
},
handleChange: function(event) {
this.setState({text: event.target.value});
},
render: function() {
return (
<div className="add-task">
<input type="text"
value={this.state.text}
onChange={this.handleChange} />
<button type="button" onClick={this.props.addTask.bind(null, this.state.text)}>
Add
</button>
</div>
);
}
});
var TaskDropBin = React.createClass({
mixins: [DragDropMixin],
statics: {
configureDragDrop: function(register) {
register(ItemTypes.TASK, {
dropTarget: {
acceptDrop: function(component, item) {
// When a task is dropped, add it to the parent task list
item.deleteTask();
component.props.list.addTask(item.text);
}
}
});
}
},
render: function() {
const dropState = this.getDropState(ItemTypes.TASK);
var stateClass = 'none';
if (dropState.isHovering) {
stateClass = 'hovering';
} else if (dropState.isDragging) {
stateClass = 'dragging';
}
return <div className={"drop drop-state-" + stateClass}
{...this.dropTargetFor(ItemTypes.TASK)}>
Drop here
</div>;
}
});
var TaskList = React.createClass({
getInitialState: function() {
return { tasks: this.props.tasks };
},
deleteTask: function(id) {
var self = this;
$.ajax({
url: '/api/' + this.props.id + '/task/' + id,
type: 'DELETE',
success: function(result) {
var tasks = self.state.tasks;
tasks.splice(id, 1);
self.setState({ tasks: tasks });
}
});
},
addTask: function(text) {
var self = this;
$.ajax({
url: '/api/' + this.props.id + '/task',
type: 'PUT',
data: { 'text' : text },
success: function(result) {
self.setState({ tasks: self.state.tasks.concat([{ text: text }]) });
}
});
},
render: function() {
var self = this;
var task_list = this.state.tasks.map(function(task, index) {
return (
<Task key={index}
text={task.text}
deleteTask={self.deleteTask.bind(self, index)} />
);
});
return (
<div className="task-list">
<h1 className="list-title">
{this.props.name}
</h1>
<ul className="list-tasks">
{task_list}
</ul>
<TaskDropBin list={this} />
<AddTask addTask={self.addTask} />
</div>
);
}
});
var App = React.createClass({
render: function() {
var lists = this.props.lists.map(function(list, index) {
return (
<TaskList key={index}
id={index}
name={list.name}
tasks={list.tasks} />
);
});
return (
<div className="lists">
{lists}
</div>
);
}
});
$(document).ready(function() {
$.getJSON('http://localhost:8000/api/board', function(data) {
React.render(
<App lists={data.lists} />,
document.body
);
});
});