/**
* Internal dependencies
*/
import { CountdownIcon } from "../util/icons";
import DateTime from "react-datetime";
import {
BOX_JUSTIFY_CONTENT,
BORDER_STYLES,
DIGIT_FONT_WEIGHT,
LABEL_TRANSFORM,
FONT_STYLES,
FONT_DECORATION,
TEXT_ALIGN,
} from "./components/constants";
import DimentionControl from "../util/dimention-control/DimentionContol";
import Box from "./components/box";
import BoxContainer from "./components/box-container";
/**
* Import styles
*/
import "./style.scss";
/**
* WordPress dependencies
*/
const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;
const { InspectorControls, PanelColorSettings } = wp.editor;
const {
PanelBody,
ToggleControl,
TextControl,
RangeControl,
SelectControl,
Button,
ButtonGroup,
BaseControl,
} = wp.components;
export const edit = ({ isSelected, attributes, setAttributes }) => {
const {
date,
days,
hours,
minutes,
seconds,
showDays,
showHours,
showMinutes,
showSeconds,
daysLabel,
hoursLabel,
minutesLabel,
secondsLabel,
boxHeight,
boxWidth,
boxSpace,
justifyItems,
paddingTop,
paddingRight,
paddingBottom,
paddingLeft,
radiusTopLeft,
radiusTopRight,
radiusBottomRight,
radiusBottomLeft,
showBorder,
borderSize,
borderStyle,
showBoxShadow,
hOffset,
vOffset,
shadowBlur,
shadowSpread,
inlineItems,
digitFontSize,
digitFontWeight,
labelTransform,
labelFontStyle,
labelFontDecoration,
labelLineHeight,
labelLeftPadding,
boxBackground,
digitColor,
labelColor,
marginTop,
marginRight,
marginBottom,
marginLeft,
boxTextAlign,
borderColor,
shadowColor,
} = attributes;
const boxContainerStyle = {
justifyContent: justifyItems,
};
const boxStyle = {
height: `${boxHeight}px`,
width: `${boxWidth}px`,
margin: `0 ${boxSpace}px`,
padding: `${paddingTop}px ${paddingRight}px ${paddingBottom}px ${paddingLeft}px`,
border: showBorder
? `${borderSize}px ${borderStyle} ${borderColor}`
: "none",
boxShadow: showBoxShadow
? `${hOffset}px ${vOffset}px ${shadowBlur}px ${shadowSpread}px ${shadowColor}`
: "none",
borderRadius: `${radiusTopLeft}% ${radiusTopRight}% ${radiusBottomRight}% ${radiusBottomLeft}%`,
backgroundColor: boxBackground ? boxBackground : "#abb8c3",
};
const boxItemStyle = {
marginTop: `${marginTop}px`,
marginRight: `${marginRight}px`,
marginBottom: `${marginBottom}px`,
marginLeft: `${marginLeft}px`,
textAlign: boxTextAlign,
};
const digitStyle = {
fontSize: `${digitFontSize}px`,
fontWeight: digitFontWeight,
color: digitColor,
display: `${inlineItems ? "inline" : "block"}`,
};
const labelStyle = {
textTransform: labelTransform,
fontStyle: labelFontStyle,
textDecoration: labelFontDecoration,
lineHeight: `${labelLineHeight}px`,
paddingLeft: `${labelLeftPadding}px`,
color: labelColor,
display: `${inlineItems ? "inline" : "block"}`,
};
const onDateTimeChange = momentObj => {
let date = momentObj._d;
let time = date.getTime();
const counter = () => {
let now = new Date().getTime();
let timer = new Date(time - now);
// Calculate days, hours, minutes and seconds
let oneDay = 24 * 60 * 60 * 1000; // hours * minutes * seconds * miliseconds
let days = Math.round((time - now) / oneDay).toString();
let hours = timer.getHours().toString();
let minutes = timer.getMinutes().toString();
let seconds = timer.getSeconds().toString();
setAttributes({ date, days, hours, minutes, seconds });
};
// Clear interval if countdown already exists
if (window.countdown) {
clearInterval(window.countdown);
}
window.countdown = setInterval(counter, 1000);
};
// console.log(attributes.date);
return [
isSelected && (
onDateTimeChange(momentObj)}
/>
setAttributes({ showDays: !showDays })}
/>
setAttributes({ showHours: !showHours })}
/>
setAttributes({ showMinutes: !showMinutes })}
/>
setAttributes({ showSeconds: !showSeconds })}
/>
setAttributes({ daysLabel: newValue })}
/>
setAttributes({ hoursLabel: newValue })}
/>
setAttributes({ minutesLabel: newValue })}
/>
setAttributes({ secondsLabel: newValue })}
/>
setAttributes({ justifyItems: newValue })}
/>
setAttributes({ boxHeight: newValue })}
min={10}
max={200}
/>
setAttributes({ boxWidth: newValue })}
min={10}
max={200}
/>
setAttributes({ boxBackground: newColor }),
label: __("Box Background Color"),
},
]}
/>
setAttributes({ inlineItems: !inlineItems })}
/>
{inlineItems && (
setAttributes({ labelLeftPadding: newValue })
}
min={0}
max={100}
/>
)}
setAttributes({
marginTop: top,
marginRight: right,
marginBottom: bottom,
marginLeft: left,
})
}
/>
setAttributes({
paddingTop: top,
paddingRight: right,
paddingBottom: bottom,
paddingLeft: left,
})
}
/>
setAttributes({
radiusTopLeft: top,
radiusTopRight: right,
radiusBottomRight: bottom,
radiusBottomLeft: left,
})
}
/>
setAttributes({ boxSpace: newValue })}
min={0}
max={100}
/>
setAttributes({ showBorder: !showBorder })}
/>
{showBorder && (
setAttributes({ borderColor: newColor }),
label: __("Border Color"),
},
]}
/>
)}
{showBorder && (
setAttributes({ borderSize: newValue })}
min={0}
max={100}
/>
)}
{showBorder && (
setAttributes({ borderStyle: newValue })}
/>
)}
setAttributes({ showBoxShadow: !showBoxShadow })}
/>
{showBoxShadow && (
setAttributes({ shadowColor: newColor }),
label: __("Shadow Color"),
},
]}
/>
)}
{showBoxShadow && (
setAttributes({ hOffset: newValue })}
min={0}
max={100}
/>
)}
{showBoxShadow && (
setAttributes({ vOffset: newValue })}
min={0}
max={100}
/>
)}
{showBoxShadow && (
setAttributes({ shadowBlur: newValue })}
min={0}
max={100}
/>
)}
{showBoxShadow && (
setAttributes({ shadowSpread: newValue })}
min={0}
max={100}
/>
)}
{TEXT_ALIGN.map(item => (
))}
setAttributes({ digitColor: newColor }),
label: __("Digit Color"),
},
]}
/>
setAttributes({ digitFontSize: newValue })}
min={6}
max={64}
/>
setAttributes({ digitFontWeight: newValue })}
/>
setAttributes({ labelColor: newColor }),
label: __("Label Color"),
},
]}
/>
setAttributes({ labelTransform: newValue })}
/>
{FONT_STYLES.map(item => (
))}
setAttributes({ labelFontDecoration: newValue })
}
/>
setAttributes({ labelLineHeight: newValue })}
min={0}
max={100}
/>
),
// Edit view here
{showDays && (
)}
{showHours && (
)}
{showMinutes && (
)}
{showSeconds && (
)}
,
];
};
export const save = ({ attributes }) => {
const {
date,
days,
hours,
minutes,
seconds,
showDays,
showHours,
showMinutes,
showSeconds,
daysLabel,
hoursLabel,
minutesLabel,
secondsLabel,
boxHeight,
boxWidth,
boxSpace,
justifyItems,
paddingTop,
paddingRight,
paddingBottom,
paddingLeft,
radiusTopLeft,
radiusTopRight,
radiusBottomRight,
radiusBottomLeft,
showBorder,
borderSize,
borderStyle,
hOffset,
vOffset,
shadowBlur,
shadowSpread,
inlineItems,
digitFontSize,
digitFontWeight,
labelTransform,
labelFontStyle,
labelFontDecoration,
labelLineHeight,
labelLeftPadding,
boxBackground,
digitColor,
labelColor,
marginTop,
marginRight,
marginBottom,
marginLeft,
boxTextAlign,
borderColor,
showBoxShadow,
shadowColor,
} = attributes;
const boxContainerStyle = {
justifyContent: justifyItems,
};
const boxStyle = {
height: `${boxHeight}px`,
width: `${boxWidth}px`,
margin: `0 ${boxSpace}px`,
padding: `${paddingTop}px ${paddingRight}px ${paddingBottom}px ${paddingLeft}px`,
border: showBorder
? `${borderSize}px ${borderStyle} ${borderColor}`
: "none",
boxShadow: showBoxShadow
? `${hOffset}px ${vOffset}px ${shadowBlur}px ${shadowSpread}px ${shadowColor}`
: "none",
borderRadius: `${radiusTopLeft}% ${radiusTopRight}% ${radiusBottomRight}% ${radiusBottomLeft}%`,
backgroundColor: boxBackground,
};
const boxItemStyle = {
marginTop: `${marginTop}px`,
marginRight: `${marginRight}px`,
marginBottom: `${marginBottom}px`,
marginLeft: `${marginLeft}px`,
textAlign: boxTextAlign,
lineHeight: "normal", // Add this line to fix box line height
};
const digitStyle = {
fontSize: `${digitFontSize}px`,
fontWeight: `${digitFontWeight}`,
color: digitColor,
display: `${inlineItems ? "inline" : "block"}`,
};
const labelStyle = {
textTransform: labelTransform,
fontStyle: labelFontStyle,
textDecoration: labelFontDecoration,
lineHeight: `${labelLineHeight}px`,
paddingLeft: `${labelLeftPadding}px`,
color: labelColor,
display: `${inlineItems ? "inline" : "block"}`,
};
return (
{showDays && (
)}
{showHours && (
)}
{showMinutes && (
)}
{showSeconds && (
)}
);
};
registerBlockType("advanced-blocks/countdown", {
title: __("Countdown"),
icon: CountdownIcon,
category: 'advanced-blocks',
attributes: {
date: {
type: "string",
source: "attribute",
selector: ".eb-countdown-get-date",
attribute: "data-date",
},
days: {
type: "string",
source: "text",
selector: ".eb-countdown-digits-days",
},
months: {
type: "string",
source: "text",
selector: ".eb-countdown-digits-months",
},
hours: {
type: "string",
source: "text",
selector: ".eb-countdown-digits-hours",
},
minutes: {
type: "string",
source: "text",
selector: ".eb-countdown-digits-minutes",
},
seconds: {
type: "string",
source: "text",
selector: ".eb-countdown-digits-seconds",
},
showDays: {
type: "boolean",
default: true,
},
showHours: {
type: "boolean",
default: true,
},
showMinutes: {
type: "boolean",
default: true,
},
showSeconds: {
type: "boolean",
default: true,
},
daysLabel: {
type: "string",
default: "Days",
},
hoursLabel: {
type: "string",
default: "Hours",
},
minutesLabel: {
type: "string",
default: "Minutes",
},
secondsLabel: {
type: "string",
default: "Seconds",
},
boxHeight: {
type: "number",
default: 100,
},
boxWidth: {
type: "number",
default: 120,
},
boxSpace: {
type: "number",
default: 10,
},
justifyItems: {
type: "string",
default: "center",
},
paddingTop: {
type: "number",
default: 0,
},
paddingRight: {
type: "number",
default: 0,
},
paddingBottom: {
type: "number",
default: 0,
},
paddingLeft: {
type: "number",
default: 0,
},
radiusTopLeft: {
type: "number",
default: 0,
},
radiusTopRight: {
type: "number",
default: 0,
},
radiusBottomRight: {
type: "number",
default: 0,
},
radiusBottomLeft: {
type: "number",
default: 0,
},
showBorder: {
type: "boolean",
default: false,
},
borderSize: {
type: "number",
default: 0,
},
borderStyle: {
type: "string",
default: "solid",
},
showBoxShadow: {
type: "boolean",
default: false,
},
hOffset: {
type: "number",
default: 0,
},
vOffset: {
type: "number",
default: 0,
},
shadowBlur: {
type: "number",
default: 0,
},
shadowSpread: {
type: "number",
default: 0,
},
inlineItems: {
type: "boolean",
default: true,
},
digitFontSize: {
type: "number",
default: 18,
},
digitFontWeight: {
type: "number",
default: 200,
},
labelTransform: {
type: "string",
default: "none",
},
labelFontStyle: {
type: "string",
default: "normal",
},
labelFontDecoration: {
type: "string",
default: "initial",
},
labelLineHeight: {
type: "number",
default: 0,
},
labelLeftPadding: {
type: "number",
default: 0,
},
boxBackground: {
type: "string",
default: '#FFF',
},
digitColor: {
type: "string",
},
labelColor: {
type: "string",
},
daysBoxBackground: {
type: "string",
},
marginTop: {
type: "number",
default: 0,
},
marginRight: {
type: "number",
default: 0,
},
marginBottom: {
type: "number",
default: 0,
},
marginLeft: {
type: "number",
default: 0,
},
boxTextAlign: {
type: "string",
default: "center",
},
},
edit: edit,
save: save,
});