Skip to content

Commit

Permalink
addCSS() method.
Browse files Browse the repository at this point in the history
removeWhiteSpaces() private method for store a cleaner code in JSON
  • Loading branch information
cotestatnt committed Feb 20, 2023
1 parent f46e38f commit 0c09881
Show file tree
Hide file tree
Showing 12 changed files with 690 additions and 589 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ In the image below, for example, the HTML and Javascript code to provision the d
![image](https://user-images.githubusercontent.com/27758688/218733394-9cd7af3e-257e-4798-98b0-b8d426e07848.png)



92 changes: 92 additions & 0 deletions examples/customHTML/customElements.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

/*
* This HTML code will be injected in /setup webpage using a <div></div> element as parent
* The parent element will hhve the HTML id properties equal to 'raw-html-<id>'
* where the id value will be equal to the id parameter passed to the function addHTML(html_code, id).
*/
static const char custom_html[] PROGMEM = R"EOF(
<form class=form>
<div class=tf-wrapper>
<label for=httpmethod class=input-label>Method</label>
<select class="select" id="httpmethod">
<option>GET</option>
<option>POST</option>
</select>
<label for=url class=input-label>Endpoint</label>
<input type=text placeholder="https://httpbin.org/" id=url value="https://httpbin.org/" />
</div>
<br>
<a id=fetch class="btn">
<span>Fecth url</span>
</a>
<br>
<pre id=payload></pre>
</form>
)EOF";
/*
* In this example, a style sections is added in order to render properly the new
* <select> and <pre> elements introduced. Since this section will be added at the end of the body,
* it is also possible to override the style of the elements already present:
* for example the background color of body will be ovverrided with a different color
*/
static const char custom_css[] PROGMEM = R"EOF(
pre {
font-family: Monaco,Menlo,Consolas,"Courier New",monospace;
color: #333;
line-height: 20px;
background-color: #f5f5f5;
border: 1px solid rgba(0,0,0,0.15);
border-radius: 6px;
overflow-y: scroll;
min-height: 350px;
font-size: 85%;
}
.select {
width: 25%;
height:40px;
padding-top: 10px;
padding-left: 20px;
border:1px solid #ccc;
border-radius: 6px;
box-shadow: 0 1px 2px 0 rgba(220, 220, 230, 0.5);
}
.body {
background-color : cadetblue;
}
)EOF";
/*
* Also the JavaScript will be added at the bottom of body
* In this example a simple 'click' event listener will be added for the button
* with id='fetch' (added as HTML). The listener will execute the function 'fetchEndpoint'
* in order to fetch a remote resource and show the response in a text box.
*
* The instruction $('<id-name>') is a "Jquery like" selector already defined
* so you can use for your purposes:
* var $ = function(el) {
* return document.getElementById(el);
* };
*/
static const char custom_script[] PROGMEM = R"EOF(
function fetchEndpoint() {
var mt = $('httpmethod').options[$('httpmethod').selectedIndex].text;
var url = $('url').value + mt.toLowerCase();
var bd = (mt != 'GET') ? 'body: ""' : '';

var options = {
method: mt,
bd
};

fetch(url, options)
.then(response => response.text())
.then(txt => {
$('payload').innerHTML = txt;
});
}

$('fetch').addEventListener('click', fetchEndpoint);
)EOF";
89 changes: 25 additions & 64 deletions examples/customHTML/customHTML.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,70 +35,14 @@ WebServer server(80);
FSWebServer myWebServer(FILESYSTEM, server);


static const char custom_html[] PROGMEM = R"EOF(
<style>
pre {
font-family: Monaco,Menlo,Consolas,"Courier New",monospace;
color: #333;
line-height: 20px;
background-color: #f5f5f5;
border: 1px solid rgba(0,0,0,0.15);
border-radius: 6px;
overflow-y: scroll;
min-height: 350px;
font-size: 85%;
}
.select {
width: 15%;
height:40px;
padding-top: 10px;
padding-left: 20px;
border:1px solid #ccc;
border-radius: 6px;
box-shadow: 0 1px 2px 0 rgba(220, 220, 230, 0.5);
}
</style>
<form class=form>
<div class=tf-wrapper>
<label for=httpmethod class=input-label>Method</label>
<select class="select" id="httpmethod">
<option>GET</option>
<option>POST</option>
</select>
<label for=url class=input-label>Endpoint</label>
<input type=text placeholder="https://httpbin.org/" id=url value="https://httpbin.org/" />
</div>
<br>
<a id=fetch class="btn">
<span>Fecth url</span>
</a>
<br>
<pre id=payload></pre>
</form>
)EOF";

static const char custom_script[] PROGMEM = R"EOF(
function fetchEndpoint() {
var mt = $('httpmethod').options[$('httpmethod').selectedIndex].text
var url = $('url').value + mt.toLowerCase();
var bd = (mt != 'GET') ? 'body: ""' : '';
var options = {
method: mt,
bd
};
fetch(url, options)
.then(response => response.text())
.then(txt => {
$('payload').innerHTML = txt;
});
}
$('fetch').addEventListener('click', fetchEndpoint);
)EOF";

/*
* Include the custom HTML, CSS and Javascript to be injected in /setup webpage.
* HTML code will be injected according to the order of options declaration.
* CSS and JavaScript will be appended to the end of body in order to work properly.
* In this manner, is also possible override the default element styles
* like for example background color, margins, paddings etc etc
*/
#include "customElements.h"

//////////////////////////////// Filesystem /////////////////////////////////////////
void startFilesystem() {
Expand Down Expand Up @@ -144,6 +88,22 @@ bool loadOptions() {
return false;
}

/* Call this if you need to save parameters from the sketch side
bool saveOptions() {
if (FILESYSTEM.exists("/config.json")) {
myWebServer.saveOptionValue(LED_LABEL, ledPin);
myWebServer.saveOptionValue(BOOL_LABEL, boolVar);
myWebServer.saveOptionValue(LONG_LABEL, longVar);
myWebServer.saveOptionValue(FLOAT_LABEL, floatVar);
myWebServer.saveOptionValue(STRING_LABEL, stringVar);
return true;
}
else
Serial.println(F("File \"config.json\" not exist"));
return false;
}
*/


void setup() {
Serial.begin(115200);
Expand All @@ -169,6 +129,7 @@ void setup() {
myWebServer.addOption(BOOL_LABEL, boolVar);
myWebServer.addOptionBox("Custom HTML");
myWebServer.addHTML(custom_html, "custom-html");
myWebServer.addCSS(custom_css);
myWebServer.addJavascript(custom_script);

if (myWebServer.begin()) {
Expand Down
1 change: 0 additions & 1 deletion examples/customHTML/readme.md

This file was deleted.

1 change: 0 additions & 1 deletion examples/customOptions/readme.md

This file was deleted.

2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=esp-fs-webserver
version=1.1.8
version=1.1.9
author=Tolentino Cotesta <cotestatnt@yahoo.com>
maintainer=Tolentino Cotesta <cotestatnt@yahoo.com>
sentence=From FSBrowser.ino example to library
Expand Down
39 changes: 34 additions & 5 deletions setup-ui/data/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const svgLogo ='<path d="M12.926 19.324a7.6 7.6 0 0 0-2.983-6.754 7.44 7.44 0 0 0-3.828-1.554.697.697 0 0 1-.606-.731.674.674 0 0 1 .743-.617 8.97 8.97 0 0 1 8 9.805 7.828 7.828 0 0 1-.298 1.542l1.989.56a11.039 11.039 0 0 0 1.714-.651 12.159 12.159 0 0 0 .217-2.343A12.57 12.57 0 0 0 7.212 6.171a5.53 5.53 0 0 0-2 0 4.354 4.354 0 0 0-2.16 1.337 4.274 4.274 0 0 0 1.909 6.856 9.896 9.896 0 0 0 1.074.195 4.011 4.011 0 0 1 3.337 3.954 3.965 3.965 0 0 1-.64 2.16l1.371.88a10.182 10.182 0 0 0 2.057.342 7.52 7.52 0 0 0 .754-2.628m.16 4.73A13.073 13.073 0 0 1 .001 10.983 12.982 12.982 0 0 1 3.83 1.737l.743.697a12.067 12.067 0 0 0 0 17.141 12.067 12.067 0 0 0 17.141 0l.697.697a12.97 12.97 0 0 1-9.336 3.726M24 10.993A10.993 10.993 0 0 0 12.949 0l-1.143.057-.252.732a18.912 18.912 0 0 1 11.588 11.576l.731-.263c0-.366.069-.732.069-1.143m-1.269 5.165A17.53 17.53 0 0 0 7.818 1.27a11.119 11.119 0 0 0-2.457 1.77v1.635A13.919 13.919 0 0 1 19.268 18.57h1.634a11.713 11.713 0 0 0 1.771-2.446M7.92 17.884a1.691 1.691 0 1 1-1.69-1.691 1.691 1.691 0 0 1 1.69 1.691" />';
svgLogo ='<svg width=64px height=64px viewBox="0 0 28 28"><path d="M12.926 19.324a7.6 7.6 0 0 0-2.983-6.754 7.44 7.44 0 0 0-3.828-1.554.697.697 0 0 1-.606-.731.674.674 0 0 1 .743-.617 8.97 8.97 0 0 1 8 9.805 7.828 7.828 0 0 1-.298 1.542l1.989.56a11.039 11.039 0 0 0 1.714-.651 12.159 12.159 0 0 0 .217-2.343A12.57 12.57 0 0 0 7.212 6.171a5.53 5.53 0 0 0-2 0 4.354 4.354 0 0 0-2.16 1.337 4.274 4.274 0 0 0 1.909 6.856 9.896 9.896 0 0 0 1.074.195 4.011 4.011 0 0 1 3.337 3.954 3.965 3.965 0 0 1-.64 2.16l1.371.88a10.182 10.182 0 0 0 2.057.342 7.52 7.52 0 0 0 .754-2.628m.16 4.73A13.073 13.073 0 0 1 .001 10.983 12.982 12.982 0 0 1 3.83 1.737l.743.697a12.067 12.067 0 0 0 0 17.141 12.067 12.067 0 0 0 17.141 0l.697.697a12.97 12.97 0 0 1-9.336 3.726M24 10.993A10.993 10.993 0 0 0 12.949 0l-1.143.057-.252.732a18.912 18.912 0 0 1 11.588 11.576l.731-.263c0-.366.069-.732.069-1.143m-1.269 5.165A17.53 17.53 0 0 0 7.818 1.27a11.119 11.119 0 0 0-2.457 1.77v1.635A13.919 13.919 0 0 1 19.268 18.57h1.634a11.713 11.713 0 0 0 1.771-2.446M7.92 17.884a1.691 1.691 0 1 1-1.69-1.691 1.691 1.691 0 0 1 1.69 1.691" /></svg>';
const svgMenu = '<path d="M3,6H21V8H3V6M3,11H21V13H3V11M3,16H21V18H3V16Z"/>'
const svgLock = '<svg height="16pt" viewBox="0 0 512 512"><path d="m336 512h-288c-26.453125 0-48-21.523438-48-48v-224c0-26.476562 21.546875-48 48-48h288c26.453125 0 48 21.523438 48 48v224c0 26.476562-21.546875 48-48 48zm-288-288c-8.8125 0-16 7.167969-16 16v224c0 8.832031 7.1875 16 16 16h288c8.8125 0 16-7.167969 16-16v-224c0-8.832031-7.1875-16-16-16zm0 0"/><path d="m304 224c-8.832031 0-16-7.167969-16-16v-80c0-52.929688-43.070312-96-96-96s-96 43.070312-96 96v80c0 8.832031-7.167969 16-16 16s-16-7.167969-16-16v-80c0-70.59375 57.40625-128 128-128s128 57.40625 128 128v80c0 8.832031-7.167969 16-16 16zm0 0"/></svg>';
const svgUnlock = '<svg height="16pt" viewBox="0 0 512 512"><path d="m336 512h-288c-26.453125 0-48-21.523438-48-48v-224c0-26.476562 21.546875-48 48-48h288c26.453125 0 48 21.523438 48 48v224c0 26.476562-21.546875 48-48 48zm-288-288c-8.8125 0-16 7.167969-16 16v224c0 8.832031 7.1875 16 16 16h288c8.8125 0 16-7.167969 16-16v-224c0-8.832031-7.1875-16-16-16zm0 0"/><path d="m80 224c-8.832031 0-16-7.167969-16-16v-80c0-70.59375 57.40625-128 128-128s128 57.40625 128 128c0 8.832031-7.167969 16-16 16s-16-7.167969-16-16c0-52.929688-43.070312-96-96-96s-96 43.070312-96 96v80c0 8.832031-7.167969 16-16 16zm0 0"/></svg>';
Expand Down Expand Up @@ -146,17 +146,36 @@ function listParameters (params) {

Object.entries(params).forEach(([key, value], i) => {

if(key.startsWith('param-box')) {
if(key.startsWith('name-logo')) {
$('name-logo').innerHTML = value;
return;
}

else if(key.startsWith('svg-logo')) {
svgLogo = '';
$('svg-logo').innerHTML = value;
return;
}

else if(key.startsWith('param-box')) {
pBox = createNewBox(i, value);
return;
}

if(key.startsWith('raw-javascript')) {
else if(key.startsWith('raw-css')) {
var css = document.createElement("style");
css.innerHTML = value.trim();
document.body.appendChild(css);
return;
}

else if(key.startsWith('raw-javascript')) {
var js = document.createElement("script");
js.innerHTML = value.trim();
document.body.appendChild(js);
return;
}

else if(key.startsWith('raw-html')) {
html = value.trim();
el = document.createElement('div');
Expand Down Expand Up @@ -375,8 +394,19 @@ function restartESP() {
}

// Initializes the app.

// Read logo from file if present, otherwise use inline svg;
fetch('logo.svg')
.then( () => {
svgLogo = '';
$('svg-logo').innerHTML = '<img class=logo src="/logo.svg"/>';
})
.catch( () => {
$('svg-logo').innerHTML = svgLogo;
});


$('svg-menu').innerHTML = svgMenu;
$('svg-logo').innerHTML = svgLogo;
$('svg-eye').innerHTML = svgEye;
$('svg-no-eye').innerHTML = svgNoEye;
$('svg-scan').innerHTML = svgScan;
Expand All @@ -397,7 +427,6 @@ $('restart').addEventListener('click', restartESP);

window.addEventListener('load', getParameters);


// Enable wifi-connect btn only if password inserted
$('connect-wifi').disabled = true;
$('password').addEventListener('input', (event) => {
Expand Down
4 changes: 2 additions & 2 deletions setup-ui/data/setup.htm
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

<body class=body>
<main id=main-box>
<header class="ctn">
<header class="ctn header">
<div class=title>
<svg id=svg-logo width=64px height=64px viewBox="0 0 28 28"></svg>
<div id=svg-logo></div>
<h1 id=name-logo class=heading>ESP WebServer</h1>
</div>
<div class=topnav id=top-nav>
Expand Down
2 changes: 1 addition & 1 deletion setup-ui/data/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ a:active,a:hover{outline:0;}
b{font-weight:700;}
h1{font-size:38px;}
h2{font-size:24px;}

.logo{width: 100%;}
.lbl-wifi {text-align: right;padding:0px}
hr {padding: 0;}
.input-label{color:#015293;position:relative;left:8px;bottom:-7px;z-index:2;display:inline-block;margin-bottom:0;padding-right:7px;padding-left:6px;border-style:solid;border-width:1px;border-color:#dcdce5;border-radius:6px;background-color:#fff;font-size:13.5px;line-height:17px;font-weight:500;}
Expand Down
26 changes: 20 additions & 6 deletions src/esp-fs-webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,10 @@ void FSWebServer::doWifiConnection()
String serverLoc = F("http://");
for (int i = 0; i < 4; i++)
serverLoc += i ? "." + String(ip[i]) : String(ip[i]);
serverLoc += "/";

String resp = "Restart ESP and then reload this page from <a href='";
resp += serverLoc;
resp += "/setup'>the new LAN address</a> or from <a href='http://";
resp += WiFi.getHostname();
resp += "/setup'>http://";
resp += WiFi.getHostname();
resp += ".local/setup</a>";
resp += "/setup'>the new LAN address</a>";

webserver->send(200, "text/plain", resp);
m_apmode = false;
Expand Down Expand Up @@ -421,6 +416,25 @@ void FSWebServer::handleScanNetworks()


#ifdef INCLUDE_SETUP_HTM
void FSWebServer::removeWhiteSpaces(const char* input, char* tr)
{
char pr = 0x00;
char ch;

int j = 0;
for (int i=0; i<strlen(input); i++) {
ch = input[i];
if (ch != '\n' && ch != '\r' && ch != '\t') {
if (ch == ' ' && pr == ' ') {
continue;
}
tr[j++] = ch;
}
pr = ch;
}
tr[j] = '\0';
}

void FSWebServer::handleSetup()
{
webserver->sendHeader(PSTR("Content-Encoding"), "gzip");
Expand Down
17 changes: 11 additions & 6 deletions src/esp-fs-webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,21 @@ class FSWebServer
inline void addHTML(const char* html, const char* id) {
String elementId = "raw-html-";
elementId += id;
addOption(elementId.c_str(), html, false);
char trimmed[strlen(html)];
removeWhiteSpaces(html, trimmed);
addOption(elementId.c_str(), trimmed, false);
}

inline void addCSS(const char* css, const char* id) {
String elementId = "raw-css-";
elementId += id;
addOption(elementId.c_str(), css, false);
inline void addCSS(const char* css) {
char trimmed[strlen(css)];
removeWhiteSpaces(css, trimmed);
addOption("raw-css", trimmed, false);
}

inline void addJavascript(const char* script) {
addOption("raw-javascript", script, true);
char trimmed[strlen(script)];
removeWhiteSpaces(script, trimmed);
addOption("raw-javascript", trimmed, true);
}

// Only for backward-compatibility
Expand Down Expand Up @@ -284,6 +288,7 @@ class FSWebServer
void getIpAddress();
void handleRequest();
#ifdef INCLUDE_SETUP_HTM
void removeWhiteSpaces(const char* input, char* tr);
void handleSetup();
uint8_t numOptions = 0;
#endif
Expand Down
Loading

0 comments on commit 0c09881

Please sign in to comment.