107 lines
3.5 KiB
JavaScript
Raw Normal View History

2024-02-12 20:28:38 +01:00
import SVGRenderer from '/src/SVGRenderer.mjs'
// Create a template element to define the structure of the preview box component
2024-02-12 20:28:38 +01:00
const template = document.createElement('template')
template.innerHTML = /* html */ `
<style> @import url("/src/PreviewBox.css"); </style>
<div id="box"></div>`
2024-02-12 20:28:38 +01:00
// Define a custom PreviewBox class that extends HTMLElement
2024-02-12 20:28:38 +01:00
class PreviewBox extends HTMLElement {
value = '' // The value to be encoded in the QR code
size = 0 // The size of the QR code
2024-02-12 20:28:38 +01:00
#root = null // Private property to store the root node
#box = null // Private property to store the box element
#animationId = 0 // Private property for requestAnimationFrame
2024-02-12 20:28:38 +01:00
/**
* Creates an instance of PreviewBox and initializes the component.
*/
2024-02-12 20:28:38 +01:00
constructor() {
super() // Call the parent class constructor
// Attach a shadow DOM tree to this element
2024-02-12 20:28:38 +01:00
this.attachShadow({ mode: 'open' })
// Append the cloned template content to the shadow root
2024-02-12 20:28:38 +01:00
this.shadowRoot.append(template.content.cloneNode(true))
// Get the root node of the shadow DOM
2024-02-12 20:28:38 +01:00
this.#root = this.shadowRoot.getRootNode()
// Get the box element from the shadow DOM
2024-02-12 20:28:38 +01:00
this.#box = this.#root.getElementById('box')
// Initialize value and size from attributes
2024-02-12 20:28:38 +01:00
this.value = this.getAttribute('value') || ''
this.size = parseFloat(this.getAttribute('size'))
}
/**
* Observed attributes for the custom element.
* @returns {Array<string>} - The list of attributes to observe.
*/
2024-02-12 20:28:38 +01:00
static get observedAttributes() {
return ['value', 'size']
}
/**
* Called when one of the observed attributes changes.
* @param {string} name - The name of the attribute that changed.
* @param {string} oldValue - The old value of the attribute.
* @param {string} newValue - The new value of the attribute.
*/
2024-02-12 20:28:38 +01:00
attributeChangedCallback(name, oldValue, newValue) {
if (newValue != oldValue) {
switch (name) {
case 'value':
this[name] = newValue // Update the value property
2024-02-12 20:28:38 +01:00
break
case 'size':
this[name] = parseFloat(newValue) // Update the size property
2024-02-12 20:28:38 +01:00
break
}
this.#render() // Re-render the QR code
2024-02-12 20:28:38 +01:00
}
}
/**
* Public method to update the QR code rendering.
*/
2024-02-12 20:28:38 +01:00
update() {
this.#render()
}
/**
* Clears the content of the box element.
* @private
*/
2024-02-12 20:28:38 +01:00
#clear() {
while (this.#box.firstChild) {
this.#box.removeChild(this.#box.firstChild)
2024-02-12 20:28:38 +01:00
}
}
/**
* Renders the QR code inside the box element.
* @private
*/
2024-02-12 20:28:38 +01:00
#render() {
// Cancel any pending animation frame
2024-02-12 20:28:38 +01:00
window.cancelAnimationFrame(this.#animationId)
// Schedule the rendering in the next animation frame
2024-02-12 20:28:38 +01:00
this.#animationId = window.requestAnimationFrame(() => {
this.#clear() // Clear existing content
2024-02-12 20:28:38 +01:00
const time = new Date()
// Generate the QR code SVG and insert it into the box
2024-02-12 20:28:38 +01:00
this.#box.innerHTML = SVGRenderer.getCode(this.value, `${this.size}mm`)
console.log('QRCode generation time: ' + (new Date() - time) + ' ms')
})
}
}
// Define the custom element 'fabaccess-preview-box' associated with the PreviewBox class
2024-02-12 20:28:38 +01:00
customElements.define('fabaccess-preview-box', PreviewBox)