loading.js 2.04 KB
/**
 * Loading mask
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/29
 */

let $ = require('yoho-jquery'),
    tip = require('./tip');

let $page = $('.yoho-page');

let $loading,
    hasInit = false,
    timeWatch,
    timeOut = 5000;

/** modify by liangzhifeng at 2015.11.2 */

// 初始化
function init($container, options) {
    let className = options && options.className || '';
    let html = '<div class="loading-mask ' + className + ' hide">' +
                    '<div class="loading">' +
                        '<div></div><div></div><div></div>' +
                    '</div>' +
               '</div>';

    hasInit = true;
    if (!$container) {
        $container = $page;
    }

    $container.append(html);

    $loading = $container.children('.loading-mask');

    // 产品优化loading时可以滑动
    // $('body').on('touchstart touchmove touchend', '.loading-mask', function() {
    //     return false;
    // });
}

// 显示loading
function showLoadingMask() {
    // if (!hasInit) {
    //     init();
    //     hasInit = true;
    // }
    // $loading.removeClass('hide');
}

// 隐藏loading
function hideLoadingMask() {
    // if (!hasInit) {
    //     init();
    //     hasInit = true;
    // }
    // $loading.addClass('hide');
}

// 隐藏loading
function hideLoading(timeout) {
    if (timeWatch) {
        clearTimeout(timeWatch);
    }
    if (timeout === true) {
        tip.show('操作失败,请重试');
    }
    if (!hasInit) {
        init();
        hasInit = true;
    }
    $loading.addClass('hide');
}

// 显示loading
function showLoading() {
    if (timeWatch) {
        clearTimeout(timeWatch);
    }
    timeWatch = setTimeout(() => {
        hideLoading(true);
    }, timeOut);
    if (!hasInit) {
        init();
        hasInit = true;
    }
    $loading.removeClass('hide');
}


exports.init = init;
exports.showLoadingMask = showLoadingMask;
exports.hideLoadingMask = hideLoadingMask;
exports.show = showLoadingMask;
exports.hide = hideLoadingMask;
exports.showLoading = showLoading;
exports.hideLoading = hideLoading;