Skip to content

Commit

Permalink
Dashboard color picker
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato committed Nov 11, 2020
1 parent 765461e commit 63edb93
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 2 deletions.
14 changes: 14 additions & 0 deletions front/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions front/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"dependencies": {
"@gladysassistant/gladys-gateway-js": "^3.2.4",
"@jaames/iro": "^5.2.3",
"axios": "^0.18.0",
"classnames": "^2.2.6",
"cropperjs": "^1.5.1",
Expand Down
15 changes: 15 additions & 0 deletions front/src/components/boxs/device-in-room/DeviceRow.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BinaryDeviceFeature from './device-features/BinaryDeviceFeature';
import ColorDeviceFeature from './device-features/ColorDeviceFeature';
import SensorDeviceFeature from './device-features/SensorDeviceFeature';
import MultilevelDeviceFeature from './device-features/MultiLevelDeviceFeature';

Expand All @@ -24,6 +25,20 @@ const DeviceRow = ({ children, ...props }) => {
/>
);
}
if (props.deviceFeature.type === 'color') {
return (
<ColorDeviceFeature
x={props.x}
y={props.y}
device={props.device}
deviceFeature={props.deviceFeature}
roomIndex={props.roomIndex}
deviceIndex={props.deviceIndex}
deviceFeatureIndex={props.deviceFeatureIndex}
updateValue={props.updateValue}
/>
);
}
if (props.deviceFeature.type === 'dimmer') {
return (
<MultilevelDeviceFeature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { DEVICE_FEATURE_TYPES } from '../../../../../server/utils/constants';

import actions from '../../../actions/dashboard/edit-boxes/editDevicesInRoom';

const SUPPORTED_FEATURE_TYPES = [DEVICE_FEATURE_TYPES.LIGHT.BINARY, DEVICE_FEATURE_TYPES.LIGHT.COLOR];

@connect('httpClient', actions)
class EditDeviceInRoom extends Component {
updateBoxRoom = room => {
Expand Down Expand Up @@ -40,7 +42,7 @@ class EditDeviceInRoom extends Component {
label: getDeviceFeatureName(this.context.intl.dictionary, device, feature)
};
// for now, we only supports binary on/off and sensors
if (feature.read_only || feature.type === DEVICE_FEATURE_TYPES.LIGHT.BINARY) {
if (feature.read_only || SUPPORTED_FEATURE_TYPES.includes(feature.type)) {
roomDeviceFeatures.push(featureOption);
}
if (this.props.box.device_features && this.props.box.device_features.indexOf(feature.selector) !== -1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { Component, createRef, Fragment } from 'preact';
import cx from 'classnames';
import iro from '@jaames/iro';
import { getDeviceName } from './utils';

import style from './style.css';

const intToHexColor = value => {
if (value) {
return `#${value.toString(16).padStart(6, '0')}`;
}

return undefined;
};

class ColorDeviceType extends Component {
colorPickerRef = createRef();

scroll = () => {
this.colorPickerRef.current.parentElement.scrollIntoView({
behavior: 'smooth'
});
};

resetAutoClose = () => {
clearTimeout(this.closeTimer);
};

autoClose = () => {
this.closeTimer = setTimeout(this.setPicketState, 2000, false);
};

toggleColorPicker = () => {
this.setPicketState(!this.state.open);

this.autoClose();
};

setPicketState = open => {
this.setState({ open }, () => {
if (this.state.open) {
setTimeout(this.scroll, 200);
}
});
};

updateValue = color => {
const colorInt = parseInt(color.hexString.substring(1), 16);
this.props.updateValue(
this.props.x,
this.props.y,
this.props.device,
this.props.deviceFeature,
this.props.deviceIndex,
this.props.deviceFeatureIndex,
colorInt
);
};

componentDidMount() {
// create a new iro color picker and pass component props to it
const color = intToHexColor(this.props.deviceFeature.last_value);
this.colorPicker = new iro.ColorPicker(this.colorPickerRef.current, {
width: 150,
color,
layout: [
{
component: iro.ui.Wheel,
options: {}
}
]
});
// call onColorChange prop whenever the color changes
this.colorPicker.on('color:change', color => this.updateValue(color));
}

render({ device, deviceFeature }, { open }) {
const color = intToHexColor(deviceFeature.last_value);

return (
<Fragment>
<tr>
<td>
<i class="fe fe-circle" />
</td>
<td>{getDeviceName(device, deviceFeature)}</td>
<td class="text-right">
<div class="m-0 float-right d-flex">
<button
class="btn py-2 border-1 border-dark"
style={{ backgroundColor: color }}
onClick={this.toggleColorPicker}
/>
</div>
</td>
</tr>
<tr>
<td colSpan="3" class="border-0 p-0">
<div
class={cx(
'd-flex',
'justify-content-center',
'position-absolute',
'w-100',
'bg-white',
style.colorPicker,
{
[style.colorPickerShow]: open
}
)}
onMouseOut={this.autoClose}
onMouseOver={this.resetAutoClose}
>
<div ref={this.colorPickerRef} class="m-2 p-2" />
</div>
</td>
</tr>
</Fragment>
);
}
}

export default ColorDeviceType;
12 changes: 12 additions & 0 deletions front/src/components/boxs/device-in-room/device-features/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.colorPicker {
max-height: 0px;
transition: max-height 0.5s ease-in;
overflow: hidden;
left: 0;
z-index: 20;
}

.colorPickerShow {
max-height: 500px;
transition: max-height 0.5s ease-out;
}
19 changes: 18 additions & 1 deletion front/src/config/demo.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,13 @@
{
"type": "devices-in-room",
"room": "living-room",
"device_features": ["main-lamp-binary", "tv-lamp-binary", "mqtt-living-room-switch", "mqtt-living-room-temp"]
"device_features": [
"main-lamp-binary",
"tv-lamp-binary",
"tv-lamp-color",
"mqtt-living-room-switch",
"mqtt-living-room-temp"
]
}
],
[
Expand Down Expand Up @@ -234,6 +240,17 @@
"read_only": false,
"last_value": 1,
"last_value_changed": "2019-02-12 07:49:07.556 +00:00"
},
{
"name": "TV Lamp color",
"selector": "tv-lamp-color",
"category": "light",
"type": "color",
"min": 0,
"max": 16777215,
"read_only": false,
"last_value": 65000,
"last_value_changed": "2019-02-12 07:49:07.556 +00:00"
}
]
},
Expand Down

0 comments on commit 63edb93

Please sign in to comment.