From 9333ec68ed5a04cdd9167643c25e08f759a03d0c Mon Sep 17 00:00:00 2001 From: Dave Madison Date: Tue, 10 Dec 2024 21:56:58 -0500 Subject: [PATCH] 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 --- WiFiManager.cpp | 43 ++++++++++++++++++++++--------------------- WiFiManager.h | 2 +- wm_consts_en.h | 13 +++++++++++++ wm_consts_fr.h | 13 +++++++++++++ 4 files changed, 49 insertions(+), 22 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index c36f46d82..708168be1 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -1272,7 +1272,7 @@ void WiFiManager::startWPS() { } #endif -String WiFiManager::getHTTPHead(String title){ +String WiFiManager::getHTTPHead(String title, String classes){ String page; page += FPSTR(HTTP_HEAD_START); page.replace(FPSTR(T_v), title); @@ -1280,14 +1280,15 @@ String WiFiManager::getHTTPHead(String title){ page += FPSTR(HTTP_STYLE); page += _customHeadElement; - if(_bodyClass != ""){ - String p = FPSTR(HTTP_HEAD_END); - p.replace(FPSTR(T_c), _bodyClass); // add class str - page += p; + String p = FPSTR(HTTP_HEAD_END); + if (_bodyClass != "") { + if (classes != "") { + classes += " "; // add spacing, if necessary + } + classes += _bodyClass; // add class str } - else { - page += FPSTR(HTTP_HEAD_END); - } + p.replace(FPSTR(T_c), classes); + page += p; return page; } @@ -1332,7 +1333,7 @@ void WiFiManager::handleRoot() { #endif if (captivePortal()) return; // If captive portal redirect instead of displaying the page handleRequest(); - String page = getHTTPHead(_title); // @token options @todo replace options with title + String page = getHTTPHead(_title, FPSTR(C_root)); // @token options @todo replace options with title String str = FPSTR(HTTP_ROOT_MAIN); // @todo custom title str.replace(FPSTR(T_t),_title); 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) { DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Wifi")); #endif handleRequest(); - String page = getHTTPHead(FPSTR(S_titlewifi)); // @token titlewifi + String page = getHTTPHead(FPSTR(S_titlewifi), FPSTR(C_wifi)); // @token titlewifi if (scan) { #ifdef WM_DEBUG_LEVEL // DEBUG_WM(WM_DEBUG_DEV,"refresh flag:",server->hasArg(F("refresh"))); @@ -1413,7 +1414,7 @@ void WiFiManager::handleParam(){ DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Param")); #endif handleRequest(); - String page = getHTTPHead(FPSTR(S_titleparam)); // @token titlewifi + String page = getHTTPHead(FPSTR(S_titleparam), FPSTR(C_param)); // @token titlewifi String pitem = ""; @@ -1878,11 +1879,11 @@ void WiFiManager::handleWifiSave() { String page; if(_ssid == ""){ - page = getHTTPHead(FPSTR(S_titlewifisettings)); // @token titleparamsaved + page = getHTTPHead(FPSTR(S_titlewifisettings), FPSTR(C_wifi)); // @token titleparamsaved page += FPSTR(HTTP_PARAMSAVED); } else { - page = getHTTPHead(FPSTR(S_titlewifisaved)); // @token titlewifisaved + page = getHTTPHead(FPSTR(S_titlewifisaved), FPSTR(C_wifi)); // @token titlewifisaved page += FPSTR(HTTP_SAVED); } @@ -1911,7 +1912,7 @@ void WiFiManager::handleParamSave() { doParamSave(); - String page = getHTTPHead(FPSTR(S_titleparamsaved)); // @token titleparamsaved + String page = getHTTPHead(FPSTR(S_titleparamsaved), FPSTR(C_param)); // @token titleparamsaved page += FPSTR(HTTP_PARAMSAVED); if(_showBack) page += FPSTR(HTTP_BACKBTN); page += FPSTR(HTTP_END); @@ -1977,7 +1978,7 @@ void WiFiManager::handleInfo() { DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Info")); #endif handleRequest(); - String page = getHTTPHead(FPSTR(S_titleinfo)); // @token titleinfo + String page = getHTTPHead(FPSTR(S_titleinfo), FPSTR(C_info)); // @token titleinfo reportStatus(page); uint16_t infos = 0; @@ -2322,7 +2323,7 @@ void WiFiManager::handleExit() { DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Exit")); #endif handleRequest(); - String page = getHTTPHead(FPSTR(S_titleexit)); // @token titleexit + String page = getHTTPHead(FPSTR(S_titleexit), FPSTR(C_exit)); // @token titleexit page += FPSTR(S_exiting); // @token exiting // ('Logout', 401, {'WWW-Authenticate': 'Basic realm="Login required"'}) server->sendHeader(F("Cache-Control"), F("no-cache, no-store, must-revalidate")); // @HTTPHEAD send cache @@ -2339,7 +2340,7 @@ void WiFiManager::handleReset() { DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Reset")); #endif handleRequest(); - String page = getHTTPHead(FPSTR(S_titlereset)); //@token titlereset + String page = getHTTPHead(FPSTR(S_titlereset), FPSTR(C_restart)); //@token titlereset page += FPSTR(S_resetting); //@token resetting page += FPSTR(HTTP_END); @@ -2364,7 +2365,7 @@ void WiFiManager::handleErase(boolean opt) { DEBUG_WM(WM_DEBUG_NOTIFY,F("<- HTTP Erase")); #endif handleRequest(); - String page = getHTTPHead(FPSTR(S_titleerase)); // @token titleerase + String page = getHTTPHead(FPSTR(S_titleerase), FPSTR(C_erase)); // @token titleerase bool ret = erase(opt); @@ -2469,7 +2470,7 @@ void WiFiManager::handleClose(){ DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP close")); #endif handleRequest(); - String page = getHTTPHead(FPSTR(S_titleclose)); // @token titleclose + String page = getHTTPHead(FPSTR(S_titleclose), FPSTR(C_close)); // @token titleclose page += FPSTR(S_closing); // @token closing HTTPSend(page); } @@ -3874,7 +3875,7 @@ void WiFiManager::handleUpdate() { DEBUG_WM(WM_DEBUG_VERBOSE,F("<- Handle update")); #endif if (captivePortal()) return; // If captive portal redirect instead of displaying the page - String page = getHTTPHead(_title); // @token options + String page = getHTTPHead(_title, FPSTR(C_update)); // @token options String str = FPSTR(HTTP_ROOT_MAIN); str.replace(FPSTR(T_t), _title); 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() { DEBUG_WM(WM_DEBUG_VERBOSE, F("<- Handle update done")); // if (captivePortal()) return; // If captive portal redirect instead of displaying the page - String page = getHTTPHead(FPSTR(S_options)); // @token options + String page = getHTTPHead(FPSTR(S_options), FPSTR(C_update)); // @token options String str = FPSTR(HTTP_ROOT_MAIN); str.replace(FPSTR(T_t),_title); str.replace(FPSTR(T_v), configPortalActive ? _apName : WiFi.localIP().toString()); // use ip if ap is not active for heading diff --git a/WiFiManager.h b/WiFiManager.h index f911beb0d..c7a34461b 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -751,7 +751,7 @@ class WiFiManager String getIpForm(String id, String title, String value); String getScanItemOut(); String getStaticOut(); - String getHTTPHead(String title); + String getHTTPHead(String title, String classes = ""); String getMenuOut(); //helpers boolean isIp(String str); diff --git a/wm_consts_en.h b/wm_consts_en.h index dac35e46a..5ba493946 100644 --- a/wm_consts_en.h +++ b/wm_consts_en.h @@ -46,6 +46,7 @@ static PGM_P _menutokens[] PROGMEM = { const uint8_t _nummenutokens = (sizeof(_menutokens) / sizeof(PGM_P)); +// Routes const char R_root[] PROGMEM = "/"; const char R_wifi[] PROGMEM = "/wifi"; const char R_wifinoscan[] PROGMEM = "/0wifi"; @@ -62,6 +63,18 @@ const char R_update[] PROGMEM = "/update"; const char R_updatedone[] PROGMEM = "/u"; +// Classes +const char C_root[] PROGMEM = "home"; +const char C_wifi[] PROGMEM = "wifi"; +const char C_info[] PROGMEM = "info"; +const char C_param[] PROGMEM = "param"; +const char C_close[] PROGMEM = "close"; +const char C_restart[] PROGMEM = "restart"; +const char C_exit[] PROGMEM = "exit"; +const char C_erase[] PROGMEM = "erase"; +const char C_update[] PROGMEM = "update"; + + //Strings const char S_ip[] PROGMEM = "ip"; const char S_gw[] PROGMEM = "gw"; diff --git a/wm_consts_fr.h b/wm_consts_fr.h index 8a4945276..86d439e4b 100644 --- a/wm_consts_fr.h +++ b/wm_consts_fr.h @@ -47,6 +47,7 @@ static PGM_P _menutokens[] PROGMEM = { const uint8_t _nummenutokens = (sizeof(_menutokens) / sizeof(PGM_P)); +// Routes const char R_root[] PROGMEM = "/"; const char R_wifi[] PROGMEM = "/wifi"; const char R_wifinoscan[] PROGMEM = "/0wifi"; @@ -63,6 +64,18 @@ const char R_update[] PROGMEM = "/update"; const char R_updatedone[] PROGMEM = "/u"; +// Classes +const char C_root[] PROGMEM = "home"; +const char C_wifi[] PROGMEM = "wifi"; +const char C_info[] PROGMEM = "info"; +const char C_param[] PROGMEM = "param"; +const char C_close[] PROGMEM = "close"; +const char C_restart[] PROGMEM = "restart"; +const char C_exit[] PROGMEM = "exit"; +const char C_erase[] PROGMEM = "erase"; +const char C_update[] PROGMEM = "update"; + + //Strings const char S_ip[] PROGMEM = "ip"; const char S_gw[] PROGMEM = "gw";