all files / js/ keydownControl.js

75.56% Statements 34/45
72.22% Branches 13/18
54.55% Functions 6/11
79.07% Lines 34/43
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                       47×       47×     47× 47× 47×             99× 99× 47×   99× 47×   99× 47×   99× 47×                 110×           63×   63× 63×                 63×       63× 63×   63×           63×   63× 63×   63×                  
define(['utils', 'IControl', 'errors'], function(Utils, IControl, Errors) {
    "use strict";
 
    /**
     * @class
     * Класс управления панелью с помощью клавиш на клавиатуре.
     * @param {JQuery} mainDiv - элемент, в котором располагается панель. Должен содержать класс rb-wrapper.
     * @param {function} actionFn - функция, определяющая действие при переходе в одну из сторон (Moving.prototype._moveByActionValue)
     * @constructor KeydownControl
     * @extends IControl
     */
    function KeydownControl(mainDiv, actionFn) {
        Iif (!(mainDiv instanceof $)) {
            throw new Errors.ArgumentError('mainDiv', mainDiv);
        }
 
        Iif (typeof actionFn !== 'function') {
            throw new Errors.ArgumentError('actionFn', actionFn);
        }
        this._isEnable = false;
        this._mainDiv = mainDiv;
        this._actionFn = actionFn;
    }
    Utils.inherite(KeydownControl, IControl);
    /**
     * Применить конфигурацию. Учитывает опции leftKey, topKey, rightKey, bottomKey.
     * @param {Moving~config} config
     * @memberOf KeydownControl
     */
    KeydownControl.prototype.configure = function(config) {
        Eif (typeof config === 'object') {
            if (config.leftKey !== undefined) {
                this._leftKey = config.leftKey;
            }
            if (config.topKey !== undefined) {
                this._topKey = config.topKey;
            }
            if (config.rightKey !== undefined) {
                this._rightKey = config.rightKey;
            }
            if (config.bottomKey !== undefined) {
                this._bottomKey = config.bottomKey;
            }
        }
    };
    /**
     *
     * @returns {boolean}
     * @memberOf KeydownControl
     */
    KeydownControl.prototype.isEnable = function() {
        return this._isEnable;
    };
    /**
     *
     * @memberOf KeydownControl
     */
    KeydownControl.prototype.enable = function() {
        Iif (this._isEnable) return;
 
        var self = this;
        var baseHandler = function(e) {
            self._actionFn(e.which, [self._leftKey, self._topKey, self._rightKey, self._bottomKey], function(value, defValue) {
                return value === defValue;
            });
            self._actionFn(e.key, [self._leftKey, self._topKey, self._rightKey, self._bottomKey], function(value, defValue) {
                return value === defValue;
            });
            e.stopPropagation();
        };
        var mainDivHandler = function(e) {
            baseHandler(e);
        };
 
        this._mainDiv.on('keydown', mainDivHandler);
        this._mainDivHandler = mainDivHandler;
 
        this._isEnable = true;
    };
    /**
     *
     * @memberOf KeydownControl
     */
    KeydownControl.prototype.disable = function() {
        Iif (!this._isEnable) return;
 
        this._mainDiv.off('keydown', this._mainDivHandler);
        this._mainDivHandler = null;
 
        this._isEnable = false;
    };
    /**
     *
     * @memberOf KeydownControl
     */
    KeydownControl.prototype.destroy = function() {
        this.disable();
    };
 
    return KeydownControl;
});