Source: hui/hui_imageque.js

'::hui_imageque::';
'use strict';
//   __    __           ______   ______  _____    __  __     
//  /\ \  /\ \ /'\_/`\ /\  _  \ /\__  _\/\  __`\ /\ \/\ \    
//  \ `\`\\/'//\      \\ \ \/\ \\/_/\ \/\ \ \/\ \\ \ \ \ \   
//   `\ `\ /' \ \ \__\ \\ \  __ \  \ \ \ \ \ \ \ \\ \ \ \ \  
//     `\ \ \  \ \ \_/\ \\ \ \/\ \  \ \ \ \ \ \_\ \\ \ \_\ \ 
//       \ \_\  \ \_\\ \_\\ \_\ \_\  \ \_\ \ \_____\\ \_____\
//        \/_/   \/_/ \/_/ \/_/\/_/   \/_/  \/_____/ \/_____/
//                                                                   

/**
 * @name @name 页面流程控制类
 * @public
 * @author wanghaiyang
 * @date 2014/05/05
 * @param {Object} options 控件初始化参数.
 */
hui.define('hui_imageque', [], function () {

    hui.ImageQue = function (list, opt) {
        var me = this;
        me.list = list ? [].slice.call(list, 0) : [];
        me.stop = opt && opt.maxCount || false;
        me.maxCount = opt && opt.maxCount || 2;
        me.loading = 0;
        me.lazyClass = opt && opt.lazyClass || 'lazy-load';

        me.loadLeft();
    };


    hui.ImageQue.prototype.loadNext = function () {
        var me = this;
        if (me.list.length && !me.stop) {
            var ele = me.list.shift();
            if (ele && ele.getAttribute(me.lazyClass)) {
                ele.onerror = ele.onload = hui.fn(me.onloadCallback, me);
                ele.src = ele.getAttribute(me.lazyClass);
                ele.removeAttribute(me.lazyClass);
                me.loading++;
            }
            else {
                me.loadNext();
            }
        }
    };

    hui.ImageQue.prototype.loadLeft = function () {
        var me = this;
        for (var i = me.maxCount - me.loading; i > 0 && !me.stop; i--) {
            me.loadNext();
        }
    };

    hui.ImageQue.prototype.onloadCallback = function (elem) {
        var me = this;
        me.loading--;
        me.loadNext();
    };

    hui.ImageQue.prototype.addImage = function (list) {
        var me = this;
        if (Object.prototype.toString.call(list) !== '[object Array]') {
            if (list && list.length) {
                list = [].slice.call(list, 0);
            }
            else if (list && String(list.tagName).toLowerCase() === 'img') {
                list = [list];
            }
        }
        if (Object.prototype.toString.call(list) === '[object Array]') {
            me.list = me.list.concat(list);
            me.loadLeft();
        }
    };
});