mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
90 lines
1.6 KiB
JavaScript
90 lines
1.6 KiB
JavaScript
import { isObject, inBrowser } from '../utils';
|
|
import { mountComponent, usePopupState } from '../utils/mount-component';
|
|
import VanNotify from './Notify';
|
|
|
|
let timer;
|
|
let instance;
|
|
|
|
function parseOptions(message) {
|
|
return isObject(message) ? message : { message };
|
|
}
|
|
|
|
function initInstance() {
|
|
({ instance } = mountComponent({
|
|
setup() {
|
|
const { state, toggle } = usePopupState();
|
|
return () => (
|
|
<VanNotify
|
|
{...{
|
|
...state,
|
|
'onUpdate:show': toggle,
|
|
}}
|
|
/>
|
|
);
|
|
},
|
|
}));
|
|
}
|
|
|
|
function Notify(options) {
|
|
if (!inBrowser) {
|
|
return;
|
|
}
|
|
|
|
if (!instance) {
|
|
initInstance();
|
|
}
|
|
|
|
options = {
|
|
...Notify.currentOptions,
|
|
...parseOptions(options),
|
|
};
|
|
|
|
instance.open(options);
|
|
clearTimeout(timer);
|
|
|
|
if (options.duration > 0) {
|
|
timer = setTimeout(Notify.clear, options.duration);
|
|
}
|
|
|
|
return instance;
|
|
}
|
|
|
|
function defaultOptions() {
|
|
return {
|
|
type: 'danger',
|
|
color: undefined,
|
|
message: '',
|
|
onClose: null,
|
|
onClick: null,
|
|
onOpened: null,
|
|
duration: 3000,
|
|
className: '',
|
|
background: undefined,
|
|
};
|
|
}
|
|
|
|
Notify.clear = () => {
|
|
if (instance) {
|
|
instance.toggle(false);
|
|
}
|
|
};
|
|
|
|
Notify.currentOptions = defaultOptions();
|
|
|
|
Notify.setDefaultOptions = (options) => {
|
|
Object.assign(Notify.currentOptions, options);
|
|
};
|
|
|
|
Notify.resetDefaultOptions = () => {
|
|
Notify.currentOptions = defaultOptions();
|
|
};
|
|
|
|
Notify.install = (app) => {
|
|
app.use(VanNotify);
|
|
app.config.globalProperties.$notify = Notify;
|
|
};
|
|
|
|
Notify.Component = VanNotify;
|
|
|
|
export default Notify;
|