-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebElementSignals.hpp
331 lines (282 loc) · 12.1 KB
/
WebElementSignals.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
* @file WebElementSignals.hpp
* @author Greg Propf ([email protected])
* @brief The signal classes that have an HTML element (or JS event listener at least) at their
* core.
* @version 0.1
* @date 2023-03-01
*
* @copyright Copyright (c) 2023
*
*/
#ifndef WebElement_hpp
#define WebElement_hpp
#include <emscripten.h>
#include <emscripten/bind.h>
#include <emscripten/val.h>
#include <stdio.h>
#include <unistd.h>
#include <functional>
#include <iostream>
#include <map>
#include <sstream>
#include "SignalPrimitives.hpp"
#include "WebElements.hpp"
// #include "SignalBuilder.hpp"
using emscripten::val;
using std::cout;
using std::endl;
namespace cl2 {
/**
* @brief A signal that originates or ends in a web element. This type may also act as a conduit or
* passthru due to inheriting both emitter and acceptor capabilities.
*
* @tparam S
*/
template <typename S>
class WebElementSignal : public SignalAcceptor<S>, public SignalEmitter<S> {
shared_ptr<WebElement> wptr_; //!< The actual WebElement this acts as a signal wrapper for.
std::string boundField_; //!< The field in the domElement that stores whatever signal data we
//!< are interested in.
std::string eventListenerName_ = "";
val eventListenerFn_ = val::null();
public:
virtual ~WebElementSignal() {
wptr_.reset();
}
virtual void emit(const S& s) { SignalEmitter<S>::emit(s); }
shared_ptr<WebElement> getWebElement() { return wptr_; }
WebElementSignal(const WebElement& wptr, const std::string& boundField,
bool emitInitialValue = true) {
boundField_ = boundField;
this->emitInitialValue_ = emitInitialValue;
wptr_ = make_shared<WebElement>(wptr);
}
/**
* @brief Everything here seems to concern setting up the output so if there isn't one we just
* return.
*
*/
virtual void update() {
if (this->output_ == nullptr) return;
val elgEmitFn = val::global("elgEmitFn");
if (wptr_->domElement_["type"] == val("range")) {
eventListenerName_ = "input";
} else if (wptr_->domElement_["type"] == val("submit")) {
elgEmitFn = val::global("elgButtonFn");
eventListenerName_ = "click";
} else {
eventListenerName_ = "change";
}
wptr_->domElement_.call<void>("removeEventListener", val(eventListenerName_),
eventListenerFn_);
eventListenerFn_ = elgEmitFn(*this);
wptr_->domElement_.call<void>("addEventListener", val(eventListenerName_),
eventListenerFn_);
if (!this->emitInitialValue_) return;
cout << "Getting initial value for id = " << wptr_->getId().as<std::string>() << endl;
// const S initialVal = wptr_->domElement_.call<val>("getAttribute",
// val(boundField_)).as<S>();
const S initialVal = wptr_->domElement_[boundField_].as<S>();
// cout << "Initial value for id = " << wptr_->getId().as<std::string>() << " is '"
// << initialVal << "'" << endl;
this->emit(initialVal);
}
/**
* @brief It seems to be necessary to call `setAttribute()` for some things while using the
* Emscripten `set()` method is needed for others, notably setting the 'value' property of input
* fields. I'm not sure if 'value' is the only such exception and calling both methods doesn't
* seem to hurt anything but this is clearly a kludge.
*
* Setting the bound field does not seem to trigger the event listener that in turn calls the
* emit() method, thus we force it here so that a signal is passed on through to the next
* link in the chain when a signal comes in.
*
* @param s
*/
virtual bool accept(const S& s) {
SignalAcceptor<S>::accept(s);
// bool storedSignalAccepts = StoredSignal<S>::accept(s);
// if (!storedSignalAccepts) return false;
wptr_->domElement_.set(boundField_, val(s));
wptr_->domElement_.call<void>("setAttribute", val(boundField_), val(s));
this->emit(s);
return true;
}
};
// class SignalBuilder;
template <typename S, typename ObjT>
class ObjectSignalLoop {
shared_ptr<ObjT> object_;
// shared_ptr<SignalBuilder> signalBuilder_;
// S (ObjT::*signalEmitterMethod_)();
// void (ObjT::*signalAcceptorMethod_)(const S& s);
shared_ptr<ObjectEmitter<S, ObjT>> objectEmitter_;
shared_ptr<ObjectAcceptor<S, ObjT>> objectAcceptor_;
shared_ptr<WebElementSignal<S>> webElementSignal_;
public:
inline shared_ptr<ObjectEmitter<S, ObjT>> getObjectEmitter() { return objectEmitter_; }
inline shared_ptr<ObjectAcceptor<S, ObjT>> getObjectAcceptor() { return objectAcceptor_; }
inline shared_ptr<WebElementSignal<S>> getWebElementSignal() { return webElementSignal_; }
ObjectSignalLoop(shared_ptr<ObjT> object, // shared_ptr<cl2::SignalBuilder> signalBuilder,
// shared_ptr<ObjectEmitter<S, ObjT>> objectEmitter,
shared_ptr<WebElementSignal<S>> webElementSignal,
void (ObjT::*signalAcceptorMethod)(const S& s),
S (ObjT::*signalEmitterMethod)())
: object_(object),
// signalBuilder_(signalBuilder),
// objectAcceptor_(objectAcceptor),
// objectEmitter_(objectEmitter),
webElementSignal_(webElementSignal) {
objectAcceptor_ = make_shared<ObjectAcceptor<S, ObjT>>(object_);
objectEmitter_ = make_shared<ObjectEmitter<S, ObjT>>(object_);
objectAcceptor_->setSignalAcceptorMethod(signalAcceptorMethod);
objectEmitter_->setSignalEmitterMethod(signalEmitterMethod);
// signalBuilder_->connect<std::string>(webElementSignal_, objectAcceptor_);
}
// void setSignalAcceptorMethod(void (ObjT::*signalAcceptorMethod)(const S& s)) {
// // signalAcceptorMethod_ = signalAcceptorMethod;
// objectAcceptor_->setSignalAcceptorMethod(signalAcceptorMethod);
// }
// void setSignalEmitterMethod(S (ObjT::*signalEmitterMethod)()) {
// // signalEmitterMethod_ = signalEmitterMethod;
// objectEmitter_->setSignalEmitterMethod(signalEmitterMethod);
// }
};
/**
* @brief A signal originating in an event listener.
*
* @tparam S data type of signal, e.g. string, int, double, etc...
*/
template <typename S>
class EventListenerEmitter : public SignalEmitter<S> {
// shared_ptr<val> eventListener_ = nullptr;
protected:
val domElement_, eventListenerFn_;
std::string eventListenerName_;
std::string eventListenerGeneratorName_;
public:
// shared_ptr<Signal<S>> getOutput() const { return output_; }
EventListenerEmitter(val domElement, const std::string& eventListenerName,
const std::string& eventListenerGeneratorName,
bool emitInitialValue = false)
: SignalEmitter<S>(emitInitialValue)
// domElement_(domElement),
// eventListenerName_(eventListenerName),
// eventListenerGeneratorName_(eventListenerGeneratorName)
// SignalEmitter<S>::SignalEmitter(emitInitialValue);
{
domElement_ = domElement;
eventListenerName_ = eventListenerName;
eventListenerGeneratorName_ = eventListenerGeneratorName;
}
virtual void update() {
val elgEmitFn = val::global(this->eventListenerGeneratorName_.c_str());
eventListenerFn_ = elgEmitFn(*this);
domElement_.call<void>("removeEventListener", val(eventListenerName_), eventListenerFn_);
domElement_.call<void>("addEventListener", val(eventListenerName_), eventListenerFn_);
}
virtual void emit(const S& s) {
cout << "EventListenerEmitter::emit called!" << endl;
SignalEmitter<S>::emit(s);
}
virtual ~EventListenerEmitter() {
// cout << "Destroying EventListenerEmitter\n";
}
};
// /**
// * @brief A signal originating in an event listener.
// *
// * @tparam S data type of signal, e.g. string, int, double, etc...
// */
// template <typename S>
// class SelectEmitter : public EventListenerEmitter<S> {
// // shared_ptr<val> eventListener_ = nullptr;
// public:
// // shared_ptr<Signal<S>> getOutput() const { return output_; }
// SelectEmitter(val domElement)
// : EventListenerEmitter<S>(domElement, "change", "elgSelectEmitFn") {
// // this->domElement_ = domElement;
// // this->eventListenerName_ = "change";
// // this->eventListenerGeneratorName_ = "elgSelectEmitFn";
// }
// virtual void update() { EventListenerEmitter<S>::update(); }
// virtual void emit(const S& s) {
// cout << "SelectEmitter::emit called!" << endl;
// EventListenerEmitter<S>::emit(s);
// }
// virtual ~SelectEmitter() {
// // cout << "Destroying SignalObject\n";
// }
// };
/**
* @brief This class is a bit special because it's essentially fixed in its template parameter. It
* will emit a pair of doubles that represents the mouse position during a mouse event. The specific
* event can be different. It may be a click, mouseover, mousedown, etc... but the tparam is going
* to reflect the location where this happened. This is also why we can include the
* EMSCRIPTEN_BINDINGS block. The specifics of the bindings are not contingent on any particular use
* of the class.
*
* @tparam S
*/
template <typename S>
class MouseSignal : public SignalEmitter<S> {
shared_ptr<WebElement> wptr_; //!< The actual WebElement this acts as a signal wrapper for.
std::string eventListenerName_ = ""; //!< i.e. 'click', 'mousedown', 'mouseover', etc...
val eventListenerFn_ = val::null();
// bool coordsUseViewBox_ = false;
public:
virtual void emit(const S& s) { SignalEmitter<S>::emit(s); }
MouseSignal(const WebElement& wptr, const std::string& eventListenerName,
bool coordsUseViewBox = false, bool emitInitialValue = true)
: eventListenerName_(eventListenerName) {
this->emitInitialValue_ = emitInitialValue;
wptr_ = make_shared<WebElement>(wptr);
}
/**
* @brief I created this so I can call it from JS to make the needed type for the emit method.
* There's probably a better way to do this but I just needed a quick way to make sure that I
* was sending out a C++ pair of doubles.
*
* @param x
* @param y
* @return std::pair<double, double>
*/
static std::pair<double, double> packageCoordPair(double x, double y) {
return std::pair(x, y);
}
/**
* @brief Everything here seems to concern setting up the output so if there isn't one we just
* return.
*
*/
virtual void update() {
if (this->output_ == nullptr) return;
val elgEmitFn = val::global("elgMouseSignal");
wptr_->domElement_.call<void>("removeEventListener", val(eventListenerName_),
eventListenerFn_);
eventListenerFn_ = elgEmitFn(*this, wptr_->domElement_);
wptr_->domElement_.call<void>("addEventListener", val(eventListenerName_),
eventListenerFn_);
// if (!this->emitInitialValue_) return;
// cout << "Getting initial value for id = " << wptr_->getId().as<std::string>() << endl;
// // const S initialVal = wptr_->domElement_.call<val>("getAttribute",
// // val(boundField_)).as<S>();
// const S initialVal = wptr_->domElement_[boundField_].as<S>();
// cout << "Initial value for id = " << wptr_->getId().as<std::string>() << " is '"
// << initialVal << "'" << endl;
// this->emit(initialVal);
}
virtual ~MouseSignal() {}
};
EMSCRIPTEN_BINDINGS(MouseSignal) {
emscripten::class_<std::pair<double, double>>("pair");
emscripten::class_<MouseSignal<std::pair<double, double>>>("MouseSignal")
.function("emit", &MouseSignal<std::pair<double, double>>::emit,
emscripten::allow_raw_pointers())
.class_function("packageCoordPair",
&MouseSignal<std::pair<double, double>>::packageCoordPair,
emscripten::allow_raw_pointers());
}
} // namespace cl2
#endif