diff --git a/blocks/aem-asset-selector/aem-asset-selector-util.js b/blocks/aem-asset-selector/aem-asset-selector-util.js index cd068d7df..2e633842e 100644 --- a/blocks/aem-asset-selector/aem-asset-selector-util.js +++ b/blocks/aem-asset-selector/aem-asset-selector-util.js @@ -1,15 +1,106 @@ -/* eslint-disable no-console */ +/* +Copyright 2023 Adobe. All rights reserved. +This file is licensed to you under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. You may obtain a copy +of the License at http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS +OF ANY KIND, either express or implied. See the License for the specific language +governing permissions and limitations under the License. +*/ + +/** + * @typedef Links + */ + +/** + * @typedef Asset + * @property {Links} _links Rels for the asset. Is expected to have a + * http://ns.adobe.com/adobecloud/rel/rendition rel for retrieving the + * asset's renditions, and a http://ns.adobe.com/adobecloud/rel/download + * rel for retrieving a URL to the asset's original content, which + * doesn't require authentication. + */ + +/** + * @typedef Rendition + * @property {string} type Content type of the rendition. + * @property {number} width Width, in pixels, of the rendition. + * @property {string} href Full URL to the rendition's binary. This URL + * will require authentication. + * @property {Links} _links Rels for the rendition. Is expected to have + * a http://ns.adobe.com/adobecloud/rel/download rel for retrieving a + * URL to the rendition's content, which doesn't require authentication. + */ + +/** + * @typedef AssetSelectorConfig + * @property {string} [imsClientId] If provided, will be used as the client ID + * when authenticating with IMS. + * @property {string} [repositoryId] If provided, will be used as the selected + * repository in the Asset Selector. + * @property {string} [imsOrgId] If provided, will be used as the IMS org to use + * when logging in through IMS. + * @property {string} [environment] If provided, will be the IMS environment to + * use when logging in through IMS. Should be STAGE or PROD. + * @property {function} [onAssetSelected] If provided, will be invoked with the + * repository metadata for the asset that was selected. + * @property {function} [onAssetDeselected] If provided, will be invoked with + * no arguments if the currently selected asset is deselected. + * @property {function} [onAccessTokenReceived] If provided, will be invoked + * when an IMS token is available for the user. May be invoked multiple times + * with the same token during the session. + */ + const AS_MFE = 'https://experience.adobe.com/solutions/CQ-assets-selectors/static-assets/resources/assets-selectors.js'; const IMS_ENV_STAGE = 'stg1'; const IMS_ENV_PROD = 'prod'; const API_KEY = 'franklin'; const WEB_TOOLS = 'https://master-sacred-dove.ngrok-free.app'; +const REL_DOWNLOAD = 'http://ns.adobe.com/adobecloud/rel/download'; +const REL_RENDITIONS = 'http://ns.adobe.com/adobecloud/rel/rendition'; // TODO: change this to Asset Link IMS client ID const IMS_CLIENT_ID = 'p66302-franklin'; +const ASSET_SELECTOR_ID = 'asset-selector'; let imsInstance = null; let imsEnvironment = IMS_ENV_PROD; +/** + * Logs a message to the console. + * @param {...any} theArgs Arguments to pass to the console log + * statement. + */ +function logMessage(...theArgs) { + // eslint-disable-next-line no-console + console.log.apply(null, theArgs); +} + +/** + * Retrieves the value of a rel from repository metadata. + * @param {Asset|Rendition} repositoryMetadata Metadata whose links + * will be used. + * @param {string} rel The rel to retrieve. + */ +function getRel(repositoryMetadata, rel) { + if (!repositoryMetadata) { + return undefined; + } + // eslint-disable-next-line no-underscore-dangle + return repositoryMetadata._links[rel]; +} + +/** + * Adds a new