mirror of
https://github.com/grocy/grocy.git
synced 2025-08-13 09:17:26 +00:00
Added a "keep screen on" option using NoSleep.js (closes #427)
This commit is contained in:
72
public/js/grocy_wakelockhandling.js
Normal file
72
public/js/grocy_wakelockhandling.js
Normal file
@@ -0,0 +1,72 @@
|
||||
Grocy.WakeLock = { };
|
||||
Grocy.WakeLock.NoSleepJsIntance = null;
|
||||
Grocy.WakeLock.InitDone = false;
|
||||
|
||||
$("#keep_screen_on").on("change", function()
|
||||
{
|
||||
var value = $(this).is(":checked");
|
||||
if (value)
|
||||
{
|
||||
Grocy.WakeLock.Enable();
|
||||
}
|
||||
else
|
||||
{
|
||||
Grocy.WakeLock.Disable();
|
||||
}
|
||||
});
|
||||
|
||||
Grocy.WakeLock.Enable = function()
|
||||
{
|
||||
if (Grocy.WakeLock.NoSleepJsIntance === null)
|
||||
{
|
||||
Grocy.WakeLock.NoSleepJsIntance = new NoSleep();
|
||||
}
|
||||
Grocy.WakeLock.NoSleepJsIntance.enable();
|
||||
Grocy.WakeLock.InitDone = true;
|
||||
}
|
||||
|
||||
Grocy.WakeLock.Disable = function()
|
||||
{
|
||||
if (Grocy.WakeLock.NoSleepJsIntance !== null)
|
||||
{
|
||||
Grocy.WakeLock.NoSleepJsIntance.disable();
|
||||
}
|
||||
}
|
||||
|
||||
// Handle "Keep screen on while displaying a fullscreen-card" when the body class "fullscreen-card" has changed
|
||||
new MutationObserver(function(mutations)
|
||||
{
|
||||
if (BoolVal(Grocy.UserSettings.keep_screen_on_when_fullscreen_card) && !BoolVal(Grocy.UserSettings.keep_screen_on))
|
||||
{
|
||||
mutations.forEach(function(mutation)
|
||||
{
|
||||
if (mutation.attributeName === "class")
|
||||
{
|
||||
var attributeValue = $(mutation.target).prop(mutation.attributeName);
|
||||
if (attributeValue.contains("fullscreen-card"))
|
||||
{
|
||||
Grocy.WakeLock.Enable();
|
||||
}
|
||||
else
|
||||
{
|
||||
Grocy.WakeLock.Disable();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}).observe(document.body, {
|
||||
attributes: true
|
||||
});
|
||||
|
||||
// Enabling NoSleep.Js only works in a user input event handler,
|
||||
// so if the user wants to keep the screen on always,
|
||||
// do this in on the first click on anything
|
||||
$(document).click(function()
|
||||
{
|
||||
if (Grocy.WakeLock.InitDone === false && BoolVal(Grocy.UserSettings.keep_screen_on))
|
||||
{
|
||||
Grocy.WakeLock.Enable();
|
||||
}
|
||||
|
||||
Grocy.WakeLock.InitDone = true;
|
||||
});
|
Reference in New Issue
Block a user