Empirical
Selector.h
Go to the documentation of this file.
1 
24 #ifndef EMP_WEB_SELECTOR_H
25 #define EMP_WEB_SELECTOR_H
26 
27 
28 #include "../base/vector.h"
29 
30 #include "JSWrap.h"
31 #include "Widget.h"
32 
33 namespace emp {
34 namespace web {
35 
36  class Selector : public internal::WidgetFacet<Selector> {
37  friend class SelectorInfo;
38  protected:
39 
41  friend Selector;
42  protected:
45  size_t select_id;
46 
47  bool autofocus;
48  bool disabled;
49 
50  size_t callback_id;
51 
52  SelectorInfo(const std::string & in_id="")
53  : internal::WidgetInfo(in_id), select_id(0), autofocus(false), disabled(false) { ; }
54  SelectorInfo(const SelectorInfo &) = delete; // No copies of INFO allowed
55  SelectorInfo & operator=(const SelectorInfo &) = delete; // No copies of INFO allowed
56  virtual ~SelectorInfo() {
57  if (callback_id) emp::JSDelete(callback_id); // Delete callback wrapper.
58  }
59 
60  std::string TypeName() const override { return "SelectorInfo"; }
61  virtual bool IsSelectorInfo() const override { return true; }
62 
63  void SetOption(const std::string & name, const std::function<void()> & cb, size_t id) {
64  // If we need more room for options, increase the array size.
65  if (id >= options.size()) {
66  options.resize(id+1);
67  callbacks.resize(id+1);
68  }
69  options[id] = name;
70  callbacks[id] = cb;
71  }
72  void SetOption(const std::string & name, const std::function<void()> & cb) {
73  SetOption(name, cb, options.size()); // No option id specified, so choose the next one.
74  }
75 
76  void DoChange(size_t new_id) {
77  select_id = new_id;
78  if (callbacks[new_id]) callbacks[new_id]();
79  }
80 
81  virtual void GetHTML(std::stringstream & HTML) override {
82  HTML << "<select"; // Start the select tag.
83  if (disabled) { HTML << " disabled=true"; } // Check if should be disabled
84  HTML << " id=\"" << id << "\""; // Indicate ID.
85 
86  // Indicate action on change.
87  HTML << " onchange=\"emp.Callback(" << callback_id << ", this.selectedIndex)\">";
88 
89  // List out options
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>";
94  }
95  HTML << "</select>";
96  }
97 
98  void UpdateAutofocus(bool in_af) {
99  autofocus = in_af;
100  if (state == Widget::ACTIVE) ReplaceHTML();
101  }
102  void UpdateDisabled(bool in_dis) {
103  disabled = in_dis;
104  if (state == Widget::ACTIVE) ReplaceHTML();
105  }
106 
107  public:
108  virtual std::string GetType() override { return "web::SelectorInfo"; }
109  }; // End of SelectorInfo class.
110 
111 
112  // Get a properly cast version of indo.
113  SelectorInfo * Info() { return (SelectorInfo *) info; }
114  const SelectorInfo * Info() const { return (SelectorInfo *) info; }
115 
116  Selector(SelectorInfo * in_info) : WidgetFacet(in_info) { ; }
117 
118  public:
119  Selector(const std::string & in_id="") : WidgetFacet(in_id)
120  {
121  info = new SelectorInfo(in_id);
122 
123  Info()->select_id = 0;
124  Info()->autofocus = false;
125  Info()->disabled = false;
126 
127  SelectorInfo * s_info = Info();
128  Info()->callback_id =
129  JSWrap( std::function<void(size_t)>([s_info](size_t new_id){s_info->DoChange(new_id);}) );
130  }
131  Selector(const Selector & in) : WidgetFacet(in) { ; }
133  virtual ~Selector() { ; }
134 
136 
138  size_t GetSelectID() const { return Info()->select_id; }
139 
141  size_t GetNumOptions() const { return Info()->options.size(); }
142 
144  const std::string & GetOption(size_t id) const { return Info()->options[id]; }
145 
147  bool HasAutofocus() const { return Info()->autofocus; }
148 
150  bool IsDisabled() const { return Info()->disabled; }
151 
153  Selector & SelectID(size_t id) { Info()->select_id = id; return *this; }
154 
156  Selector & SetOption(const std::string & in_option,
157  const std::function<void()> & in_cb) {
158  Info()->SetOption(in_option, in_cb);
159  return *this;
160  }
161 
164  Selector & SetOption(const std::string & in_option,
165  const std::function<void()> & in_cb,
166  size_t opt_id) {
167  Info()->SetOption(in_option, in_cb, opt_id);
168  return *this;
169  }
170 
172  Selector & SetOption(const std::string & in_option) {
173  return SetOption(in_option, std::function<void()>([](){}));
174  }
175 
177  Selector & SetOption(const std::string & in_option, size_t opt_id) {
178  return SetOption(in_option, std::function<void()>([](){}), opt_id);
179  }
180 
182  Selector & Autofocus(bool in_af) { Info()->UpdateAutofocus(in_af); return *this; }
183 
185  Selector & Disabled(bool in_dis) { Info()->UpdateDisabled(in_dis); return *this; }
186  };
187 
188 
189 }
190 }
191 
192 #endif
size_t select_id
Which index is currently selected?
Definition: Selector.h:45
Definition: Widget.h:206
virtual void ReplaceHTML()
Definition: Widget.h:319
emp::vector< std::function< void()> > callbacks
Which funtion to run for each option?
Definition: Selector.h:44
std::string id
ID used for associated DOM element.
Definition: Widget.h:212
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
WidgetInfo * info
Information associated with this widget.
Definition: Widget.h:82
WidgetFacet is a template that provides accessors into Widget with a derived return type...
Definition: Widget.h:543
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
Definition: Widget.h:102
size_t size() const
Definition: vector.h:151
bool autofocus
Definition: Selector.h:47
Widget::ActivityState state
Is this element active in DOM?
Definition: Widget.h:218
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
Widgets maintain individual components on a web page and link to Elements.
virtual bool IsSelectorInfo() const
Definition: Widget.h:243
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
Widget is effectively a smart pointer to a WidgetInfo object, plus some basic accessors.
Definition: Widget.h:78
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
WidgetFacet(const std::string &in_id="")
WidgetFacet cannot be built unless within derived class, so constructors are protected.
Definition: Widget.h:546
size_t GetSelectID() const
Get the ID of the currently active selection.
Definition: Selector.h:138