@plattar/plattar-ar-adapter 1.154.2 → 1.155.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,219 +2,210 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ConfiguratorController = void 0;
4
4
  const plattar_api_1 = require("@plattar/plattar-api");
5
- const plattar_services_1 = require("@plattar/plattar-services");
6
- const raw_ar_1 = require("../../ar/raw-ar");
7
- const scene_ar_1 = require("../../ar/scene-ar");
8
5
  const scene_product_ar_1 = require("../../ar/scene-product-ar");
9
- const configurator_state_1 = require("../../util/configurator-state");
10
6
  const util_1 = require("../../util/util");
11
7
  const plattar_controller_1 = require("./plattar-controller");
8
+ const configurator_ar_1 = require("../../ar/configurator-ar");
12
9
  /**
13
10
  * Manages an instance of the <plattar-configurator> HTML Element
14
11
  */
15
12
  class ConfiguratorController extends plattar_controller_1.PlattarController {
16
- constructor(parent) {
17
- super(parent);
13
+ constructor() {
14
+ super(...arguments);
15
+ this._cachedConfigState = null;
18
16
  }
19
- onAttributesUpdated() {
17
+ async getConfiguratorState() {
18
+ if (this._cachedConfigState) {
19
+ return this._cachedConfigState;
20
+ }
21
+ this._cachedConfigState = this.createConfiguratorState();
22
+ return this._cachedConfigState;
23
+ }
24
+ async onAttributesUpdated(attributeName) {
20
25
  const state = this._state;
26
+ if (state === plattar_controller_1.ControllerState.Renderer) {
27
+ const viewer = this.element;
28
+ if (viewer) {
29
+ if (attributeName === "variation-id") {
30
+ const variationIDs = this.getAttribute("variation-id");
31
+ const variationIDsList = variationIDs ? variationIDs.split(",") : [];
32
+ if (variationIDsList.length > 0) {
33
+ await viewer.messenger.selectVariationID(variationIDsList);
34
+ }
35
+ }
36
+ if (attributeName === "variation-sku") {
37
+ const variationSKUs = this.getAttribute("variation-sku");
38
+ const variationSKUList = variationSKUs ? variationSKUs.split(",") : [];
39
+ if (variationSKUList.length > 0) {
40
+ await viewer.messenger.selectVariationSKU(variationSKUList);
41
+ }
42
+ }
43
+ }
44
+ return;
45
+ }
21
46
  // re-render the QR Code when attributes have changed
22
47
  if (state === plattar_controller_1.ControllerState.QRCode) {
48
+ if (attributeName === "variation-id") {
49
+ const configState = await this.getConfiguratorState();
50
+ const variationIDs = this.getAttribute("variation-id");
51
+ const variationIDsList = variationIDs ? variationIDs.split(",") : [];
52
+ variationIDsList.forEach((variationID) => {
53
+ configState.state.setVariationID(variationID);
54
+ });
55
+ }
56
+ if (attributeName === "variation-sku") {
57
+ const configState = await this.getConfiguratorState();
58
+ const variationSKUs = this.getAttribute("variation-sku");
59
+ const variationSKUList = variationSKUs ? variationSKUs.split(",") : [];
60
+ variationSKUList.forEach((variationSKU) => {
61
+ configState.state.setVariationSKU(variationSKU);
62
+ });
63
+ }
23
64
  this.startQRCode(this._prevQROpt);
24
65
  return;
25
66
  }
26
67
  }
27
- startViewerQRCode(options) {
68
+ async startViewerQRCode(options) {
69
+ // remove the old renderer instance if any
70
+ this.removeRenderer();
71
+ const sceneID = this.getAttribute("scene-id");
72
+ if (!sceneID) {
73
+ throw new Error("ConfiguratorController.startQRCode() - minimum required attributes not set, use scene-id as a minimum");
74
+ }
75
+ const opt = options || this._GetDefaultQROptions();
76
+ const viewer = document.createElement("plattar-qrcode");
77
+ this._element = viewer;
78
+ // required attributes with defaults for plattar-viewer node
79
+ const width = this.getAttribute("width") || "500px";
80
+ const height = this.getAttribute("height") || "500px";
81
+ viewer.setAttribute("width", width);
82
+ viewer.setAttribute("height", height);
83
+ if (opt.color) {
84
+ viewer.setAttribute("color", opt.color);
85
+ }
86
+ if (opt.margin) {
87
+ viewer.setAttribute("margin", "" + opt.margin);
88
+ }
89
+ if (opt.qrType) {
90
+ viewer.setAttribute("qr-type", opt.qrType);
91
+ }
92
+ viewer.setAttribute("shorten", (opt.shorten && (opt.shorten === true || opt.shorten === "true")) ? "true" : "false");
93
+ let dst = plattar_api_1.Server.location().base + "renderer/configurator.html?scene_id=" + sceneID;
94
+ // optional attributes
95
+ let configState = null;
96
+ const showAR = this.getAttribute("show-ar");
97
+ const showUI = this.getAttribute("show-ui");
98
+ try {
99
+ configState = (await this.getConfiguratorState()).state.encode();
100
+ }
101
+ catch (_err) {
102
+ // config state is not available
103
+ configState = null;
104
+ }
105
+ if (showUI && showUI === "true") {
106
+ dst = plattar_api_1.Server.location().base + "configurator/dist/index.html?scene_id=" + sceneID;
107
+ }
108
+ if (configState) {
109
+ dst += "&config_state=" + configState;
110
+ }
111
+ if (showAR) {
112
+ dst += "&show_ar=" + showAR;
113
+ }
114
+ viewer.setAttribute("url", opt.url || dst);
115
+ this._state = plattar_controller_1.ControllerState.QRCode;
116
+ this._prevQROpt = opt;
28
117
  return new Promise((accept, reject) => {
29
- // remove the old renderer instance if any
30
- this.removeRenderer();
31
- const sceneID = this.getAttribute("scene-id");
32
- if (sceneID) {
33
- const opt = options || this._GetDefaultQROptions();
34
- const viewer = document.createElement("plattar-qrcode");
35
- // required attributes with defaults for plattar-viewer node
36
- const width = this.getAttribute("width") || "500px";
37
- const height = this.getAttribute("height") || "500px";
38
- viewer.setAttribute("width", width);
39
- viewer.setAttribute("height", height);
40
- if (opt.color) {
41
- viewer.setAttribute("color", opt.color);
42
- }
43
- if (opt.margin) {
44
- viewer.setAttribute("margin", "" + opt.margin);
45
- }
46
- if (opt.qrType) {
47
- viewer.setAttribute("qr-type", opt.qrType);
48
- }
49
- viewer.setAttribute("shorten", (opt.shorten && (opt.shorten === true || opt.shorten === "true")) ? "true" : "false");
50
- let dst = plattar_api_1.Server.location().base + "renderer/configurator.html?scene_id=" + sceneID;
51
- // optional attributes
52
- const configState = this.getAttribute("config-state");
53
- const showAR = this.getAttribute("show-ar");
54
- const showUI = this.getAttribute("show-ui");
55
- if (showUI && showUI === "true") {
56
- dst = plattar_api_1.Server.location().base + "configurator/dist/index.html?scene_id=" + sceneID;
57
- }
58
- if (configState) {
59
- dst += "&config_state=" + configState;
60
- }
61
- if (showAR) {
62
- dst += "&show_ar=" + showAR;
63
- }
64
- viewer.setAttribute("url", opt.url || dst);
65
- viewer.onload = () => {
66
- return accept(viewer);
67
- };
68
- this.append(viewer);
69
- this._element = viewer;
70
- this._state = plattar_controller_1.ControllerState.QRCode;
71
- this._prevQROpt = opt;
72
- return;
73
- }
74
- return reject(new Error("ConfiguratorController.startQRCode() - minimum required attributes not set, use scene-id as a minimum"));
118
+ viewer.onload = () => {
119
+ return accept(viewer);
120
+ };
121
+ this.append(viewer);
75
122
  });
76
123
  }
77
- startRenderer() {
124
+ async startRenderer() {
125
+ // remove the old renderer instance if any
126
+ this.removeRenderer();
127
+ const sceneID = this.getAttribute("scene-id");
128
+ if (!sceneID) {
129
+ throw new Error("ConfiguratorController.startRenderer() - minimum required attributes not set, use scene-id as a minimum");
130
+ }
131
+ // required attributes with defaults for plattar-configurator node
132
+ const width = this.getAttribute("width") || "500px";
133
+ const height = this.getAttribute("height") || "500px";
134
+ const server = this.getAttribute("server") || "production";
135
+ const viewer = document.createElement("plattar-configurator");
136
+ this._element = viewer;
137
+ viewer.setAttribute("width", width);
138
+ viewer.setAttribute("height", height);
139
+ viewer.setAttribute("server", server);
140
+ viewer.setAttribute("scene-id", sceneID);
141
+ // optional attributes
142
+ let configState = null;
143
+ try {
144
+ configState = await this.getConfiguratorState();
145
+ }
146
+ catch (_err) {
147
+ // config state is not available
148
+ configState = null;
149
+ }
150
+ const showAR = this.getAttribute("show-ar");
151
+ const showUI = this.getAttribute("show-ui");
152
+ if (configState) {
153
+ viewer.setAttribute("config-state", configState.state.encode());
154
+ }
155
+ if (showAR) {
156
+ viewer.setAttribute("show-ar", showAR);
157
+ }
158
+ if (showUI) {
159
+ viewer.setAttribute("show-ui", showUI);
160
+ }
161
+ this._state = plattar_controller_1.ControllerState.Renderer;
78
162
  return new Promise((accept, reject) => {
79
- // remove the old renderer instance if any
80
- this.removeRenderer();
81
- const sceneID = this.getAttribute("scene-id");
82
- if (sceneID) {
83
- // required attributes with defaults for plattar-configurator node
84
- const width = this.getAttribute("width") || "500px";
85
- const height = this.getAttribute("height") || "500px";
86
- const server = this.getAttribute("server") || "production";
87
- const viewer = document.createElement("plattar-configurator");
88
- viewer.setAttribute("width", width);
89
- viewer.setAttribute("height", height);
90
- viewer.setAttribute("server", server);
91
- viewer.setAttribute("scene-id", sceneID);
92
- // optional attributes
93
- const configState = this.getAttribute("config-state");
94
- const showAR = this.getAttribute("show-ar");
95
- const showUI = this.getAttribute("show-ui");
96
- if (configState) {
97
- viewer.setAttribute("config-state", configState);
98
- }
99
- if (showAR) {
100
- viewer.setAttribute("show-ar", showAR);
101
- }
102
- if (showUI) {
103
- viewer.setAttribute("show-ui", showUI);
104
- }
105
- viewer.onload = () => {
106
- return accept(viewer);
107
- };
108
- this.append(viewer);
109
- this._element = viewer;
110
- this._state = plattar_controller_1.ControllerState.Renderer;
111
- return;
163
+ this.append(viewer);
164
+ if (configState) {
165
+ this.setupMessengerObservers(viewer, configState);
112
166
  }
113
- return reject(new Error("ConfiguratorController.startRenderer() - minimum required attributes not set, use scene-id as a minimum"));
167
+ return accept(viewer);
114
168
  });
115
169
  }
116
- initAR() {
117
- return new Promise((accept, reject) => {
118
- if (!util_1.Util.canAugment()) {
119
- return reject(new Error("ConfiguratorController.initAR() - cannot proceed as AR not available in context"));
120
- }
121
- const arMode = this.getAttribute("ar-mode") || "generated";
122
- switch (arMode.toLowerCase()) {
123
- case "inherited":
124
- this._InitARInherited(accept, reject);
125
- return;
126
- case "generated":
127
- default:
128
- this._InitARGenerated(accept, reject);
129
- }
130
- });
170
+ async initAR() {
171
+ if (!util_1.Util.canAugment()) {
172
+ throw new Error("ConfiguratorController.initAR() - cannot proceed as AR not available in context");
173
+ }
174
+ const arMode = this.getAttribute("ar-mode") || "generated";
175
+ switch (arMode.toLowerCase()) {
176
+ case "inherited":
177
+ return this._InitARInherited();
178
+ case "generated":
179
+ default:
180
+ return this._InitARGenerated();
181
+ }
131
182
  }
132
183
  /**
133
184
  * Private Function - This launches the Static/Inherited AR Mode
134
185
  */
135
- _InitARInherited(accept, reject) {
186
+ async _InitARInherited() {
136
187
  const sceneID = this.getAttribute("scene-id");
137
- const configState = this.getAttribute("config-state");
138
- // use config-state if its available
139
- if (sceneID && configState) {
140
- const state = configurator_state_1.ConfiguratorState.decode(configState);
141
- const first = state.first();
142
- if (first) {
143
- const sceneProductAR = new scene_product_ar_1.SceneProductAR(first.scene_product_id, first.product_variation_id);
144
- sceneProductAR.init().then(accept).catch(reject);
145
- return;
146
- }
147
- return reject(new Error("ConfiguratorController.initAR() - invalid config-state does not have any product states"));
148
- }
149
- // otherwise fallback to using scene
150
- if (sceneID) {
151
- configurator_state_1.ConfiguratorState.decodeScene(sceneID).then((state) => {
152
- const first = state.first();
153
- if (first) {
154
- const sceneProductAR = new scene_product_ar_1.SceneProductAR(first.scene_product_id, first.product_variation_id);
155
- return sceneProductAR.init().then(accept).catch(reject);
156
- }
157
- return reject(new Error("ConfiguratorController.initAR() - invalid Scene does not have any product states"));
158
- }).catch(reject);
159
- return;
188
+ if (!sceneID) {
189
+ throw new Error("ConfiguratorController.initAR() - inherited AR minimum required attributes not set, use scene-id as a minimum");
160
190
  }
161
- return reject(new Error("ConfiguratorController.initAR() - minimum required attributes not set, use scene-id as a minimum"));
191
+ const state = (await this.getConfiguratorState()).state;
192
+ const first = state.first();
193
+ if (first) {
194
+ const sceneProductAR = new scene_product_ar_1.SceneProductAR(first.scene_product_id, first.product_variation_id);
195
+ return sceneProductAR.init();
196
+ }
197
+ throw new Error("ConfiguratorController.initAR() - invalid decoded config-state does not have any product states");
162
198
  }
163
199
  /**
164
200
  * Private Function - This launches the Dynamic/Generated AR Mode
165
201
  */
166
- _InitARGenerated(accept, reject) {
167
- // if scene ID is available and the state is a configurator viewer
168
- // we can use the real-time configurator state to launch the AR view
169
- const viewer = this.element;
202
+ async _InitARGenerated() {
170
203
  const sceneID = this.getAttribute("scene-id");
171
- if (viewer && sceneID && viewer.messenger) {
172
- let output = "glb";
173
- if (util_1.Util.isSafari() || util_1.Util.isChromeOnIOS()) {
174
- output = "usdz";
175
- }
176
- viewer.messenger.getARFile(output).then((result) => {
177
- const rawAR = new raw_ar_1.RawAR(result.filename, sceneID);
178
- return rawAR.init().then(accept).catch(reject);
179
- }).catch(reject);
180
- return;
181
- }
182
- const configState = this.getAttribute("config-state");
183
- // otherwise scene ID is available to the viewer is not launched
184
- // we can use the static configuration state to launch the AR view
185
- if (sceneID && configState) {
186
- const state = configurator_state_1.ConfiguratorState.decode(configState);
187
- if (state.length > 0) {
188
- const server = this.getAttribute("server") || "production";
189
- const configurator = new plattar_services_1.Configurator();
190
- configurator.server = server;
191
- if (util_1.Util.isSafari() || util_1.Util.isChromeOnIOS()) {
192
- configurator.output = "usdz";
193
- }
194
- if (util_1.Util.canSceneViewer()) {
195
- configurator.output = "glb";
196
- }
197
- state.forEach((productState) => {
198
- if (productState.meta_data.augment === true) {
199
- configurator.addSceneProduct(productState.scene_product_id, productState.product_variation_id);
200
- }
201
- });
202
- configurator.get().then((result) => {
203
- const rawAR = new raw_ar_1.RawAR(result.filename, sceneID);
204
- rawAR.init().then(accept).catch(reject);
205
- }).catch(reject);
206
- return;
207
- }
208
- return reject(new Error("ConfiguratorController.initAR() - invalid config-state does not have any product states"));
209
- }
210
- // otherwise no config-state or viewer is active
211
- // fallback to using default SceneAR implementation
212
- if (sceneID) {
213
- const sceneAR = new scene_ar_1.SceneAR(sceneID);
214
- sceneAR.init().then(accept).catch(reject);
215
- return;
204
+ if (!sceneID) {
205
+ throw new Error("VTOController.initAR() - generated AR minimum required attributes not set, use scene-id as a minimum");
216
206
  }
217
- return reject(new Error("ConfiguratorController.initAR() - minimum required attributes not set, use scene-id as a minimum"));
207
+ const configAR = new configurator_ar_1.ConfiguratorAR(await this.getConfiguratorState());
208
+ return configAR.init();
218
209
  }
219
210
  removeRenderer() {
220
211
  if (this._element) {
@@ -222,6 +213,7 @@ class ConfiguratorController extends plattar_controller_1.PlattarController {
222
213
  this._element = null;
223
214
  return true;
224
215
  }
216
+ this.removeMessengerObservers();
225
217
  return false;
226
218
  }
227
219
  get element() {
@@ -1,4 +1,5 @@
1
1
  import { LauncherAR } from "../../ar/launcher-ar";
2
+ import { DecodedConfiguratorState } from "../../util/configurator-state";
2
3
  export declare enum ControllerState {
3
4
  None = 0,
4
5
  Renderer = 1,
@@ -16,16 +17,35 @@ export declare abstract class PlattarController {
16
17
  protected _state: ControllerState;
17
18
  protected _element: HTMLElement | null;
18
19
  protected _prevQROpt: any;
20
+ private _selectVariationObserver;
21
+ private _selectVariationSKUObserver;
19
22
  constructor(parent: HTMLElement);
23
+ /**
24
+ * Generates a brand new Configurator State from the provided SceneID or inputted Configurator State
25
+ */
26
+ protected createConfiguratorState(): Promise<DecodedConfiguratorState>;
27
+ /**
28
+ * Generates and returns a new instance of the ConfiguratorState for this node
29
+ */
30
+ abstract getConfiguratorState(): Promise<DecodedConfiguratorState>;
20
31
  /**
21
32
  * Called by the parent when a HTML Attribute has changed and the controller
22
33
  * requires an update
23
34
  */
24
- abstract onAttributesUpdated(): void;
35
+ abstract onAttributesUpdated(attributeName: string): Promise<void>;
25
36
  /**
26
37
  * Start the underlying Plattar Renderer for this Controller
27
38
  */
28
39
  abstract startRenderer(): Promise<HTMLElement>;
40
+ /**
41
+ * Setup messenger observers to detect variation changes and apply to the internal
42
+ * configuration state
43
+ */
44
+ protected setupMessengerObservers(viewer: any, configState: DecodedConfiguratorState): void;
45
+ /**
46
+ * Remove all pre-existing observers
47
+ */
48
+ protected removeMessengerObservers(): void;
29
49
  /**
30
50
  * Initialise and start AR mode if available
31
51
  */