web-apps/vendor/bootstrap/less/plugins/gray.js
2020-05-21 19:29:40 +03:00

52 lines
1.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

let grays = {}
//
// Helper Functions
//
function lookupVariable(context, variableName) {
const { frames, importantScope } = context
return tree.Variable.prototype.find(frames, frame => {
const { value, important } = frame.variable(variableName) || {}
if (value === undefined)
return
if (important && importantScope[importantScope.length - 1])
importantScope[importantScope.length - 1].important = important
return value.eval(context)
})
}
// @TODO: [@calvinjuarez] unify this function between files, maybe even canonize it as a
// `Ruleset`/`DetachedRuleset` method at some point.
function rulesetToMap(context, { ruleset: { rules } } = { ruleset: { rules: [] } }) {
const map = {}
rules.forEach(rule => {
// Not exactly sure how to handle other types (or if they should be handled at all).
if (! (rule instanceof tree.Declaration))
return
const { name: key, value } = rule.eval(context)
map[key] = value
})
return map
}
//
// Less Functions
//
functions.add('gray', function ({ value: colorName } = { value: '100' }) {
// If `grays` hasnt been defined yet, set it to the value of `@grays`.
if (Object.keys(grays).length === 0)
grays = rulesetToMap(this.context, lookupVariable(this.context, '@grays'))
return grays[colorName]
})