rich-tip.js 2.12 KB
/**
 * Created by yoho on 2016/11/22.
 */
let $ = require('yoho-jquery');

let $tip = $('#yoho-order-bg'),
    $tipMsg = $tip.find('.order-tip-msg'),
    $tipBtns = $tip.find('.order-tip-btn-group');

let touchCallback,
    params;

function setCallback(t, p) {
    touchCallback = t;
    params = p;
}

/**
 * 跳转还是消失控制
 */
(function() {
    $tip.on('click', function(event) {
        if (event.target.tagName === 'SPAN') {
            if (touchCallback) {
                touchCallback(params);
            }

            touchCallback = null;
            params = null;

            setTimeout(function() {
                $tip.hide();
            }, 200);
        } else if (event.target.tagName === 'A') {
            setTimeout(function() {
                $tip.hide();
            }, 200);
        } else {
            $tip.hide();
        }
    });
}());

/**
 * 显示提示
 */
function show(con, btn) {
    let content = '',
        buttons = '';

    if (con && $.isArray(con)) {
        $.each(con, function(key, value) {
            content += '<p>' + value + '</p>';
        });
    }

    if (btn && $.isArray(btn) && btn.length === 1) {
        if (!btn[0].href) {
            buttons += '<a class="order-tip-btn1 ' + btn[0].class +
                '"><span>' + btn[0].text +
                '</span></a>';
        } else {
            buttons += '<a href="' + btn[0].href +
                '" class="order-tip-btn1 ' + btn[0].class +
                '">' + btn[0].text +
                '</a>';
        }
    }

    if (btn && $.isArray(btn) && btn.length > 1) {
        $.each(btn, function(key, value) {
            if (!value.href) {
                buttons += '<a class="' + value.class +
                     '"><span>' + value.text +
                     '</span></a>';
            } else {
                buttons += '<a href="' + value.href +
                     '" class="' + value.class +
                     '">' + value.text +
                     '</a>';
            }
        });
    }

    $tipMsg.html(content);
    $tipBtns.html(buttons);
    $tip.show();
}

exports.setCallback = setCallback;
exports.show = show;