2024-02-12 20:28:38 +01:00
|
|
|
import SVGRenderer from '/src/SVGRenderer.mjs'
|
|
|
|
|
2024-12-03 21:32:18 +01:00
|
|
|
// 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 */ `
|
2024-12-03 21:32:18 +01:00
|
|
|
<style> @import url("/src/PreviewBox.css"); </style>
|
|
|
|
<div id="box"></div>`
|
2024-02-12 20:28:38 +01:00
|
|
|
|
2024-12-03 21:32:18 +01:00
|
|
|
// Define a custom PreviewBox class that extends HTMLElement
|
2024-02-12 20:28:38 +01:00
|
|
|
class PreviewBox extends HTMLElement {
|
2024-12-03 21:32:18 +01:00
|
|
|
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
|
|
|
|
2024-12-03 21:32:18 +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
|
|
|
|
2024-12-03 21:32:18 +01:00
|
|
|
/**
|
|
|
|
* Creates an instance of PreviewBox and initializes the component.
|
|
|
|
*/
|
2024-02-12 20:28:38 +01:00
|
|
|
constructor() {
|
2024-12-03 21:32:18 +01:00
|
|
|
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' })
|
2024-12-03 21:32:18 +01:00
|
|
|
// Append the cloned template content to the shadow root
|
2024-02-12 20:28:38 +01:00
|
|
|
this.shadowRoot.append(template.content.cloneNode(true))
|
|
|
|
|
2024-12-03 21:32:18 +01:00
|
|
|
// Get the root node of the shadow DOM
|
2024-02-12 20:28:38 +01:00
|
|
|
this.#root = this.shadowRoot.getRootNode()
|
2024-12-03 21:32:18 +01:00
|
|
|
// Get the box element from the shadow DOM
|
2024-02-12 20:28:38 +01:00
|
|
|
this.#box = this.#root.getElementById('box')
|
|
|
|
|
2024-12-03 21:32:18 +01:00
|
|
|
// 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'))
|
|
|
|
}
|
|
|
|
|
2024-12-03 21:32:18 +01:00
|
|
|
/**
|
|
|
|
* 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']
|
|
|
|
}
|
|
|
|
|
2024-12-03 21:32:18 +01:00
|
|
|
/**
|
|
|
|
* 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':
|
2024-12-03 21:32:18 +01:00
|
|
|
this[name] = newValue // Update the value property
|
2024-02-12 20:28:38 +01:00
|
|
|
break
|
|
|
|
case 'size':
|
2024-12-03 21:32:18 +01:00
|
|
|
this[name] = parseFloat(newValue) // Update the size property
|
2024-02-12 20:28:38 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2024-12-03 21:32:18 +01:00
|
|
|
this.#render() // Re-render the QR code
|
2024-02-12 20:28:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-03 21:32:18 +01:00
|
|
|
/**
|
|
|
|
* Public method to update the QR code rendering.
|
|
|
|
*/
|
2024-02-12 20:28:38 +01:00
|
|
|
update() {
|
|
|
|
this.#render()
|
|
|
|
}
|
|
|
|
|
2024-12-03 21:32:18 +01:00
|
|
|
/**
|
|
|
|
* Clears the content of the box element.
|
|
|
|
* @private
|
|
|
|
*/
|
2024-02-12 20:28:38 +01:00
|
|
|
#clear() {
|
2024-12-03 21:32:18 +01:00
|
|
|
while (this.#box.firstChild) {
|
|
|
|
this.#box.removeChild(this.#box.firstChild)
|
2024-02-12 20:28:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-03 21:32:18 +01:00
|
|
|
/**
|
|
|
|
* Renders the QR code inside the box element.
|
|
|
|
* @private
|
|
|
|
*/
|
2024-02-12 20:28:38 +01:00
|
|
|
#render() {
|
2024-12-03 21:32:18 +01:00
|
|
|
// Cancel any pending animation frame
|
2024-02-12 20:28:38 +01:00
|
|
|
window.cancelAnimationFrame(this.#animationId)
|
2024-12-03 21:32:18 +01:00
|
|
|
// Schedule the rendering in the next animation frame
|
2024-02-12 20:28:38 +01:00
|
|
|
this.#animationId = window.requestAnimationFrame(() => {
|
2024-12-03 21:32:18 +01:00
|
|
|
this.#clear() // Clear existing content
|
2024-02-12 20:28:38 +01:00
|
|
|
const time = new Date()
|
|
|
|
|
2024-12-03 21:32:18 +01:00
|
|
|
// 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')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-03 21:32:18 +01:00
|
|
|
// 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)
|