EventDispatcher.js 835 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * https://github.com/mrdoob/eventdispatcher.js/
  3. */
  4. THREE.EventDispatcher = function () {
  5. var listeners = {};
  6. this.addEventListener = function ( type, listener ) {
  7. if ( listeners[ type ] === undefined ) {
  8. listeners[ type ] = [];
  9. }
  10. if ( listeners[ type ].indexOf( listener ) === - 1 ) {
  11. listeners[ type ].push( listener );
  12. }
  13. };
  14. this.removeEventListener = function ( type, listener ) {
  15. var index = listeners[ type ].indexOf( listener );
  16. if ( index !== - 1 ) {
  17. listeners[ type ].splice( index, 1 );
  18. }
  19. };
  20. this.dispatchEvent = function ( event ) {
  21. var listenerArray = listeners[ event.type ];
  22. if ( listenerArray !== undefined ) {
  23. event.target = this;
  24. for ( var i = 0, l = listenerArray.length; i < l; i ++ ) {
  25. listenerArray[ i ].call( this, event );
  26. }
  27. }
  28. };
  29. };