Commit 0f6532a8 authored by Kyle Keating's avatar Kyle Keating Committed by Kyle Keating

Change configFrom() to configDefinitions()

The main difference is that configFrom returned config key:value pairs, but now configDefinitions returns key:object pairs. Where the object may hold additional flags and methods for each specific config value. Currently the only method included is valueFn() which gets the value from its source. Others will follow.

Additionally:
- Remove `experimental` from configDefinitions/configFrom as it is not used
parent 8b427ff6
...@@ -3,6 +3,14 @@ import { toBoolean } from '../../shared/type-coercions'; ...@@ -3,6 +3,14 @@ import { toBoolean } from '../../shared/type-coercions';
/** /**
* @typedef {'sidebar'|'notebook'|'annotator'|'all'} AppContext * @typedef {'sidebar'|'notebook'|'annotator'|'all'} AppContext
*
* @typedef {import('./settings').SettingsGetters} SettingsGetters
*
* @typedef ConfigDefinition
* @prop {(settings: SettingsGetters) => any} getValue -
* Method to retrieve the value from the incoming source
*
* @typedef {Record<string, ConfigDefinition>} ConfigDefinitionMap
*/ */
/** /**
...@@ -60,61 +68,83 @@ function configurationKeys(appContext) { ...@@ -60,61 +68,83 @@ function configurationKeys(appContext) {
} }
/** /**
* Reads the Hypothesis configuration from the environment. * Definitions of configuration keys
* * @type {ConfigDefinitionMap}
* @param {string[]} settingsKeys - List of settings that should be returned.
* @param {Window} window_ - The Window object to read config from.
*/ */
function configFrom(settingsKeys, window_) { const configDefinitions = {
const settings = settingsFrom(window_); annotations: {
const allConfigSettings = { getValue: settings => settings.annotations,
annotations: settings.annotations, },
appType: settings.hostPageSetting('appType', { appType: {
allowInBrowserExt: true, getValue: settings =>
}), settings.hostPageSetting('appType', {
branding: settings.hostPageSetting('branding'), allowInBrowserExt: true,
// URL of the client's boot script. Used when injecting the client into }),
// child iframes. },
clientUrl: settings.clientUrl, branding: {
enableExperimentalNewNoteButton: settings.hostPageSetting( getValue: settings => settings.hostPageSetting('branding'),
'enableExperimentalNewNoteButton' },
), // URL of the client's boot script. Used when injecting the client into
experimental: settings.hostPageSetting('experimental', { // child iframes.
defaultValue: {}, clientUrl: {
}), getValue: settings => settings.clientUrl,
group: settings.group, },
focus: settings.hostPageSetting('focus'), enableExperimentalNewNoteButton: {
theme: settings.hostPageSetting('theme'), getValue: settings =>
usernameUrl: settings.hostPageSetting('usernameUrl'), settings.hostPageSetting('enableExperimentalNewNoteButton'),
onLayoutChange: settings.hostPageSetting('onLayoutChange'), },
openSidebar: settings.hostPageSetting('openSidebar', { group: {
allowInBrowserExt: true, getValue: settings => settings.group,
// Coerce value to a boolean because it may come from via as a string },
coerce: toBoolean, focus: {
}), getValue: settings => settings.hostPageSetting('focus'),
query: settings.query, },
requestConfigFromFrame: settings.hostPageSetting('requestConfigFromFrame'), theme: {
services: settings.hostPageSetting('services'), getValue: settings => settings.hostPageSetting('theme'),
showHighlights: settings.showHighlights, },
notebookAppUrl: settings.notebookAppUrl, usernameUrl: {
sidebarAppUrl: settings.sidebarAppUrl, getValue: settings => settings.hostPageSetting('usernameUrl'),
// Subframe identifier given when a frame is being embedded into },
// by a top level client onLayoutChange: {
subFrameIdentifier: settings.hostPageSetting('subFrameIdentifier', { getValue: settings => settings.hostPageSetting('onLayoutChange'),
allowInBrowserExt: true, },
}), openSidebar: {
externalContainerSelector: settings.hostPageSetting( getValue: settings =>
'externalContainerSelector' settings.hostPageSetting('openSidebar', {
), allowInBrowserExt: true,
}; coerce: toBoolean,
}),
// Only return what we asked for },
const resultConfig = {}; query: {
settingsKeys.forEach(key => { getValue: settings => settings.query,
resultConfig[key] = allConfigSettings[key]; },
}); requestConfigFromFrame: {
return resultConfig; getValue: settings => settings.hostPageSetting('requestConfigFromFrame'),
} },
services: {
getValue: settings => settings.hostPageSetting('services'),
},
showHighlights: {
getValue: settings => settings.showHighlights,
},
notebookAppUrl: {
getValue: settings => settings.notebookAppUrl,
},
sidebarAppUrl: {
getValue: settings => settings.sidebarAppUrl,
},
// Sub-frame identifier given when a frame is being embedded into
// by a top level client
subFrameIdentifier: {
getValue: settings =>
settings.hostPageSetting('subFrameIdentifier', {
allowInBrowserExt: true,
}),
},
externalContainerSelector: {
getValue: settings => settings.hostPageSetting('externalContainerSelector'),
},
};
/** /**
* Return the configuration for a given application context. * Return the configuration for a given application context.
...@@ -122,9 +152,16 @@ function configFrom(settingsKeys, window_) { ...@@ -122,9 +152,16 @@ function configFrom(settingsKeys, window_) {
* @param {AppContext} [appContext] - The name of the app. * @param {AppContext} [appContext] - The name of the app.
*/ */
export function getConfig(appContext = 'annotator', window_ = window) { export function getConfig(appContext = 'annotator', window_ = window) {
const settings = settingsFrom(window_);
const config = {};
// Filter the config based on the application context as some config values // Filter the config based on the application context as some config values
// may be inappropriate or erroneous for some applications. // may be inappropriate or erroneous for some applications.
const filteredKeys = configurationKeys(appContext); let filteredKeys = configurationKeys(appContext);
const config = configFrom(filteredKeys, window_); filteredKeys.forEach(name => {
const configDef = configDefinitions[name];
// Get the value from the configuration source
config[name] = configDef.getValue(settings);
});
return config; return config;
} }
...@@ -3,6 +3,22 @@ import { parseJsonConfig } from '../../boot/parse-json-config'; ...@@ -3,6 +3,22 @@ import { parseJsonConfig } from '../../boot/parse-json-config';
import configFuncSettingsFrom from './config-func-settings-from'; import configFuncSettingsFrom from './config-func-settings-from';
import isBrowserExtension from './is-browser-extension'; import isBrowserExtension from './is-browser-extension';
/**
* @typedef SettingsGetters
* @prop {string|null} annotations
* @prop {string|null} query
* @prop {string|null} group
* @prop {string} showHighlights
* @prop {string} clientUrl
* @prop {string} sidebarAppUrl
* @prop {string} notebookAppUrl
* @prop {(name: string, options?: Object) => (string|null)} hostPageSetting
*/
/**
* @return {SettingsGetters}
*/
export default function settingsFrom(window_) { export default function settingsFrom(window_) {
const jsonConfigs = parseJsonConfig(window_.document); const jsonConfigs = parseJsonConfig(window_.document);
const configFuncSettings = configFuncSettingsFrom(window_); const configFuncSettings = configFuncSettingsFrom(window_);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment