Skip to content

Commit

Permalink
http module: improve testing for old/new value in shnginserttext meth…
Browse files Browse the repository at this point in the history
…od, use initial value as an argument to prevent highlighting effect on first page load
  • Loading branch information
onkelandy committed Sep 18, 2023
1 parent 51600cf commit d908bb2
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions modules/http/webif/gstatic/js/smarthomeng.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,20 @@ function shngInsertText (id, text, table_id=null, highlight=0) {
text = text.toString();
if (table_id == null) {
element = $("#" + $.escapeSelector(id));
if (highlight > 0) {
let old_text = sanitize($('#' + $.escapeSelector(id)).text(), true);
let alternative_old_text = sanitize(old_text, false, true);
let new_content = (old_text !== sanitize(text)) && (alternative_old_text !== sanitize(text));
// compare old value of cell with new one and highlight
if (old_text != "..." && new_content) {
if (element.length == 0) {
console.warn("Element with id " + id + " does not exist");
return;
}
let old_text = sanitize(element.text(), true);
let alternative_old_text = sanitize(old_text, false, true);
let new_content = (old_text !== sanitize(text, true)) && (alternative_old_text !== sanitize(text, false, true));
if (highlight > 0 && old_text != "..." && new_content) {
startAnimation(element, highlight);
}
}
// update HTML element
element.html(text);
if (new_content) {
console.log("Updating html element with id " + id + " because new data found: " + text + ", old text: " + old_text);
element.html(text);
}
}
else {
try {
Expand All @@ -104,7 +107,7 @@ function shngInsertText (id, text, table_id=null, highlight=0) {
// fix HTML entities and quotation
let old_text = sanitize($('#' + table_id).DataTable().cell( $('#' + $.escapeSelector(id)) ).data(), true);
let alternative_old_text = sanitize(old_text, false, true);
let new_content = (old_text !== sanitize(text)) && (alternative_old_text !== sanitize(text));
let new_content = (old_text !== sanitize(text, true)) && (alternative_old_text !== sanitize(text, false, true));

if (highlight > 0) {
element = $('#' + table_id).DataTable().cell( $('#' + $.escapeSelector(id)) ).node();
Expand Down Expand Up @@ -133,49 +136,49 @@ function shngInsertText (id, text, table_id=null, highlight=0) {
* @param {string} optional name of dataset to get. Only needed, if the webinterface gets multiple different datasets from the plugin
* @param {undefined} optional set of parameters. Has to be handled in get_data_html function of web interface init.py. Can be any type, e.g. an item name, date, dict with multiple values, etc.
*/
function shngGetUpdatedData(dataSet=null, update_params=null) {
function shngGetUpdatedData(dataSet=null, update_params=null, initial=1) {
if (dataSet == null) dataSet = window.dataSet;
if (update_params == null) update_params = window.update_params;
if (dataSet) {
if (update_params) {
console.log("Running page update with dataSet: " + dataSet + " and params: " + update_params);
console.log("Running page update with dataSet: " + dataSet + " and params: " + update_params + ". Initial: " + initial);
$.ajax({
url: "get_data.html",
type: "GET",
data: { dataSet : dataSet, params: update_params
},
contentType: "application/json; charset=utf-8",
success: function (response) {
handleUpdatedData(response, dataSet, update_params);
handleUpdatedData(response, dataSet, update_params, initial);
},
error: function () {
console.warn("Error - while getting updated data for dataSet: "+dataSet)
}
});
} else {
console.log("Running page update with dataSet: " + dataSet);
console.log("Running page update with dataSet: " + dataSet + ". Initial: " + initial);
$.ajax({
url: "get_data.html",
type: "GET",
data: { dataSet : dataSet
},
contentType: "application/json; charset=utf-8",
success: function (response) {
handleUpdatedData(response, dataSet);
handleUpdatedData(response, dataSet, null, initial);
},
error: function () {
console.warn("Error - while getting updated data for dataSet: "+dataSet)
}
});
}
} else {
console.log("Running page update.");
console.log("Running page update. Initial: " + initial);
$.ajax({
url: "get_data.html",
type: "GET",
contentType: "application/json; charset=utf-8",
success: function (response) {
handleUpdatedData(response);
handleUpdatedData(response, null, null, initial);
},
error: function () {
console.warn("Error - while getting updated data")
Expand Down

0 comments on commit d908bb2

Please sign in to comment.