Skip to content

Commit

Permalink
Merge pull request #74 from team5499/dev/selector
Browse files Browse the repository at this point in the history
Add dropdown selector
  • Loading branch information
andycate authored Mar 2, 2019
2 parents 547f4e9 + bff91f0 commit f5ffed3
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies {
}

group = 'org.team5499'
version = '0.10.0' /* Change this when deploying a new version */
version = '0.11.0' /* Change this when deploying a new version */

task sourcesJar(type: Jar) {
from sourceSets.main.allJava
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/org/team5499/dashboard/DashboardType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.team5499.dashboard

interface DashboardType
62 changes: 62 additions & 0 deletions src/main/kotlin/org/team5499/dashboard/StringChooser.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.team5499.dashboard

import org.json.JSONObject
import org.json.JSONArray

@Suppress("SpreadOperator")
class StringChooser(val dashboardName: String, val default: String, vararg initialOptions: String) : DashboardType {
public var options: MutableList<String>
public var selected: String
get() {
return Dashboard.getVariable<JSONObject>(dashboardName).getString("selected")
}
set(value) {
Dashboard.setVariable(dashboardName, getBroadcastObject(value))
}

init {
options = mutableListOf(*initialOptions)
selected = default
}

private fun getBroadcastObject(value: String): JSONObject {
val newJSON = JSONObject()
val optionsJSON = JSONArray()
options.forEach({
optionsJSON.put(it)
})
newJSON.put("options", optionsJSON)
newJSON.put("selected", value)
newJSON.put("vartype", "StringChooser")
return newJSON
}

fun addVarListener(callback: VariableCallback): Int {
return Dashboard.addVarListener(dashboardName) {
key: String, value: Any? ->
callback(key, (value as? JSONObject)!!.get("selected") as? Any)
}
}

fun removeVarListener(id: Int): Boolean {
return Dashboard.removeVarListener(dashboardName, id)
}

fun runIfUpdate(callback: VariableCallback): Boolean {
return Dashboard.runIfUpdate(dashboardName) {
key: String, value: Any? ->
callback(key, (value as? JSONObject)!!.get("selected") as? Any)
}
}

fun addInlineListener(callback: VariableCallback): Int {
return Dashboard.addInlineListener(dashboardName) {
key: String, value: Any? ->
callback(key, (value as? JSONObject)!!.get("selected") as? Any)
}
}

fun removeInlineListener(id: Int): Boolean {
return Dashboard.removeInlineListener(dashboardName, id)
}
}
1 change: 1 addition & 0 deletions src/main/resources/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@
<script src="/javascript/widgets/double-editor.jsx" data-plugins="transform-es2015-modules-umd" type="text/babel"></script>
<script src="/javascript/widgets/pidf-widget.jsx" data-plugins="transform-es2015-modules-umd" type="text/babel"></script>
<script src="/javascript/widgets/pid-widget.jsx" data-plugins="transform-es2015-modules-umd" type="text/babel"></script>
<script src="/javascript/widgets/string-chooser.jsx" data-plugins="transform-es2015-modules-umd" type="text/babel"></script>
<script src="/javascript/widgets/camera-widget.jsx" data-plugins="transform-es2015-modules-umd" type="text/babel"></script>
{% endblock %}
31 changes: 16 additions & 15 deletions src/main/resources/static/javascript/socket-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,19 @@ export default class SocketHandler {
}

static addVariableListener(key, callback) {
if(!Array.isArray(callbacks[key])) {
callbacks[key] = [];
if(callbacks[key] === undefined) {
callbacks[key] = {};
}
callbacks[key].push(callback);
return callbacks[key].length - 1;
let uid = key + Date.now().toString();
callbacks[key][uid] = callback;
return uid;
}

static removeVariableListener(key, id) {
callbacks[key].splice(id, 1);
return delete callbacks[key][id];
}

static _getVariable(key) {
static getVariable(key) {
if((!(key in variables)) && (!(key in variableUpdates))) {
console.warn("variable " + key + " not found!");
return undefined;
Expand All @@ -87,56 +88,56 @@ export default class SocketHandler {
}
}

static _setVariable(key, value) {
static setVariable(key, value) {
variableUpdates[key] = value;
}

static setInt(key, value) {
let newValue = parseInt(value);
SocketHandler._setVariable(key, newValue);
SocketHandler.setVariable(key, newValue);
}

static setDouble(key, value) {
let newValue = parseFloat(value);
SocketHandler._setVariable(key, newValue);
SocketHandler.setVariable(key, newValue);
}

static setString(key, value) {
let newValue = String(value);
SocketHandler._setVariable(key, newValue);
SocketHandler.setVariable(key, newValue);
}

static setBoolean(key, value) {
let newValue = Boolean(value);
SocketHandler._setVariable(key, newValue);
SocketHandler.setVariable(key, newValue);
}

static getInt(key) {
let value = SocketHandler._getVariable(key);
let value = SocketHandler.getVariable(key);
if(value === undefined) {
return undefined;
}
return parseInt(value);
}

static getDouble(key) {
let value = SocketHandler._getVariable(key);
let value = SocketHandler.getVariable(key);
if(value === undefined) {
return undefined;
}
return parseFloat(value);
}

static getString(key) {
let value = SocketHandler._getVariable(key);
let value = SocketHandler.getVariable(key);
if(value === undefined) {
return undefined;
}
return String(value);
}

static getBoolean(key) {
let value = SocketHandler._getVariable(key);
let value = SocketHandler.getVariable(key);
if(value === undefined) {
return undefined;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/static/javascript/widget-components.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ export class WidgetContainer extends React.Component {
{React.createElement(this.props.widgetClass.Body,
{registerVarListener: (variable, callback) => SocketHandler.addVariableListener(variable, callback),
removeVarListener: (variable, id) => SocketHandler.removeVariableListener(variable, id),
setVariable: (key, value) => SocketHandler.setVariable(key, value),
setInt: (key, value) => SocketHandler.setInt(key, value),
setDouble: (key, value) => SocketHandler.setDouble(key, value),
setString: (key, value) => SocketHandler.setString(key, value),
setBoolean: (key, value) => SocketHandler.setBoolean(key, value),
getVariable: (key) => SocketHandler.getVariable(key),
getInt: (key) => SocketHandler.getInt(key),
getDouble: (key) => SocketHandler.getDouble(key),
getString: (key) => SocketHandler.getString(key),
Expand All @@ -70,10 +72,12 @@ export class WidgetContainer extends React.Component {
{React.createElement(this.props.widgetClass.Settings,
{registerVarListener: (variable, callback) => SocketHandler.addVariableListener(variable, callback),
removeVarListener: (variable, id) => SocketHandler.removeVariableListener(variable, id),
setVariable: (key, value) => SocketHandler.setVariable(key, value),
setInt: (key, value) => SocketHandler.setInt(key, value),
setDouble: (key, value) => SocketHandler.setDouble(key, value),
setString: (key, value) => SocketHandler.setString(key, value),
setBoolean: (key, value) => SocketHandler.setBoolean(key, value),
getVariable: (key) => SocketHandler.getVariable(key),
getInt: (key) => SocketHandler.getInt(key),
getDouble: (key) => SocketHandler.getDouble(key),
getString: (key) => SocketHandler.getString(key),
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/static/javascript/widget.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ Widget.Body = class extends React.Component {
return this.settingsSave(newConfig);
}

getVariable(key) {
return this.props.getVariable(key);
}

getInt(key) {
return this.props.getInt(key);
}
Expand All @@ -39,6 +43,10 @@ Widget.Body = class extends React.Component {
return this.props.getBoolean(key);
}

setVariable(key, value) {
return this.props.setVariable(key, value);
}

setInt(key, value) {
return this.props.setInt(key, value);
}
Expand Down Expand Up @@ -85,6 +93,10 @@ Widget.Settings = class extends React.Component {
return this.widgetConfig
}

getVariable(key) {
return this.props.getVariable(key);
}

getInt(key) {
return this.props.getInt(key);
}
Expand All @@ -101,6 +113,10 @@ Widget.Settings = class extends React.Component {
return this.props.getBoolean(key);
}

setVariable(key, value) {
return this.props.setVariable(key, value);
}

setInt(key, value) {
return this.props.setInt(key, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ DoubleEditor.Body = class extends Widget.Body {
}

settingsSave(newConfig) {
this.removeVarListener(this.state.targetName);
this.removeVarListener(this.state.targetName, this.callbackId);
// do something with success, and also update event listener for variable changes
let newValue = (this.getDouble(newConfig.variables.target) === undefined) ? '' : this.getDouble(newConfig.variables.target);
this.setState({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ IntEditor.Body = class extends Widget.Body {
}

settingsSave(newConfig) {
this.removeVarListener(this.state.targetName);
this.removeVarListener(this.state.targetName, this.callbackId);
let newValue = (this.getInt(newConfig.variables.target) === undefined) ? "" : this.getInt(newConfig.variables.target);
this.setState({
targetName: newConfig.variables.target,
Expand Down
90 changes: 90 additions & 0 deletions src/main/resources/static/javascript/widgets/string-chooser.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import PageUtils from 'page-utils';
import Widget from 'widget';

class StringChooser {}

StringChooser.Body = class extends Widget.Body {
init() {
console.log(this.constructor.name)
let newValue = (this.getVariable(this.widgetConfig.variables.target) === undefined) ? {options: [''], selected: ''} : this.getVariable(this.widgetConfig.variables.target);
this.state = {
targetName: this.widgetConfig.variables.target,
targetValue: newValue
};
this.callbackId = this.registerVarListener(this.state.targetName, (key, value) => this.updateState(key, value));
}

updateState(key, value) {
this.setState({targetValue: value});
}

settingsSave(newConfig) {
this.removeVarListener(this.state.targetName, this.callbackId);
// do something with success, and also update event listener for variable changes
let newValue = (this.getString(newConfig.variables.target) === undefined) ? '' : this.getString(newConfig.variables.target);
this.setState({
targetName: newConfig.variables.target,
targetValue: newValue
});
this.callbackId = this.registerVarListener(newConfig.variables.target, (key, value) => this.updateState(key, value));
}

getOptions() {
let options = [];
for(let i in this.state.targetValue.options) {
options.push(<a className="dropdown-item" onClick={() => this.updateSelectedValue(this.state.targetValue.options[i])} key={this.state.targetValue.options[i]} href="#">{this.state.targetValue.options[i]}</a>);
}
return options;
}

updateSelectedValue(select) {
let newValue = this.state.targetValue;
newValue.selected = select;
this.setState({targetValue: newValue});
this.setVariable(this.state.targetName, newValue);
}

render() {
let buttonId = this.widgetConfig.id + "dropdownButton";
return (
<div className="dropdown show">
<a className="btn btn-primary dropdown-toggle" href="#" role="button" id={buttonId} data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{this.state.targetValue.selected}
</a>

<div className="dropdown-menu" aria-labelledby={buttonId}>
{this.getOptions()}
</div>
</div>
);
}
}

StringChooser.Settings = class extends Widget.Settings {
init() {
this.state = {
targetName: this.widgetConfig.variables.target
};
}

settingsData(config) {
let newConfig = config;
newConfig.variables.target = this.state.targetName;
return newConfig;
}

onSettingsEdit(e) {
this.setState({targetName: e.target.value});
}

render() {
return (
<input className='form-control mb-2' type='text' placeholder='variable' value={this.state.targetName} onChange={(e) => this.onSettingsEdit(e)} />
);
}
}

export default StringChooser;

// make sure to do this for every widget
PageUtils.addWidgetClass('StringChooser', StringChooser);
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ StringEditor.Body = class extends Widget.Body {
}

settingsSave(newConfig) {
this.removeVarListener(this.state.targetName);
this.removeVarListener(this.state.targetName, this.callbackId);
// do something with success, and also update event listener for variable changes
let newValue = (this.getString(newConfig.variables.target) === undefined) ? '' : this.getString(newConfig.variables.target);
this.setState({
Expand Down
Loading

0 comments on commit f5ffed3

Please sign in to comment.