-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignalBuilder.hpp
409 lines (371 loc) · 13.9 KB
/
SignalBuilder.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/**
* @file SignalBuilder.hpp
* @author Greg Propf ([email protected])
* @brief This is the factory class that creates and then connects the signals.
* @version 0.1
* @date 2023-03-05
*
* @copyright Copyright (c) 2023
*
*/
#ifndef SignalBuilder_hpp
#define SignalBuilder_hpp
#include "SignalPrimitives.hpp"
#include "Util.hpp"
#include "WebElementSignals.hpp"
#include "WebElements.hpp"
namespace cl2 {
/**
* @brief Factory class that creates WebElements and signals. Also connects signals to one another.
*
*/
class SignalBuilder {
TicketMachine tm_;
bool labelAllInputs_ = true;
bool labelsSwallowTheirReferents_ = true;
bool addBRAfterAllCalls_ =
true; //!< add a '<BR>' tag after each call so everything goes on its own line.
val parentDOMElement_ = val::null();
std::map<const std::string, const WebElement*>
elementMap_; //!< a map of elements we've created so we can re-use them.
std::map<std::string, val> attrs_;
bool attrsResetAfterSingleUse_ = true;
public:
SignalBuilder(int startId = 0, bool labelAllInputs = true,
bool labelsSwallowTheirReferents = true, val parentDOMElement = val::null())
: labelAllInputs_(labelAllInputs),
labelsSwallowTheirReferents_(labelsSwallowTheirReferents),
parentDOMElement_(parentDOMElement) {
tm_ = TicketMachine(startId);
}
void setDomElement(val domElement) { this->parentDOMElement_ = domElement; }
val getDomElement() { return this->parentDOMElement_; }
/**
* @brief Do this in any method that creates a WebElement.
*
*/
void postCall(const WebElement& wel) {
wel.setAttributes(attrs_);
if (attrsResetAfterSingleUse_) attrs_ = {};
if (addBRAfterAllCalls_) BR(this->parentDOMElement_);
}
/**
* @brief Add a WebElement to the global map.
*
* @param wel
* @param name
*/
void addElementToMap(const WebElement& wel, const std::string& name) {
elementMap_.insert({name, &wel});
}
/**
* @brief Get an element from the global map. Returns a null pointer if element is not found.
*
* @param name
* @return const WebElement*
*/
const WebElement* getElementByName(const std::string& name) {
if (elementMap_.find(name) != elementMap_.end()) {
return elementMap_[name];
}
return nullptr;
}
/**
* @brief Creates a `WebElementSignalObject` that represents the specified attribute of a
* `WebElement`. An example would be creating an attribute signal to represent the radius of an
* SVG circle object.
*
* @tparam S
* @param wel
* @param attributeName
* @param emitInitialValue
* @return shared_ptr<WebElementSignalObject<S>>
*/
// template <typename S>
// shared_ptr<WebElementSignalObject<S>> attributeSignal(const WebElement& wel,
// const std::string& attributeName,
// bool emitInitialValue = true) {
// shared_ptr<WebElementSignalObject<S>> aso =
// make_shared<cl2::WebElementSignalObject<S>>(wel, attributeName, emitInitialValue);
// return aso;
// }
/**
* @brief Creates a `WebElementSignalObject` that represents the specified attribute of a
* `WebElement` but gets the WE from the global map using the identifier string.
*
* @tparam S
* @param elementName
* @param attributeName
* @param emitInitialValue
* @return shared_ptr<WebElementSignalObject<S>>
*/
// template <typename S>
// shared_ptr<WebElementSignalObject<S>> attributeSignal(const std::string& elementName,
// const std::string& attributeName,
// bool emitInitialValue = true) {
// const WebElement* welptr = getElementByName(elementName);
// if (welptr != nullptr) {
// return attributeSignal<S>(*welptr, attributeName, emitInitialValue);
// }
// return nullptr;
// }
/**
* @brief Create future elements with the provided attributes.
*
* @param attrs
* @param attrsResetAfterSingleUse
* @return SignalBuilder
*/
SignalBuilder withAttributes(const std::map<std::string, val>& attrs,
bool attrsResetAfterSingleUse = true) const& {
SignalBuilder cpy(*this);
cpy.attrs_ = attrs;
cpy.attrsResetAfterSingleUse_ = attrsResetAfterSingleUse;
return cpy;
}
/**
* @brief Create future elements with the provided attributes. (r-value version)
*
* @param attrs
* @param attrsResetAfterSingleUse
* @return SignalBuilder
*/
SignalBuilder withAttributes(const std::map<std::string, val>& attrs,
bool attrsResetAfterSingleUse = true) && {
SignalBuilder cpy(std::move(*this));
cpy.attrs_ = attrs;
cpy.attrsResetAfterSingleUse_ = attrsResetAfterSingleUse;
return cpy;
}
/**
* @brief Create future elements as children of the provided DOM element.
*
* @param parentDOMElement
* @return SignalBuilder
*/
SignalBuilder withParentDOMElement(val parentDOMElement) const& {
SignalBuilder cpy(*this);
cpy.parentDOMElement_ = parentDOMElement;
return cpy;
}
/**
* @brief r-value version of above.
*
* @param parentDOMElement
* @return SignalBuilder
*/
SignalBuilder withParentDOMElement(val parentDOMElement) && {
SignalBuilder cpy(std::move(*this));
cpy.parentDOMElement_ = parentDOMElement;
return cpy;
}
/**
* @brief Does what `withParentDOMElement()` does except using a `WebElement` reference.
*
* @param parentElement
* @return SignalBuilder
*/
SignalBuilder withParentWebElement(const WebElement& parentElement) const& {
SignalBuilder cpy(*this);
cpy.parentDOMElement_ = parentElement.domElement_;
return cpy;
}
/**
* @brief r-value version of above.
*
* @param parentElement
* @return SignalBuilder
*/
SignalBuilder withParentWebElement(const WebElement& parentElement) && {
SignalBuilder cpy(std::move(*this));
cpy.parentDOMElement_ = parentElement.domElement_;
return cpy;
}
/**
* @brief Create a text input field.
*
* @param name
* @param labelText
* @return WebElement
*/
InputElement textInput(const std::string& name, const std::string& labelText) {
const InputElement inp =
InputElement("input", name, "text", TicketMachine::getNextStrSid(), parentDOMElement_);
const Label lbl = Label(labelText, inp, labelsSwallowTheirReferents_,
TicketMachine::getNextStrSid(), parentDOMElement_);
postCall(inp);
addElementToMap(inp, name);
return inp;
}
SVG svg(const std::string& name, int pixelWidth, int pixelHeight, const std::string& id = "",
val parentDOMElement = val::null()) {
SVG svg = SVG(name, pixelWidth, pixelHeight, id, parentDOMElement_);
return svg;
}
Div div(const std::string& name = "", const std::string& id = "") {
if (id == "") {
const Div div = Div(name, TicketMachine::getNextStrSid(), parentDOMElement_);
return div;
}
else {
const Div div = Div(name, id, parentDOMElement_);
return div;
}
}
WebElement textArea(const std::string& name, int rows, int cols, const std::string& labelText) {
const Textarea inp =
Textarea(name, rows, cols, TicketMachine::getNextStrSid(), parentDOMElement_);
const Label lbl = Label(labelText, inp, labelsSwallowTheirReferents_,
TicketMachine::getNextStrSid(), parentDOMElement_);
postCall(inp);
addElementToMap(inp, name);
return inp;
}
/**
* @brief Creates a text input signal emitter using an `EventListenerEmitter`
*
* @tparam S
* @param name
* @param labelText
* @param emitInitialValue
* @return shared_ptr<EventListenerEmitter<S>>
*/
template <typename S>
shared_ptr<EventListenerEmitter<S>> textInputELE(const std::string& name,
const std::string& labelText,
bool emitInitialValue = true) {
InputElement inp = textInput(name, labelText);
val inpDE = inp.getDomElement();
shared_ptr<EventListenerEmitter<S>> wso =
make_shared<EventListenerEmitter<std::string>>(inpDE, "change");
return wso;
}
/**
* @brief Creates a "two-sided" text field signal. It can be used as an emitter or acceptor.
*
* @tparam S
* @param name
* @param labelText
* @param emitInitialValue
* @return shared_ptr<WebElementSignal<S>>
*/
template <typename S>
shared_ptr<WebElementSignal<S>> textInputWSS(const std::string& name,
const std::string& labelText,
bool emitInitialValue = true) {
InputElement inp = textInput(name, labelText);
shared_ptr<WebElementSignal<S>> wso =
make_shared<WebElementSignal<S>>(inp, "value", emitInitialValue);
return wso;
}
/**
* @brief Make a range input
*
* @param name
* @param labelText
* @return InputElement
*/
InputElement rangeInput(const std::string& name, const std::string& labelText) {
const InputElement inp =
InputElement("input", name, "range", TicketMachine::getNextStrSid(), parentDOMElement_);
const Label lbl = Label(labelText, inp, labelsSwallowTheirReferents_,
TicketMachine::getNextStrSid(), parentDOMElement_);
postCall(inp);
return inp;
}
/**
* @brief Creates a "two-sided" range field signal. It can be used as an emitter or acceptor.
*
* @tparam S
* @param name
* @param labelText
* @param emitInitialValue
* @return shared_ptr<WebElementSignal<S>>
*/
template <typename S>
shared_ptr<WebElementSignal<S>> rangeInputWSS(const std::string& name,
const std::string& labelText,
bool emitInitialValue = true) {
InputElement inp = rangeInput(name, labelText);
shared_ptr<WebElementSignal<S>> wso =
make_shared<WebElementSignal<S>>(inp, "value", emitInitialValue);
return wso;
}
template <typename S>
shared_ptr<WebElementSignal<S>> textAreaWSS(const std::string& name, int rows, int cols,
const std::string& labelText,
bool emitInitialValue = true) {
WebElement inp = textArea(name, rows, cols, labelText);
shared_ptr<WebElementSignal<S>> wso =
make_shared<WebElementSignal<S>>(inp, "value", emitInitialValue);
return wso;
}
template <typename S>
shared_ptr<WebElementSignal<S>> selectBoxWSS(const std::string& name,
const std::string& labelText,
bool emitInitialValue = true) {
// InputElement inp = textInput(name, labelText);
auto selectElement = Select(name, getStrId());
postCall(selectElement);
shared_ptr<WebElementSignal<S>> wso =
make_shared<WebElementSignal<S>>(selectElement, "value", emitInitialValue);
return wso;
}
/**
* @brief Create a button with provided callback.
*
* @param displayedText
* @param onClickFn
* @return Button
*/
Button button(const std::string& displayedText, val onClickFn) {
Button btn = Button(displayedText, displayedText, onClickFn, TicketMachine::getNextStrSid(),
parentDOMElement_);
postCall(btn);
return btn;
}
template <typename S>
shared_ptr<WebElementSignal<S>> buttonWSS(const std::string& displayedText,
val onClickFn = val::null()) {
Button inp = button(displayedText, onClickFn);
shared_ptr<WebElementSignal<S>> wso = make_shared<WebElementSignal<S>>(inp, "name", false);
return wso;
}
/**
* @brief Plug an emitter signal into an acceptor.
*
* @tparam S
* @param s1
* @param s2
*/
template <typename S>
void connect(shared_ptr<SignalEmitter<S>> s1, shared_ptr<SignalAcceptor<S>> s2) const {
s1->setOutput(s2);
}
/**
* @brief Connect the two inputs to the Merge and then connect the Merge to the optional output.
*
* @tparam inT1
* @tparam inT2
* @tparam outT
* @param s1
* @param s2
* @param merge
* @param mergeOut
*/
template <typename inT1, typename inT2, typename outT>
void connect(shared_ptr<SignalEmitter<inT1>> s1, shared_ptr<SignalEmitter<inT2>> s2,
shared_ptr<Merge<inT1, inT2, outT>> merge,
shared_ptr<SignalAcceptor<outT>> mergeOut = nullptr) const {
s1->setOutput(merge->getInput1());
s2->setOutput(merge->getInput2());
if (mergeOut != nullptr) merge->setOutput(mergeOut);
}
template <typename S, typename ObjT>
void connectLoop(shared_ptr<ObjectSignalLoop<S, ObjT>> objectSignalLoop) const {
connect<S>(objectSignalLoop->getWebElementSignal(), objectSignalLoop->getObjectAcceptor());
connect<S>(objectSignalLoop->getObjectEmitter(), objectSignalLoop->getWebElementSignal());
}
};
} // namespace cl2
#endif