24 #ifndef EMP_WEB_SELECTOR_H 25 #define EMP_WEB_SELECTOR_H 28 #include "../base/vector.h" 53 : internal::
WidgetInfo(in_id), select_id(0), autofocus(false), disabled(false) { ; }
57 if (callback_id) emp::JSDelete(callback_id);
60 std::string
TypeName()
const override {
return "SelectorInfo"; }
63 void SetOption(
const std::string & name,
const std::function<
void()> & cb,
size_t id) {
65 if (
id >= options.
size()) {
72 void SetOption(
const std::string & name,
const std::function<
void()> & cb) {
78 if (callbacks[new_id]) callbacks[new_id]();
81 virtual void GetHTML(std::stringstream & HTML)
override {
83 if (disabled) { HTML <<
" disabled=true"; }
84 HTML <<
" id=\"" <<
id <<
"\"";
87 HTML <<
" onchange=\"emp.Callback(" << callback_id <<
", this.selectedIndex)\">";
90 for (
size_t i = 0; i < options.
size(); i++) {
91 HTML <<
"<option value=\"" << i <<
"\"";
92 if (i == select_id) HTML <<
" selected";
93 HTML <<
">" << options[i] <<
"</option>";
108 virtual std::string
GetType()
override {
return "web::SelectorInfo"; }
129 JSWrap( std::function<
void(
size_t)>([s_info](
size_t new_id){s_info->
DoChange(new_id);}) );
157 const std::function<
void()> & in_cb) {
165 const std::function<
void()> & in_cb,
173 return SetOption(in_option, std::function<
void()>([](){}));
178 return SetOption(in_option, std::function<
void()>([](){}), opt_id);
size_t select_id
Which index is currently selected?
Definition: Selector.h:45
emp::vector< std::function< void()> > callbacks
Which funtion to run for each option?
Definition: Selector.h:44
const SelectorInfo * Info() const
Definition: Selector.h:114
Selector & SelectID(size_t id)
Set a specific ID as currently active.
Definition: Selector.h:153
Selector(const std::string &in_id="")
Definition: Selector.h:119
virtual ~SelectorInfo()
Definition: Selector.h:56
virtual ~Selector()
Definition: Selector.h:133
const std::string & GetOption(size_t id) const
Get the label associated with a specific option ID.
Definition: Selector.h:144
Selector & SetOption(const std::string &in_option)
Set a selector option name, but no function to be called.
Definition: Selector.h:172
Definition: Selector.h:40
bool disabled
Definition: Selector.h:48
Selector(const Selector &in)
Definition: Selector.h:131
std::string TypeName() const override
Debugging helpers...
Definition: Selector.h:60
size_t size() const
Definition: vector.h:151
bool autofocus
Definition: Selector.h:47
Selector & Autofocus(bool in_af)
Update autofocus setting.
Definition: Selector.h:182
Selector & Disabled(bool in_dis)
Update disabled status.
Definition: Selector.h:185
void DoChange(size_t new_id)
Definition: Selector.h:76
virtual void GetHTML(std::stringstream &HTML) override
Definition: Selector.h:81
Selector & SetOption(const std::string &in_option, const std::function< void()> &in_cb, size_t opt_id)
Definition: Selector.h:164
void SetOption(const std::string &name, const std::function< void()> &cb, size_t id)
Definition: Selector.h:63
void UpdateAutofocus(bool in_af)
Definition: Selector.h:98
bool IsDisabled() const
Is the selector currently disabled?
Definition: Selector.h:150
size_t GetNumOptions() const
Get the total number of options setup in the selector.
Definition: Selector.h:141
void resize(size_t new_size)
Definition: vector.h:161
Wrap a C++ function and convert it to an integer that can be called from Javascript.
If we are in emscripten, make sure to include the header.
Definition: array.h:37
Selector(const Widget &in)
Definition: Selector.h:132
virtual std::string GetType() override
Definition: Selector.h:108
#define emp_assert(...)
Definition: assert.h:199
Definition: Selector.h:36
virtual bool IsSelectorInfo() const override
Definition: Selector.h:61
bool HasAutofocus() const
Determine if the selector has autofocus.
Definition: Selector.h:147
SelectorInfo(const std::string &in_id="")
Definition: Selector.h:52
Selector & SetOption(const std::string &in_option, size_t opt_id)
Set a specific selection option name, determined by the ID, but no function to call.
Definition: Selector.h:177
void UpdateDisabled(bool in_dis)
Definition: Selector.h:102
Selector(SelectorInfo *in_info)
Definition: Selector.h:116
SelectorInfo & operator=(const SelectorInfo &)=delete
emp::vector< std::string > options
What are the options to choose from?
Definition: Selector.h:43
Selector & SetOption(const std::string &in_option, const std::function< void()> &in_cb)
Add a new option to the selector and the function to be called if it is chosen.
Definition: Selector.h:156
SelectorInfo * Info()
Definition: Selector.h:113
void SetOption(const std::string &name, const std::function< void()> &cb)
Definition: Selector.h:72
size_t callback_id
Definition: Selector.h:50
size_t GetSelectID() const
Get the ID of the currently active selection.
Definition: Selector.h:138