@plattar/plattar-ar-adapter 1.122.1 → 1.122.2

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.
@@ -4,6 +4,8 @@ import { LauncherAR } from "../ar/launcher-ar";
4
4
  * of Plattar related content
5
5
  */
6
6
  export default class PlattarEmbed extends HTMLElement {
7
+ private _currentState;
8
+ private _qrCodeOptions;
7
9
  private _sceneID;
8
10
  private _productID;
9
11
  private _variationID;
@@ -19,4 +21,9 @@ export default class PlattarEmbed extends HTMLElement {
19
21
  startAR(): Promise<void>;
20
22
  startViewer(): Promise<HTMLElement>;
21
23
  startQRCode(options?: any | undefined | null): Promise<HTMLElement>;
24
+ /**
25
+ * This is called by the observer if any of the embed attributes have changed
26
+ * based on the state of the embed, we update the internal structure accordingly
27
+ */
28
+ private _OnAttributesUpdated;
22
29
  }
@@ -2,6 +2,17 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const plattar_api_1 = require("@plattar/plattar-api");
4
4
  const product_ar_1 = require("../ar/product-ar");
5
+ /**
6
+ * This tracks the current state of the Embed
7
+ */
8
+ var EmbedState;
9
+ (function (EmbedState) {
10
+ EmbedState[EmbedState["None"] = 0] = "None";
11
+ EmbedState[EmbedState["SceneViewer"] = 1] = "SceneViewer";
12
+ EmbedState[EmbedState["ProductViewer"] = 2] = "ProductViewer";
13
+ EmbedState[EmbedState["ProductAR"] = 3] = "ProductAR";
14
+ EmbedState[EmbedState["QRCode"] = 4] = "QRCode";
15
+ })(EmbedState || (EmbedState = {}));
5
16
  /**
6
17
  * This is the primary <plattar-embed /> node that allows easy embedding
7
18
  * of Plattar related content
@@ -9,6 +20,13 @@ const product_ar_1 = require("../ar/product-ar");
9
20
  class PlattarEmbed extends HTMLElement {
10
21
  constructor() {
11
22
  super();
23
+ // this is the current state of the embed, none by default
24
+ this._currentState = EmbedState.None;
25
+ this._qrCodeOptions = {
26
+ color: "#101721",
27
+ qrType: "default",
28
+ margin: 0
29
+ };
12
30
  this._sceneID = null;
13
31
  this._productID = null;
14
32
  this._variationID = null;
@@ -22,6 +40,35 @@ class PlattarEmbed extends HTMLElement {
22
40
  return this._viewer;
23
41
  }
24
42
  connectedCallback() {
43
+ const observer = new MutationObserver((mutations) => {
44
+ mutations.forEach((mutation) => {
45
+ if (mutation.type === "attributes") {
46
+ const sceneID = this.hasAttribute("scene-id") ? this.getAttribute("scene-id") : null;
47
+ const productID = this.hasAttribute("product-id") ? this.getAttribute("product-id") : null;
48
+ const variationID = this.hasAttribute("variation-id") ? this.getAttribute("variation-id") : null;
49
+ let updated = false;
50
+ if (sceneID !== this._sceneID) {
51
+ this._sceneID = sceneID;
52
+ updated = true;
53
+ }
54
+ if (productID !== this._productID) {
55
+ this._productID = productID;
56
+ updated = true;
57
+ }
58
+ if (variationID !== this._variationID) {
59
+ this._variationID = variationID;
60
+ updated = true;
61
+ }
62
+ if (updated) {
63
+ // re-render based on internal state
64
+ this._OnAttributesUpdated();
65
+ }
66
+ }
67
+ });
68
+ });
69
+ observer.observe(this, {
70
+ attributes: true
71
+ });
25
72
  const server = this.hasAttribute("server") ? this.getAttribute("server") : "production";
26
73
  plattar_api_1.Server.create(plattar_api_1.Server.match(server || "production"));
27
74
  if (server) {
@@ -74,6 +121,13 @@ class PlattarEmbed extends HTMLElement {
74
121
  const product = new product_ar_1.ProductAR(this._productID, this._variationID);
75
122
  return product.init().then(accept).catch(reject);
76
123
  }
124
+ // If Product is set (under any scenario) then use ProductAR
125
+ // NOTE: At some point this should check for Scenes when SceneAR
126
+ // is implemented
127
+ if (this._productID) {
128
+ const product = new product_ar_1.ProductAR(this._productID, this._variationID);
129
+ return product.init().then(accept).catch(reject);
130
+ }
77
131
  // otherwise, scene was set so use SceneAR
78
132
  if (this._sceneID) {
79
133
  return reject(new Error("PlattarEmbed.initAR() - scene-id not yet supported"));
@@ -120,6 +174,7 @@ class PlattarEmbed extends HTMLElement {
120
174
  const shadow = this.shadowRoot || this.attachShadow({ mode: 'open' });
121
175
  shadow.append(viewer);
122
176
  this._viewer = viewer;
177
+ this._currentState = EmbedState.SceneViewer;
123
178
  return;
124
179
  }
125
180
  // if product is set, we use <plattar-product /> node from plattar-web
@@ -138,6 +193,7 @@ class PlattarEmbed extends HTMLElement {
138
193
  const shadow = this.shadowRoot || this.attachShadow({ mode: 'open' });
139
194
  shadow.append(viewer);
140
195
  this._viewer = viewer;
196
+ this._currentState = EmbedState.ProductViewer;
141
197
  return;
142
198
  }
143
199
  return reject(new Error("PlattarEmbed.startViewer() - minimum required attributes not set, use scene-id or product-id as a minimum"));
@@ -148,11 +204,9 @@ class PlattarEmbed extends HTMLElement {
148
204
  if (!this._isReady) {
149
205
  return reject(new Error("PlattarEmbed.startQRCode() - cannot execute as page has not loaded yet"));
150
206
  }
151
- const opt = options || {
152
- color: "#101721",
153
- qrType: "default",
154
- margin: 0
155
- };
207
+ const opt = options || this._qrCodeOptions;
208
+ // reset instance for later use
209
+ this._qrCodeOptions = opt;
156
210
  if (this._viewer) {
157
211
  this._viewer.remove();
158
212
  this._viewer = null;
@@ -185,6 +239,7 @@ class PlattarEmbed extends HTMLElement {
185
239
  const shadow = this.shadowRoot || this.attachShadow({ mode: 'open' });
186
240
  shadow.append(viewer);
187
241
  this._viewer = viewer;
242
+ this._currentState = EmbedState.QRCode;
188
243
  return;
189
244
  }
190
245
  // if product is set, we embed a QR code that takes us to product.html
@@ -212,10 +267,41 @@ class PlattarEmbed extends HTMLElement {
212
267
  const shadow = this.shadowRoot || this.attachShadow({ mode: 'open' });
213
268
  shadow.append(viewer);
214
269
  this._viewer = viewer;
270
+ this._currentState = EmbedState.QRCode;
215
271
  return;
216
272
  }
217
273
  return reject(new Error("PlattarEmbed.startQRCode() - minimum required attributes not set, use scene-id or product-id as a minimum"));
218
274
  });
219
275
  }
276
+ /**
277
+ * This is called by the observer if any of the embed attributes have changed
278
+ * based on the state of the embed, we update the internal structure accordingly
279
+ */
280
+ _OnAttributesUpdated() {
281
+ // nothing to update in these scenarios
282
+ if (this._currentState === EmbedState.None || this._currentState === EmbedState.ProductAR) {
283
+ return;
284
+ }
285
+ // re-render the QR Code when attributes have changed
286
+ if (this._currentState === EmbedState.QRCode) {
287
+ this.startQRCode(this._qrCodeOptions);
288
+ return;
289
+ }
290
+ // use the messenger function to change variation when attributes have changed
291
+ if (this._currentState === EmbedState.SceneViewer) {
292
+ const viewer = this.viewer;
293
+ if (viewer) {
294
+ viewer.messenger.selectVariation(this._productID, this._variationID);
295
+ }
296
+ return;
297
+ }
298
+ if (this._currentState === EmbedState.ProductViewer) {
299
+ const viewer = this.viewer;
300
+ if (viewer) {
301
+ viewer.messenger.selectVariation(this._variationID);
302
+ }
303
+ return;
304
+ }
305
+ }
220
306
  }
221
307
  exports.default = PlattarEmbed;
package/dist/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- declare const _default: "1.122.1";
1
+ declare const _default: "1.122.2";
2
2
  export default _default;
package/dist/version.js CHANGED
@@ -1,3 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = "1.122.1";
3
+ exports.default = "1.122.2";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plattar/plattar-ar-adapter",
3
- "version": "1.122.1",
3
+ "version": "1.122.2",
4
4
  "description": "Plattar AR Adapter for interfacing with Google & Apple WebAR",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -44,10 +44,10 @@
44
44
  },
45
45
  "devDependencies": {
46
46
  "@babel/cli": "^7.16.8",
47
- "@babel/core": "^7.16.10",
48
- "@babel/preset-env": "^7.16.10",
47
+ "@babel/core": "^7.16.12",
48
+ "@babel/preset-env": "^7.16.11",
49
49
  "browserify": "^17.0.0",
50
- "typescript": "^4.5.4",
50
+ "typescript": "^4.5.5",
51
51
  "uglify-es": "^3.3.9"
52
52
  },
53
53
  "publishConfig": {