2017-10-29 20:01:13 +00:00
|
|
|
/* Baby Buddy Dashboard
|
|
|
|
*
|
|
|
|
* Provides a "watch" function to update the dashboard at one minute intervals
|
|
|
|
* and/or on visibility state changes.
|
|
|
|
*/
|
2024-02-06 08:26:41 +00:00
|
|
|
BabyBuddy.Dashboard = (function ($) {
|
|
|
|
var runIntervalId = null;
|
|
|
|
var dashboardElement = null;
|
|
|
|
var hidden = null;
|
2017-10-29 20:01:13 +00:00
|
|
|
|
2024-02-06 08:26:41 +00:00
|
|
|
var Dashboard = {
|
|
|
|
watch: function (element_id, refresh_rate) {
|
|
|
|
dashboardElement = $("#" + element_id);
|
2017-10-29 20:01:13 +00:00
|
|
|
|
2024-02-06 08:26:41 +00:00
|
|
|
if (dashboardElement.length == 0) {
|
|
|
|
console.error("Baby Buddy: Dashboard element not found.");
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-29 20:01:13 +00:00
|
|
|
|
2024-02-06 08:26:41 +00:00
|
|
|
if (typeof document.hidden !== "undefined") {
|
|
|
|
hidden = "hidden";
|
|
|
|
} else if (typeof document.msHidden !== "undefined") {
|
|
|
|
hidden = "msHidden";
|
|
|
|
} else if (typeof document.webkitHidden !== "undefined") {
|
|
|
|
hidden = "webkitHidden";
|
|
|
|
}
|
2017-10-29 20:01:13 +00:00
|
|
|
|
2024-02-06 08:26:41 +00:00
|
|
|
if (
|
|
|
|
typeof window.addEventListener === "undefined" ||
|
|
|
|
typeof document.hidden === "undefined"
|
|
|
|
) {
|
|
|
|
if (refresh_rate) {
|
|
|
|
runIntervalId = setInterval(this.update, refresh_rate);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
window.addEventListener(
|
|
|
|
"focus",
|
|
|
|
Dashboard.handleVisibilityChange,
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
if (refresh_rate) {
|
|
|
|
runIntervalId = setInterval(
|
|
|
|
Dashboard.handleVisibilityChange,
|
|
|
|
refresh_rate,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2018-03-23 03:29:40 +00:00
|
|
|
|
2024-02-06 08:26:41 +00:00
|
|
|
handleVisibilityChange: function () {
|
|
|
|
if (!document[hidden]) {
|
|
|
|
Dashboard.update();
|
|
|
|
}
|
|
|
|
},
|
2017-10-29 20:01:13 +00:00
|
|
|
|
2024-02-06 08:26:41 +00:00
|
|
|
update: function () {
|
|
|
|
// TODO: Someday maybe update in place?
|
|
|
|
location.reload();
|
|
|
|
},
|
|
|
|
};
|
2017-10-29 20:01:13 +00:00
|
|
|
|
2024-02-06 08:26:41 +00:00
|
|
|
return Dashboard;
|
|
|
|
})(jQuery);
|