import { AEM_MODE } from './Constants' ; import { PathUtils } from './PathUtils' ; import MetaProperty from './MetaProperty' ; export class AuthoringUtils { private readonly _apiDomain : string | null ; /**
     * Base path for editor clientlibs.
     */ public static readonly EDITOR_CLIENTLIB_PATH = '/etc.clientlibs/cq/gui/components/authoring/editors/clientlibs/' ; /**
     * Authoring libraries.
     */ public static readonly AUTHORING_LIBRARIES = { JS : [ AuthoringUtils . EDITOR_CLIENTLIB_PATH + 'internal/messaging.js' , AuthoringUtils . EDITOR_CLIENTLIB_PATH + 'utils.js' , AuthoringUtils . EDITOR_CLIENTLIB_PATH + 'internal/page.js' , AuthoringUtils . EDITOR_CLIENTLIB_PATH + 'internal/pagemodel/messaging.js' ] , CSS : [ AuthoringUtils . EDITOR_CLIENTLIB_PATH + 'internal/page.css' ] , META : { [ MetaProperty . WCM_DATA_TYPE ] : 'JSON' } } ; private constructor ( domain ) { this . _apiDomain = domain ; } private getApiDomain ( ) { return this . _apiDomain ; } returns public getAemLibraries ( ) { const docFragment = document . createDocumentFragment ( ) ; if ( ! AuthoringUtils . isRemoteApp ( ) || ! AuthoringUtils . isEditMode ( ) ) { return docFragment ; } const jsUrls = this . prependDomain ( AuthoringUtils . AUTHORING_LIBRARIES . JS ) ; const cssUrls = this . prependDomain ( AuthoringUtils . AUTHORING_LIBRARIES . CSS ) ; const metaInfo = AuthoringUtils . AUTHORING_LIBRARIES . META ; docFragment . append ( this . generateScriptElements ( jsUrls ) ) ; docFragment . append ( this . generateLinkElements ( cssUrls ) ) ; docFragment . append ( this . generateMetaElements ( metaInfo ) ) ; return docFragment ; } param docFragment param callback public setOnLoadCallback ( docFragment , callback ) { const scriptTags = docFragment . querySelectorAll ( 'script' ) ; if ( ! scriptTags . length ) { callback ( ) ; } else { scriptTags [ scriptTags . length - 1 ] . onload = ( ) => { callback ( ) ; } ; } } private generateMetaElements ( metaInfo ) { const docFragment = document . createDocumentFragment ( ) ; Object . entries ( metaInfo ) . forEach ( ( entry ) => { const [ key , val ] = entry ; const metaElement = document . createElement ( 'meta' ) ; metaElement . setAttribute ( 'property' , key ) ; metaElement . content = val ; docFragment . appendChild ( metaElement ) ; } ) ; return docFragment ; } private generateLinkElements ( cssUrls ) { const docFragment = document . createDocumentFragment ( ) ; cssUrls . forEach ( ( url ) => { const linkElement = document . createElement ( 'link' ) ; linkElement . type = 'text/css' ; linkElement . rel = 'stylesheet' ; linkElement . href = url ; docFragment . appendChild ( linkElement ) ; } ) ; return docFragment ; } private generateScriptElements ( jsUrls ) { const docFragment = document . createDocumentFragment ( ) ; jsUrls . forEach ( ( url ) => { const htmlScriptElement = document . createElement ( 'script' ) ; htmlScriptElement . type = 'text/javascript' ; htmlScriptElement . src = url ; htmlScriptElement . async = false ; docFragment . appendChild ( htmlScriptElement ) ; } ) ; return docFragment ; } private static isMode ( mode ) { const viaMetaProperty = PathUtils . getMetaPropertyValue ( MetaProperty . WCM_MODE ) === mode ; const viaQueryParam = PathUtils . isBrowser ( ) && ( AuthoringUtils . getWCMModeFromURL ( ) === mode ) ; return viaMetaProperty || viaQueryParam ; } returns public static isEditMode ( ) { return AuthoringUtils . isMode ( AEM_MODE . EDIT ) ; } returns public static isPreviewMode ( ) { return AuthoringUtils . isMode ( AEM_MODE . PREVIEW ) ; } returns public static isRemoteApp ( ) { try { const url = new URL ( PathUtils . getCurrentURL ( ) ) ; return ! ! url . searchParams . get ( MetaProperty . WCM_MODE ) ; } catch ( e ) { } return false ; } private returns private static getWCMModeFromURL ( ) { let url ; try { url = new URL ( PathUtils . getCurrentURL ( ) ) ; return url . searchParams . get ( MetaProperty . WCM_MODE ) || '' ; } catch ( e ) { } return '' ; } private returns private prependDomain ( libraries ) { const result = [ ] ; const domain = this . getApiDomain ( ) ; libraries . forEach ( ( library ) => { result . push ( `${ domain || '' }${ library }` ) ; } ) ; return result ; } returns public static isInEditor ( ) { return AuthoringUtils . isEditMode ( ) || AuthoringUtils . isPreviewMode ( ) || AuthoringUtils . isRemoteApp ( ) ; } }