| 1 |
/** |
| 2 |
* External dependencies |
| 3 |
*/ |
| 4 |
import classnames from 'classnames/dedupe'; |
| 5 |
|
| 6 |
/** |
| 7 |
* Internal dependencies |
| 8 |
*/ |
| 9 |
import ColorPicker from '../../components/color-picker'; |
| 10 |
import ColorIndicator from '../../components/color-indicator'; |
| 11 |
import RangeControl from '../../components/range-control'; |
| 12 |
import ApplyFilters from '../../components/apply-filters'; |
| 13 |
|
| 14 |
/** |
| 15 |
* WordPress dependencies |
| 16 |
*/ |
| 17 |
const { applyFilters } = wp.hooks; |
| 18 |
|
| 19 |
const { __ } = wp.i18n; |
| 20 |
|
| 21 |
const { Component, Fragment } = wp.element; |
| 22 |
const { PanelBody, TextControl, ToggleControl, TabPanel, ResizableBox } = wp.components; |
| 23 |
|
| 24 |
const { InspectorControls, RichText } = wp.blockEditor; |
| 25 |
|
| 26 |
/** |
| 27 |
* Block Edit Class. |
| 28 |
*/ |
| 29 |
class BlockEdit extends Component { |
| 30 |
render() { |
| 31 |
const { attributes, setAttributes, isSelected, toggleSelection } = this.props; |
| 32 |
|
| 33 |
let { className = '' } = this.props; |
| 34 |
|
| 35 |
const { |
| 36 |
caption, |
| 37 |
height, |
| 38 |
percent, |
| 39 |
borderRadius, |
| 40 |
striped, |
| 41 |
animateInViewport, |
| 42 |
showCount, |
| 43 |
countPrefix, |
| 44 |
countSuffix, |
| 45 |
color, |
| 46 |
backgroundColor, |
| 47 |
hoverColor, |
| 48 |
hoverBackgroundColor, |
| 49 |
} = attributes; |
| 50 |
|
| 51 |
className = classnames('ghostkit-progress', className); |
| 52 |
|
| 53 |
className = applyFilters('ghostkit.editor.className', className, this.props); |
| 54 |
|
| 55 |
return ( |
| 56 |
<Fragment> |
| 57 |
<InspectorControls> |
| 58 |
<PanelBody> |
| 59 |
<RangeControl |
| 60 |
label={__('Height', 'ghostkit')} |
| 61 |
value={height || ''} |
| 62 |
onChange={(value) => setAttributes({ height: value })} |
| 63 |
min={1} |
| 64 |
allowCustomMax |
| 65 |
/> |
| 66 |
<RangeControl |
| 67 |
label={__('Percent', 'ghostkit')} |
| 68 |
value={percent || ''} |
| 69 |
onChange={(value) => setAttributes({ percent: value })} |
| 70 |
min={0} |
| 71 |
max={100} |
| 72 |
/> |
| 73 |
<RangeControl |
| 74 |
label={__('Corner Radius', 'ghostkit')} |
| 75 |
value={borderRadius} |
| 76 |
min={0} |
| 77 |
max={10} |
| 78 |
onChange={(value) => setAttributes({ borderRadius: value })} |
| 79 |
allowCustomMax |
| 80 |
/> |
| 81 |
</PanelBody> |
| 82 |
<PanelBody> |
| 83 |
<ToggleControl |
| 84 |
label={__('Show Count', 'ghostkit')} |
| 85 |
checked={!!showCount} |
| 86 |
onChange={(val) => setAttributes({ showCount: val })} |
| 87 |
/> |
| 88 |
{showCount ? ( |
| 89 |
<Fragment> |
| 90 |
<TextControl |
| 91 |
label={__('Count Prefix', 'ghostkit')} |
| 92 |
value={countPrefix} |
| 93 |
onChange={(value) => setAttributes({ countPrefix: value })} |
| 94 |
/> |
| 95 |
<TextControl |
| 96 |
label={__('Count Suffix', 'ghostkit')} |
| 97 |
value={countSuffix} |
| 98 |
onChange={(value) => setAttributes({ countSuffix: value })} |
| 99 |
/> |
| 100 |
</Fragment> |
| 101 |
) : ( |
| 102 |
'' |
| 103 |
)} |
| 104 |
<ToggleControl |
| 105 |
label={__('Striped', 'ghostkit')} |
| 106 |
checked={!!striped} |
| 107 |
onChange={(val) => setAttributes({ striped: val })} |
| 108 |
/> |
| 109 |
<ToggleControl |
| 110 |
label={__('Animate in viewport', 'ghostkit')} |
| 111 |
checked={!!animateInViewport} |
| 112 |
onChange={(val) => setAttributes({ animateInViewport: val })} |
| 113 |
/> |
| 114 |
</PanelBody> |
| 115 |
<PanelBody |
| 116 |
title={ |
| 117 |
<Fragment> |
| 118 |
{__('Colors', 'ghostkit')} |
| 119 |
<ColorIndicator colorValue={color} /> |
| 120 |
<ColorIndicator colorValue={backgroundColor} /> |
| 121 |
</Fragment> |
| 122 |
} |
| 123 |
initialOpen={false} |
| 124 |
> |
| 125 |
<TabPanel |
| 126 |
className="ghostkit-control-tabs ghostkit-control-tabs-wide" |
| 127 |
tabs={[ |
| 128 |
{ |
| 129 |
name: 'normal', |
| 130 |
title: __('Normal', 'ghostkit'), |
| 131 |
className: 'ghostkit-control-tabs-tab', |
| 132 |
}, |
| 133 |
{ |
| 134 |
name: 'hover', |
| 135 |
title: __('Hover', 'ghostkit'), |
| 136 |
className: 'ghostkit-control-tabs-tab', |
| 137 |
}, |
| 138 |
]} |
| 139 |
> |
| 140 |
{(tabData) => { |
| 141 |
const isHover = tabData.name === 'hover'; |
| 142 |
return ( |
| 143 |
<Fragment> |
| 144 |
<ApplyFilters |
| 145 |
name="ghostkit.editor.controls" |
| 146 |
attribute={isHover ? 'hoverColor' : 'color'} |
| 147 |
props={this.props} |
| 148 |
> |
| 149 |
<ColorPicker |
| 150 |
label={__('Bar', 'ghostkit')} |
| 151 |
value={isHover ? hoverColor : color} |
| 152 |
onChange={(val) => |
| 153 |
setAttributes(isHover ? { hoverColor: val } : { color: val }) |
| 154 |
} |
| 155 |
alpha |
| 156 |
/> |
| 157 |
</ApplyFilters> |
| 158 |
<ApplyFilters |
| 159 |
name="ghostkit.editor.controls" |
| 160 |
attribute={isHover ? 'hoverBackgroundColor' : 'backgroundColor'} |
| 161 |
props={this.props} |
| 162 |
> |
| 163 |
<ColorPicker |
| 164 |
label={__('Background', 'ghostkit')} |
| 165 |
value={isHover ? hoverBackgroundColor : backgroundColor} |
| 166 |
onChange={(val) => |
| 167 |
setAttributes( |
| 168 |
isHover ? { hoverBackgroundColor: val } : { backgroundColor: val } |
| 169 |
) |
| 170 |
} |
| 171 |
alpha |
| 172 |
/> |
| 173 |
</ApplyFilters> |
| 174 |
</Fragment> |
| 175 |
); |
| 176 |
}} |
| 177 |
</TabPanel> |
| 178 |
</PanelBody> |
| 179 |
</InspectorControls> |
| 180 |
<div className={className}> |
| 181 |
{!RichText.isEmpty(caption) || isSelected ? ( |
| 182 |
<RichText |
| 183 |
tagName="div" |
| 184 |
className="ghostkit-progress-caption" |
| 185 |
placeholder={__('Write caption…', 'ghostkit')} |
| 186 |
value={caption} |
| 187 |
onChange={(newCaption) => setAttributes({ caption: newCaption })} |
| 188 |
/> |
| 189 |
) : ( |
| 190 |
'' |
| 191 |
)} |
| 192 |
{showCount ? ( |
| 193 |
<div className="ghostkit-progress-bar-count" style={{ width: `${percent}%` }}> |
| 194 |
<div> |
| 195 |
{countPrefix} |
| 196 |
{percent} |
| 197 |
{countSuffix} |
| 198 |
</div> |
| 199 |
</div> |
| 200 |
) : ( |
| 201 |
'' |
| 202 |
)} |
| 203 |
<ResizableBox |
| 204 |
className={classnames({ 'is-selected': isSelected })} |
| 205 |
size={{ |
| 206 |
width: '100%', |
| 207 |
height, |
| 208 |
}} |
| 209 |
minWidth="0%" |
| 210 |
maxWidth="100%" |
| 211 |
minHeight="1" |
| 212 |
enable={{ bottom: true }} |
| 213 |
onResizeStart={() => { |
| 214 |
toggleSelection(false); |
| 215 |
}} |
| 216 |
onResizeStop={(event, direction, elt, delta) => { |
| 217 |
setAttributes({ |
| 218 |
height: parseInt(height + delta.height, 10), |
| 219 |
}); |
| 220 |
toggleSelection(true); |
| 221 |
}} |
| 222 |
> |
| 223 |
<div |
| 224 |
className={classnames({ |
| 225 |
'ghostkit-progress-wrap': true, |
| 226 |
'ghostkit-progress-bar-striped': striped, |
| 227 |
})} |
| 228 |
> |
| 229 |
<ResizableBox |
| 230 |
className={classnames('ghostkit-progress-bar', { 'is-selected': isSelected })} |
| 231 |
size={{ |
| 232 |
width: `${percent}%`, |
| 233 |
}} |
| 234 |
minWidth="0%" |
| 235 |
maxWidth="100%" |
| 236 |
minHeight="100%" |
| 237 |
maxHeight="100%" |
| 238 |
enable={{ right: true }} |
| 239 |
onResizeStart={() => { |
| 240 |
toggleSelection(false); |
| 241 |
}} |
| 242 |
onResizeStop={(event, direction, elt, delta) => { |
| 243 |
setAttributes({ |
| 244 |
percent: Math.min( |
| 245 |
100, |
| 246 |
Math.max( |
| 247 |
0, |
| 248 |
percent + |
| 249 |
parseInt((100 * delta.width) / window.jQuery(elt).parent().width(), 10) |
| 250 |
) |
| 251 |
), |
| 252 |
}); |
| 253 |
toggleSelection(true); |
| 254 |
}} |
| 255 |
/> |
| 256 |
</div> |
| 257 |
</ResizableBox> |
| 258 |
</div> |
| 259 |
</Fragment> |
| 260 |
); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
export default BlockEdit; |
| 265 |
|