Skip to content

Commit 9333ec6

Browse files
authored
Add CSS body classes for each page (#1767)
* Add body classes argument Note that the additional function argument for getHTTPHead() includes a default argument for backwards compatibility with the API. * Add page-specific classes Allows easy styling of each page * Change root CSS class to 'home' Trying to avoid confusion between the class for the root level and the CSS ":root" pseudo-class * Use conditional body class spacing Avoids an extra space if a body class is set, but an empty string is passed to the function
1 parent 922059b commit 9333ec6

File tree

4 files changed

+49
-22
lines changed

4 files changed

+49
-22
lines changed

WiFiManager.cpp

+22-21
Original file line numberDiff line numberDiff line change
@@ -1272,22 +1272,23 @@ void WiFiManager::startWPS() {
12721272
}
12731273
#endif
12741274

1275-
String WiFiManager::getHTTPHead(String title){
1275+
String WiFiManager::getHTTPHead(String title, String classes){
12761276
String page;
12771277
page += FPSTR(HTTP_HEAD_START);
12781278
page.replace(FPSTR(T_v), title);
12791279
page += FPSTR(HTTP_SCRIPT);
12801280
page += FPSTR(HTTP_STYLE);
12811281
page += _customHeadElement;
12821282

1283-
if(_bodyClass != ""){
1284-
String p = FPSTR(HTTP_HEAD_END);
1285-
p.replace(FPSTR(T_c), _bodyClass); // add class str
1286-
page += p;
1283+
String p = FPSTR(HTTP_HEAD_END);
1284+
if (_bodyClass != "") {
1285+
if (classes != "") {
1286+
classes += " "; // add spacing, if necessary
1287+
}
1288+
classes += _bodyClass; // add class str
12871289
}
1288-
else {
1289-
page += FPSTR(HTTP_HEAD_END);
1290-
}
1290+
p.replace(FPSTR(T_c), classes);
1291+
page += p;
12911292

12921293
return page;
12931294
}
@@ -1332,7 +1333,7 @@ void WiFiManager::handleRoot() {
13321333
#endif
13331334
if (captivePortal()) return; // If captive portal redirect instead of displaying the page
13341335
handleRequest();
1335-
String page = getHTTPHead(_title); // @token options @todo replace options with title
1336+
String page = getHTTPHead(_title, FPSTR(C_root)); // @token options @todo replace options with title
13361337
String str = FPSTR(HTTP_ROOT_MAIN); // @todo custom title
13371338
str.replace(FPSTR(T_t),_title);
13381339
str.replace(FPSTR(T_v),configPortalActive ? _apName : (getWiFiHostname() + " - " + WiFi.localIP().toString())); // use ip if ap is not active for heading @todo use hostname?
@@ -1357,7 +1358,7 @@ void WiFiManager::handleWifi(boolean scan) {
13571358
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Wifi"));
13581359
#endif
13591360
handleRequest();
1360-
String page = getHTTPHead(FPSTR(S_titlewifi)); // @token titlewifi
1361+
String page = getHTTPHead(FPSTR(S_titlewifi), FPSTR(C_wifi)); // @token titlewifi
13611362
if (scan) {
13621363
#ifdef WM_DEBUG_LEVEL
13631364
// DEBUG_WM(WM_DEBUG_DEV,"refresh flag:",server->hasArg(F("refresh")));
@@ -1413,7 +1414,7 @@ void WiFiManager::handleParam(){
14131414
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Param"));
14141415
#endif
14151416
handleRequest();
1416-
String page = getHTTPHead(FPSTR(S_titleparam)); // @token titlewifi
1417+
String page = getHTTPHead(FPSTR(S_titleparam), FPSTR(C_param)); // @token titlewifi
14171418

14181419
String pitem = "";
14191420

@@ -1878,11 +1879,11 @@ void WiFiManager::handleWifiSave() {
18781879
String page;
18791880

18801881
if(_ssid == ""){
1881-
page = getHTTPHead(FPSTR(S_titlewifisettings)); // @token titleparamsaved
1882+
page = getHTTPHead(FPSTR(S_titlewifisettings), FPSTR(C_wifi)); // @token titleparamsaved
18821883
page += FPSTR(HTTP_PARAMSAVED);
18831884
}
18841885
else {
1885-
page = getHTTPHead(FPSTR(S_titlewifisaved)); // @token titlewifisaved
1886+
page = getHTTPHead(FPSTR(S_titlewifisaved), FPSTR(C_wifi)); // @token titlewifisaved
18861887
page += FPSTR(HTTP_SAVED);
18871888
}
18881889

@@ -1911,7 +1912,7 @@ void WiFiManager::handleParamSave() {
19111912

19121913
doParamSave();
19131914

1914-
String page = getHTTPHead(FPSTR(S_titleparamsaved)); // @token titleparamsaved
1915+
String page = getHTTPHead(FPSTR(S_titleparamsaved), FPSTR(C_param)); // @token titleparamsaved
19151916
page += FPSTR(HTTP_PARAMSAVED);
19161917
if(_showBack) page += FPSTR(HTTP_BACKBTN);
19171918
page += FPSTR(HTTP_END);
@@ -1977,7 +1978,7 @@ void WiFiManager::handleInfo() {
19771978
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Info"));
19781979
#endif
19791980
handleRequest();
1980-
String page = getHTTPHead(FPSTR(S_titleinfo)); // @token titleinfo
1981+
String page = getHTTPHead(FPSTR(S_titleinfo), FPSTR(C_info)); // @token titleinfo
19811982
reportStatus(page);
19821983

19831984
uint16_t infos = 0;
@@ -2322,7 +2323,7 @@ void WiFiManager::handleExit() {
23222323
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Exit"));
23232324
#endif
23242325
handleRequest();
2325-
String page = getHTTPHead(FPSTR(S_titleexit)); // @token titleexit
2326+
String page = getHTTPHead(FPSTR(S_titleexit), FPSTR(C_exit)); // @token titleexit
23262327
page += FPSTR(S_exiting); // @token exiting
23272328
// ('Logout', 401, {'WWW-Authenticate': 'Basic realm="Login required"'})
23282329
server->sendHeader(F("Cache-Control"), F("no-cache, no-store, must-revalidate")); // @HTTPHEAD send cache
@@ -2339,7 +2340,7 @@ void WiFiManager::handleReset() {
23392340
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Reset"));
23402341
#endif
23412342
handleRequest();
2342-
String page = getHTTPHead(FPSTR(S_titlereset)); //@token titlereset
2343+
String page = getHTTPHead(FPSTR(S_titlereset), FPSTR(C_restart)); //@token titlereset
23432344
page += FPSTR(S_resetting); //@token resetting
23442345
page += FPSTR(HTTP_END);
23452346

@@ -2364,7 +2365,7 @@ void WiFiManager::handleErase(boolean opt) {
23642365
DEBUG_WM(WM_DEBUG_NOTIFY,F("<- HTTP Erase"));
23652366
#endif
23662367
handleRequest();
2367-
String page = getHTTPHead(FPSTR(S_titleerase)); // @token titleerase
2368+
String page = getHTTPHead(FPSTR(S_titleerase), FPSTR(C_erase)); // @token titleerase
23682369

23692370
bool ret = erase(opt);
23702371

@@ -2469,7 +2470,7 @@ void WiFiManager::handleClose(){
24692470
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP close"));
24702471
#endif
24712472
handleRequest();
2472-
String page = getHTTPHead(FPSTR(S_titleclose)); // @token titleclose
2473+
String page = getHTTPHead(FPSTR(S_titleclose), FPSTR(C_close)); // @token titleclose
24732474
page += FPSTR(S_closing); // @token closing
24742475
HTTPSend(page);
24752476
}
@@ -3874,7 +3875,7 @@ void WiFiManager::handleUpdate() {
38743875
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- Handle update"));
38753876
#endif
38763877
if (captivePortal()) return; // If captive portal redirect instead of displaying the page
3877-
String page = getHTTPHead(_title); // @token options
3878+
String page = getHTTPHead(_title, FPSTR(C_update)); // @token options
38783879
String str = FPSTR(HTTP_ROOT_MAIN);
38793880
str.replace(FPSTR(T_t), _title);
38803881
str.replace(FPSTR(T_v), configPortalActive ? _apName : (getWiFiHostname() + " - " + WiFi.localIP().toString())); // use ip if ap is not active for heading
@@ -3984,7 +3985,7 @@ void WiFiManager::handleUpdateDone() {
39843985
DEBUG_WM(WM_DEBUG_VERBOSE, F("<- Handle update done"));
39853986
// if (captivePortal()) return; // If captive portal redirect instead of displaying the page
39863987

3987-
String page = getHTTPHead(FPSTR(S_options)); // @token options
3988+
String page = getHTTPHead(FPSTR(S_options), FPSTR(C_update)); // @token options
39883989
String str = FPSTR(HTTP_ROOT_MAIN);
39893990
str.replace(FPSTR(T_t),_title);
39903991
str.replace(FPSTR(T_v), configPortalActive ? _apName : WiFi.localIP().toString()); // use ip if ap is not active for heading

WiFiManager.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ class WiFiManager
751751
String getIpForm(String id, String title, String value);
752752
String getScanItemOut();
753753
String getStaticOut();
754-
String getHTTPHead(String title);
754+
String getHTTPHead(String title, String classes = "");
755755
String getMenuOut();
756756
//helpers
757757
boolean isIp(String str);

wm_consts_en.h

+13
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static PGM_P _menutokens[] PROGMEM = {
4646
const uint8_t _nummenutokens = (sizeof(_menutokens) / sizeof(PGM_P));
4747

4848

49+
// Routes
4950
const char R_root[] PROGMEM = "/";
5051
const char R_wifi[] PROGMEM = "/wifi";
5152
const char R_wifinoscan[] PROGMEM = "/0wifi";
@@ -62,6 +63,18 @@ const char R_update[] PROGMEM = "/update";
6263
const char R_updatedone[] PROGMEM = "/u";
6364

6465

66+
// Classes
67+
const char C_root[] PROGMEM = "home";
68+
const char C_wifi[] PROGMEM = "wifi";
69+
const char C_info[] PROGMEM = "info";
70+
const char C_param[] PROGMEM = "param";
71+
const char C_close[] PROGMEM = "close";
72+
const char C_restart[] PROGMEM = "restart";
73+
const char C_exit[] PROGMEM = "exit";
74+
const char C_erase[] PROGMEM = "erase";
75+
const char C_update[] PROGMEM = "update";
76+
77+
6578
//Strings
6679
const char S_ip[] PROGMEM = "ip";
6780
const char S_gw[] PROGMEM = "gw";

wm_consts_fr.h

+13
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ static PGM_P _menutokens[] PROGMEM = {
4747
const uint8_t _nummenutokens = (sizeof(_menutokens) / sizeof(PGM_P));
4848

4949

50+
// Routes
5051
const char R_root[] PROGMEM = "/";
5152
const char R_wifi[] PROGMEM = "/wifi";
5253
const char R_wifinoscan[] PROGMEM = "/0wifi";
@@ -63,6 +64,18 @@ const char R_update[] PROGMEM = "/update";
6364
const char R_updatedone[] PROGMEM = "/u";
6465

6566

67+
// Classes
68+
const char C_root[] PROGMEM = "home";
69+
const char C_wifi[] PROGMEM = "wifi";
70+
const char C_info[] PROGMEM = "info";
71+
const char C_param[] PROGMEM = "param";
72+
const char C_close[] PROGMEM = "close";
73+
const char C_restart[] PROGMEM = "restart";
74+
const char C_exit[] PROGMEM = "exit";
75+
const char C_erase[] PROGMEM = "erase";
76+
const char C_update[] PROGMEM = "update";
77+
78+
6679
//Strings
6780
const char S_ip[] PROGMEM = "ip";
6881
const char S_gw[] PROGMEM = "gw";

0 commit comments

Comments
 (0)