@plattar/plattar-ar-adapter 1.123.6 → 1.123.7

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.
@@ -628,6 +628,9 @@ exports.ConfiguratorController = void 0;
628
628
  const plattar_api_1 = require("@plattar/plattar-api");
629
629
  const plattar_services_1 = require("@plattar/plattar-services");
630
630
  const raw_ar_1 = require("../../ar/raw-ar");
631
+ const scene_ar_1 = require("../../ar/scene-ar");
632
+ const scene_product_ar_1 = require("../../ar/scene-product-ar");
633
+ const configurator_state_1 = require("../../util/configurator-state");
631
634
  const util_1 = require("../../util/util");
632
635
  const plattar_controller_1 = require("./plattar-controller");
633
636
  /**
@@ -733,59 +736,103 @@ class ConfiguratorController extends plattar_controller_1.PlattarController {
733
736
  if (!util_1.Util.canAugment()) {
734
737
  return reject(new Error("ConfiguratorController.initAR() - cannot proceed as AR not available in context"));
735
738
  }
736
- // if scene ID is available and the state is a configurator viewer
737
- // we can use the real-time configurator state to launch the AR view
738
- const viewer = this.element;
739
- const sceneID = this.getAttribute("scene-id");
740
- if (viewer && sceneID) {
741
- let output = "glb";
742
- if (util_1.Util.isSafari() || util_1.Util.isChromeOnIOS()) {
743
- output = "usdz";
739
+ const arMode = this.getAttribute("ar-mode") || "generated";
740
+ switch (arMode.toLowerCase()) {
741
+ case "inherited":
742
+ this._InitARInherited(accept, reject);
743
+ return;
744
+ case "generated":
745
+ default:
746
+ this._InitARGenerated(accept, reject);
747
+ }
748
+ });
749
+ }
750
+ /**
751
+ * Private Function - This launches the Static/Inherited AR Mode
752
+ */
753
+ _InitARInherited(accept, reject) {
754
+ const sceneID = this.getAttribute("scene-id");
755
+ const configState = this.getAttribute("config-state");
756
+ // use config-state if its available
757
+ if (sceneID && configState) {
758
+ const state = configurator_state_1.ConfiguratorState.decode(configState);
759
+ const first = state.first();
760
+ if (first) {
761
+ const sceneProductAR = new scene_product_ar_1.SceneProductAR(first.scene_product_id, first.product_variation_id);
762
+ sceneProductAR.init().then(accept).catch(reject);
763
+ return;
764
+ }
765
+ return reject(new Error("ConfiguratorController.initAR() - invalid config-state does not have any product states"));
766
+ }
767
+ // otherwise fallback to using scene
768
+ if (sceneID) {
769
+ configurator_state_1.ConfiguratorState.decodeScene(sceneID).then((state) => {
770
+ const first = state.first();
771
+ if (first) {
772
+ const sceneProductAR = new scene_product_ar_1.SceneProductAR(first.scene_product_id, first.product_variation_id);
773
+ return sceneProductAR.init().then(accept).catch(reject);
744
774
  }
745
- return viewer.messenger.getARFile(output).then((result) => {
746
- const rawAR = new raw_ar_1.RawAR(result.filename);
747
- return rawAR.init().then(accept).catch(reject);
748
- }).catch(reject);
775
+ return reject(new Error("ConfiguratorController.initAR() - invalid Scene does not have any product states"));
776
+ }).catch(reject);
777
+ return;
778
+ }
779
+ return reject(new Error("ConfiguratorController.initAR() - minimum required attributes not set, use scene-id as a minimum"));
780
+ }
781
+ /**
782
+ * Private Function - This launches the Dynamic/Generated AR Mode
783
+ */
784
+ _InitARGenerated(accept, reject) {
785
+ // if scene ID is available and the state is a configurator viewer
786
+ // we can use the real-time configurator state to launch the AR view
787
+ const viewer = this.element;
788
+ const sceneID = this.getAttribute("scene-id");
789
+ if (viewer && sceneID && viewer.messenger) {
790
+ let output = "glb";
791
+ if (util_1.Util.isSafari() || util_1.Util.isChromeOnIOS()) {
792
+ output = "usdz";
749
793
  }
750
- const configState = this.getAttribute("config-state");
751
- // otherwise scene ID is available to the viewer is not launched
752
- // we can use the static configuration state to launch the AR view
753
- if (sceneID && configState) {
754
- try {
755
- const decodedb64State = atob(configState);
756
- const state = JSON.parse(decodedb64State);
757
- if (state.meta) {
758
- const sceneProductIndex = state.meta.scene_product_index || 0;
759
- const variationIndex = state.meta.product_variation_index || 1;
760
- const states = state.states || [];
761
- if (states.length > 0) {
762
- const configurator = new plattar_services_1.Configurator();
763
- states.forEach((productState) => {
764
- configurator.addSceneProduct(productState[sceneProductIndex], productState[variationIndex]);
765
- });
766
- if (util_1.Util.isSafari() || util_1.Util.isChromeOnIOS()) {
767
- configurator.output = "usdz";
768
- }
769
- if (util_1.Util.canSceneViewer()) {
770
- configurator.output = "glb";
771
- }
772
- const server = this.getAttribute("server") || "production";
773
- configurator.server = server;
774
- return configurator.get().then((result) => {
775
- const rawAR = new raw_ar_1.RawAR(result.filename);
776
- rawAR.init().then(accept).catch(reject);
777
- }).catch(reject);
778
- }
779
- return reject(new Error("ConfiguratorController.initAR() - invalid config-state does not have any product states"));
780
- }
781
- return reject(new Error("ConfiguratorController.initAR() - invalid config-state for configurator"));
794
+ viewer.messenger.getARFile(output).then((result) => {
795
+ const rawAR = new raw_ar_1.RawAR(result.filename);
796
+ return rawAR.init().then(accept).catch(reject);
797
+ }).catch(reject);
798
+ return;
799
+ }
800
+ const configState = this.getAttribute("config-state");
801
+ // otherwise scene ID is available to the viewer is not launched
802
+ // we can use the static configuration state to launch the AR view
803
+ if (sceneID && configState) {
804
+ const state = configurator_state_1.ConfiguratorState.decode(configState);
805
+ if (state.length > 0) {
806
+ const server = this.getAttribute("server") || "production";
807
+ const configurator = new plattar_services_1.Configurator();
808
+ configurator.server = server;
809
+ if (util_1.Util.isSafari() || util_1.Util.isChromeOnIOS()) {
810
+ configurator.output = "usdz";
782
811
  }
783
- catch (err) {
784
- return reject(err);
812
+ if (util_1.Util.canSceneViewer()) {
813
+ configurator.output = "glb";
785
814
  }
815
+ state.forEach((productState) => {
816
+ if (productState.meta_data.augment === true) {
817
+ configurator.addSceneProduct(productState.scene_product_id, productState.product_variation_id);
818
+ }
819
+ });
820
+ configurator.get().then((result) => {
821
+ const rawAR = new raw_ar_1.RawAR(result.filename);
822
+ rawAR.init().then(accept).catch(reject);
823
+ }).catch(reject);
824
+ return;
786
825
  }
787
- return reject(new Error("ConfiguratorController.initAR() - minimum required attributes not set, use scene-id as a minimum"));
788
- });
826
+ return reject(new Error("ConfiguratorController.initAR() - invalid config-state does not have any product states"));
827
+ }
828
+ // otherwise no config-state or viewer is active
829
+ // fallback to using default SceneAR implementation
830
+ if (sceneID) {
831
+ const sceneAR = new scene_ar_1.SceneAR(sceneID);
832
+ sceneAR.init().then(accept).catch(reject);
833
+ return;
834
+ }
835
+ return reject(new Error("ConfiguratorController.initAR() - minimum required attributes not set, use scene-id as a minimum"));
789
836
  }
790
837
  removeRenderer() {
791
838
  if (this._element) {
@@ -801,7 +848,7 @@ class ConfiguratorController extends plattar_controller_1.PlattarController {
801
848
  }
802
849
  exports.ConfiguratorController = ConfiguratorController;
803
850
 
804
- },{"../../ar/raw-ar":4,"../../util/util":15,"./plattar-controller":8,"@plattar/plattar-api":42,"@plattar/plattar-services":113}],8:[function(require,module,exports){
851
+ },{"../../ar/raw-ar":4,"../../ar/scene-ar":5,"../../ar/scene-product-ar":6,"../../util/configurator-state":14,"../../util/util":15,"./plattar-controller":8,"@plattar/plattar-api":42,"@plattar/plattar-services":113}],8:[function(require,module,exports){
805
852
  "use strict";
806
853
  Object.defineProperty(exports, "__esModule", { value: true });
807
854
  exports.PlattarController = exports.ControllerState = void 0;
@@ -1278,46 +1325,94 @@ class VTOController extends plattar_controller_1.PlattarController {
1278
1325
  if (!(util_1.Util.isSafari() || util_1.Util.isChromeOnIOS())) {
1279
1326
  return reject(new Error("VTOController.initAR() - cannot proceed as VTO AR only available on IOS Mobile devices"));
1280
1327
  }
1281
- // if scene ID is available and the state is a configurator viewer
1282
- // we can use the real-time configurator state to launch the AR view
1283
- const viewer = this.element;
1284
- const sceneID = this.getAttribute("scene-id");
1285
- if (viewer && sceneID) {
1286
- return viewer.messenger.getARFile("vto").then((result) => {
1287
- const rawAR = new raw_ar_1.RawAR(result.filename);
1288
- return rawAR.init().then(accept).catch(reject);
1289
- }).catch(reject);
1328
+ const arMode = this.getAttribute("ar-mode") || "generated";
1329
+ switch (arMode.toLowerCase()) {
1330
+ case "inherited":
1331
+ this._InitARInherited(accept, reject);
1332
+ return;
1333
+ case "generated":
1334
+ default:
1335
+ this._InitARGenerated(accept, reject);
1290
1336
  }
1291
- const configState = this.getAttribute("config-state");
1292
- // otherwise scene ID is available to the viewer is not launched
1293
- // we can use the static configuration state to launch the AR view
1294
- if (sceneID && configState) {
1295
- const state = __1.ConfiguratorState.decode(configState);
1296
- if (state.length > 0) {
1297
- const server = this.getAttribute("server") || "production";
1298
- const configurator = new plattar_services_1.Configurator();
1299
- configurator.server = server;
1300
- configurator.output = "vto";
1301
- state.forEach((productState) => {
1302
- if (productState.meta_data.augment === true) {
1303
- configurator.addSceneProduct(productState.scene_product_id, productState.product_variation_id);
1304
- }
1305
- });
1306
- return configurator.get().then((result) => {
1307
- const rawAR = new raw_ar_1.RawAR(result.filename);
1308
- rawAR.init().then(accept).catch(reject);
1309
- }).catch(reject);
1310
- }
1311
- return reject(new Error("VTOController.initAR() - invalid config-state does not have any product states"));
1337
+ });
1338
+ }
1339
+ /**
1340
+ * Private Function - This launches the Static/Inherited AR Mode
1341
+ */
1342
+ _InitARInherited(accept, reject) {
1343
+ const sceneID = this.getAttribute("scene-id");
1344
+ const configState = this.getAttribute("config-state");
1345
+ // use config-state if its available
1346
+ if (sceneID && configState) {
1347
+ const state = __1.ConfiguratorState.decode(configState);
1348
+ const first = state.first();
1349
+ if (first) {
1350
+ const sceneProductAR = new __1.SceneProductAR(first.scene_product_id, first.product_variation_id);
1351
+ sceneProductAR.init().then(accept).catch(reject);
1352
+ return;
1312
1353
  }
1313
- // otherwise no config-state or viewer is active
1314
- // fallback to using default SceneAR implementation
1315
- if (sceneID) {
1316
- const sceneAR = new __1.SceneAR(sceneID);
1317
- return sceneAR.init().then(accept).catch(reject);
1354
+ return reject(new Error("VTOController.initAR() - invalid config-state does not have any product states"));
1355
+ }
1356
+ // otherwise fallback to using scene
1357
+ if (sceneID) {
1358
+ __1.ConfiguratorState.decodeScene(sceneID).then((state) => {
1359
+ const first = state.first();
1360
+ if (first) {
1361
+ const sceneProductAR = new __1.SceneProductAR(first.scene_product_id, first.product_variation_id);
1362
+ return sceneProductAR.init().then(accept).catch(reject);
1363
+ }
1364
+ return reject(new Error("VTOController.initAR() - invalid Scene does not have any product states"));
1365
+ }).catch(reject);
1366
+ return;
1367
+ }
1368
+ return reject(new Error("VTOController.initAR() - minimum required attributes not set, use scene-id as a minimum"));
1369
+ }
1370
+ /**
1371
+ * Private Function - This launches the Dynamic/Generated AR Mode
1372
+ */
1373
+ _InitARGenerated(accept, reject) {
1374
+ // if scene ID is available and the state is a configurator viewer
1375
+ // we can use the real-time configurator state to launch the AR view
1376
+ const viewer = this.element;
1377
+ const sceneID = this.getAttribute("scene-id");
1378
+ if (viewer && sceneID && viewer.messenger) {
1379
+ viewer.messenger.getARFile("vto").then((result) => {
1380
+ const rawAR = new raw_ar_1.RawAR(result.filename);
1381
+ return rawAR.init().then(accept).catch(reject);
1382
+ }).catch(reject);
1383
+ return;
1384
+ }
1385
+ const configState = this.getAttribute("config-state");
1386
+ // otherwise scene ID is available to the viewer is not launched
1387
+ // we can use the static configuration state to launch the AR view
1388
+ if (sceneID && configState) {
1389
+ const state = __1.ConfiguratorState.decode(configState);
1390
+ if (state.length > 0) {
1391
+ const server = this.getAttribute("server") || "production";
1392
+ const configurator = new plattar_services_1.Configurator();
1393
+ configurator.server = server;
1394
+ configurator.output = "vto";
1395
+ state.forEach((productState) => {
1396
+ if (productState.meta_data.augment === true) {
1397
+ configurator.addSceneProduct(productState.scene_product_id, productState.product_variation_id);
1398
+ }
1399
+ });
1400
+ configurator.get().then((result) => {
1401
+ const rawAR = new raw_ar_1.RawAR(result.filename);
1402
+ rawAR.init().then(accept).catch(reject);
1403
+ }).catch(reject);
1404
+ return;
1318
1405
  }
1319
- return reject(new Error("VTOController.initAR() - minimum required attributes not set, use scene-id as a minimum"));
1320
- });
1406
+ return reject(new Error("VTOController.initAR() - invalid config-state does not have any product states"));
1407
+ }
1408
+ // otherwise no config-state or viewer is active
1409
+ // fallback to using default SceneAR implementation
1410
+ if (sceneID) {
1411
+ const sceneAR = new __1.SceneAR(sceneID);
1412
+ sceneAR.init().then(accept).catch(reject);
1413
+ return;
1414
+ }
1415
+ return reject(new Error("VTOController.initAR() - minimum required attributes not set, use scene-id as a minimum"));
1321
1416
  }
1322
1417
  removeRenderer() {
1323
1418
  if (this._element) {
@@ -1547,6 +1642,7 @@ console.log("using @plattar/plattar-ar-adapter v" + version_1.default);
1547
1642
  "use strict";
1548
1643
  Object.defineProperty(exports, "__esModule", { value: true });
1549
1644
  exports.ConfiguratorState = void 0;
1645
+ const plattar_api_1 = require("@plattar/plattar-api");
1550
1646
  class ConfiguratorState {
1551
1647
  constructor(state = null) {
1552
1648
  const defaultState = {
@@ -1576,6 +1672,16 @@ class ConfiguratorState {
1576
1672
  }
1577
1673
  this._state = defaultState;
1578
1674
  }
1675
+ /**
1676
+ * Adds a new Scene Product/Variation combo with meta-data into the Configurator State
1677
+ *
1678
+ * @param sceneProductID - The Scene Product ID to be used (as defined in Plattar CMS)
1679
+ * @param productVariationID - The Product Variation ID to be used (as defined in Plattar CMS)
1680
+ * @param metaData - Arbitrary meta-data that can be used against certain operaions
1681
+ */
1682
+ setSceneProduct(sceneProductID, productVariationID, metaData = null) {
1683
+ this.addSceneProduct(sceneProductID, productVariationID, metaData);
1684
+ }
1579
1685
  /**
1580
1686
  * Adds a new Scene Product/Variation combo with meta-data into the Configurator State
1581
1687
  *
@@ -1587,14 +1693,62 @@ class ConfiguratorState {
1587
1693
  if (sceneProductID && productVariationID) {
1588
1694
  const states = this._state.states;
1589
1695
  const meta = this._state.meta;
1590
- const newData = [];
1591
- newData.splice(meta.scene_product_index, 0, sceneProductID);
1592
- newData.splice(meta.product_variation_index, 0, productVariationID);
1696
+ let newData = null;
1697
+ const existingData = this.findSceneProductIndex(sceneProductID);
1698
+ if (existingData) {
1699
+ newData = existingData;
1700
+ }
1701
+ else {
1702
+ newData = [];
1703
+ // push the new data into the stack
1704
+ states.push(newData);
1705
+ }
1706
+ newData[meta.scene_product_index] = sceneProductID;
1707
+ newData[meta.product_variation_index] = productVariationID;
1593
1708
  if (metaData) {
1594
- newData.splice(meta.meta_index, 0, metaData);
1709
+ newData[meta.meta_index] = metaData;
1710
+ }
1711
+ }
1712
+ }
1713
+ /**
1714
+ * Search and return the data index reference for the provided Scene Product ID
1715
+ * if not found, will return null
1716
+ * @param sceneProductID
1717
+ */
1718
+ findSceneProductIndex(sceneProductID) {
1719
+ const states = this._state.states;
1720
+ if (states.length > 0) {
1721
+ const meta = this._state.meta;
1722
+ const found = states.find((productState) => {
1723
+ return productState[meta.scene_product_index] === sceneProductID;
1724
+ });
1725
+ return found ? found : null;
1726
+ }
1727
+ return null;
1728
+ }
1729
+ /**
1730
+ * Search and return the data for the provided Scene Product ID
1731
+ * if not found, will return null
1732
+ * @param sceneProductID
1733
+ */
1734
+ findSceneProduct(sceneProductID) {
1735
+ const found = this.findSceneProductIndex(sceneProductID);
1736
+ if (found) {
1737
+ const meta = this._state.meta;
1738
+ const data = {
1739
+ scene_product_id: found[meta.scene_product_index],
1740
+ product_variation_id: found[meta.product_variation_index],
1741
+ meta_data: {
1742
+ augment: true
1743
+ }
1744
+ };
1745
+ // include the meta-data
1746
+ if (found.length === 3) {
1747
+ data.meta_data.augment = found[meta.meta_index].augment || true;
1595
1748
  }
1596
- states.push(newData);
1749
+ return data;
1597
1750
  }
1751
+ return null;
1598
1752
  }
1599
1753
  /**
1600
1754
  * Iterate over the internal state data
@@ -1625,19 +1779,91 @@ class ConfiguratorState {
1625
1779
  });
1626
1780
  }
1627
1781
  }
1782
+ /**
1783
+ * @returns Returns the first reference of data in the stack, otherwise returns null
1784
+ */
1785
+ first() {
1786
+ const states = this._state.states;
1787
+ if (states.length > 0) {
1788
+ const meta = this._state.meta;
1789
+ const found = states.find((productState) => {
1790
+ const check = productState[meta.scene_product_index];
1791
+ // ensure the data contains valid elements
1792
+ return check !== null && check !== undefined;
1793
+ });
1794
+ if (!found) {
1795
+ return null;
1796
+ }
1797
+ const data = {
1798
+ scene_product_id: found[meta.scene_product_index],
1799
+ product_variation_id: found[meta.product_variation_index],
1800
+ meta_data: {
1801
+ augment: true
1802
+ }
1803
+ };
1804
+ // include the meta-data
1805
+ if (found.length === 3) {
1806
+ data.meta_data.augment = found[meta.meta_index].augment || true;
1807
+ }
1808
+ return data;
1809
+ }
1810
+ return null;
1811
+ }
1628
1812
  get length() {
1629
1813
  return this._state.states.length;
1630
1814
  }
1815
+ /**
1816
+ * Decodes and returns an instance of ConfiguratorState from a previously
1817
+ * encoded state
1818
+ * @param state - The previously encoded state as a Base64 String
1819
+ * @returns - ConfiguratorState instance
1820
+ */
1631
1821
  static decode(state) {
1632
1822
  return new ConfiguratorState(state);
1633
1823
  }
1824
+ /**
1825
+ * Generates a new ConfiguratorState instance from all SceneProducts and default
1826
+ * variations from the provided Scene ID
1827
+ * @param sceneID - the Scene ID to generate
1828
+ * @returns - Promise that resolves into a ConfiguratorState instance
1829
+ */
1830
+ static decodeScene(sceneID = null) {
1831
+ return new Promise((accept, reject) => {
1832
+ const configState = new ConfiguratorState();
1833
+ if (!sceneID) {
1834
+ return reject(new Error("ConfiguratorState.decodeScene(sceneID) - sceneID must be defined"));
1835
+ }
1836
+ const scene = new plattar_api_1.Scene(sceneID);
1837
+ scene.include(plattar_api_1.SceneProduct);
1838
+ scene.include(plattar_api_1.SceneProduct.include(plattar_api_1.Product));
1839
+ scene.get().then((scene) => {
1840
+ const sceneProducts = scene.relationships.filter(plattar_api_1.SceneProduct);
1841
+ // nothing to do if no AR components can be found
1842
+ if (sceneProducts.length <= 0) {
1843
+ return accept(configState);
1844
+ }
1845
+ // add out scene models
1846
+ sceneProducts.forEach((sceneProduct) => {
1847
+ const product = sceneProduct.relationships.find(plattar_api_1.Product);
1848
+ if (product && product.attributes.product_variation_id) {
1849
+ configState.setSceneProduct(sceneProduct.id, product.attributes.product_variation_id);
1850
+ }
1851
+ });
1852
+ accept(configState);
1853
+ }).catch(reject);
1854
+ });
1855
+ }
1856
+ /**
1857
+ * Encode and return the internal ConfiguratorState as a Base64 String
1858
+ * @returns - Base64 String
1859
+ */
1634
1860
  encode() {
1635
1861
  return btoa(JSON.stringify(this._state));
1636
1862
  }
1637
1863
  }
1638
1864
  exports.ConfiguratorState = ConfiguratorState;
1639
1865
 
1640
- },{}],15:[function(require,module,exports){
1866
+ },{"@plattar/plattar-api":42}],15:[function(require,module,exports){
1641
1867
  "use strict";
1642
1868
  Object.defineProperty(exports, "__esModule", { value: true });
1643
1869
  exports.Util = void 0;
@@ -1727,7 +1953,7 @@ exports.Util = Util;
1727
1953
  },{}],16:[function(require,module,exports){
1728
1954
  "use strict";
1729
1955
  Object.defineProperty(exports, "__esModule", { value: true });
1730
- exports.default = "1.123.6";
1956
+ exports.default = "1.123.7";
1731
1957
 
1732
1958
  },{}],17:[function(require,module,exports){
1733
1959
  "use strict";