-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
updated sizeMapping to use sizeConfig and support labels #1772
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
455466a
updated sizeMapping to use sizeConfig and support labels
snapwich 408958f
added new tests for labels and sizes w/ sizeConfig when making auction
snapwich fc323b9
made some names clearer and added type to labels for sizeMapping
snapwich 2b6fcb5
make error message more descriptive in adaptermanager
snapwich 79382ae
remove extra line in adpatermanager
snapwich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯 for dropping all this code!