{"size": 3393, "ext": "js", "max_stars_repo_path": "lib/mouse/default/mouse-push-button.js", "max_stars_repo_name": "veloce/mobile-button", "max_stars_repo_stars_event_min_datetime": "2015-07-13T02:43:35.000Z", "max_stars_repo_stars_event_max_datetime": "2015-07-13T02:43:35.000Z", "max_issues_repo_path": "lib/mouse/default/mouse-push-button.js", "max_issues_repo_name": "veloce/mobile-button", "max_issues_repo_issues_event_min_datetime": "2015-02-27T15:26:19.000Z", "max_issues_repo_issues_event_max_datetime": "2015-03-04T14:49:10.000Z", "max_forks_repo_path": "lib/mouse/default/mouse-push-button.js", "max_forks_repo_name": "veloce/mobile-button", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 28.7542372881, "max_line_length": 78, "alphanum_fraction": 0.6077217801}
/*
 * default/mouse-push-button.js
 */

"use strict";

var Q = require('q'),
    Button = require('./../../button');

var MousePushButton = function (options) {
    Button.prototype.constructor.call(this, options);
    this.delay = options.delay > 0 ? options.delay : 0;
    this.g = null;
    if(typeof options.g === 'function') this.g = options.g;
    this.promisef = null;
    this.boundaries = { minX : 0, maxX : 0, minY : 0, maxY : 0 };
    this.leftOrEnded = false;
};

MousePushButton.prototype = (function (proto) {
    function F() {};
    F.prototype = proto;
    return new F();
})(Button.prototype);

MousePushButton.prototype.constructor = MousePushButton;

MousePushButton.prototype.setG = function (g) {
    if (typeof g !== 'function')
        throw new Error("Button setG method needs a g function as argument.");
    this.g = g;
    return this;
};

MousePushButton.prototype._isInActiveZone = function (touch) {
    var x = touch.clientX,
        y = touch.clientY,
        b = this.boundaries;
    return x < b.maxX && x > b.minX && y < b.maxY && y > b.minY;
};

MousePushButton.prototype.bind = function () {
    Button.prototype.bind.call(this);
    this.el.addEventListener('mousedown', this, false);
    this.binded = true;
    return this;
};

MousePushButton.prototype.unbind = function () {
    Button.prototype.unbind.call(this);
    this.el.removeEventListener('mousedown', this, false);
    this.binded = false;
    return this;
};

MousePushButton.prototype.handleEvent = function (evt) {
    switch (evt.type) {
        case 'mousedown':
            this.onMousedown(evt);
            break;
        case 'mousemove':
            this.onMousemove(evt);
            break;
        case 'mouseup':
            this.onMouseup(evt);
            break;
    }
};

MousePushButton.prototype.onMousedown = function (evt) {
    if (!this.active) {
        if (evt.button === 0) {
            evt.preventDefault();
            this.setActive(true);
            var boundingRect = this.el.getBoundingClientRect();
            this.boundaries.minX = boundingRect.left;
            this.boundaries.maxX = boundingRect.left + boundingRect.width;
            this.boundaries.minY = boundingRect.top;
            this.boundaries.maxY = boundingRect.bottom;
            this.el.ownerDocument.addEventListener('mousemove', this, false);
            this.el.ownerDocument.addEventListener('mouseup', this, false);
            this.promisef = Q.delay(evt, this.delay).then(this.f);
        }
    }
};

MousePushButton.prototype.onMousemove = function (evt) {
    if(this.active && !this.leftOrEnded) {
        evt.preventDefault();
        if (!this._isInActiveZone(evt))
            this.onMouseup(evt);
    }
};

MousePushButton.prototype.onMouseup = function (evt) {
    if(this.active && !this.leftOrEnded) {
        this._removeCls();
        this.leftOrEnded = true;
        this.promisef
                .then(evt)
                .then(this.g)
                .finally(this._done(evt))
                .done();
    }
};

MousePushButton.prototype._done = function (evt) {
    var btn = this;
    return function () {
        btn.setActive(false);
        btn.leftOrEnded = false;
        btn.el.ownerDocument.removeEventListener('mousemove', btn, false);
        btn.el.ownerDocument.removeEventListener('mouseup', btn, false);
    };
};

module.exports = MousePushButton;
