index.uts 6.7 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
import {
    getTabBar,
    getTabBarBorderStyle,
    isString,
} from '@dcloudio/uni-runtime'
import {
    HideTabBarOptions,
    HideTabBarRedDotOptions,
    HideTabBarRedDotSuccess,
    HideTabBarSuccess,
    RemoveTabBarBadgeOptions,
    RemoveTabBarBadgeSuccess,
    SetTabBarBadgeOptions,
    SetTabBarBadgeSuccess,
    SetTabBarItemOptions,
    SetTabBarItemSuccess,
    SetTabBarStyleOptions,
    SetTabBarStyleSuccess,
    ShowTabBarOptions,
    ShowTabBarRedDotOptions,
    ShowTabBarRedDotSuccess,
    ShowTabBarSuccess,
} from '../interface.uts'
import { SetTabBarFailImpl } from '../unierror.uts'

export const setTabBarBadge = defineAsyncApi<
    SetTabBarBadgeOptions,
    SetTabBarBadgeSuccess
>('setTabBarBadge', (options: SetTabBarBadgeOptions, res: ApiExecutor) => {
    const tabBar = getTabBar()
    if (tabBar === null) {
        res.reject(new SetTabBarFailImpl('tabBar is not exist'))
        return
    }
    tabBar!.setTabBarBadge(
        new Map<string, any | null>([
            ['index', options.index],
            ['text', options.text],
        ]),
    )
    res.resolve(null)
})

export const removeTabBarBadge = defineAsyncApi<
    RemoveTabBarBadgeOptions,
    RemoveTabBarBadgeSuccess
>(
    'removeTabBarBadge',
    (options: RemoveTabBarBadgeOptions, res: ApiExecutor) => {
        const tabBar = getTabBar()
        if (tabBar === null) {
            res.reject(new SetTabBarFailImpl('tabBar is not exist'))
            return
        }
        tabBar!.removeTabBarBadge(
            new Map<string, any | null>([['index', options.index]]),
        )
        res.resolve(null)
    },
)

export const setTabBarItem = defineAsyncApi<
    SetTabBarItemOptions,
    SetTabBarItemSuccess
>('setTabBarItem', (options: SetTabBarItemOptions, res: ApiExecutor) => {
    const tabBar = getTabBar()
    if (tabBar === null) {
        res.reject(new SetTabBarFailImpl('tabBar is not exist'))
        return
    }
    const item = new Map<string, any>([
        ['index', options.index],
        ['text', options.text],
        ['iconPath', options.iconPath],
        ['selectedIconPath', options.selectedIconPath],
        ['pagePath', options.pagePath],
        ['visible', options.visible],
    ])
    if (options.iconfont !== null) {
        const iconfontOptions = options.iconfont!
        const iconfont: Map<string, any> = new Map<string, any>([
            ['text', iconfontOptions.text],
            ['selectedText', iconfontOptions.selectedText],
            ['fontSize', iconfontOptions.fontSize],
            ['color', iconfontOptions.color],
            ['selectedColor', iconfontOptions.selectedColor],
        ])
        item.set('iconfont', iconfont)
    }
    tabBar!.setTabBarItem(item as Map<string, any | null>)
    res.resolve(null)
})

export const setTabBarStyle = defineAsyncApi<
    SetTabBarStyleOptions,
    SetTabBarStyleSuccess
>('setTabBarStyle', (options: SetTabBarStyleOptions, res: ApiExecutor) => {
    const tabBar = getTabBar()
    if (tabBar === null) {
        res.reject(new SetTabBarFailImpl('tabBar is not exist'))
        return
    }
    const style = new Map<string, any | null>([
        ['color', options.color],
        ['selectedColor', options.selectedColor],
        ['backgroundColor', options.backgroundColor],
        ['backgroundImage', options.backgroundImage],
        ['backgroundRepeat', options.backgroundRepeat],
    ])
DCloud-yyl's avatar
DCloud-yyl 已提交
110 111 112 113 114
    if (isString(options.borderColor)) {
        style.set(
            'borderStyle', options.borderColor as string,
        )
    } else if (isString(options.borderStyle)) {
DCloud-yyl's avatar
DCloud-yyl 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        style.set(
            'borderStyle',
            getTabBarBorderStyle(options.borderStyle as string),
        )
    }
    if (options.midButton !== null) {
        const midButtonOptions = options.midButton!
        const midButton: Map<string, any> = new Map<string, any>([
            ['width', midButtonOptions.width],
            ['height', midButtonOptions.height],
            ['iconPath', midButtonOptions.iconPath],
            ['text', midButtonOptions.text],
            ['iconPath', midButtonOptions.iconPath],
            ['iconWidth', midButtonOptions.iconWidth],
            ['backgroundImage', midButtonOptions.backgroundImage],
        ])
        if (midButtonOptions.iconfont !== null) {
            const iconfontOptions = midButtonOptions.iconfont!
            const iconfont: Map<string, any> = new Map<string, any>([
                ['text', iconfontOptions.text],
                ['selectedText', iconfontOptions.selectedText],
                ['fontSize', iconfontOptions.fontSize],
                ['color', iconfontOptions.color],
                ['selectedColor', iconfontOptions.selectedColor],
            ])
            midButton.set('iconfont', iconfont)
        }
        style.set('midButton', midButton)
    }
    tabBar!.setTabBarStyle(style)
    res.resolve(null)
})

export const hideTabBar = defineAsyncApi<
    HideTabBarOptions | null,
    HideTabBarSuccess
>('hideTabBar', (options: HideTabBarOptions | null, res: ApiExecutor) => {
    const tabBar = getTabBar()
    if (tabBar === null) {
        res.reject(new SetTabBarFailImpl('tabBar is not exist'))
        return
    }
    tabBar!.hideTabBar(
        new Map<string, any | null>([['animation', options?.animation]]),
    )
    res.resolve(null)
})

export const showTabBar = defineAsyncApi<
    ShowTabBarOptions | null,
    ShowTabBarSuccess
>('showTabBar', (options: ShowTabBarOptions | null, res: ApiExecutor) => {
    const tabBar = getTabBar()
    if (tabBar === null) {
        res.reject(new SetTabBarFailImpl('tabBar is not exist'))
        return
    }
    tabBar!.showTabBar(
        new Map<string, any | null>([['animation', options?.animation]]),
    )
    res.resolve(null)
})

export const showTabBarRedDot = defineAsyncApi<
    ShowTabBarRedDotOptions,
    ShowTabBarRedDotSuccess
>('showTabBarRedDot', (options: ShowTabBarRedDotOptions, res: ApiExecutor) => {
    const tabBar = getTabBar()
    if (tabBar === null) {
        res.reject(new SetTabBarFailImpl('tabBar is not exist'))
        return
    }
    tabBar!.showTabBarRedDot(
        new Map<string, any | null>([['index', options.index]]),
    )
    res.resolve(null)
})

export const hideTabBarRedDot = defineAsyncApi<
    HideTabBarRedDotOptions,
    HideTabBarRedDotSuccess
>('hideTabBarRedDot', (options: HideTabBarRedDotOptions, res: ApiExecutor) => {
    const tabBar = getTabBar()
    if (tabBar === null) {
        res.reject(new SetTabBarFailImpl('tabBar is not exist'))
        return
    }
    tabBar!.hideTabBarRedDot(
        new Map<string, any | null>([['index', options.index]]),
    )
    res.resolve(null)
})

export const onTabBarMidButtonTap = defineOnApi(
    'onTabBarMidButtonTap',
    function () {
        // noop
    },
)