1 changed files with 78 additions and 0 deletions
@ -0,0 +1,78 @@ |
|||
export const LOADING_SHOW_EVENT = 'common-loading:show' |
|||
export const LOADING_HIDE_EVENT = 'common-loading:hide' |
|||
|
|||
const defaultOptions = { |
|||
title: '加载中...', |
|||
mask: true |
|||
} |
|||
|
|||
const loadingState = { |
|||
visible: false, |
|||
title: defaultOptions.title, |
|||
mask: defaultOptions.mask |
|||
} |
|||
|
|||
let installed = false |
|||
let nativeShowLoading = null |
|||
let nativeHideLoading = null |
|||
|
|||
function normalizeOptions(options = {}) { |
|||
if (typeof options === 'string') { |
|||
return { |
|||
...defaultOptions, |
|||
title: options |
|||
} |
|||
} |
|||
|
|||
return { |
|||
...defaultOptions, |
|||
...options, |
|||
title: options.title || defaultOptions.title, |
|||
mask: options.mask !== false |
|||
} |
|||
} |
|||
|
|||
export function getLoadingState() { |
|||
return { |
|||
...loadingState |
|||
} |
|||
} |
|||
|
|||
export function showLoading(options) { |
|||
const loadingOptions = normalizeOptions(options) |
|||
loadingState.visible = true |
|||
loadingState.title = loadingOptions.title |
|||
loadingState.mask = loadingOptions.mask |
|||
uni.$emit(LOADING_SHOW_EVENT, loadingOptions) |
|||
loadingOptions.success && loadingOptions.success({ errMsg: 'showLoading:ok' }) |
|||
loadingOptions.complete && loadingOptions.complete({ errMsg: 'showLoading:ok' }) |
|||
} |
|||
|
|||
export function hideLoading(options = {}) { |
|||
loadingState.visible = false |
|||
uni.$emit(LOADING_HIDE_EVENT) |
|||
nativeHideLoading && nativeHideLoading.call(uni) |
|||
options.success && options.success({ errMsg: 'hideLoading:ok' }) |
|||
options.complete && options.complete({ errMsg: 'hideLoading:ok' }) |
|||
} |
|||
|
|||
export function installLoading() { |
|||
if (installed) { |
|||
return |
|||
} |
|||
|
|||
installed = true |
|||
nativeShowLoading = uni.showLoading |
|||
nativeHideLoading = uni.hideLoading |
|||
uni.$nativeShowLoading = nativeShowLoading |
|||
uni.$nativeHideLoading = nativeHideLoading |
|||
uni.showLoading = showLoading |
|||
uni.hideLoading = hideLoading |
|||
} |
|||
|
|||
export default { |
|||
installLoading, |
|||
showLoading, |
|||
hideLoading, |
|||
getLoadingState |
|||
} |
|||
Loading…
Reference in new issue