|
| 1 | +/** |
| 2 | + * @title GCDS loader |
| 3 | + * @overview Quick script to load latest version of GCDS verison that tested by GCWeb |
| 4 | + * @license wet-boew.github.io/wet-boew/License-en.html / wet-boew.github.io/wet-boew/Licence-fr.html |
| 5 | + * @author @duboisp |
| 6 | + */ |
| 7 | +( function( wnd, doc, create, append, escape ) { |
| 8 | +"use strict"; |
| 9 | + |
| 10 | + |
| 11 | +// |
| 12 | +// Configuration updated during the build process with info in package.json |
| 13 | +// |
| 14 | +const gcdsVersion = "@gcdsComponentVersion@", // pkg.peerDependencies[ "@cdssnc/gcds-components" ] |
| 15 | + gcdsSriJs = "@gcdsSriJs@", // pkg[ "io.github.wet-boew" ].gcdsSriJs |
| 16 | + gcdsSriCss = "@gcdsSriCss@"; // pkg[ "io.github.wet-boew" ].gcdsSriCss |
| 17 | + |
| 18 | + |
| 19 | +// |
| 20 | +// General configuration used to detect and load GCSD |
| 21 | +// |
| 22 | +const gcdsBasePath = "https://cdn.design-system.alpha.canada.ca/@cdssnc/gcds-components@", |
| 23 | + gcdsSuffixJs = "/dist/gcds/gcds.esm.js", |
| 24 | + gcdsSuffixCss = "/dist/gcds/gcds.css", |
| 25 | + txtScript = "script", |
| 26 | + txtLink = "link", |
| 27 | + blockingState = "render"; |
| 28 | + |
| 29 | + |
| 30 | +// |
| 31 | +// Check if GCDS lib are already defined in the header |
| 32 | +// |
| 33 | +let traceFound = doc.querySelector( txtLink + "[href$=" + escape( gcdsSuffixCss ) + "]," + txtScript + "[src$=" + escape( gcdsSuffixJs ) + "]" ); |
| 34 | + |
| 35 | + |
| 36 | +// |
| 37 | +// Import the GCDS libs |
| 38 | +// |
| 39 | +if ( !traceFound && gcdsSriJs && gcdsSriCss ) { |
| 40 | + let scrGcds = create( txtScript ); |
| 41 | + scrGcds.blocking = blockingState; |
| 42 | + scrGcds.type = "module"; |
| 43 | + scrGcds.integrity = gcdsSriJs; |
| 44 | + scrGcds.src = gcdsBasePath + gcdsVersion + gcdsSuffixJs; |
| 45 | + append( scrGcds ); |
| 46 | + |
| 47 | + let lnkGcds = create( txtLink ); |
| 48 | + lnkGcds.blocking = blockingState; |
| 49 | + lnkGcds.rel = "stylesheet"; |
| 50 | + lnkGcds.crossOrigin = "anonymous"; |
| 51 | + lnkGcds.integrity = gcdsSriCss; |
| 52 | + lnkGcds.href = gcdsBasePath + gcdsVersion + gcdsSuffixCss; |
| 53 | + append( lnkGcds ); |
| 54 | + |
| 55 | + |
| 56 | + // |
| 57 | + // Set GCDS version globally once loaded |
| 58 | + // |
| 59 | + wnd.addEventListener( "appload", ( event ) => { |
| 60 | + if ( event.detail.namespace === "gcds" ) { |
| 61 | + wnd.wbgcds = gcdsVersion; |
| 62 | + } |
| 63 | + } ); |
| 64 | +} |
| 65 | + |
| 66 | +} )( window, document, document.createElement, document.head.appendChild, CSS.escape ); |
0 commit comments