-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortable-features.js
More file actions
executable file
·234 lines (213 loc) · 6.03 KB
/
Copy pathsortable-features.js
File metadata and controls
executable file
·234 lines (213 loc) · 6.03 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
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
/**
* WordPress dependencies
*/
import { __ } from "@wordpress/i18n";
import { Component, Fragment } from "@wordpress/element";
import { BaseControl, TextControl, ToggleControl } from "@wordpress/components";
// import _ from "lodash";
/**
* External dependencies
*/
import {
SortableContainer,
SortableElement,
SortableHandle,
} from "react-sortable-hoc";
import arrayMove from "array-move";
import FontIconPicker from "@fonticonpicker/react-fonticonpicker";
/**
* Internal dependencies
*/
// import faIcons from "../../../util/faIcons";
const { faIcons, ColorControl } = window.EBPricingTableControls;
// Style objects
const trashStyle = {
fontSize: 14,
borderLeft: "1px solid #b4b4cb",
lineHeight: "2.5em",
flex: 2,
textAlign: "center",
display: "flex",
justifyContent: "center",
};
const DragHandle = SortableHandle(() => (
<span className="drag-handle">
<svg
xmlns="http://www.w3.org/2000/svg"
x="0"
y="0"
enableBackground="new 0 0 512 512"
version="1.1"
viewBox="0 0 512 512"
xmlSpace="preserve"
style={{ height: 14 }}
>
<path
d="M512 256L402.6 146.6 402.6 210.3 301 210.3 301 109.4 365.4 109.4 256 0 146.6 109.4 211 109.4 211 210.3 109.4 210.3 109.4 146.6 0 256 109.4 365.4 109.4 300.3 211 300.3 211 402.6 146.6 402.6 256 512 365.4 402.6 301 402.6 301 300.3 402.6 300.3 402.6 365.4z"
style={{ fill: "#a9a9a9" }}
></path>
</svg>
</span>
));
// const DragHandle = SortableHandle(() => (
// <span className="fa fa-ellipsis-v drag-handle" />
// ));
const TrashIcon = ({ position, onDeleteFeature }) => (
<span
className="eb-social-delete-icon"
style={trashStyle}
onClick={() => onDeleteFeature(position)}
>
<svg
xmlns="http://www.w3.org/2000/svg"
x="0"
y="0"
enableBackground="new 0 0 512 512"
version="1.1"
viewBox="0 0 512 512"
xmlSpace="preserve"
style={{ width: 14 }}
>
<path
d="M423.3 86.6H89c-16.8.1-32.2 9.3-40.1 24.1-7.9 14.8-7.1 32.7 2.2 46.8l37.2 55.6V456c0 30.9 25.1 56 56 56h223.9c30.9 0 56-25.1 56-56V213.1l37.2-56c9.1-14 9.8-31.8 1.9-46.5-8.1-14.7-23.4-23.9-40-24zm-198 347c0 13.9-11.3 25.2-25.2 25.2-13.9 0-25.2-11.3-25.2-25.2V220.9c0-13.9 11.3-25.2 25.2-25.2 13.9 0 25.2 11.3 25.2 25.2v212.7zm112 0c0 13.9-11.3 25.2-25.2 25.2-13.9 0-25.2-11.3-25.2-25.2V220.9c0-13.9 11.3-25.2 25.2-25.2 13.9 0 25.2 11.3 25.2 25.2v212.7zM325.8 19.4C309.9 7.1 290.2 0 269.3 0h-26.4c-20.9 0-40.6 7.1-56.5 19.4-11.2 8.7-20.5 20.1-26.9 33.4h193.1c-6.3-13.3-15.6-24.7-26.8-33.4z"
style={{ fill: "#FF6464" }}
></path>
</svg>
</span>
);
// const TrashIcon = ({ position, onDeleteFeature }) => (
// <span
// className="fa fa-trash eb-pricebox-sortable-trash"
// onClick={() => onDeleteFeature(position)}
// />
// );
const SortableItem = SortableElement(
({
feature,
position,
onFeatureClick,
onFeatureChange,
onDeleteFeature,
clickedItem,
}) => {
return (
<li className="drag-helper">
<span className="eb-pricebox-sortable-item">
<span
className="eb-pricebox-sortable-title"
onClick={() => onFeatureClick(position)}
>
{feature.text}
</span>
<DragHandle />
<TrashIcon position={position} onDeleteFeature={onDeleteFeature} />
</span>
{position === clickedItem && (
<Fragment>
<TextControl
label={__("Text", "essential-blocks")}
value={feature.text}
onChange={(value) => onFeatureChange("text", value, position)}
/>
<ToggleControl
label={__("Link", "essential-blocks")}
checked={feature.clickable === "true"}
onChange={(value) =>
onFeatureChange("clickable", value.toString(), position)
}
/>
{feature.clickable === "true" && (
<TextControl
label={__("Link", "essential-blocks")}
value={feature.link}
onChange={(value) => onFeatureChange("link", value, position)}
/>
)}
<BaseControl label={__("Select Icon", "essential-blocks")}>
<FontIconPicker
icons={faIcons}
onChange={(value) => onFeatureChange("icon", value, position)}
value={feature.icon}
appendTo="body"
isMulti={false}
/>
</BaseControl>
{feature.icon && (
<ColorControl
label={__("Icon Color", "essential-blocks")}
color={feature.color}
onChange={(value) => onFeatureChange("color", value, position)}
/>
)}
</Fragment>
)}
</li>
);
}
);
const SortableList = SortableContainer(
({
features,
onFeatureClick,
onFeatureChange,
onDeleteFeature,
clickedItem,
}) => {
return (
<ul>
{features.map((item, index) => (
<SortableItem
key={`item-${index}`}
index={index}
position={index}
feature={item}
clickedItem={clickedItem}
onFeatureClick={onFeatureClick}
onFeatureChange={onFeatureChange}
onDeleteFeature={onDeleteFeature}
/>
))}
</ul>
);
}
);
class SortableFeatures extends Component {
state = {
clickedItem: null,
};
onSortEnd = ({ newIndex, oldIndex }) => {
const { features, setAttributes } = this.props;
setAttributes({ features: arrayMove(features, oldIndex, newIndex) });
};
onFeatureClick = (index) => {
let clickedItem = this.state.clickedItem === index ? null : index;
this.setState({ clickedItem });
};
onFeatureChange = (key, value, position) => {
const newFeature = { ...this.props.features[position] };
const newFeatureList = [...this.props.features];
newFeatureList[position] = newFeature;
newFeatureList[position][key] = value;
this.props.setAttributes({ features: newFeatureList });
};
onDeleteFeature = (position) => {
const { setAttributes } = this.props;
let features = [...this.props.features];
features.splice(position, 1);
setAttributes({ features });
};
render = () => {
return (
<SortableList
features={this.props.features}
onFeatureClick={this.onFeatureClick}
onFeatureChange={this.onFeatureChange}
onDeleteFeature={this.onDeleteFeature}
onSortEnd={this.onSortEnd}
clickedItem={this.state.clickedItem}
useDragHandle
/>
);
};
}
export default SortableFeatures;