@plattar/plattar-ar-adapter 1.123.4 → 1.123.8

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.
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ConfiguratorState = void 0;
4
+ const plattar_api_1 = require("@plattar/plattar-api");
4
5
  class ConfiguratorState {
5
6
  constructor(state = null) {
6
7
  const defaultState = {
@@ -30,6 +31,16 @@ class ConfiguratorState {
30
31
  }
31
32
  this._state = defaultState;
32
33
  }
34
+ /**
35
+ * Adds a new Scene Product/Variation combo with meta-data into the Configurator State
36
+ *
37
+ * @param sceneProductID - The Scene Product ID to be used (as defined in Plattar CMS)
38
+ * @param productVariationID - The Product Variation ID to be used (as defined in Plattar CMS)
39
+ * @param metaData - Arbitrary meta-data that can be used against certain operaions
40
+ */
41
+ setSceneProduct(sceneProductID, productVariationID, metaData = null) {
42
+ this.addSceneProduct(sceneProductID, productVariationID, metaData);
43
+ }
33
44
  /**
34
45
  * Adds a new Scene Product/Variation combo with meta-data into the Configurator State
35
46
  *
@@ -41,15 +52,63 @@ class ConfiguratorState {
41
52
  if (sceneProductID && productVariationID) {
42
53
  const states = this._state.states;
43
54
  const meta = this._state.meta;
44
- const newData = [];
45
- newData.splice(meta.scene_product_index, 0, sceneProductID);
46
- newData.splice(meta.product_variation_index, 0, productVariationID);
55
+ let newData = null;
56
+ const existingData = this.findSceneProductIndex(sceneProductID);
57
+ if (existingData) {
58
+ newData = existingData;
59
+ }
60
+ else {
61
+ newData = [];
62
+ // push the new data into the stack
63
+ states.push(newData);
64
+ }
65
+ newData[meta.scene_product_index] = sceneProductID;
66
+ newData[meta.product_variation_index] = productVariationID;
47
67
  if (metaData) {
48
- newData.splice(meta.meta_index, 0, metaData);
68
+ newData[meta.meta_index] = metaData;
49
69
  }
50
- states.push(newData);
51
70
  }
52
71
  }
72
+ /**
73
+ * Search and return the data index reference for the provided Scene Product ID
74
+ * if not found, will return null
75
+ * @param sceneProductID
76
+ */
77
+ findSceneProductIndex(sceneProductID) {
78
+ const states = this._state.states;
79
+ if (states.length > 0) {
80
+ const meta = this._state.meta;
81
+ const found = states.find((productState) => {
82
+ return productState[meta.scene_product_index] === sceneProductID;
83
+ });
84
+ return found ? found : null;
85
+ }
86
+ return null;
87
+ }
88
+ /**
89
+ * Search and return the data for the provided Scene Product ID
90
+ * if not found, will return null
91
+ * @param sceneProductID
92
+ */
93
+ findSceneProduct(sceneProductID) {
94
+ const found = this.findSceneProductIndex(sceneProductID);
95
+ if (found) {
96
+ const meta = this._state.meta;
97
+ const data = {
98
+ scene_product_id: found[meta.scene_product_index],
99
+ product_variation_id: found[meta.product_variation_index],
100
+ meta_data: {
101
+ augment: true
102
+ }
103
+ };
104
+ // include the meta-data
105
+ if (found.length === 3) {
106
+ data.meta_data.augment = found[meta.meta_index].augment || true;
107
+ }
108
+ return data;
109
+ }
110
+ return null;
111
+ }
53
112
  /**
54
113
  * Iterate over the internal state data
55
114
  */
@@ -79,12 +138,84 @@ class ConfiguratorState {
79
138
  });
80
139
  }
81
140
  }
141
+ /**
142
+ * @returns Returns the first reference of data in the stack, otherwise returns null
143
+ */
144
+ first() {
145
+ const states = this._state.states;
146
+ if (states.length > 0) {
147
+ const meta = this._state.meta;
148
+ const found = states.find((productState) => {
149
+ const check = productState[meta.scene_product_index];
150
+ // ensure the data contains valid elements
151
+ return check !== null && check !== undefined;
152
+ });
153
+ if (!found) {
154
+ return null;
155
+ }
156
+ const data = {
157
+ scene_product_id: found[meta.scene_product_index],
158
+ product_variation_id: found[meta.product_variation_index],
159
+ meta_data: {
160
+ augment: true
161
+ }
162
+ };
163
+ // include the meta-data
164
+ if (found.length === 3) {
165
+ data.meta_data.augment = found[meta.meta_index].augment || true;
166
+ }
167
+ return data;
168
+ }
169
+ return null;
170
+ }
82
171
  get length() {
83
172
  return this._state.states.length;
84
173
  }
174
+ /**
175
+ * Decodes and returns an instance of ConfiguratorState from a previously
176
+ * encoded state
177
+ * @param state - The previously encoded state as a Base64 String
178
+ * @returns - ConfiguratorState instance
179
+ */
85
180
  static decode(state) {
86
181
  return new ConfiguratorState(state);
87
182
  }
183
+ /**
184
+ * Generates a new ConfiguratorState instance from all SceneProducts and default
185
+ * variations from the provided Scene ID
186
+ * @param sceneID - the Scene ID to generate
187
+ * @returns - Promise that resolves into a ConfiguratorState instance
188
+ */
189
+ static decodeScene(sceneID = null) {
190
+ return new Promise((accept, reject) => {
191
+ const configState = new ConfiguratorState();
192
+ if (!sceneID) {
193
+ return reject(new Error("ConfiguratorState.decodeScene(sceneID) - sceneID must be defined"));
194
+ }
195
+ const scene = new plattar_api_1.Scene(sceneID);
196
+ scene.include(plattar_api_1.SceneProduct);
197
+ scene.include(plattar_api_1.SceneProduct.include(plattar_api_1.Product));
198
+ scene.get().then((scene) => {
199
+ const sceneProducts = scene.relationships.filter(plattar_api_1.SceneProduct);
200
+ // nothing to do if no AR components can be found
201
+ if (sceneProducts.length <= 0) {
202
+ return accept(configState);
203
+ }
204
+ // add out scene models
205
+ sceneProducts.forEach((sceneProduct) => {
206
+ const product = sceneProduct.relationships.find(plattar_api_1.Product);
207
+ if (product && product.attributes.product_variation_id) {
208
+ configState.setSceneProduct(sceneProduct.id, product.attributes.product_variation_id);
209
+ }
210
+ });
211
+ accept(configState);
212
+ }).catch(reject);
213
+ });
214
+ }
215
+ /**
216
+ * Encode and return the internal ConfiguratorState as a Base64 String
217
+ * @returns - Base64 String
218
+ */
88
219
  encode() {
89
220
  return btoa(JSON.stringify(this._state));
90
221
  }
package/dist/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- declare const _default: "1.123.4";
1
+ declare const _default: "1.123.8";
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.123.4";
3
+ exports.default = "1.123.8";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plattar/plattar-ar-adapter",
3
- "version": "1.123.4",
3
+ "version": "1.123.8",
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,8 +44,8 @@
44
44
  "@plattar/plattar-web": "^1.123.1"
45
45
  },
46
46
  "devDependencies": {
47
- "@babel/cli": "^7.17.0",
48
- "@babel/core": "^7.17.2",
47
+ "@babel/cli": "^7.17.3",
48
+ "@babel/core": "^7.17.4",
49
49
  "@babel/preset-env": "^7.16.11",
50
50
  "browserify": "^17.0.0",
51
51
  "typescript": "^4.5.5",