@plattar/plattar-ar-adapter
Version:
Plattar AR Adapter for interfacing with Google & Apple WebAR
121 lines (120 loc) • 5.11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GalleryController = void 0;
const plattar_api_1 = require("@plattar/plattar-api");
const plattar_controller_1 = require("./plattar-controller");
/**
* Manages an instance of the <plattar-configurator> HTML Element
*/
class GalleryController extends plattar_controller_1.PlattarController {
constructor() {
super(...arguments);
this._cachedConfigState = null;
}
async getConfiguratorState() {
if (this._cachedConfigState) {
return this._cachedConfigState;
}
this._cachedConfigState = this.createConfiguratorState();
return this._cachedConfigState;
}
async onAttributesUpdated(attributeName) {
const state = this._state;
// re-render the QR Code when attributes have changed
if (state === plattar_controller_1.ControllerState.QRCode) {
if (attributeName === "variation-id") {
const configState = await this.getConfiguratorState();
const variationIDs = this.getAttribute("variation-id");
const variationIDsList = variationIDs ? variationIDs.split(",") : [];
variationIDsList.forEach((variationID) => {
configState.state.setVariationID(variationID);
});
}
if (attributeName === "variation-sku") {
const configState = await this.getConfiguratorState();
const variationSKUs = this.getAttribute("variation-sku");
const variationSKUList = variationSKUs ? variationSKUs.split(",") : [];
variationSKUList.forEach((variationSKU) => {
configState.state.setVariationSKU(variationSKU);
});
}
this.startQRCode(this._prevQROpt);
return;
}
}
async startViewerQRCode(options) {
const opt = this._GetDefaultQROptions(options);
// remove the old renderer instance if any
if (!opt.detached) {
this.removeRenderer();
}
const sceneID = this.getAttribute("scene-id");
if (!sceneID) {
throw new Error("GalleryController.startViewerQRCode() - minimum required attributes not set, use scene-id as a minimum");
}
const viewer = document.createElement("plattar-qrcode");
if (!opt.detached) {
this._element = viewer;
}
// required attributes with defaults for plattar-viewer node
const width = this.getAttribute("width") || "500px";
const height = this.getAttribute("height") || "500px";
viewer.setAttribute("width", width);
viewer.setAttribute("height", height);
if (opt.color) {
viewer.setAttribute("color", opt.color);
}
if (opt.margin) {
viewer.setAttribute("margin", "" + opt.margin);
}
if (opt.qrType) {
viewer.setAttribute("qr-type", opt.qrType);
}
viewer.setAttribute("shorten", (opt.shorten && (opt.shorten === true || opt.shorten === "true")) ? "true" : "false");
const dst = plattar_api_1.Server.location().base + "renderer/gallery.html?scene_id=" + sceneID;
viewer.setAttribute("url", opt.url || dst);
this._prevQROpt = opt;
if (!opt.detached) {
this._state = plattar_controller_1.ControllerState.QRCode;
return new Promise((accept, reject) => {
viewer.onload = () => {
return accept(viewer);
};
this.append(viewer);
});
}
return new Promise((accept, reject) => {
return accept(viewer);
});
}
async startRenderer() {
// remove the old renderer instance if any
this.removeRenderer();
const sceneID = this.getAttribute("scene-id");
if (!sceneID) {
throw new Error("GalleryController.startRenderer() - minimum required attributes not set, use scene-id as a minimum");
}
this._state = plattar_controller_1.ControllerState.Renderer;
// required attributes with defaults for plattar-configurator node
const width = this.getAttribute("width") || "500px";
const height = this.getAttribute("height") || "500px";
const server = this.getAttribute("server") || "production";
const viewer = document.createElement("plattar-gallery");
this._element = viewer;
viewer.setAttribute("width", width);
viewer.setAttribute("height", height);
viewer.setAttribute("server", server);
viewer.setAttribute("scene-id", sceneID);
return new Promise((accept, reject) => {
this.append(viewer);
return accept(viewer);
});
}
async initAR() {
throw new Error("GalleryController.initAR() - cannot proceed as AR not available in gallery context");
}
get element() {
return this._element;
}
}
exports.GalleryController = GalleryController;