closeDialogPage.uts 2.1 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
import {
    ANIMATION_DURATION,
    ANIMATION_TYPE,
    getCurrentPage,
    Page,
} from '@dcloudio/uni-runtime'
import { CloseDialogPageOptions } from '../interface.uts'
import { CloseDialogPageSuccessImpl, CloseDialogPageFailImpl } from '../unierror.uts'

export const closeDialogPage = (options: CloseDialogPageOptions | null) => {
    const closeOptions = new Map<string, any | null>([
        [ANIMATION_TYPE, 'none'],
        [ANIMATION_DURATION, 0],
    ])
    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()
        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 {
            (dialogPage!.$vm as Page).$close(closeOptions)
            if (options !== null) {
                const successRes = new CloseDialogPageSuccessImpl()
                options.success?.(successRes)
                options.complete?.(successRes)
            }
        }
    }
}