YH.slide.js 2.96 KB
var me = require('./YH.base');

var slide = function(options) {
    this.__lastTime = null;
    this.__isStop = false;
    options = me.extend(this.defaults, options);
    slide.superclass.constructor.call(this, options);
};

me.inherit(slide, me.assembly);

slide.prototype.oninit = function() {
    var __self = this,
        _o = __self.options;

    if (_o.auto) {
        __self.play();
    }
    __self.go(_o.index);
    return this;
};

slide.prototype.go = function(_to, _from) {
    var __self = this,
        _o = __self.options;
    var _direction,_loop,_current,_index,_originalto,o,key,_e;

    if (__self.__lastTime) {
        clearTimeout(__self.__lastTime);
        __self.__lastTime = null;
    }
    _from = 'undefined' === typeof _from ? _o.index : _from;
    _direction = _to === _from ? 0 : _to > _from ? 1 : -1;
    _loop = _o.loop, _index = _o.length - 1, _originalto = _to;
    if (_loop) {
        if (_to > _index) {
            _to = _to - _index - 1;
        } else {
            if (0 > _to) {
                _to = _to + _index + 1;
            } else {
                _to = _to;
            }
        }
    } else {
        if (_to > _index) {
            _to = _index;
        } else {
            if (0 > _to) {
                _to = 0;
            } else {
                _to = _to;
            }
        }
    }
    _current = _o.index = _to;

    o = {
        from: _from,
        to: _to,
        originalto: _originalto,
        direction: _direction
    };

    for (key in __self.registerEvent) {
        if (__self.registerEvent[key].length > 0) {
            for (_e in __self.registerEvent[key]) {
                if (__self.registerEvent[key].hasOwnProperty(_e)) {
                    __self.registerEvent[key][_e](o);
                }
            }
        }
    }

    if (_current !== _index || _to) {
        if (!__self.__isStop && _o.auto) {
            __self.play();
        }
    } else {
        if (__self.__lastTime) {
            clearTimeout(__self.__lastTime);
        }
    }
};

slide.prototype.play = function() {
    var __self = this,
        _o = __self.options;

    __self.__lastTime = setTimeout(function() {
        __self.next();
    }, 1000 * _o.timeout);
    return this;
};

slide.prototype.next = function() {
    var __self = this,
    _o = __self.options;
    var _from = _o.index;
    var _to = _from + _o.step;

    __self.go(_to, _from);
};

slide.prototype.prev = function() {
    var __self = this,
        _o = __self.options;
    var _from = _o.index;
    var _to = _from - _o.step;

    __self.go(_to, _from);
};

slide.prototype.pause = function() {
    var __self = this;

    if (__self.__lastTime) {
        clearTimeout(__self.__lastTime);
    }
    __self.__isStop = true;
};

slide.prototype.resume = function() {
    var __self = this;

    __self.__isStop = false;
    __self.play();
};

slide.prototype.defaults = {
    index: 0,
    timeout: 5,
    step: 1,
    per: 1,
    auto: false,
    loop: false
};

module.exports = slide;