-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated sizeMapping to use sizeConfig and support labels (#1772)
* updated sizeMapping to use sizeConfig and support labels * added new tests for labels and sizes w/ sizeConfig when making auction * made some names clearer and added type to labels for sizeMapping * make error message more descriptive in adaptermanager * remove extra line in adpatermanager
- Loading branch information
Showing
8 changed files
with
470 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,84 @@ | ||
import { config } from 'src/config'; | ||
|
||
let sizeConfig = []; | ||
|
||
/** | ||
* @module sizeMapping | ||
* @typedef {object} SizeConfig | ||
* | ||
* @property {string} [mediaQuery] A CSS media query string that will to be interpreted by window.matchMedia. If the | ||
* media query matches then the this config will be active and sizesSupported will filter bid and adUnit sizes. If | ||
* this property is not present then this SizeConfig will only be active if triggered manually by a call to | ||
* pbjs.setConfig({labels:['label']) specifying one of the labels present on this SizeConfig. | ||
* @property {Array<Array>} sizesSupported The sizes to be accepted if this SizeConfig is enabled. | ||
* @property {Array<string>} labels The active labels to match this SizeConfig to an adUnits and/or bidders. | ||
*/ | ||
import * as utils from './utils'; | ||
let _win; | ||
|
||
function mapSizes(adUnit) { | ||
if (!isSizeMappingValid(adUnit.sizeMapping)) { | ||
return adUnit.sizes; | ||
} | ||
const width = getScreenWidth(); | ||
if (!width) { | ||
// size not detected - get largest value set for desktop | ||
const mapping = adUnit.sizeMapping.reduce((prev, curr) => { | ||
return prev.minWidth < curr.minWidth ? curr : prev; | ||
}); | ||
if (mapping.sizes && mapping.sizes.length) { | ||
return mapping.sizes; | ||
} | ||
return adUnit.sizes; | ||
} | ||
let sizes = ''; | ||
const mapping = adUnit.sizeMapping.find(sizeMapping => { | ||
return width >= sizeMapping.minWidth; | ||
}); | ||
if (mapping && mapping.sizes && mapping.sizes.length) { | ||
sizes = mapping.sizes; | ||
utils.logMessage(`AdUnit : ${adUnit.code} resized based on device width to : ${sizes}`); | ||
} else { | ||
utils.logMessage(`AdUnit : ${adUnit.code} not mapped to any sizes for device width. This request will be suppressed.`); | ||
} | ||
return sizes; | ||
} | ||
|
||
function isSizeMappingValid(sizeMapping) { | ||
if (utils.isArray(sizeMapping) && sizeMapping.length > 0) { | ||
return true; | ||
} | ||
utils.logInfo('No size mapping defined'); | ||
return false; | ||
/** | ||
* | ||
* @param {Array<SizeConfig>} config | ||
*/ | ||
export function setSizeConfig(config) { | ||
sizeConfig = config; | ||
} | ||
config.getConfig('sizeConfig', config => setSizeConfig(config.sizeConfig)); | ||
|
||
function getScreenWidth(win) { | ||
var w = win || _win || window; | ||
var d = w.document; | ||
/** | ||
* Resolves the unique set of the union of all sizes and labels that are active from a SizeConfig.mediaQuery match | ||
* @param {Array<string>} labels Labels specified on adUnit or bidder | ||
* @param {boolean} labelAll if true, all labels must match to be enabled | ||
* @param {Array<string>} activeLabels Labels passed in through requestBids | ||
* @param {Array<Array<number>>} sizes Sizes specified on adUnit | ||
* @param {Array<SizeConfig>} configs | ||
* @returns {{labels: Array<string>, sizes: Array<Array<number>>}} | ||
*/ | ||
export function resolveStatus({labels = [], labelAll = false, activeLabels = []} = {}, sizes = [], configs = sizeConfig) { | ||
let maps = evaluateSizeConfig(configs); | ||
|
||
if (w.innerWidth) { | ||
return w.innerWidth; | ||
} else if (d.body.clientWidth) { | ||
return d.body.clientWidth; | ||
} else if (d.documentElement.clientWidth) { | ||
return d.documentElement.clientWidth; | ||
let filteredSizes; | ||
if (maps.shouldFilter) { | ||
filteredSizes = sizes.filter(size => maps.sizesSupported[size]); | ||
} else { | ||
filteredSizes = sizes; | ||
} | ||
return 0; | ||
} | ||
|
||
function setWindow(win) { | ||
_win = win; | ||
return { | ||
active: filteredSizes.length > 0 && ( | ||
labels.length === 0 || ( | ||
(!labelAll && ( | ||
labels.some(label => maps.labels[label]) || | ||
labels.some(label => activeLabels.includes(label)) | ||
)) || | ||
(labelAll && ( | ||
labels.reduce((result, label) => !result ? result : ( | ||
maps.labels[label] || activeLabels.includes(label) | ||
), true) | ||
)) | ||
) | ||
), | ||
sizes: filteredSizes | ||
}; | ||
} | ||
|
||
export { mapSizes, getScreenWidth, setWindow }; | ||
function evaluateSizeConfig(configs) { | ||
return configs.reduce((results, config) => { | ||
if ( | ||
typeof config === 'object' && | ||
typeof config.mediaQuery === 'string' && | ||
matchMedia(config.mediaQuery).matches | ||
) { | ||
if (Array.isArray(config.sizesSupported)) { | ||
results.shouldFilter = true; | ||
} | ||
['labels', 'sizesSupported'].forEach( | ||
type => (config[type] || []).forEach( | ||
thing => results[type][thing] = true | ||
) | ||
); | ||
} | ||
return results; | ||
}, { | ||
labels: {}, | ||
sizesSupported: {}, | ||
shouldFilter: false | ||
}); | ||
} |
Oops, something went wrong.