@plattar/plattar-ar-adapter 1.154.1 → 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,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PlattarController = exports.ControllerState = void 0;
4
4
  const plattar_api_1 = require("@plattar/plattar-api");
5
+ const configurator_state_1 = require("../../util/configurator-state");
5
6
  var ControllerState;
6
7
  (function (ControllerState) {
7
8
  ControllerState[ControllerState["None"] = 0] = "None";
@@ -28,25 +29,88 @@ class PlattarController {
28
29
  this._state = ControllerState.None;
29
30
  this._element = null;
30
31
  this._prevQROpt = null;
32
+ this._selectVariationObserver = null;
33
+ this._selectVariationSKUObserver = null;
31
34
  this._parent = parent;
32
35
  }
33
36
  /**
34
- * Initialise and start AR mode if available
37
+ * Generates a brand new Configurator State from the provided SceneID or inputted Configurator State
35
38
  */
36
- startAR() {
37
- return new Promise((accept, reject) => {
38
- this.initAR().then((launcher) => {
39
- launcher.start();
40
- accept();
41
- }).catch(reject);
39
+ async createConfiguratorState() {
40
+ // get our Scene ID
41
+ const sceneID = this.getAttribute("scene-id");
42
+ if (!sceneID) {
43
+ throw new Error("PlattarController.createConfiguratorState() - cannot create as required attribute scene-id is not defined");
44
+ }
45
+ const configState = this.getAttribute("config-state");
46
+ // get a list of variation ID's to use for initialising
47
+ const variationIDs = this.getAttribute("variation-id");
48
+ // get a list of variation SKU's to use for initialising
49
+ const variationSKUs = this.getAttribute("variation-sku");
50
+ // generate the decoded configurator state
51
+ const decodedState = configState ? await configurator_state_1.ConfiguratorState.decodeState(sceneID, configState) : await configurator_state_1.ConfiguratorState.decodeScene(sceneID);
52
+ // change the ID's and SKU's (if any) of the default configuration state
53
+ const variationIDList = variationIDs ? variationIDs.split(",") : [];
54
+ const variationSKUList = variationSKUs ? variationSKUs.split(",") : [];
55
+ variationIDList.forEach((variationID) => {
56
+ decodedState.state.setVariationID(variationID);
57
+ });
58
+ variationSKUList.forEach((variationSKU) => {
59
+ decodedState.state.setVariationSKU(variationSKU);
60
+ });
61
+ // return fully modified configuration state
62
+ return decodedState;
63
+ }
64
+ /**
65
+ * Setup messenger observers to detect variation changes and apply to the internal
66
+ * configuration state
67
+ */
68
+ setupMessengerObservers(viewer, configState) {
69
+ this._selectVariationObserver = viewer.messengerInstance.observer.subscribe("selectVariation", (cd) => {
70
+ if (cd.type === "call") {
71
+ const args = cd.data[0];
72
+ const variations = args ? (Array.isArray(args) ? args : [args]) : [];
73
+ variations.forEach((variationID) => {
74
+ configState.state.setVariationID(variationID);
75
+ });
76
+ }
77
+ });
78
+ this._selectVariationSKUObserver = viewer.messengerInstance.observer.subscribe("selectVariationSKU", (cd) => {
79
+ if (cd.type === "call") {
80
+ const args = cd.data[0];
81
+ const variations = args ? (Array.isArray(args) ? args : [args]) : [];
82
+ variations.forEach((variationSKU) => {
83
+ configState.state.setVariationSKU(variationSKU);
84
+ });
85
+ }
42
86
  });
43
87
  }
88
+ /**
89
+ * Remove all pre-existing observers
90
+ */
91
+ removeMessengerObservers() {
92
+ if (this._selectVariationObserver) {
93
+ this._selectVariationObserver();
94
+ this._selectVariationObserver = null;
95
+ }
96
+ if (this._selectVariationSKUObserver) {
97
+ this._selectVariationSKUObserver();
98
+ this._selectVariationSKUObserver = null;
99
+ }
100
+ }
101
+ /**
102
+ * Initialise and start AR mode if available
103
+ */
104
+ async startAR() {
105
+ const launcher = await this.initAR();
106
+ return launcher.start();
107
+ }
44
108
  /**
45
109
  * Decide which QR Code to render according to the qr-type attribute
46
110
  * @param options
47
111
  * @returns
48
112
  */
49
- startQRCode(options) {
113
+ async startQRCode(options) {
50
114
  const qrType = this.getAttribute("qr-type") || "viewer";
51
115
  switch (qrType.toLowerCase()) {
52
116
  case "ar":
@@ -61,69 +125,76 @@ class PlattarController {
61
125
  * @param options
62
126
  * @returns
63
127
  */
64
- startARQRCode(options) {
128
+ async startARQRCode(options) {
129
+ // remove the old renderer instance if any
130
+ this.removeRenderer();
131
+ const opt = options || this._GetDefaultQROptions();
132
+ const viewer = document.createElement("plattar-qrcode");
133
+ this._element = viewer;
134
+ // required attributes with defaults for plattar-viewer node
135
+ const width = this.getAttribute("width") || "500px";
136
+ const height = this.getAttribute("height") || "500px";
137
+ viewer.setAttribute("width", width);
138
+ viewer.setAttribute("height", height);
139
+ if (opt.color) {
140
+ viewer.setAttribute("color", opt.color);
141
+ }
142
+ if (opt.margin) {
143
+ viewer.setAttribute("margin", "" + opt.margin);
144
+ }
145
+ if (opt.qrType) {
146
+ viewer.setAttribute("qr-type", opt.qrType);
147
+ }
148
+ viewer.setAttribute("shorten", (opt.shorten && (opt.shorten === true || opt.shorten === "true")) ? "true" : "false");
149
+ const qrOptions = btoa(JSON.stringify(opt));
150
+ let dst = plattar_api_1.Server.location().base + "renderer/launcher.html?qr_options=" + qrOptions;
151
+ let configState = null;
152
+ const sceneID = this.getAttribute("scene-id");
153
+ const embedType = this.getAttribute("embed-type");
154
+ const productID = this.getAttribute("product-id");
155
+ const sceneProductID = this.getAttribute("scene-product-id");
156
+ const variationID = this.getAttribute("variation-id");
157
+ const variationSKU = this.getAttribute("variation-sku");
158
+ const arMode = this.getAttribute("ar-mode");
159
+ try {
160
+ configState = (await this.getConfiguratorState()).state.encode();
161
+ }
162
+ catch (_err) {
163
+ // config state not available for some reason
164
+ configState = null;
165
+ }
166
+ if (configState) {
167
+ dst += "&config_state=" + configState;
168
+ }
169
+ if (embedType) {
170
+ dst += "&embed_type=" + embedType;
171
+ }
172
+ if (productID) {
173
+ dst += "&product_id=" + productID;
174
+ }
175
+ if (sceneProductID) {
176
+ dst += "&scene_product_id=" + sceneProductID;
177
+ }
178
+ if (variationID) {
179
+ dst += "&variation_id=" + variationID;
180
+ }
181
+ if (variationSKU) {
182
+ dst += "&variation_sku=" + variationSKU;
183
+ }
184
+ if (arMode) {
185
+ dst += "&ar_mode=" + arMode;
186
+ }
187
+ if (sceneID) {
188
+ dst += "&scene_id=" + sceneID;
189
+ }
190
+ viewer.setAttribute("url", opt.url || dst);
191
+ this._state = ControllerState.QRCode;
192
+ this._prevQROpt = opt;
65
193
  return new Promise((accept, reject) => {
66
- // remove the old renderer instance if any
67
- this.removeRenderer();
68
- const opt = options || this._GetDefaultQROptions();
69
- const viewer = document.createElement("plattar-qrcode");
70
- // required attributes with defaults for plattar-viewer node
71
- const width = this.getAttribute("width") || "500px";
72
- const height = this.getAttribute("height") || "500px";
73
- viewer.setAttribute("width", width);
74
- viewer.setAttribute("height", height);
75
- if (opt.color) {
76
- viewer.setAttribute("color", opt.color);
77
- }
78
- if (opt.margin) {
79
- viewer.setAttribute("margin", "" + opt.margin);
80
- }
81
- if (opt.qrType) {
82
- viewer.setAttribute("qr-type", opt.qrType);
83
- }
84
- viewer.setAttribute("shorten", (opt.shorten && (opt.shorten === true || opt.shorten === "true")) ? "true" : "false");
85
- const qrOptions = btoa(JSON.stringify(opt));
86
- let dst = plattar_api_1.Server.location().base + "renderer/launcher.html?qr_options=" + qrOptions;
87
- const sceneID = this.getAttribute("scene-id");
88
- const configState = this.getAttribute("config-state");
89
- const embedType = this.getAttribute("embed-type");
90
- const productID = this.getAttribute("product-id");
91
- const sceneProductID = this.getAttribute("scene-product-id");
92
- const variationID = this.getAttribute("variation-id");
93
- const variationSKU = this.getAttribute("variation-sku");
94
- const arMode = this.getAttribute("ar-mode");
95
- if (configState) {
96
- dst += "&config_state=" + configState;
97
- }
98
- if (embedType) {
99
- dst += "&embed_type=" + embedType;
100
- }
101
- if (productID) {
102
- dst += "&product_id=" + productID;
103
- }
104
- if (sceneProductID) {
105
- dst += "&scene_product_id=" + sceneProductID;
106
- }
107
- if (variationID) {
108
- dst += "&variation_id=" + variationID;
109
- }
110
- if (variationSKU) {
111
- dst += "&variation_sku=" + variationSKU;
112
- }
113
- if (arMode) {
114
- dst += "&ar_mode=" + arMode;
115
- }
116
- if (sceneID) {
117
- dst += "&scene_id=" + sceneID;
118
- }
119
- viewer.setAttribute("url", opt.url || dst);
194
+ this.append(viewer);
120
195
  viewer.onload = () => {
121
196
  return accept(viewer);
122
197
  };
123
- this._element = viewer;
124
- this._state = ControllerState.QRCode;
125
- this._prevQROpt = opt;
126
- this.append(viewer);
127
198
  });
128
199
  }
129
200
  /**
@@ -1,11 +1,16 @@
1
1
  import { LauncherAR } from "../../ar/launcher-ar";
2
2
  import { PlattarController } from "./plattar-controller";
3
+ import { DecodedConfiguratorState } from "../../util/configurator-state";
3
4
  /**
4
5
  * Manages an instance of the <plattar-product> HTML Element
6
+ *
7
+ * NOTE: As of 14th June 2023, this is now a legacy Controller and only used in legacy embeds
8
+ * and should be deprecated from both documentation and previous integrations
5
9
  */
6
10
  export declare class ProductController extends PlattarController {
11
+ getConfiguratorState(): Promise<DecodedConfiguratorState>;
7
12
  constructor(parent: HTMLElement);
8
- onAttributesUpdated(): void;
13
+ onAttributesUpdated(attributeName: string): Promise<void>;
9
14
  startViewerQRCode(options: any): Promise<HTMLElement>;
10
15
  startRenderer(): Promise<HTMLElement>;
11
16
  initAR(): Promise<LauncherAR>;
@@ -7,12 +7,20 @@ const util_1 = require("../../util/util");
7
7
  const plattar_controller_1 = require("./plattar-controller");
8
8
  /**
9
9
  * Manages an instance of the <plattar-product> HTML Element
10
+ *
11
+ * NOTE: As of 14th June 2023, this is now a legacy Controller and only used in legacy embeds
12
+ * and should be deprecated from both documentation and previous integrations
10
13
  */
11
14
  class ProductController extends plattar_controller_1.PlattarController {
15
+ async getConfiguratorState() {
16
+ throw new Error("ProductController.getConfiguratorState() - legacy embeds do not support configurator states");
17
+ }
12
18
  constructor(parent) {
19
+ // this is a hack against DecodedConfiguratorState that's now stored in PlattarController
20
+ // this is not used in legacy mode
13
21
  super(parent);
14
22
  }
15
- onAttributesUpdated() {
23
+ async onAttributesUpdated(attributeName) {
16
24
  const state = this._state;
17
25
  // re-render the QR Code when attributes have changed
18
26
  if (state === plattar_controller_1.ControllerState.QRCode) {
@@ -28,7 +36,6 @@ class ProductController extends plattar_controller_1.PlattarController {
28
36
  viewer.messenger.selectVariation(variationID);
29
37
  }
30
38
  }
31
- return;
32
39
  }
33
40
  }
34
41
  startViewerQRCode(options) {
@@ -1,11 +1,13 @@
1
1
  import { LauncherAR } from "../../ar/launcher-ar";
2
+ import { DecodedConfiguratorState } from "../../util/configurator-state";
2
3
  import { PlattarController } from "./plattar-controller";
3
4
  /**
4
5
  * Manages an instance of the <plattar-configurator> HTML Element
5
6
  */
6
7
  export declare class VTOController extends PlattarController {
7
- constructor(parent: HTMLElement);
8
- onAttributesUpdated(): void;
8
+ private _cachedConfigState;
9
+ getConfiguratorState(): Promise<DecodedConfiguratorState>;
10
+ onAttributesUpdated(attributeName: string): Promise<void>;
9
11
  startViewerQRCode(options: any): Promise<HTMLElement>;
10
12
  startRenderer(): Promise<HTMLElement>;
11
13
  initAR(): Promise<LauncherAR>;