Skip to content

Commit

Permalink
Add CSS body classes for each page (#1767)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dmadison authored Dec 11, 2024
1 parent 922059b commit 9333ec6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 22 deletions.
43 changes: 22 additions & 21 deletions WiFiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1272,22 +1272,23 @@ 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);
page += FPSTR(HTTP_SCRIPT);
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;
}
Expand Down Expand Up @@ -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?
Expand All @@ -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")));
Expand Down Expand Up @@ -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 = "";

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion WiFiManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions wm_consts_en.h
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand Down
13 changes: 13 additions & 0 deletions wm_consts_fr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand Down

0 comments on commit 9333ec6

Please sign in to comment.