Files
wekan/models/lib/fullscreen.js
T
Lauri Ojansivu f25957dcf3 A full screen button in the header bar, to the right of the logo.
It puts the whole page in full screen with the Fullscreen API (the unprefixed one
and the webkit-prefixed one) and takes it out again; the icon follows the
browser's own events, so Esc is understood. The label is the existing Maximize
translation, so no language file changes. It is drawn only where the browser
offers the API and the page may use it: not on iPhone, not in an iframe without
allowfullscreen. Unit tests drive the pure part with fake documents, including a
refusal; a browser spec checks the position, the label, entering, leaving, Esc,
and that nothing is drawn where the API is not enabled, in Chromium and WebKit.

Thanks to xet7 !
2026-11-04 00:27:28 +02:00

56 lines
2.0 KiB
JavaScript

'use strict';
// Full screen for the whole page, with the unprefixed Fullscreen API and the
// webkit-prefixed one older Safari (and iPadOS Safari before 16.4) has. Pure and
// Meteor-free, the document is passed in, so plain Node tests drive it with a
// fake one.
//
// Where it is NOT available the button must not be drawn at all:
// - iPhone Safari (and every iPhone browser, they are all WebKit) has the API for
// video only: document.fullscreenEnabled is false or missing;
// - a page inside an <iframe> without allowfullscreen (a Sandstorm grain, say) has
// fullscreenEnabled false;
// - a very old browser has neither name.
// "Add to Home Screen" (the manifest says display: standalone) is the way there.
function isSupported(doc) {
if (!doc) return false;
return doc.fullscreenEnabled === true || doc.webkitFullscreenEnabled === true;
}
function currentElement(doc) {
return (doc && (doc.fullscreenElement || doc.webkitFullscreenElement)) || null;
}
function isFullscreen(doc) {
return currentElement(doc) !== null;
}
// Enters full screen, or leaves it. Resolves true when the page is in full screen
// afterwards, false when it is not; it never rejects - a browser that refuses (no
// user gesture, a policy) is an answer, not an error to show. The request has to
// come from a click or a key press, which is what the button is.
async function toggle(doc) {
if (!isSupported(doc)) return false;
try {
if (isFullscreen(doc)) {
const exit = doc.exitFullscreen || doc.webkitExitFullscreen;
if (exit) await exit.call(doc);
} else {
const el = doc.documentElement;
const request = el && (el.requestFullscreen || el.webkitRequestFullscreen);
if (request) await request.call(el);
}
} catch (e) {
// refused: the state below says what happened
}
return isFullscreen(doc);
}
// The events a change arrives on, both spellings.
const CHANGE_EVENTS = ['fullscreenchange', 'webkitfullscreenchange'];
module.exports = { isSupported, isFullscreen, toggle, CHANGE_EVENTS };