all files / js/ animation.js

97.12% Statements 135/139
81.71% Branches 67/82
100% Functions 27/27
96.99% Lines 129/133
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256                       47×   47× 47×                     279× 279×                   99× 99× 93×   99× 93×   99× 93×   99× 47×   99× 47×   99× 47×   99× 47×           483×   483× 483× 483×             483×     221×   221× 221× 221× 221× 221× 221× 221×               15× 15× 15× 15× 15× 15×   15×   45× 45× 45× 45× 45×   26× 26×         15×           15×   15×   10×         15× 15× 15×     15× 15×   10×     15× 15× 15×                   206× 206× 206× 206× 206×   206×   412× 412× 412× 412× 412×   409×       206×         206×   206× 47× 47×   159× 86× 86×   73× 37× 37×   36× 36× 36×     206× 206× 206×   206×     206× 203× 203×                 235× 235×             47× 47× 47× 47× 47× 47×      
define(['utils', 'IPlugin', 'errors', 'jquery.easing'], function(Utils, IPlugin, Errors) {
    "use strict";
 
    /**
     * @class
     * Класс анимации переходов в панели
     * @param {JQuery} mainDiv - элемент, в котором располагается панель. Должен содержать класс rb-wrapper.
     * @param {ElementsPool} elementsPool - хранилище элементов ячеек панели
     * @constructor Animation
     * @extends IPlugin
     */
    function Animation(mainDiv, elementsPool) {
        this._elementsPool = elementsPool;
 
        Eif (mainDiv instanceof $) {
            this._mainDiv = mainDiv;
        } else {
            throw new Errors.ArgumentError('mainDiv', mainDiv);
        }
    }
    Utils.inherite(Animation, IPlugin);
    /**
     * Применить конфигурацию. Учитывает опции wrongTime1, wrongTime2, correctTime, wrongEasing1, wrongEasing2, correctEasing, showAdjacentScreens.
     * @param {Moving~config} config - конфигурация
     * @memberOf Animation
     */
    Animation.prototype.configure = function(config) {
        function fixTime(argName, time) {
            Eif (typeof time === 'number') {
                return time > 0 ? time : 1;
            } else {
                if (time === undefined) {
                    return 500;
                } else {
                    throw new Errors.ArgumentError(argName, time);
                }
            }
        }
 
        Eif (typeof config === 'object') {
            if (config.wrongTime1 !== undefined) {
                this._wrongTime1 = fixTime('wrongTime1', config.wrongTime1);
            }
            if (config.wrongTime2 !== undefined) {
                this._wrongTime2 = fixTime('wrongTime2', config.wrongTime2);
            }
            if (config.correctTime !== undefined) {
                this._correctTime = fixTime('correctTime', config.correctTime);
            }
            if (config.wrongEasing1 !== undefined) {
                this._wrongEasing1 = config.wrongEasing1;
            }
            if (config.wrongEasing2 !== undefined) {
                this._wrongEasing2 = config.wrongEasing2;
            }
            if (config.correctEasing !== undefined) {
                this._correctEasing = config.correctEasing;
            }
            if (config.showAdjacentScreens !== undefined) {
                this._showAdjacentScreens = config.showAdjacentScreens;
            }
        }
    };
 
    Animation.prototype._animate = function(elem, side, value, easing, time, beforeFn, afterFn) {
 
        beforeFn && beforeFn();
 
        var css = {}, opts;
        css[Utils.getStartSide(side)] = value + '%';
        opts = {
            duration: time,
            easing: easing,
            queue: false,
            done: afterFn
        };
 
        elem.animate(css, opts);
    };
 
    Animation.prototype._updateState = function(res, elements) {
        if (this._isAnimate) {
            this._isAnimate = false;
            this._new && this._new.stop();
            this._old && this._old.stop(false, true);
            this._cur && this._cur.stop();
            this._prev && this._prev.stop(false, true);
            this._next && this._next.stop(false, true);
            this._res(false);
        }
        this._isAnimate = true;
        this._old = elements.oldElem;
        this._new = elements.newElem;
        this._cur = elements.curElem;
        this._prev = elements.prevElem;
        this._next = elements.nextElem;
        this._res = res;
    };
 
    /**
     * Анимация неудачного перехода
     * @param {string} side - сторона перехода
     * @memberOf Animation
     */
    Animation.prototype.goToWrongSide = function(side) {
        var self = this,
            width = 100,
            height = 100,
            elem = this._elementsPool.getElementBySide('center'),
            nextElem = this._elementsPool.getElementBySide(side),
            prevElem = this._elementsPool.getElementBySide(Utils.oppositeSide(side));
 
        return new Promise(function(res, rej) {
            function wrongAnimate(elem, startLeft, startTop, beforeFn, afterFn) {
                self._animate(elem, side, '+=' + value, self._wrongEasing1, self._isAnimate ? self._wrongTime1 : 10, function() {
                    startLeft -= 100;
                    startTop -= 100;
                    elem.css({'left': startLeft + '%', 'top': startTop + '%'});
                    beforeFn && beforeFn();
                }, function() {
                    self._animate(elem, side, '-=' + value, self._wrongEasing2, self._isAnimate ? self._wrongTime2 : 10, undefined, function() {
                        afterFn && afterFn();
                    });
                });
            }
 
            self._updateState(res, {
                curElem: elem,
                prevElem: prevElem,
                nextElem: nextElem
            });
 
            var dw = width/10, dh = height/10, value = 0, relValWidth = 0, relValHeight = 0;
 
            if (side === 'left') {
                relValWidth = -width;
                value = - dw;
            }
            else if (side === 'right') {
                relValWidth = width;
                value = dw;
            }
            else if (side === 'top') {
                relValHeight = -height;
                value = - dh;
            }
            else Eif (side === 'bottom') {
                relValHeight = height;
                value = dh;
            }
 
            Eif (self._showAdjacentScreens) {
                wrongAnimate(nextElem, width + relValWidth, height + relValHeight, function() {
                    nextElem.toggleClass('rb__hiding-screen', true);
                }, function() {
                    nextElem.toggleClass('rb__hiding-screen', false);
                }, true);
                wrongAnimate(prevElem, width - relValWidth, height - relValHeight, function() {
                    prevElem.toggleClass('rb__hiding-screen', true);
                }, function() {
                    prevElem.toggleClass('rb__hiding-screen', false);
                }, true);
            }
            wrongAnimate(elem, width, height, undefined, function() {
                self._isAnimate = false;
                self._res(true);
            });
        });
    };
 
    /**
     * Анимация удачного перехода
     * @param {string} side - сторона перехода
     * @memberOf Animation
     */
    Animation.prototype.goToCorrectSide = function(side) {
        var self = this,
            newElem = this._elementsPool.getElementBySide('center'),
            oldElem = this._elementsPool.getElementBySide(Utils.oppositeSide(side)),
            width = 100,
            height = 100;
 
        return new Promise(function(res, rej) {
            function correctAnimate(elem, startLeft, startTop, beforeFn, afterFn) {
                self._animate(elem, side, '-=' + value, self._correctEasing, self._correctTime, function() {
                    startLeft -= 100;
                    startTop -= 100;
                    elem.css({'left': startLeft + '%', 'top': startTop + '%'});
                    beforeFn && beforeFn();
                }, function() {
                    afterFn && afterFn();
                });
            }
 
            self._updateState(res, {
                oldElem: oldElem,
                newElem: newElem
            });
 
            var value = 0, relValWidth = 0, relValHeight = 0;
 
            if (side === 'left') {
                relValWidth = -width;
                value = - width;
            }
            else if (side === 'right') {
                relValWidth = width;
                value = width;
            }
            else if (side === 'top') {
                relValHeight = -height;
                value = - height;
            }
            else Eif (side === 'bottom') {
                relValHeight = height;
                value = height;
            }
 
            Eif (self._showAdjacentScreens) {
                correctAnimate(oldElem, width, height, function () {
                    oldElem.toggleClass('rb__hiding-screen', true);
                }, function () {
                    oldElem.toggleClass('rb__hiding-screen', false);
                }, true);
            }
            correctAnimate(newElem, width + relValWidth, height + relValHeight, undefined, function() {
                self._isAnimate = false;
                self._res(true);
            });
        });
    };
 
    /**
     * Переход в центральную ячейку
     * @memberOf Animation
     */
    Animation.prototype.goToCenter = function() {
        var elem = this._elementsPool.getElementBySide('center');
        elem.css({'left': '0%', 'top': '0%'});
    };
 
    /**
     * Уничтожить экземпляр класса анимации
     * @memberOf Animation
     */
    Animation.prototype.destroy = function() {
        this._new && this._new.stop();
        this._old && this._old.stop();
        this._cur && this._cur.stop();
        this._prev && this._prev.stop();
        this._next && this._next.stop();
        this._res && this._res(false);
    };
 
    return Animation;
});