remove duplicate flashMsg code

This commit is contained in:
glyph 2021-11-22 15:29:40 +02:00
parent 554997a5c0
commit e54ff8829a
4 changed files with 16 additions and 158 deletions

View File

@ -1,7 +1,7 @@
/*
behavioural layer for the `network_detail.html.tera` template,
corresponding to the web route `/network/wifi?<ssid>`
corresponding to the web route `/settings/network/wifi?<ssid>`
- intercept button clicks for connect, disconnect and forget
- perform json api call
@ -12,7 +12,6 @@ methods:
PEACH_NETWORK.connect();
PEACH_NETWORK.disconnect();
PEACH_NETWORK.forget();
PEACH_NETWORK.flashMsg(status, msg);
*/
@ -33,7 +32,7 @@ PEACH_NETWORK.connect = function() {
// perform json serialization
var jsonData = JSON.stringify(ssidData);
// write in-progress status message to ui
PEACH_NETWORK.flashMsg("info", "Connecting to access point...");
PEACH.flashMsg("info", "Connecting to access point...");
// send add_wifi POST request
fetch("/api/v1/network/wifi/connect", {
method: "post",
@ -47,7 +46,7 @@ PEACH_NETWORK.connect = function() {
})
.then( (jsonData) => {
// write json response message to ui
PEACH_NETWORK.flashMsg(jsonData.status, jsonData.msg);
PEACH.flashMsg(jsonData.status, jsonData.msg);
})
}, false);
};
@ -69,7 +68,7 @@ PEACH_NETWORK.disconnect = function() {
// perform json serialization
var jsonData = JSON.stringify(ssidData);
// write in-progress status message to ui
PEACH_NETWORK.flashMsg("info", "Disconnecting from access point...");
PEACH.flashMsg("info", "Disconnecting from access point...");
// send disconnect_wifi POST request
fetch("/api/v1/network/wifi/disconnect", {
method: "post",
@ -83,7 +82,7 @@ PEACH_NETWORK.disconnect = function() {
})
.then( (jsonData) => {
// write json response message to ui
PEACH_NETWORK.flashMsg(jsonData.status, jsonData.msg);
PEACH.flashMsg(jsonData.status, jsonData.msg);
})
}, false);
};
@ -105,7 +104,7 @@ PEACH_NETWORK.forget = function() {
// perform json serialization
var jsonData = JSON.stringify(ssidData);
// write in-progress status message to ui
PEACH_NETWORK.flashMsg("info", "Removing credentials for access point...");
PEACH.flashMsg("info", "Removing credentials for access point...");
// send forget_ap POST request
fetch("/api/v1/network/wifi/forget", {
method: "post",
@ -119,48 +118,13 @@ PEACH_NETWORK.forget = function() {
})
.then( (jsonData) => {
// write json response message to ui
PEACH_NETWORK.flashMsg(jsonData.status, jsonData.msg);
PEACH.flashMsg(jsonData.status, jsonData.msg);
})
}, false);
};
});
}
// display a message by appending a paragraph element
PEACH_NETWORK.flashMsg = function(status, msg) {
// set the class of the element according to status
var elementClass;
if (status === "success") {
elementClass = "capsule center-text flash-message font-success";
} else if (status === "info") {
elementClass = "capsule center-text flash-message font-info";
} else {
elementClass = "capsule center-text flash-message font-failure";
};
var flashElement = document.getElementById("flashMsg");
// if flashElement exists, update the class & text
if (flashElement) {
flashElement.className = elementClass;
flashElement.innerText = msg;
// if flashElement does not exist, create it, set id, class, text & append
} else {
// create new div for flash message
var flashDiv = document.createElement("DIV");
// set div attributes
flashDiv.id = "flashMsg";
flashDiv.className = elementClass;
// add json response message to flash message div
var flashMsg = document.createTextNode(msg);
flashDiv.appendChild(flashMsg);
// insert the flash message div below the button div
var buttonDiv = document.getElementById("buttonDiv");
// flashDiv will be added to the end since buttonDiv is the last
// child within the parent element (card-container div)
buttonDiv.parentNode.insertBefore(flashDiv, buttonDiv.nextSibling);
}
}
var detailInstance = PEACH_NETWORK;
detailInstance.connect();
detailInstance.disconnect();

View File

@ -9,7 +9,6 @@ behavioural layer for the `network_modify.html.tera` template
methods:
PEACH_NETWORK.modify();
PEACH_NETWORK.flashMsg(status, msg);
*/
@ -33,7 +32,7 @@ PEACH_NETWORK.modify = function() {
// perform json serialization
var jsonData = JSON.stringify(object);
// write in-progress status message to ui
PEACH_NETWORK.flashMsg("info", "Updating WiFi password...");
PEACH.flashMsg("info", "Updating WiFi password...");
// send new_password POST request
fetch("/api/v1/network/wifi/modify", {
method: "post",
@ -47,46 +46,11 @@ PEACH_NETWORK.modify = function() {
})
.then( (jsonData) => {
// write json response message to ui
PEACH_NETWORK.flashMsg(jsonData.status, jsonData.msg);
PEACH.flashMsg(jsonData.status, jsonData.msg);
})
}, false);
});
}
// display a message by appending a paragraph element
PEACH_NETWORK.flashMsg = function(status, msg) {
// set the class of the element according to status
var elementClass;
if (status === "success") {
elementClass = "capsule center-text flash-message font-success";
} else if (status === "info") {
elementClass = "capsule center-text flash-message font-info";
} else {
elementClass = "capsule center-text flash-message font-failure";
};
var flashElement = document.getElementById("flashMsg");
// if flashElement exists, update the class & text
if (flashElement) {
flashElement.className = elementClass;
flashElement.innerText = msg;
// if flashElement does not exist, create it, set id, class, text & append
} else {
// create new div for flash message
var flashDiv = document.createElement("DIV");
// set div attributes
flashDiv.id = "flashMsg";
flashDiv.className = elementClass;
// add json response message to flash message div
var flashMsg = document.createTextNode(msg);
flashDiv.appendChild(flashMsg);
// insert the flash message div below the button div
var buttonDiv = document.getElementById("buttonDiv");
// flashDiv will be added to the end since buttonDiv is the last
// child within the parent element (card-container div)
buttonDiv.parentNode.insertBefore(flashDiv, buttonDiv.nextSibling);
}
}
var modifyInstance = PEACH_NETWORK;
modifyInstance.modify();

View File

@ -1,7 +1,7 @@
/*
behavioural layer for the `network_usage.html.tera` template,
corresponding to the web route `/network/wifi/usage`
corresponding to the web route `/settings/network/wifi/usage`
- intercept form submissions
- perform json api calls
@ -13,7 +13,6 @@ methods:
PEACH_NETWORK.resetUsage();
PEACH_NETWORK.toggleWarning();
PEACH_NETWORK.toggleCutoff();
PEACH_NETWORK.flashMsg(status, msg);
*/
@ -51,7 +50,7 @@ PEACH_NETWORK.updateAlerts = function() {
})
.then( (jsonData) => {
// write json response message to ui
PEACH_NETWORK.flashMsg(jsonData.status, jsonData.msg);
PEACH.flashMsg(jsonData.status, jsonData.msg);
})
}, false);
});
@ -79,7 +78,7 @@ PEACH_NETWORK.resetUsage = function() {
.then( (jsonData) => {
console.log(jsonData.msg);
// write json response message to ui
PEACH_NETWORK.flashMsg(jsonData.status, jsonData.msg);
PEACH.flashMsg(jsonData.status, jsonData.msg);
// if reset is successful, update the ui
if (jsonData.status === "success") {
console.log(jsonData.data);
@ -133,39 +132,6 @@ PEACH_NETWORK.toggleCutoff = function() {
});
};
// display a message by appending a paragraph element
PEACH_NETWORK.flashMsg = function(status, msg) {
// set the class of the element according to status
var elementClass;
if (status === "success") {
elementClass = "capsule center-text flash-message font-success";
} else if (status === "info") {
elementClass = "capsule center-text flash-message font-info";
} else {
elementClass = "capsule center-text flash-message font-failure";
};
var flashElement = document.getElementById("flashMsg");
// if flashElement exists, update the class & text
if (flashElement) {
flashElement.className = elementClass;
flashElement.innerText = msg;
// if flashElement does not exist, create it, set id, class, text & append
} else {
// create new div for flash message
var flashDiv = document.createElement("DIV");
// set div attributes
flashDiv.id = "flashMsg";
flashDiv.className = elementClass;
// add json response message to flash message div
var flashMsg = document.createTextNode(msg);
flashDiv.appendChild(flashMsg);
// insert the flash message div below the button div
var buttonDiv = document.getElementById("buttonDiv");
buttonDiv.parentNode.insertBefore(flashDiv, buttonDiv.nextSibling);
}
}
var usageInstance = PEACH_NETWORK;
usageInstance.resetUsage();
usageInstance.toggleWarning();

View File

@ -11,7 +11,6 @@ methods:
PEACH_DEVICE.reboot();
PEACH_DEVICE.shutdown();
PEACH_DEVICE.flashMsg(status, msg);
*/
@ -26,7 +25,7 @@ PEACH_DEVICE.reboot = function() {
// prevent redirect on button press (default behavior)
e.preventDefault();
// write reboot flash message
PEACH_DEVICE.flashMsg("success", "Rebooting the device...");
PEACH.flashMsg("success", "Rebooting the device...");
// send reboot_device POST request
fetch("/api/v1/admin/reboot", {
method: "post",
@ -41,7 +40,7 @@ PEACH_DEVICE.reboot = function() {
.then( (jsonData) => {
console.log(jsonData.msg);
// write json response message to ui
PEACH_DEVICE.flashMsg(jsonData.status, jsonData.msg);
PEACH.flashMsg(jsonData.status, jsonData.msg);
})
}, false);
}
@ -57,7 +56,7 @@ PEACH_DEVICE.shutdown = function() {
// prevent form submission (default behavior)
e.preventDefault();
// write shutdown flash message
PEACH_DEVICE.flashMsg("success", "Shutting down the device...");
PEACH.flashMsg("success", "Shutting down the device...");
// send shutdown_device POST request
fetch("/api/v1/shutdown", {
method: "post",
@ -72,48 +71,13 @@ PEACH_DEVICE.shutdown = function() {
.then( (jsonData) => {
console.log(jsonData.msg);
// write json response message to ui
PEACH_DEVICE.flashMsg(jsonData.status, jsonData.msg);
PEACH.flashMsg(jsonData.status, jsonData.msg);
})
}, false);
}
});
}
// display a message by appending a paragraph element
PEACH_DEVICE.flashMsg = function(status, msg) {
// set the class of the element according to status
var elementClass;
if (status === "success") {
elementClass = "capsule center-text flash-message font-success";
} else if (status === "info") {
elementClass = "capsule center-text flash-message font-info";
} else {
elementClass = "capsule center-text flash-message font-failure";
};
var flashElement = document.getElementById("flashMsg");
// if flashElement exists, update the class & text
if (flashElement) {
flashElement.className = elementClass;
flashElement.innerText = msg;
// if flashElement does not exist, create it, set id, class, text & append
} else {
// create new div for flash message
var flashDiv = document.createElement("DIV");
// set div attributes
flashDiv.id = "flashMsg";
flashDiv.className = elementClass;
// add json response message to flash message div
var flashMsg = document.createTextNode(msg);
flashDiv.appendChild(flashMsg);
// insert the flash message div below the button div
var buttonDiv = document.getElementById("buttonDiv");
// flashDiv will be added to the end since buttonDiv is the last
// child within the parent element (card-container div)
buttonDiv.parentNode.insertBefore(flashDiv, buttonDiv.nextSibling);
}
}
var deviceInstance = PEACH_DEVICE;
deviceInstance.reboot();
deviceInstance.shutdown();