@bitblit/asyncglk
Version:
A Typescript Glk library
65 lines (61 loc) • 2.06 kB
JavaScript
/*
Web GlkOte shared things
========================
Copyright (c) 2022 Dannii Willis
MIT licenced
https://github.com/curiousdannii/asyncglk
*/
import * as protocol from '../../common/protocol.js';
/** Create an element with specified classes */
export function create(tag, className) {
return $(`<${tag}>`, { 'class': className });
}
/** Manage the DOM elements used by GlkOte */
export class DOM {
context_element;
errorcontent_id;
errorpane_id;
gameport_id;
loadingpane_id;
prefix;
windowport_id;
constructor(options) {
this.context_element = options.context_element;
this.errorcontent_id = options.errorcontent_id;
this.errorpane_id = options.errorpane_id;
this.gameport_id = options.gameport_id;
this.loadingpane_id = options.loadingpane_id;
this.prefix = options.prefix;
this.windowport_id = options.windowport_id;
}
/** Create an element, adding prefix to the ID */
create(tag, id, props) {
props ??= {};
// Handle class name as props
if (typeof props === 'string') {
props = {
class: props,
};
}
props.id = this.prefix + id;
return $(`<${tag}>`, props);
}
gameport() {
return $(`#${this.gameport_id}`, this.context_element);
}
/** Return a jQuery search for an element ID, using prefix and context_element */
id(id) {
return $(`#${this.prefix}${id}`, this.context_element);
}
windowport() {
return $(`#${this.windowport_id}`, this.context_element);
}
}
/** Is any input element focused? */
export function is_input_focused() {
const activeElement_tagName = document.activeElement?.tagName;
return activeElement_tagName === 'INPUT' || activeElement_tagName === 'TEXTAREA';
}
/** Try to detect iOS */
// From https://stackoverflow.com/a/58065241/2854284
export const is_iOS = /iPad|iPhone|iPod/.test(navigator.platform) || (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1);