EmbAJAX
Simplistic framework for creating and handling displays and controls on a web page served by an embeddable device (Arduino or other microcontroller with Arduino support).
EmbAJAXScriptedSpan.h
1 /*
2  *
3  * EmbAJAX - Simplistic framework for creating and handling displays and controls on a WebPage served by an Arduino (or other small device).
4  *
5  * Copyright (C) 2018 Thomas Friedrichsmeier
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as published
9  * by the Free Software Foundation, either version 3 of the License, or (at
10  * your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20 */
21 
22 #ifndef EMBAJAXSCRIPTEDSPAN_H
23 #define EMBAJAXSCRIPTEDSPAN_H
24 
25 #include "EmbAJAX.h"
26 
44 public:
53  EmbAJAXScriptedSpan(const char* id, const char* script, char* rec_buffer = 0, size_t rec_buffer_size=0) : EmbAJAXElement(id) {
54  _value = EmbAJAXBase::null_string;
55  _script = script;
56  _rec_buffer = rec_buffer;
57  _rec_buffer_size = rec_buffer_size;
58  }
59  void print() const override {
60  _driver->printFormatted("<span id=", HTML_QUOTED_STRING(_id), "><script>{\n"
61  "let spn=document.getElementById(", JS_QUOTED_STRING(_id), ");\n"
62  "Object.defineProperty(spn, 'EmbAJAXValue', {\n"
63  " set: function(value) {\n"
64  " if (this.receiveValue) this.receiveValue(value);\n"
65  " }\n"
66  "})\n"
67  "spn.sendValue = function(value) {\n"
68  " doRequest(this.id, value);\n"
69  "}\n"
70  "spn.init=function() {\n",
71  PLAIN_STRING(_script),
72  "\n};\n"
73  "spn.init();\n"
74  "spn.EmbAJAXValue=", JS_QUOTED_STRING(_value), ";\n"
75  "}</script></span>\n");
76  }
77  const char* value(uint8_t which = EmbAJAXBase::Value) const override {
78  if (which == EmbAJAXBase::Value) return _value;
79  return EmbAJAXElement::value(which);
80  }
81  const char* valueProperty(uint8_t which = EmbAJAXBase::Value) const override {
82  if (which == EmbAJAXBase::Value) return "EmbAJAXValue";
83  return EmbAJAXElement::valueProperty(which);
84  }
100  void setValue(const char* value) {
101  // TODO: Check whether the value has actually changed
102  _value = value;
103  setChanged();
104  };
105 
106  void updateFromDriverArg(const char* argname) override {
107  _driver->getArg(argname, _rec_buffer, _rec_buffer_size);
108  _value = _rec_buffer;
109  }
110 private:
111  const char* _value;
112  const char* _script;
113  char* _rec_buffer;
114  size_t _rec_buffer_size;
115 };
116 
117 #endif
Abstract base class for modifiable elements.
Definition: EmbAJAX.h:296
virtual const char * value(uint8_t which=EmbAJAXBase::Value) const
Definition: EmbAJAX.h:309
virtual const char * valueProperty(uint8_t which=EmbAJAXBase::Value) const
Definition: EmbAJAX.h:324
A span element containing a custom javascript script, meant to creating custom dispays.
Definition: EmbAJAXScriptedSpan.h:43
void setValue(const char *value)
Definition: EmbAJAXScriptedSpan.h:100
void updateFromDriverArg(const char *argname) override
Definition: EmbAJAXScriptedSpan.h:106
const char * value(uint8_t which=EmbAJAXBase::Value) const override
Definition: EmbAJAXScriptedSpan.h:77
EmbAJAXScriptedSpan(const char *id, const char *script, char *rec_buffer=0, size_t rec_buffer_size=0)
Definition: EmbAJAXScriptedSpan.h:53
const char * valueProperty(uint8_t which=EmbAJAXBase::Value) const override
Definition: EmbAJAXScriptedSpan.h:81
#define PLAIN_STRING(X)
Definition: macro_definitions.h:54
#define HTML_QUOTED_STRING(X)
Definition: macro_definitions.h:49
#define JS_QUOTED_STRING(X)
Definition: macro_definitions.h:47