Empirical
FileInput.h
Go to the documentation of this file.
1 
12 #ifndef EMP_WEB_FILE_INPUT_H
13 #define EMP_WEB_FILE_INPUT_H
14 
15 #include <functional>
16 #include <string>
17 
18 #include "../tools/File.h"
19 #include "Widget.h"
20 
21 namespace emp {
22 namespace web {
23 
31 
32  class FileInput : public internal::WidgetFacet<FileInput> {
33  friend class FileInputInfo;
34  protected:
35 
36  // FileInputs associated with the same DOM element share a single FileInputInfo object.
38  friend FileInput;
39  protected:
40  std::string title;
41 
42  bool autofocus;
43  bool disabled;
44 
45  std::function<void(const std::string &)> callback;
46  uint32_t callback_id;
47 
48  FileInputInfo(const std::string & in_id="") : internal::WidgetInfo(in_id) { ; }
49  FileInputInfo(const FileInputInfo &) = delete; // No copies of INFO allowed
50  FileInputInfo & operator=(const FileInputInfo &) = delete; // No copies of INFO allowed
51  virtual ~FileInputInfo() {
52  if (callback_id) emp::JSDelete(callback_id); // Delete callback wrapper.
53  }
54 
55  std::string TypeName() const override { return "FileInputInfo"; }
56 
57  void DoCallback(const std::string & file_body) {
58  callback(file_body);
60  }
61 
62  virtual void GetHTML(std::stringstream & HTML) override {
63  HTML.str(""); // Clear the current text.
64  HTML <<"<input type=\"file\"";
65  if (title != "") HTML << " title=\"" << title << "\""; // Add a title if there is one
66  if (disabled) { HTML << " disabled=true"; } // Check if should be disabled
67  HTML << " id=\"" << id << "\""; // Indicate ID.
68  HTML << " name=\"" << id << "\""; // Use same name as ID
69  HTML << " onchange=\"emp.LoadFileEvent(this.files, " << callback_id << ")\"";
70  HTML << ">";
71  }
72 
73  void UpdateCallback(const std::function<void(const std::string &)> & in_cb) {
74  callback = in_cb;
75  }
76 
77  void UpdateTitle(const std::string & in_title) {
78  title = in_title;
79  if (state == Widget::ACTIVE) ReplaceHTML(); // If node is active, immediately redraw!
80  }
81  void UpdateAutofocus(bool in_af) {
82  autofocus = in_af;
83  if (state == Widget::ACTIVE) ReplaceHTML(); // If node is active, immediately redraw!
84  }
85  void UpdateDisabled(bool in_dis) {
86  disabled = in_dis;
87  if (state == Widget::ACTIVE) ReplaceHTML(); // If node is active, immediately redraw!
88  }
89 
90  public:
91  virtual std::string GetType() override { return "web::FileInputInfo"; }
92  }; // End of FileInputInfo definition
93 
94 
95  // Get a properly cast version of indo.
96  FileInputInfo * Info() { return (FileInputInfo *) info; }
97  const FileInputInfo * Info() const { return (FileInputInfo *) info; }
98 
99  FileInput(FileInputInfo * in_info) : WidgetFacet(in_info) { ; }
100 
101  public:
104  FileInput(const std::function<void(const std::string &)> & in_cb, const std::string & in_id="")
105  : WidgetFacet(in_id)
106  {
107  info = new FileInputInfo(in_id);
108 
109  Info()->title = "";
110  Info()->autofocus = false;
111  Info()->disabled = false;
112 
113  Info()->callback = in_cb;
114  FileInputInfo * w_info = Info();
115  using callback_t = std::function<void(const std::string & file_body)>;
116  Info()->callback_id = JSWrap( callback_t( [w_info](const std::string & file_body){w_info->DoCallback(file_body);} ) );
117  }
118 
121  FileInput(const std::function<void(const emp::File &)> & cb, const std::string & in_id="")
122  : FileInput( [cb](const std::string & in){ std::stringstream ss(in); File file(ss); cb(file); } ) { ; }
123 
125  FileInput(const FileInput & in) : WidgetFacet(in) { ; }
126  FileInput(const Widget & in) : WidgetFacet(in) { ; }
127  virtual ~FileInput() { ; }
128 
130 
132  FileInput & Callback(const std::function<void(const std::string &)> & in_cb) {
133  Info()->UpdateCallback(in_cb);
134  return *this;
135  }
136 
138  FileInput & Title(const std::string & in_t) { Info()->UpdateTitle(in_t); return *this; }
139 
141  FileInput & Autofocus(bool in_af) { Info()->UpdateAutofocus(in_af); return *this; }
142 
144  FileInput & Disabled(bool in_dis) { Info()->UpdateDisabled(in_dis); return *this; }
145 
147  const std::string & GetTitle() const { return Info()->title; }
148 
150  bool HasAutofocus() const { return Info()->autofocus; }
151 
153  bool IsDisabled() const { return Info()->disabled; }
154  };
155 
156 
157 }
158 }
159 
160 #endif
void UpdateAutofocus(bool in_af)
Definition: FileInput.h:81
Definition: Widget.h:206
FileInput(const FileInput &in)
Load a pre-existing FileInput object.
Definition: FileInput.h:125
FileInput & Title(const std::string &in_t)
Set a ToolTip for this FileInput object.
Definition: FileInput.h:138
FileInput & Callback(const std::function< void(const std::string &)> &in_cb)
Change the callback function to use when a new file is loaded.
Definition: FileInput.h:132
virtual void ReplaceHTML()
Definition: Widget.h:319
FileInputInfo(const std::string &in_id="")
Definition: FileInput.h:48
FileInputInfo * Info()
Definition: FileInput.h:96
void UpdateTitle(const std::string &in_title)
Definition: FileInput.h:77
virtual ~FileInputInfo()
Definition: FileInput.h:51
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
bool HasAutofocus() const
Determine if this object currently has autofocus.
Definition: FileInput.h:150
Definition: FileInput.h:32
FileInput & Disabled(bool in_dis)
Set this FileInput object to be disabled (or renable it.)
Definition: FileInput.h:144
void DoCallback(const std::string &file_body)
Definition: FileInput.h:57
A class to maintin files for loading, writing, storing, and easy access to components.
Definition: File.h:32
std::function< void(const std::string &)> callback
Definition: FileInput.h:45
Definition: BitVector.h:785
void UpdateDisabled(bool in_dis)
Definition: FileInput.h:85
FileInput(const std::function< void(const std::string &)> &in_cb, const std::string &in_id="")
Definition: FileInput.h:104
uint32_t callback_id
Definition: FileInput.h:46
Definition: Widget.h:102
const FileInputInfo * Info() const
Definition: FileInput.h:97
Widget::ActivityState state
Is this element active in DOM?
Definition: Widget.h:218
FileInput(const std::function< void(const emp::File &)> &cb, const std::string &in_id="")
Definition: FileInput.h:121
bool IsDisabled() const
Determine if this object is currently disabled.
Definition: FileInput.h:153
bool autofocus
Definition: FileInput.h:42
Definition: FileInput.h:37
FileInput(const Widget &in)
Definition: FileInput.h:126
std::string title
Definition: FileInput.h:40
Widgets maintain individual components on a web page and link to Elements.
virtual std::string GetType() override
Definition: FileInput.h:91
If we are in emscripten, make sure to include the header.
Definition: array.h:37
FileInput(FileInputInfo *in_info)
Definition: FileInput.h:99
virtual void GetHTML(std::stringstream &HTML) override
Definition: FileInput.h:62
Widget is effectively a smart pointer to a WidgetInfo object, plus some basic accessors.
Definition: Widget.h:78
void UpdateCallback(const std::function< void(const std::string &)> &in_cb)
Definition: FileInput.h:73
FileInput & Autofocus(bool in_af)
Set this FileInput object to have autofocus (or not)
Definition: FileInput.h:141
std::string TypeName() const override
Debugging helpers...
Definition: FileInput.h:55
const std::string & GetTitle() const
Get the current tooltip.
Definition: FileInput.h:147
FileInputInfo & operator=(const FileInputInfo &)=delete
virtual ~FileInput()
Definition: FileInput.h:127
void UpdateDependants()
Definition: Widget.h:274
WidgetFacet(const std::string &in_id="")
WidgetFacet cannot be built unless within derived class, so constructors are protected.
Definition: Widget.h:546
bool disabled
Definition: FileInput.h:43