closeDialogPage.uts 2.6 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3 4 5
import {
    ANIMATION_DURATION,
    ANIMATION_TYPE,
    getCurrentPage,
    Page,
DCloud-yyl's avatar
DCloud-yyl 已提交
6
    isSystemDialogPage,
DCloud-yyl's avatar
DCloud-yyl 已提交
7
} from '@dcloudio/uni-runtime'
DCloud-yyl's avatar
DCloud-yyl 已提交
8 9 10 11
import {
    DEFAULT_ANIMATION_DURATION,
    DEFAULT_ANIMATION_OUT,
} from '../constants.uts'
DCloud-yyl's avatar
DCloud-yyl 已提交
12 13 14 15
import { CloseDialogPageOptions } from '../interface.uts'
import { CloseDialogPageSuccessImpl, CloseDialogPageFailImpl } from '../unierror.uts'

export const closeDialogPage = (options: CloseDialogPageOptions | null) => {
DCloud-yyl's avatar
DCloud-yyl 已提交
16 17 18
    if (options?.animationType === 'pop-out') {
        options.animationType = 'none'
    }
DCloud-yyl's avatar
DCloud-yyl 已提交
19
    const closeOptions = new Map<string, any | null>([
DCloud-yyl's avatar
DCloud-yyl 已提交
20 21 22 23 24 25 26 27
        [
            ANIMATION_TYPE,
            options?.animationType ?? DEFAULT_ANIMATION_OUT,
        ],
        [
            ANIMATION_DURATION,
            options?.animationDuration ?? DEFAULT_ANIMATION_DURATION,
        ],
DCloud-yyl's avatar
DCloud-yyl 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    ])
    if (options?.dialogPage === null) {
        const currentPage = getCurrentPage()
        if (currentPage === null) {
            if (options !== null) {
                const failRes = new CloseDialogPageFailImpl('currentPage is null')
                options.fail?.(failRes)
                options.complete?.(failRes)
            }
        } else {
            const dialogPages = currentPage.getDialogPages()
            for (let i = dialogPages.length - 1;i >= 0;i--) {
                (dialogPages[i].$vm as Page).$close(closeOptions)
            }
            if (options !== null) {
                const successRes = new CloseDialogPageSuccessImpl()
                options.success?.(successRes)
                options.complete?.(successRes)
            }
        }
    } else {
        const dialogPage = options?.dialogPage!
        const currentPages = getCurrentPages()
        const parentPage = dialogPage.getParentPage()
DCloud-yyl's avatar
DCloud-yyl 已提交
52 53 54 55
        if (isSystemDialogPage(dialogPage)) {
            (dialogPage.$vm as Page).$close(closeOptions)
            return
        }
DCloud-yyl's avatar
DCloud-yyl 已提交
56 57 58 59 60 61 62 63 64
        if ((dialogPage.$vm as Page).$dialog === null ||
            currentPages.indexOf(parentPage) === -1 ||
            parentPage!.getDialogPages().indexOf(dialogPage) === -1) {
            if (options !== null) {
                const errRes = new CloseDialogPageFailImpl('dialogPage is not a valid page')
                options.fail?.(errRes)
                options.complete?.(errRes)
            }
        } else {
DCloud-yyl's avatar
DCloud-yyl 已提交
65
            (dialogPage.$vm as Page).$close(closeOptions)
DCloud-yyl's avatar
DCloud-yyl 已提交
66 67 68 69 70 71 72 73
            if (options !== null) {
                const successRes = new CloseDialogPageSuccessImpl()
                options.success?.(successRes)
                options.complete?.(successRes)
            }
        }
    }
}