2024-09-24 11:50:50 +05:30
|
|
|
import React from "react"
|
|
|
|
|
|
2024-09-28 16:04:02 +05:30
|
|
|
import { timePicker } from 'analogue-time-picker'
|
2024-09-24 11:50:50 +05:30
|
|
|
|
|
|
|
|
import Widget from "../../../canvas/widgets/base"
|
|
|
|
|
import Tools from "../../../canvas/constants/tools"
|
2024-09-28 16:04:02 +05:30
|
|
|
import { convertObjectToKeyValueString, removeKeyFromObject } from "../../../utils/common"
|
|
|
|
|
import { TkinterBase } from "../widgets/base"
|
2024-09-24 11:50:50 +05:30
|
|
|
|
2024-09-28 16:04:02 +05:30
|
|
|
import "./styles/timepickerStyle.css"
|
2024-09-24 11:50:50 +05:30
|
|
|
|
2024-09-28 16:04:02 +05:30
|
|
|
|
2024-09-28 17:49:54 +05:30
|
|
|
const Themes = {
|
|
|
|
|
NAVY_BLUE: "Navy Blue",
|
|
|
|
|
DRACULA: "Dracula",
|
|
|
|
|
PURPLE: "Purple",
|
|
|
|
|
NONE: ""
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-28 16:04:02 +05:30
|
|
|
class AnalogTimePicker extends TkinterBase{
|
2024-09-24 11:50:50 +05:30
|
|
|
|
2024-09-28 21:50:30 +05:30
|
|
|
static widgetType = "analog_timepicker"
|
2024-09-24 11:50:50 +05:30
|
|
|
|
2024-09-28 16:04:02 +05:30
|
|
|
static requiredImports = [
|
|
|
|
|
...TkinterBase.requiredImports,
|
2024-09-28 17:49:54 +05:30
|
|
|
'from tktimepicker import AnalogPicker, AnalogThemes, constants'
|
2024-09-28 16:04:02 +05:30
|
|
|
]
|
|
|
|
|
|
|
|
|
|
static requirements = ["tkTimePicker"]
|
|
|
|
|
|
2024-09-24 11:50:50 +05:30
|
|
|
constructor(props) {
|
|
|
|
|
super(props)
|
|
|
|
|
|
|
|
|
|
this.droppableTags = null
|
|
|
|
|
|
|
|
|
|
const newAttrs = removeKeyFromObject("layout", this.state.attrs)
|
|
|
|
|
|
|
|
|
|
this.timePicker = null
|
|
|
|
|
|
|
|
|
|
this.timePickerRef = React.createRef()
|
|
|
|
|
|
2024-09-30 09:14:16 +05:30
|
|
|
this.minSize = {width: 100, height: 100}
|
|
|
|
|
|
2024-09-24 11:50:50 +05:30
|
|
|
this.state = {
|
|
|
|
|
...this.state,
|
2024-09-28 19:14:24 +05:30
|
|
|
widgetName: "Timepicker",
|
2024-09-30 09:14:16 +05:30
|
|
|
size: { width: 250, height: 350 },
|
2024-09-24 11:50:50 +05:30
|
|
|
attrs: {
|
|
|
|
|
...newAttrs,
|
|
|
|
|
styling: {
|
2024-09-28 16:04:02 +05:30
|
|
|
theme:{
|
|
|
|
|
label: "Theme",
|
|
|
|
|
tool: Tools.SELECT_DROPDOWN,
|
|
|
|
|
toolProps: {placeholder: "select theme"},
|
|
|
|
|
value: "",
|
2024-09-28 17:49:54 +05:30
|
|
|
options: Object.values(Themes).map(val => ({value: val, label: val})),
|
2024-09-28 16:04:02 +05:30
|
|
|
onChange: (value) => this.handleThemeChange(value)
|
|
|
|
|
},
|
2024-09-24 11:50:50 +05:30
|
|
|
...newAttrs.styling,
|
2024-09-28 16:04:02 +05:30
|
|
|
clockColor: {
|
|
|
|
|
label: "Clock Color",
|
|
|
|
|
tool: Tools.COLOR_PICKER, // the tool to display, can be either HTML ELement or a constant string
|
|
|
|
|
value: "#EEEEEE",
|
|
|
|
|
onChange: (value) => {
|
|
|
|
|
this.setAttrValue("styling.clockColor", value)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
displayColor: {
|
|
|
|
|
label: "Display Color",
|
|
|
|
|
tool: Tools.COLOR_PICKER, // the tool to display, can be either HTML ELement or a constant string
|
|
|
|
|
value: "#000",
|
|
|
|
|
onChange: (value) => {
|
|
|
|
|
this.setAttrValue("styling.displayColor", value)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
numberColor: {
|
|
|
|
|
label: "Numbers Color",
|
2024-09-24 11:50:50 +05:30
|
|
|
tool: Tools.COLOR_PICKER, // the tool to display, can be either HTML ELement or a constant string
|
|
|
|
|
value: "#000",
|
|
|
|
|
onChange: (value) => {
|
2024-09-28 16:04:02 +05:30
|
|
|
this.setAttrValue("styling.numberColor", value)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
handleColor: {
|
|
|
|
|
label: "Handle Color",
|
|
|
|
|
tool: Tools.COLOR_PICKER, // the tool to display, can be either HTML ELement or a constant string
|
2024-09-30 15:30:46 +05:30
|
|
|
value: "#000000",
|
2024-09-28 16:04:02 +05:30
|
|
|
onChange: (value) => {
|
|
|
|
|
this.setAttrValue("styling.handleColor", value)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
clockMode:{
|
|
|
|
|
label: "Clock Mode",
|
|
|
|
|
tool: Tools.SELECT_DROPDOWN,
|
|
|
|
|
toolProps: {placeholder: "select mode", defaultValue: 12},
|
|
|
|
|
value: 12,
|
|
|
|
|
options: [12, 24].map(val => ({value: val, label: val})),
|
|
|
|
|
onChange: (value) => {
|
|
|
|
|
this.setAttrValue("clockMode", value)
|
|
|
|
|
if (value === 24){
|
|
|
|
|
// FIXME: the timepicker for 24 hrs also shows 12 hrs time
|
|
|
|
|
|
|
|
|
|
this.timePicker.set24h()
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
this.timePicker.set12h()
|
2024-09-24 11:50:50 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-28 16:04:02 +05:30
|
|
|
|
|
|
|
|
this.handleThemeChange = this.handleThemeChange.bind(this)
|
2024-09-24 11:50:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount(){
|
|
|
|
|
super.componentDidMount()
|
|
|
|
|
this.timePicker = timePicker({
|
|
|
|
|
element: this.timePickerRef.current,
|
2024-09-30 09:14:16 +05:30
|
|
|
mode: "12",
|
|
|
|
|
width: this.state.size.width,
|
|
|
|
|
// height: this.state.size.height
|
2024-09-24 11:50:50 +05:30
|
|
|
})
|
|
|
|
|
|
2024-09-28 16:04:02 +05:30
|
|
|
// used to remove ok and cancel buttons
|
2024-09-24 11:50:50 +05:30
|
|
|
const timePickerBtns = this.timePickerRef.current.getElementsByClassName("atp-clock-btn")
|
|
|
|
|
for (let i = 0; i < timePickerBtns.length; i++) {
|
|
|
|
|
timePickerBtns[i].remove()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount(){
|
|
|
|
|
this.timePicker.dispose()
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-30 09:14:16 +05:30
|
|
|
setResize(pos, size){
|
|
|
|
|
super.setResize(pos, size)
|
|
|
|
|
this.timePicker.setWidth(size.width)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-28 16:04:02 +05:30
|
|
|
handleThemeChange(value){
|
|
|
|
|
this.setAttrValue("styling.theme", value)
|
|
|
|
|
|
2024-09-28 17:49:54 +05:30
|
|
|
if (value === Themes.NAVY_BLUE){
|
2024-09-28 16:04:02 +05:30
|
|
|
this.setAttrValue("styling.handleColor", "#009688")
|
|
|
|
|
this.setAttrValue("styling.displayColor", "#009688")
|
|
|
|
|
this.setAttrValue("styling.backgroundColor", "#fff")
|
|
|
|
|
this.setAttrValue("styling.clockColor", "#EEEEEE")
|
|
|
|
|
this.setAttrValue("styling.numberColor", "#000")
|
2024-09-28 17:49:54 +05:30
|
|
|
}else if (value === Themes.DRACULA){
|
2024-09-28 16:04:02 +05:30
|
|
|
this.setAttrValue("styling.handleColor", "#863434")
|
|
|
|
|
this.setAttrValue("styling.displayColor", "#404040")
|
|
|
|
|
this.setAttrValue("styling.backgroundColor", "#404040")
|
|
|
|
|
this.setAttrValue("styling.clockColor", "#363636")
|
|
|
|
|
this.setAttrValue("styling.numberColor", "#fff")
|
2024-09-28 17:49:54 +05:30
|
|
|
}else if (value === Themes.PURPLE){
|
2024-09-28 16:04:02 +05:30
|
|
|
this.setAttrValue("styling.handleColor", "#EE333D")
|
|
|
|
|
this.setAttrValue("styling.displayColor", "#71135C")
|
|
|
|
|
this.setAttrValue("styling.backgroundColor", "#4E0D3A")
|
|
|
|
|
this.setAttrValue("styling.clockColor", "#71135C")
|
|
|
|
|
this.setAttrValue("styling.numberColor", "#fff")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generateCode(variableName, parent){
|
|
|
|
|
|
|
|
|
|
const theme = this.getAttrValue("styling.theme")
|
|
|
|
|
|
|
|
|
|
const mode = this.getAttrValue("clockMode")
|
|
|
|
|
const bgColor = this.getAttrValue("styling.backgroundColor")
|
|
|
|
|
const clockColor = this.getAttrValue("styling.clockColor")
|
|
|
|
|
const displayColor = this.getAttrValue("styling.displayColor")
|
|
|
|
|
const numColor = this.getAttrValue("styling.numberColor")
|
|
|
|
|
const handleColor = this.getAttrValue("styling.handleColor")
|
|
|
|
|
|
2024-09-28 17:49:54 +05:30
|
|
|
const code = [
|
2024-09-30 15:30:46 +05:30
|
|
|
`${variableName} = AnalogPicker(parent=${parent}, type=${mode===12 ? "constants.HOURS12" : "constants.HOURS24"})`,
|
2024-09-28 17:49:54 +05:30
|
|
|
]
|
|
|
|
|
|
|
|
|
|
if (theme){
|
|
|
|
|
|
|
|
|
|
code.push(`${variableName}_theme = AnalogThemes(${variableName})`)
|
|
|
|
|
if (theme === Themes.NAVY_BLUE){
|
|
|
|
|
code.push(`${variableName}_theme.setNavyBlue()`)
|
|
|
|
|
}else if (theme === Themes.DRACULA){
|
|
|
|
|
code.push(`${variableName}_theme.setDracula()`)
|
|
|
|
|
}else if (theme === Themes.PURPLE){
|
|
|
|
|
code.push(`${variableName}_theme.setPurple()`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
|
|
const configAnalog = {
|
|
|
|
|
"canvas_bg": `"${bgColor}"`,
|
|
|
|
|
"textcolor": `"${numColor}"`,
|
|
|
|
|
"bg": `"${clockColor}"`,
|
2024-09-30 15:30:46 +05:30
|
|
|
"handcolor": `"${handleColor}"`,
|
2024-09-28 17:49:54 +05:30
|
|
|
"headcolor": `"${handleColor}"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const displayConfig = {
|
|
|
|
|
bg: `"${displayColor}"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
code.push(`${variableName}.configAnalog(${convertObjectToKeyValueString(configAnalog)})`)
|
|
|
|
|
code.push(`${variableName}.configSpin(${convertObjectToKeyValueString(displayConfig)})`)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// code.push(`configAnalog(canvas_bg="${bgColor}", textcolor="${numColor}",
|
|
|
|
|
// bg="${clockColor}", handcolor="${handleColor}", headcolor="${handleColor}")`)
|
|
|
|
|
|
|
|
|
|
// code.push(`configSpin(bg="${displayColor}"`)
|
|
|
|
|
}
|
2024-09-28 16:04:02 +05:30
|
|
|
|
|
|
|
|
return [
|
2024-09-28 17:49:54 +05:30
|
|
|
...code,
|
2024-09-28 16:04:02 +05:30
|
|
|
`${variableName}.${this.getLayoutCode()}`
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 11:50:50 +05:30
|
|
|
getToolbarAttrs(){
|
|
|
|
|
|
|
|
|
|
const toolBarAttrs = super.getToolbarAttrs()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ({
|
|
|
|
|
id: this.__id,
|
|
|
|
|
widgetName: toolBarAttrs.widgetName,
|
|
|
|
|
size: toolBarAttrs.size,
|
|
|
|
|
|
|
|
|
|
...this.state.attrs,
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-28 16:04:02 +05:30
|
|
|
|
2024-09-24 11:50:50 +05:30
|
|
|
renderContent(){
|
2024-09-28 16:04:02 +05:30
|
|
|
const timePickerStyling = {
|
|
|
|
|
'--bg': this.getAttrValue("styling.backgroundColor"),
|
|
|
|
|
'--clock-color': this.getAttrValue("styling.clockColor"),
|
|
|
|
|
'--number-color': this.getAttrValue("styling.numberColor"),
|
|
|
|
|
'--time-display': this.getAttrValue("styling.displayColor"),
|
|
|
|
|
'--main-handle-color': this.getAttrValue("styling.handleColor"),
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 11:50:50 +05:30
|
|
|
return (
|
|
|
|
|
<div className="tw-w-flex tw-flex-col tw-w-full tw-h-full tw-rounded-md
|
|
|
|
|
tw-border tw-border-solid tw-border-gray-400 tw-overflow-hidden">
|
2024-09-30 09:14:16 +05:30
|
|
|
<div className="tw-p-2 tw-w-full tw-h-full tw-flex tw-content-start tw-pointer-events-none"
|
2024-09-28 16:04:02 +05:30
|
|
|
style={timePickerStyling}
|
|
|
|
|
ref={this.timePickerRef}>
|
2024-09-24 11:50:50 +05:30
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default AnalogTimePicker
|