Source: VertexListeners.js

/**
 * @classdesc An event listeners wrapper. This is just a set of three listener 
 *              queues (drag, dragStart, dragEnd) and their respective firing
 *              functions.
 *
 * @author   Ikaros Kappler
 * @date     2018-08-27
 * @modified 2018-11-28 Added the vertex-param to the constructor and extended the event. Vertex events now have a 'params' attribute object.
 * @modified 2019-03-20 Added JSDoc tags.
 * @modified 2020-02-22 Added 'return this' to the add* functions (for chanining).
 * @version  1.0.3
 *
 * @file VertexListeners
 * @public
 **/

(function(_context) {
    "use strict";


    /**
     * The constructor.
     *
     * @constructor
     * @name VertexListeners
     * @param {Vertex} vertex - The vertex to use these listeners on (just a backward reference).
     **/
    var VertexListeners = function( vertex ) {
	this.drag = [];
	this.dragStart = [];
	this.dragEnd = [];
	this.vertex = vertex;
    };


    /**
     * Add a drag listener.
     *
     * @method addDragListener
     * @param {VertexListeners~dragListener} listener - The drag listener to add (a callback).
     * @return {void}
     * @instance
     * @memberof VertexListeners
     **/
    VertexListeners.prototype.addDragListener = function( listener ) {
	this.drag.push( listener );
	return this;
    };
    /**
     * The drag listener is a function with a single drag event param.
     * @callback VertexListeners~dragListener
     * @param {Event} e - The (extended) drag event.
     */
    

    
    /**
     * Add a dragStart listener.
     *
     * @method addDragListener
     * @param {VertexListeners~dragStartListener} listener - The drag-start listener to add (a callback).
     * @return {void}
     * @instance
     * @memberof VertexListeners
     **/
    VertexListeners.prototype.addDragStartListener = function( listener ) {
	this.dragStart.push( listener );
	return this;
    };
    /**
     * The drag-start listener is a function with a single drag event param.
     * @callback VertexListeners~dragStartListener
     * @param {Event} e - The (extended) drag event.
     */

    
 
    /**
     * Add a dragEnd listener.
     *
     * @method addDragListener
     * @param {VertexListeners~dragEndListener} listener - The drag-end listener to add (a callback).
     * @return {void}
     * @instance
     * @memberof VertexListeners
     **/
    VertexListeners.prototype.addDragEndListener = function( listener ) {
	this.dragEnd.push( listener );
	return this;
    };
    /**
     * The drag-end listener is a function with a single drag event param.
     * @callback VertexListeners~dragEndListener
     * @param {Event} e - The (extended) drag event.
     */

    

 
    /**
     * Fire a drag event with the given event instance to all
     * installed drag listeners.
     *
     * @method fireDragEvent
     * @param {Event} e - The drag event itself to be fired to all installed drag listeners.
     * @return {void}
     * @instance
     * @memberof VertexListeners
     **/
    VertexListeners.prototype.fireDragEvent = function( e ) {
	_fireEvent(this,this.drag,e);
    };


    
    /**
     * Fire a dragStart event with the given event instance to all
     * installed drag-start listeners.
     *
     * @method fireDragStartEvent
     * @param {Event} e - The drag-start event itself to be fired to all installed dragStart listeners.
     * @return {void}
     * @instance
     * @memberof VertexListeners
     **/
    VertexListeners.prototype.fireDragStartEvent = function( e ) {
	_fireEvent(this,this.dragStart,e);
    };


    
    /**
     * Fire a dragEnd event with the given event instance to all
     * installed drag-end listeners.
     *
     * @method fireDragEndEvent
     * @param {Event} e - The drag-end event itself to be fired to all installed dragEnd listeners.
     * @return {void}
     * @instance
     * @memberof VertexListeners
     **/
    VertexListeners.prototype.fireDragEndEvent = function( e ) {
	_fireEvent(this,this.dragEnd,e);
    };


    
    /**
     * @private
     **/
    var _fireEvent = function( _self, listeners, e ) {
	if( typeof e.params == 'undefined' )
	    e.params = {};
	e.params.vertex = _self.vertex;
	for( var i in listeners ) {
	    listeners[i]( e );
	}
    };

    // Export constructor to context.
    _context.VertexListeners = VertexListeners;
    
})(window);