-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from team5499/dev/selector
Add dropdown selector
- Loading branch information
Showing
13 changed files
with
225 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package org.team5499.dashboard | ||
|
||
interface DashboardType |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/resources/static/javascript/widgets/string-chooser.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.