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

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

var touchCallback,
    params;

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

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

            touchCallback = null;
            params = null;

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

/**
 * 显示提示
 */
function show(con, btn) {
    var 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;