forked from WikiEducationFoundation/WikiEduDashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popover_button.jsx
114 lines (104 loc) · 3.01 KB
/
popover_button.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
import React from 'react';
import PopoverExpandable from '../high_order/popover_expandable.jsx';
import Popover from '../common/popover.jsx';
import Conditional from '../high_order/conditional.jsx';
import ServerActions from '../../actions/server_actions.js';
import Lookup from '../common/lookup.jsx';
import LookupSelect from '../common/lookup_select.jsx';
import { capitalize } from '../../utils/strings.js';
const PopoverButton = function (Key, ValueKey, Store, New, Items, IsSelect = false) {
const getState = () => ({ exclude: _.map(Store.getModels(), ValueKey) });
const format = function (value) {
const data = {};
data[Key] = {};
data[Key][ValueKey] = value;
return data;
};
const component = React.createClass({
displayName: `${capitalize(Key)}Button`,
propTypes: {
course_id: React.PropTypes.string,
is_open: React.PropTypes.bool,
open: React.PropTypes.func
},
mixins: [Store.mixin],
getInitialState() {
return getState();
},
getKey() {
return `${Key}_button`;
},
storeDidChange() {
if (this.refs.entry !== null) {
const item = this.refs.entry.getValue();
if (!New(item)) {
this.refs.entry.clear();
}
}
return this.setState(getState());
},
add(e) {
if (e.preventDefault) { e.preventDefault(); }
const item = this.refs.entry.getValue();
if (New(item)) {
return ServerActions.add(Key, this.props.course_id, format(item));
}
return alert(I18n.t('courses.already_exists'));
},
remove(itemId) {
const item = Store.getFiltered({ id: itemId })[0];
return ServerActions.remove(Key, this.props.course_id, format(item[ValueKey]));
},
stop(e) {
return e.stopPropagation();
},
render() {
let lookup;
const placeholder = capitalize(Key);
if (IsSelect) {
lookup = (
<LookupSelect
model={Key}
exclude={this.state.exclude}
placeholder={placeholder}
ref="entry"
onSubmit={this.add}
/>
);
} else {
lookup = (
<Lookup
model={Key}
exclude={this.state.exclude}
placeholder={placeholder}
ref="entry"
onSubmit={this.add}
/>
);
}
const editRow = (
<tr className="edit">
<td>
<form onSubmit={this.add}>
{lookup}
<button type="submit" className="button border">Add</button>
</form>
</td>
</tr>
);
return (
<div className="pop__container" onClick={this.stop}>
<button className="button border plus" onClick={this.props.open}>+</button>
<Popover
is_open={this.props.is_open}
edit_row={editRow}
rows={Items(this.props, this.remove)}
/>
</div>
);
}
}
);
return Conditional(PopoverExpandable(component));
};
export default PopoverButton;