dialoger.js 4.61 KB
var tipTmpl = '<div class="tip {{tipClassName}}"><div class="title">{{title}}</div><div class="content">{{content}}</div><a class="button {{buttonClass}}">{{button}}</a></div>'
var toastTmpl = '<div style="{{style}}" class="feature-toast-content">{{content}}</div>';
var frameRate = 75; // 动画帧率
var showTip = function (data) {
    var tipNode = null;
    data = data || {
        title: '',
        content: '',
        close: true
    };
    data.tipClassName = data.tipClassName || '';
    if (data.close) {
        data.buttonClass = 'close';
        data.button = '返回'
    } else {
        data.buttonClass = 'refresh';
        data.button = '刷新'
    }
    if (document.getElementById('yo-tip')) {
        tipNode = document.getElementById('yo-tip');
        var tip = tipNode.querySelector('.tip');
        tip.setAttribute('class', 'tip ' + data.tipClassName);
        tip.querySelector('.title').innerHTML = data.title;
        tip.querySelector('.content').innerHTML = data.content;
        var button = tip.querySelector('.button');
        button.setAttribute('class', 'button ' + data.buttonClass);
        button.innerHTML = data.button;
    } else {
        for (var key in data) {
            tipTmpl = tipTmpl.replace(new RegExp("\\{\\{" + key + "\\}\\}", "g"), data[key]);
        }
        tipNode = document.createElement('div');
        tipNode.setAttribute('id', 'yo-tip');
        tipNode.setAttribute('class', 'featuretip tip-wrap');
        tipNode.innerHTML = tipTmpl;
        document.body.appendChild(tipNode);
        tipNode.addEventListener('click',function (e) {
            if ('featuretip tip-wrap' === e.target.className) {
                fadeOut(e.currentTarget,300);
            }
        })
    }
    if (data.close) {
        tipNode.querySelector('.close').onclick = function (e) {
            fadeOut(tipNode,300)
        }
    } else {
        tipNode.querySelector('.refresh').onclick = function (e) {
            location.reload();
        }
    }
    fadeIn(tipNode,300);
};

var toast = function (content, options) {
    if (typeof content === 'undefined') {
        return;
    }

    var obj = Object.assign({
        bottom: '10%',
        paddingX: 6,
        paddingY: 10,
        radius: 3
    }, options || {});

    var param = {
        style: 'bottom:' + obj.bottom + ';padding:' + obj.paddingY + 'px ' + obj.paddingX + ';border-radius:' + obj.radius + 'px;',
        content: content
    };
    var toastNode
    if (document.getElementById('yo-toast')) {
        toastNode = document.getElementById('yo-toast');
        var toast = toastNode.querySelector('.feature-toast-content');
        toast.setAttribute('style', param.style);
        toast.innerHTML = param.content;
    } else {
        toastNode = document.createElement('div');
        toastNode.setAttribute('class', 'feature-toast feature-toast-wrap');
        toastNode.setAttribute('id', 'yo-toast');
        var _toastTmpl;
        for (var key in param) {
            _toastTmpl = toastTmpl.replace(new RegExp("\\{\\{" + key + "\\}\\}", "g"), param[key]);
        }
        toastNode.innerHTML = _toastTmpl;
        document.body.appendChild(toastNode);
    }
    toastNode.style.display = 'block';
    this.toastTimer && clearTimeout(this.toastTimer);
    this.toastTimer = setTimeout(function () {
        toastNode.style.display = 'none';
    }, (obj.duration && obj.duration > 0) ? obj.duration : 2000);
};
function fadeIn(el,time){
    var mt = 1000 / frameRate,
        opacityStep = mt / time;

    if(el.style.opacity === ""){
        el.style.opacity = 0;
    }
    if(el.style.display === "" || el.style.display === 'none'){
        el.style.display = 'block';
    }

    el.timer && clearInterval(el.timer);
    el.timer = setInterval(function(){
        if(el.style.opacity < 1 - opacityStep){
            el.style.opacity = parseFloat(el.style.opacity) + opacityStep;
        }
        else{
            clearInterval(el.timer);
            el.style.opacity = 1;
        }
    }, mt);
}

function fadeOut(el,time){
    var mt = 1000 / frameRate,
        opacityStep = mt / time;

    if(el.style.opacity === ""){
        el.style.opacity = 1;
    }
    if(el.style.display === "" || el.style.display === 'none'){
        el.style.display = 'block';
    }

    el.timer && clearInterval(el.timer);
    el.timer = setInterval(function(){
        if(el.style.opacity > opacityStep){
            el.style.opacity = parseFloat(el.style.opacity) - mt / time;
        }
        else{
            clearInterval(el.timer);
            el.style.opacity = 0;
            el.style.display = 'none'
        }
    }, mt);
}

module.exports = {
    showTip: showTip,
    toast: toast
};