RSS Git Download  Clone
Raw Blame History 3kB 93 lines
p3xr.ng.factory('p3xrCommon', function ($mdToast, $mdDialog, $mdColors, $rootScope, p3xrRedisParser) {

    const generalHandleError = (dataOrError) => {
        if (dataOrError === undefined) {
            return;
        }
        if (! (dataOrError instanceof Error || dataOrError instanceof Object)) {
            dataOrError = new Error(dataOrError)
        }
        if (dataOrError instanceof Error || dataOrError.status === 'error') {
            let error
            if (dataOrError instanceof Error) {
                error = dataOrError
            } else {
                error = dataOrError.error
            }
            console.error(error)
            if (error.hasOwnProperty('code') && p3xr.strings.code.hasOwnProperty(error.code)) {
                error.message = p3xr.strings.code[error.code]
            }
            $mdToast.show(
                $mdToast.simple()
                    .textContent(error.hasOwnProperty('message') ? error.message : error)
                    .position(p3xr.settings.toast.position)
                    .theme(p3xr.state.themeLayout)
                    .hideDelay(p3xr.settings.toast.timeout)
                  //  .capsule(true)
            );
            return false
        }
        return true
    }

    const toast = (options) => {
        $mdToast.show(
            $mdToast.simple()
                .textContent(options.message)
                .position(p3xr.settings.toast.position)
                .theme(p3xr.state.themeLayout)
                .hideDelay(p3xr.settings.toast.timeout)
            //    .capsule(true)
        );
    }

    const confirm = (options) => {
        const confirm = $mdDialog.confirm()
            .title(p3xr.strings.confirm.title)
            .textContent(options.message)
            .targetEvent(options.event)
            .ok(p3xr.strings.intention.sure)

        if (!options.hasOwnProperty('disableCancel')) {
            confirm.cancel(p3xr.strings.intention.cancel);
        }

        return $mdDialog.show(confirm)
    }

    const loadRedisInfoResponse = (options) => {
        const { response } = options

        $rootScope.p3xr.state.info = p3xrRedisParser.info(response.info)

        if ($rootScope.p3xr.settings.sort) {
            $rootScope.p3xr.state.keysRaw = response.keys.sort()
        } else {
            $rootScope.p3xr.state.keysRaw = response.keys
        }
        $rootScope.p3xr.state.keysInfo = response.keysInfo
        $rootScope.$digest()

    }

    const setTableZebraStyles = (options) => {
        const { $odd } = options
        if (!$odd) {
            return '';
        }
        let style = '';
        const bg = $mdColors.getThemeColor(`${p3xr.state.themeLayout}-background-500-0.1`)
        style += `background-color: ${bg};`
        return style;
    }

    return {
        generalHandleError: generalHandleError,
        toast: toast,
        confirm: confirm,
        loadRedisInfoResponse: loadRedisInfoResponse,
        setTableZebraStyles: setTableZebraStyles,
    };
});