Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
FentPams committed Aug 12, 2024
1 parent 5f942a2 commit 54414ff
Showing 1 changed file with 55 additions and 5 deletions.
60 changes: 55 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
* governing permissions and limitations under the License.
*/

/**
* Set isDebugEnabled accordingly based on the URL and plugin options.
* @param {Object} url window.location
* @param {Object} pluginOptions with default options and custom options
* @returns {Boolean} whether debug mode is enabled
*/
let isDebugEnabled;
export function setDebugMode(url, pluginOptions) {
const { host, hostname, origin } = url;
Expand All @@ -29,6 +35,12 @@ export function debug(...args) {
}
}

/**
* Default options for the plugin.
* could be extended and overridden by passing custom options
* e.g. 'abtest' instead of 'experiment'
* by passing { experimentsMetaTagPrefix: 'abtest' } in custom options
*/
export const DEFAULT_OPTIONS = {

// Audiences related properties
Expand Down Expand Up @@ -199,10 +211,11 @@ function getAllSectionMeta(block, scope) {
}

/**
* Replaces element with content from path
* @param {String} path
* @param {HTMLElement} el
* @return Returns the path that was loaded or null if the loading failed
* Replaces the inner content of an element with content from a specified path.
* @param {String} path - the url path to load the content from
* @param {HTMLElement} el - the element to replace the content of
* @param {String} selector - taget CSS selector for fragment replacement
* @return The path that was loaded or null if the loading failed
*/
async function replaceInner(path, el, selector) {
try {
Expand Down Expand Up @@ -516,6 +529,7 @@ async function applyAllModifications(
}
}));

// Fragment-level modifications
if (pageMetadata.manifest) {
let entries = await getManifestEntriesForCurrentPage(pageMetadata.manifest);
if (entries) {
Expand All @@ -537,6 +551,12 @@ async function applyAllModifications(
return configs;
}

/**
* Aggregates the entries into a single object.
* @param {String} type experiment, campaign or audience
* @param {String[]} allowedMultiValuesProperties properties that can have multiple values
* @returns object with the aggregated entries
*/
function aggregateEntries(type, allowedMultiValuesProperties) {
return (entries) => entries.reduce((aggregator, entry) => {
Object.entries(entry).forEach(([key, value]) => {
Expand Down Expand Up @@ -672,7 +692,9 @@ async function getExperimentConfig(pluginOptions, metadata, overrides) {
}

/**
* Parses the campaign manifest.
* Parses the experiemnt manifest
* @param {Object} entries fragment manifest entries
* @returns {Object} parsed fragment manifest that grouped by experiment
*/
function parseExperimentManifest(entries) {
return Object.values(Object.groupBy(
Expand All @@ -687,6 +709,12 @@ function getUrlFromExperimentConfig(config) {
: null;
}

/**
* Runs the experiment on page/section/fragment.
* Page and Section level modifications are applied immediately,
* while fragment level modifications will be watched by observer.
* And dispatches custom events for further handling if needed.
*/
async function runExperiment(document, pluginOptions) {
return applyAllModifications(
pluginOptions.experimentsMetaTagPrefix,
Expand Down Expand Up @@ -790,6 +818,12 @@ function getUrlFromCampaignConfig(config) {
: null;
}

/**
* Runs the campaign on page/section/fragment.
* Page and Section level modifications are applied immediately,
* while fragment level modifications will be watched by observer.
* And dispatches custom events for further handling if needed.
*/
async function runCampaign(document, pluginOptions) {
return applyAllModifications(
pluginOptions.campaignsMetaTagPrefix,
Expand Down Expand Up @@ -872,6 +906,12 @@ function getUrlFromAudienceConfig(config) {
: null;
}

/**
* Serves the audience on page/section/fragment.
* Page and Section level modifications are applied immediately,
* while fragment level modifications will be watched by observer.
* And dispatches custom events for further handling if needed.
*/
async function serveAudience(document, pluginOptions) {
document.body.dataset.audiences = Object.keys(pluginOptions.audiences).join(',');
return applyAllModifications(
Expand Down Expand Up @@ -901,6 +941,11 @@ async function serveAudience(document, pluginOptions) {
);
}

/**
* Loads and initializes plugins and functionalities eagerly,
* @param {Document} document The HTML document to be processed
* @param {Object} [options={}] Custom options passed from customer
*/
export async function loadEager(document, options = {}) {
const pluginOptions = { ...DEFAULT_OPTIONS, ...options };
setDebugMode(window.location, pluginOptions);
Expand All @@ -916,14 +961,19 @@ export async function loadEager(document, options = {}) {
ns.campaign = ns.campaigns.find((e) => e.type === 'page');
}

/**
* Loads the experimentation pill when debug mode is enabled
*/
export async function loadLazy(document, options = {}) {
const pluginOptions = { ...DEFAULT_OPTIONS, ...options };
// do not show the experimentation pill on prod domains
if (!isDebugEnabled) {
return;
}
// fetch the central-host preview script from github pages

Check warning on line 973 in src/index.js

View check run for this annotation

Codecov / codecov/patch

src/index.js#L973

Added line #L973 was not covered by tests
// eslint-disable-next-line import/no-unresolved
const preview = await import('https://opensource.adobe.com/aem-experimentation/preview.js');
// functions to be passed to the preview script
const context = {
getMetadata,
toClassName,
Expand Down

0 comments on commit 54414ff

Please sign in to comment.