dialog.js 2.87 KB
/*
 * @Description: dialog
 * @Time: 2015/11/18
 * @author: chenglong.wang
 */

let $ = require('yoho-jquery');
let dialogHbs = require('hbs/plugin/dialog.hbs');

// fullWithBtn是供详情页获取限购码使用的特殊参数
exports.showDialog = function(data, callback, callbackForLeft, fullWithBtn) {
    let $dialogBox,
        defaultHideDuraton,
        $dialogWrapper;

    $('.dialog-wrapper').remove();

    $('body').append(dialogHbs(data));

    $dialogBox = $('.dialog-box');
    $dialogWrapper = $('.dialog-wrapper');

    // 显示
    if (data.fast) {
        $dialogWrapper.css({
            display: 'block'
        });
    } else {
        $dialogWrapper.fadeIn();
    }

    if (fullWithBtn) {
        $('.dialog-wrapper .dialog-footer > span').css('width', '100%');
        $('.dialog-wrapper .dialog-content').css({
            'padding-left': '1.85rem',
            'padding-right': '1.85rem'
        });
        $dialogWrapper.css('z-index', '10');
    }

    $dialogBox.css({
        top: '50%',
        marginTop: -($dialogBox.height() / 2)
    });

    // 隐藏
    if (data.autoHide) {
        defaultHideDuraton = 1000;
        if (data.autoHide > 1) {
            defaultHideDuraton = data.autoHide;
        }
        setTimeout(function() {
            $dialogWrapper.fadeOut();
        }, defaultHideDuraton);
    }

    // 禁止在dialog上可以上下滚动
    if (!data.student) {
        $dialogWrapper.on('touchmove', function() {
            return false;
        });
    }

    // 延迟绑定事件,防止触发touch弹出dialog之后直接触发这个click
    setTimeout(() => {
        $dialogWrapper.on('touchend', function(event) {
            if ($(event.target).hasClass('dialog-left-btn')) {
                if (typeof callbackForLeft === 'function') {
                    callbackForLeft();
                }
                $dialogWrapper.fadeOut();
            } else if ($(event.target).hasClass('dialog-right-btn')) {
                return callback();
            } else if ($(event.target).hasClass('dialog-center-btn')) {
                $('.dialog-wrapper').remove();
                $('body').css({
                    overflow: '',
                    position: '',
                });
            } else if ($(event.target).closest('.dialog-box').length) {
                return true;
            } else {
                $('.dialog-wrapper').remove();
                $('body').css({
                    overflow: '',
                    position: '',
                });
            }

            // 防止出现点透问题
            event.preventDefault();
            event.stopPropagation();
        });
    }, 500);
};

exports.hideDialog = function() {
    $('.dialog-wrapper').remove();
};

/**
 * 重写事件绑定
 */
exports.rewriteEvents = () => {
    setTimeout(() => {
        $('.dialog-wrapper').off();
    }, 600);
    return Promise.resolve();
};