AutoConnectElementJson.h 16 KB
Newer Older
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
/**
 * Declaration of AutoConnectElement extended classes using JSON.
 * @file AutoConnectElementJson.h
 * @author hieromon@gmail.com
 * @version  1.2.0
 * @date 2020-11-11
 * @copyright  MIT license.
 */

#ifndef _AUTOCONNECTELEMENTJSON_H_
#define _AUTOCONNECTELEMENTJSON_H_

#include "AutoConnectElementBasis.h"
#include "AutoConnectJsonDefs.h"

#define AUTOCONNECT_JSON_KEY_ACTION       "action"
#define AUTOCONNECT_JSON_KEY_APPLY        "apply"
#define AUTOCONNECT_JSON_KEY_ARRANGE      "arrange"
#define AUTOCONNECT_JSON_KEY_AUTH         "auth"
#define AUTOCONNECT_JSON_KEY_CHECKED      "checked"
#define AUTOCONNECT_JSON_KEY_ELEMENT      "element"
#define AUTOCONNECT_JSON_KEY_FORMAT       "format"
#define AUTOCONNECT_JSON_KEY_GLOBAL       "global"
#define AUTOCONNECT_JSON_KEY_LABEL        "label"
#define AUTOCONNECT_JSON_KEY_LABELPOSITION "labelposition"
#define AUTOCONNECT_JSON_KEY_MENU         "menu"
#define AUTOCONNECT_JSON_KEY_NAME         "name"
#define AUTOCONNECT_JSON_KEY_OPTION       "option"
#define AUTOCONNECT_JSON_KEY_PATTERN      "pattern"
#define AUTOCONNECT_JSON_KEY_PLACEHOLDER  "placeholder"
#define AUTOCONNECT_JSON_KEY_POSTERIOR    "posterior"
#define AUTOCONNECT_JSON_KEY_SELECTED     "selected"
#define AUTOCONNECT_JSON_KEY_STORE        "store"
#define AUTOCONNECT_JSON_KEY_STYLE        "style"
#define AUTOCONNECT_JSON_KEY_TITLE        "title"
#define AUTOCONNECT_JSON_KEY_TYPE         "type"
#define AUTOCONNECT_JSON_KEY_URI          "uri"
#define AUTOCONNECT_JSON_KEY_VALUE        "value"
#define AUTOCONNECT_JSON_TYPE_ACBUTTON    "ACButton"
#define AUTOCONNECT_JSON_TYPE_ACCHECKBOX  "ACCheckBox"
#define AUTOCONNECT_JSON_TYPE_ACELEMENT   "ACElement"
#define AUTOCONNECT_JSON_TYPE_ACFILE      "ACFile"
#define AUTOCONNECT_JSON_TYPE_ACINPUT     "ACInput"
#define AUTOCONNECT_JSON_TYPE_ACRADIO     "ACRadio"
#define AUTOCONNECT_JSON_TYPE_ACSELECT    "ACSelect"
#define AUTOCONNECT_JSON_TYPE_ACSTYLE     "ACStyle"
#define AUTOCONNECT_JSON_TYPE_ACSUBMIT    "ACSubmit"
#define AUTOCONNECT_JSON_TYPE_ACTEXT      "ACText"
#define AUTOCONNECT_JSON_VALUE_BASIC      "basic"
#define AUTOCONNECT_JSON_VALUE_BEHIND     "behind"
#define AUTOCONNECT_JSON_VALUE_BR         "br"
#define AUTOCONNECT_JSON_VALUE_DIGEST     "digest"
#define AUTOCONNECT_JSON_VALUE_EXTERNAL   "extern"
#define AUTOCONNECT_JSON_VALUE_FS         "fs"
#define AUTOCONNECT_JSON_VALUE_HORIZONTAL "horizontal"
#define AUTOCONNECT_JSON_VALUE_INFRONT    "infront"
#define AUTOCONNECT_JSON_VALUE_NONE       "none"
#define AUTOCONNECT_JSON_VALUE_NUMBER     "number"
#define AUTOCONNECT_JSON_VALUE_PAR        "par"
#define AUTOCONNECT_JSON_VALUE_PASSWORD   "password"
#define AUTOCONNECT_JSON_VALUE_TEXT       "text"
#define AUTOCONNECT_JSON_VALUE_SD         "sd"
#define AUTOCONNECT_JSON_VALUE_VERTICAL   "vertical"

/**
 * AutoConnectAux element base with handling with JSON object.
 * Placed a raw text that can be added by user sketch.
 * @param  name     A name string for the element.
 * @param  value    A raw text to be placed in HTML.
 */
class AutoConnectElementJson : virtual public AutoConnectElementBasis {
 public:
  explicit AutoConnectElementJson(const char* name = "", const char* value = "", const ACPosterior_t post = AC_Tag_None) : _defaultPost(AC_Tag_None) {
    AutoConnectElementBasis::name = String(name);
    AutoConnectElementBasis::value = String(value);
    AutoConnectElementBasis::post = post;
  }
  ~AutoConnectElementJson() {}
  virtual size_t  getObjectSize(void) const;
  virtual bool  loadMember(const JsonObject& json);
  virtual void  serialize(JsonObject& json);
  template<typename T>
  T&  as(void);

 protected:
  void  _setMember(const JsonObject& json);
  void  _serialize(JsonObject& json);

 protected:
  ACPosterior_t   _defaultPost;
};

/**
 * Button arrangement class, a part of AutoConnectAux element with
 * handling JSON object.
 * Place a labeled button that can be added by user sketch.
 * @param  name     Button element name string.
 * @param  value    Value string with the placed button.
 * @param  action   Script code to execute with the button pushed.
 */
class AutoConnectButtonJson : public AutoConnectElementJson, public AutoConnectButtonBasis {
 public:
  explicit AutoConnectButtonJson(const char* name = "", const char* value = "", const String& action = String(""), const ACPosterior_t post = AC_Tag_None) {
    AutoConnectButtonBasis::name = String(name);
    AutoConnectButtonBasis::value = String(value);
    AutoConnectButtonBasis::action = String(action);
    AutoConnectButtonBasis::post = post;
    _defaultPost = AC_Tag_None;
  }
  ~AutoConnectButtonJson() {}
  size_t  getObjectSize(void) const override;
  bool  loadMember(const JsonObject& json) override;
  void  serialize(JsonObject& json) override;
};

/**
 * Checkbox arrangement class, a part of AutoConnectAux element with
 * handling JSON object.
 * Place a optionally labeled input-box that can be added by user sketch.
 * @param  name     Checkbox name string.
 * @param  value    A string value associated with the input.
 * @param  label    A label string that follows checkbox, optionally.
 * The label is placed on the right side of the checkbox.
 */
class AutoConnectCheckboxJson : public AutoConnectElementJson, public AutoConnectCheckboxBasis {
 public:
  explicit AutoConnectCheckboxJson(const char* name = "", const char* value = "", const char* label = "", const bool checked = false, const ACPosition_t labelPosition = AC_Behind, const ACPosterior_t post = AC_Tag_BR) {
    AutoConnectCheckboxBasis::name = String(name);
    AutoConnectCheckboxBasis::value = String(value);
    AutoConnectCheckboxBasis::label = String(label);
    AutoConnectCheckboxBasis::checked = checked;
    AutoConnectCheckboxBasis::labelPosition = labelPosition;
    AutoConnectCheckboxBasis::post = post;
    _defaultPost = AC_Tag_BR;
  }
  ~AutoConnectCheckboxJson() {}
  size_t  getObjectSize(void) const override;
  bool  loadMember(const JsonObject& json) override;
  void  serialize(JsonObject& json) override;
};

/**
 * File-select input arrangement class, a part of AutoConnectAux element.
 * Place a optionally labeled file-select input box that can be added by user sketch.
 * @param  name     File-select input box name string.
 * @param  value    A string value entered by the selected file name.
 * @param  label    A label string that follows file-select box, optionally.
 * The label is placed in front of file-select box.
 * @param  store    An enumeration value of store type.
 */
class AutoConnectFileJson : public AutoConnectElementJson, public AutoConnectFileBasis {
 public:
  explicit AutoConnectFileJson(const char* name = "", const char* value= "", const char* label = "", const ACFile_t store = AC_File_FS, const ACPosterior_t post = AC_Tag_BR) {
    AutoConnectFileBasis::name = String(name);
    AutoConnectFileBasis::value = String(value);
    AutoConnectFileBasis::label = String(label);
    AutoConnectFileBasis::store = store;
    AutoConnectFileBasis::post = post;
    _defaultPost = AC_Tag_BR;
  }
  ~AutoConnectFileJson() {}
  size_t  getObjectSize(void) const override;
  bool  loadMember(const JsonObject& json) override;
  void  serialize(JsonObject& json) override;
};

/**
 * Input-box arrangement class, a part of AutoConnectAux element with
 * handling JSON object.
 * Place a optionally labeled input-box that can be added by user sketch.
 * @param  name     Input-box name string.
 * @param  value    Default value string. This string display as a placeholder by the default.
 * @param  label    A label string that follows Input-box, optionally.
 * The label is placed in front of Input-box.
 */
class AutoConnectInputJson : public AutoConnectElementJson, public AutoConnectInputBasis {
 public:
  explicit AutoConnectInputJson(const char* name = "", const char* value = "", const char* label = "", const char* pattern = "", const char* placeholder = "", const ACPosterior_t post = AC_Tag_BR, const ACInput_t apply = AC_Input_Text) {
    AutoConnectInputBasis::name = String(name);
    AutoConnectInputBasis::value = String(value);
    AutoConnectInputBasis::label = String(label);
    AutoConnectInputBasis::pattern = String(pattern);
    AutoConnectInputBasis::placeholder = String(placeholder);
    AutoConnectInputBasis::apply = apply;
    AutoConnectInputBasis::post = post;
    _defaultPost = AC_Tag_BR;
  }
  ~AutoConnectInputJson() {}
  size_t  getObjectSize(void) const override;
  bool  loadMember(const JsonObject& json) override;
  void  serialize(JsonObject& json) override;
};

/**
* Radio-button arrangement class, a part of AutoConnectAux element.
* Place a group of radio-button items and selectable mark checked.
* @param  name     Radio-button name string.
* @param  options  Array of value collection.
* @param  label    A label string that follows radio-buttons group.
* @param  checked  Index of check marked item.
*/
class AutoConnectRadioJson : public AutoConnectElementJson, public AutoConnectRadioBasis {
 public:
  explicit AutoConnectRadioJson(const char* name = "", std::vector<String> const& values = {}, const char* label = "", const ACArrange_t order = AC_Vertical, const uint8_t checked = 0, const ACPosterior_t post = AC_Tag_BR) {
    AutoConnectRadioBasis::name = String(name);
    AutoConnectRadioBasis::_values = values;
    AutoConnectRadioBasis::label = String(label);
    AutoConnectRadioBasis::order = order;
    AutoConnectRadioBasis::checked = checked;
    AutoConnectRadioBasis::post = post;
    _defaultPost = AC_Tag_BR;
  }
  ~AutoConnectRadioJson() {}
  size_t  getObjectSize(void) const override;
  bool  loadMember(const JsonObject& json) override;
  void  serialize(JsonObject& json) override;
};

/**
 * Selection-box arrangement class, A part of AutoConnectAux element.
 * Place a optionally labeled Selection-box that can be added by user sketch.
 * @param  name     Input-box name string.
 * @param  options  String array display in a selection list.
 * @param  label    A label string that follows Input-box, optionally.
 * The label is placed in front of Input-box.
 */
class AutoConnectSelectJson : public AutoConnectElementJson, public AutoConnectSelectBasis {
 public:
  explicit AutoConnectSelectJson(const char* name = "", std::vector<String> const& options = {}, const char* label = "", const uint8_t selected = 0, const ACPosterior_t post = AC_Tag_BR) {
    AutoConnectSelectBasis::name = String(name);
    AutoConnectSelectBasis::_options = options;
    AutoConnectSelectBasis::label = String(label);
    AutoConnectSelectBasis::selected = selected;
    AutoConnectSelectBasis::post = post;
    _defaultPost = AC_Tag_BR;
  }
  ~AutoConnectSelectJson() {}
  size_t  getObjectSize(void) const override;
  bool  loadMember(const JsonObject& json) override;
  void  serialize(JsonObject& json) override;
};

/**
 * CSS style arrangement class, a part of AutoConnectAux element.
 * This element assumes CSS that came into effect as a style code will
 * assign. Therefore, it does not check whether the CSS error exists in
 * the value set in AutoConnectStyle. Also, because AutoConnect inserts
 * its style code at the end of the style block on the AutoConnectAux
 * page, it may affect the AutoConnect web page elements.
 * @param  name  A style name string.
 * @param  value CSS style code.
 */
class AutoConnectStyleJson : public AutoConnectElementJson, public AutoConnectStyleBasis {
 public:
  explicit AutoConnectStyleJson(const char* name = "", const char* value = "") {
    AutoConnectStyleBasis::name = String(name);
    AutoConnectStyleBasis::value = String(value);
    AutoConnectStyleBasis::post = AC_Tag_None;
  }
  ~AutoConnectStyleJson() {}
  bool  loadMember(const JsonObject& json) override;
  void  serialize(JsonObject& json) override;
};

/**
 * Submit button arrangement class, a part of AutoConnectAux element.
 * Place a submit button with a label that can be added by user sketch.
 * With the button behavior, the values of the elements contained in
 * the form would be sent using the post method.
 * @param  name  Button name string.
 * @param  value Sending value string.
 * @param  uri   Sending uri string.
 */
class AutoConnectSubmitJson : public AutoConnectElementJson, public AutoConnectSubmitBasis {
 public:
  explicit AutoConnectSubmitJson(const char* name = "", const char* value = "", const char* uri = "", const ACPosterior_t post = AC_Tag_None) {
    AutoConnectSubmitBasis::name = String(name);
    AutoConnectSubmitBasis::value = String(value);
    AutoConnectSubmitBasis::uri = String(uri);
    AutoConnectSubmitBasis::post = post;
    _defaultPost = AC_Tag_None;
  }
  ~AutoConnectSubmitJson() {}
  size_t  getObjectSize(void) const override;
  bool  loadMember(const JsonObject& json) override;
  void  serialize(JsonObject& json) override;
};

/**
 * Text arrangement class, a part of AutoConnectAux element.
 * @param
 * @param  name     Text name string.
 * @param  value    Text value string.
 * @param  style    A string of style-code for decoration, optionally.
 * An arrangement text would be placed with <div> contains. A string
 * of style-codes are given for '<div style=>'.
 */
class AutoConnectTextJson : public AutoConnectElementJson, public AutoConnectTextBasis {
 public:
  explicit AutoConnectTextJson(const char* name = "", const char* value = "", const char* style = "", const char* format = "", const ACPosterior_t post = AC_Tag_None) {
    AutoConnectTextBasis::name = String(name);
    AutoConnectTextBasis::value = String(value);
    AutoConnectTextBasis::style = String(style);
    AutoConnectTextBasis::format = String(format);
    AutoConnectTextBasis::post = post;
    _defaultPost = AC_Tag_None;
  }
  ~AutoConnectTextJson() {}
  size_t  getObjectSize(void) const override;
  bool  loadMember(const JsonObject& json) override;
  void  serialize(JsonObject& json) override;
};

/**
 * Casts only a class derived from the AutoConnectElement class to the
 * actual element class.
 */
template<>
inline AutoConnectButtonJson& AutoConnectElementJson::as<AutoConnectButtonJson>(void) {
  if (typeOf() != AC_Button) {
    AC_DBG("%s mismatched type as <%d>\n", name.c_str(), (int)typeOf());
  }
  return *(reinterpret_cast<AutoConnectButtonJson*>(this));
}

template<>
inline AutoConnectCheckboxJson& AutoConnectElementJson::as<AutoConnectCheckboxJson>(void) {
  if (typeOf() != AC_Checkbox) {
    AC_DBG("%s mismatched type as <%d>\n", name.c_str(), (int)typeOf());
  }
  return *(reinterpret_cast<AutoConnectCheckboxJson*>(this));
}

template<>
inline AutoConnectFileJson& AutoConnectElementJson::as<AutoConnectFileJson>(void) {
  if (typeOf() != AC_File) {
    AC_DBG("%s mismatched type as <%d>\n", name.c_str(), (int)typeOf());
  }
  return *(reinterpret_cast<AutoConnectFileJson*>(this));
}

template<>
inline AutoConnectInputJson& AutoConnectElementJson::as<AutoConnectInputJson>(void) {
  if (typeOf() != AC_Input) {
    AC_DBG("%s mismatched type as <%d>\n", name.c_str(), (int)typeOf());
  }
  return *(reinterpret_cast<AutoConnectInputJson*>(this));
}

template<>
inline AutoConnectRadioJson& AutoConnectElementJson::as<AutoConnectRadioJson>(void) {
  if (typeOf() != AC_Radio) {
    AC_DBG("%s mismatched type as <%d>\n", name.c_str(), (int)typeOf());
  }
  return *(reinterpret_cast<AutoConnectRadioJson*>(this));
}

template<>
inline AutoConnectSelectJson& AutoConnectElementJson::as<AutoConnectSelectJson>(void) {
  if (typeOf() != AC_Select) {
    AC_DBG("%s mismatched type as <%d>\n", name.c_str(), (int)typeOf());
  }
  return *(reinterpret_cast<AutoConnectSelectJson*>(this));
}

template<>
inline AutoConnectStyleJson& AutoConnectElementJson::as<AutoConnectStyleJson>(void) {
  if (typeOf() != AC_Style) {
    AC_DBG("%s mismatched type as <%d>\n", name.c_str(), (int)typeOf());
  }
  return *(reinterpret_cast<AutoConnectStyleJson*>(this));
}

template<>
inline AutoConnectSubmitJson& AutoConnectElementJson::as<AutoConnectSubmitJson>(void) {
  if (typeOf() != AC_Submit) {
    AC_DBG("%s mismatched type as <%d>\n", name.c_str(), (int)typeOf());
  }
  return *(reinterpret_cast<AutoConnectSubmitJson*>(this));
}

template<>
inline AutoConnectTextJson& AutoConnectElementJson::as<AutoConnectTextJson>(void) {
  if (typeOf() != AC_Text) {
    AC_DBG("%s mismatched type as <%d>\n", name.c_str(), (int)typeOf());
  }
  return *(reinterpret_cast<AutoConnectTextJson*>(this));
}

#endif // _AUTOCONNECTELEMENTJSON_H_