Source: media/data/GalleryConfigLoader.js

  1. import { IndexBinder } from "../../map/importer/binder/IndexBinder.js";
  2. import { ServiceDataRequest } from "./utils.js";
  3. import { MediaGalleryQueryParameter } from "../grid/MediaGalleryQueryParameter.js";
  4. export { GalleryConfigLoader }
  5. /**
  6. * Class representing the loading of the gallery configuration.
  7. */
  8. class GalleryConfigLoader {
  9. /**
  10. * Create the gallery configuration loading object.
  11. */
  12. constructor() {
  13. this.catalogConfigDict;
  14. this.galleryConfigDict;
  15. this.queryConfigDict;
  16. }
  17. /**
  18. * Return the service configuration dictionary, regardless of whether it was loaded via catalogue or somewhere else (future).
  19. */
  20. get serviceConfigDict() {
  21. const serviceConfigDict = {}
  22. if (this.catalogConfigDict)
  23. serviceConfigDict['catalogConfigDict'] = this.catalogConfigDict
  24. return serviceConfigDict
  25. }
  26. /**
  27. * Load the entire gallery configuration based on an identifier.
  28. * This identifier is known to a catalog response.
  29. * With it, the gallery service configuration can be loaded from the catalog.
  30. * Furthermore, it contains the URL to the gallery.config.json file, that is finally loaded too.
  31. * @param {*} sourceID - Identifier for the gallery source data.
  32. * @returns Promise with the gallery configuration.
  33. */
  34. loadConfigData(sourceID) {
  35. this._loadQueryConfig();
  36. sourceID = sourceID || this.queryConfigDict['sourceID'];
  37. return this._loadConfigFromCatalog(sourceID).then((catalogConfig) => {
  38. const caseFallback = catalogConfig == null;
  39. if (caseFallback) {
  40. return Promise.reject('Info is not in catalog')
  41. }
  42. return catalogConfig
  43. }).catch((e) => {
  44. const message = 'Catalog does not provide data'
  45. console.error(`${message}:`, e ? e : 'Error when loading catalog');
  46. throw new Error(message);
  47. }).then((config) => {
  48. const id = Object.keys(config)[0];
  49. const galleryConfigUrlCatalog = config[id]?.['galleryConfig']?.['url'];
  50. const galleryConfigUrl = galleryConfigUrlCatalog;
  51. return this._loadGalleryConfig(galleryConfigUrl)
  52. }).catch((e) => {
  53. const message = 'Gallery configuration cannot be loaded'
  54. console.error(`${message}:`, e ? e : 'Error when loading gallery configuration file');
  55. throw new Error(message);
  56. })
  57. }
  58. _checkForEmptyResponse(config) {
  59. if (config.constructor == Object) {
  60. return Object.keys(config).length === 0
  61. }
  62. else if (config.constructor == Array) {
  63. return config.length === 0
  64. }
  65. return null
  66. }
  67. _loadConfigFromCatalog(idCatalogLayer) {
  68. const binder = new IndexBinder();
  69. return binder.fetchLayers({ query: `(uniqueId:"${idCatalogLayer}")`, hits: 10 }).then((catalogConfig) => {
  70. this.catalogConfigDict = this._checkForEmptyResponse(catalogConfig) ? null : catalogConfig
  71. return this.catalogConfigDict
  72. })
  73. }
  74. _loadQueryConfig() {
  75. const GalleryQueryParameter = new MediaGalleryQueryParameter();
  76. this.queryConfigDict = GalleryQueryParameter.queryParameterDict;
  77. }
  78. _loadGalleryConfig(galleryConfigUrl) {
  79. return ServiceDataRequest.requestJSON(decodeURIComponent(galleryConfigUrl)).then((data) => {
  80. this.galleryConfigDict = data;
  81. return data
  82. })
  83. }
  84. }