naive-ui/packages/utils/installThemeAwarableProperty.js

59 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-12-06 23:11:15 +08:00
function createThemeChangeHandler (property) {
return function (theme) {
if (property.handleThemeChange) {
property.handleThemeChange(theme)
}
}
}
function getTheme (componentInstance, property, configProviderToWatchThemeChange) {
let cursor = componentInstance
let lastThemedConfigProvider = null
let theme = null
while (cursor) {
const name = cursor.$options.name
if (name === 'NConfigProvider') {
while (cursor) {
if (cursor.synthesizedTheme) {
theme = cursor.synthesizedTheme
lastThemedConfigProvider = cursor
}
cursor = cursor.NConfigProvider
}
} else {
cursor = cursor.$parent
}
}
if (lastThemedConfigProvider && property && configProviderToWatchThemeChange) {
if (!configProviderToWatchThemeChange.has(lastThemedConfigProvider)) {
lastThemedConfigProvider.$watch('synthesizedTheme', createThemeChangeHandler(property))
configProviderToWatchThemeChange.add(lastThemedConfigProvider)
}
}
return { theme, configProvider: lastThemedConfigProvider }
2019-12-06 23:11:15 +08:00
}
function install (Vue, property, name) {
const configProviderToWatchThemeChange = new WeakSet()
property.getTheme = getTheme
const prototypeProxy = new Proxy(
function () {
return property
},
{
apply (target, thisArg, argumentsList) {
if (thisArg instanceof Vue) {
const { theme } = getTheme(thisArg, property, configProviderToWatchThemeChange)
property.theme = theme
2019-12-06 23:11:15 +08:00
}
return target.bind(thisArg)(...argumentsList)
}
}
)
Object.defineProperty(Vue.prototype, name, {
get: prototypeProxy
})
}
export { getTheme, install }