EventDispatcher.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @author mrdoob / http://mrdoob.com/ https://github.com/mrdoob/eventdispatcher.js
  3. *
  4. * with slight modifications by mschuetz, http://potree.org
  5. *
  6. */
  7. // The MIT License
  8. //
  9. // Copyright (c) 2011 Mr.doob
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy
  12. // of this software and associated documentation files (the "Software"), to deal
  13. // in the Software without restriction, including without limitation the rights
  14. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. // copies of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in
  19. // all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. // THE SOFTWARE.
  28. export class EventDispatcher{
  29. constructor(){
  30. this._listeners = {};
  31. }
  32. addEventListener(type, listener, importance=0 ){//add importance
  33. const listeners = this._listeners;
  34. if(listeners[type] === undefined){
  35. listeners[type] = [];
  36. }
  37. if(listeners[type].indexOf(listener) === - 1){
  38. listeners[type].push({ listener, importance});
  39. listeners[type] = listeners[type].sort((e,a)=> a.importance - e.importance)//add
  40. }
  41. }
  42. hasEventListener(type, listener){
  43. const listeners = this._listeners;
  44. return listeners[type] !== undefined && listeners[type].indexOf(listener) !== - 1;
  45. }
  46. removeEventListener(type, listener){
  47. let listeners = this._listeners;
  48. let listenerArray = listeners[type];
  49. if (listenerArray !== undefined){
  50. /* let index = listenerArray.indexOf(listener);
  51. if(index !== - 1){
  52. listenerArray.splice(index, 1);
  53. } */
  54. let item = listenerArray.find(e=>e.listener == listener)
  55. item && listenerArray.splice(listenerArray.indexOf(item), 1);
  56. }
  57. }
  58. removeEventListeners(type){
  59. if(this._listeners[type] !== undefined){
  60. delete this._listeners[type];
  61. }
  62. };
  63. dispatchEvent(event){
  64. if(typeof event == 'string'){//add
  65. event = {type:event}
  66. }
  67. let listeners = this._listeners;
  68. let listenerArray = listeners[event.type];
  69. if ( listenerArray !== undefined ) {
  70. event.target = this;
  71. for(let {listener} of listenerArray.slice(0)){
  72. let result = listener.call(this, event); //add stopContinue
  73. if(result && result.stopContinue){
  74. break
  75. }
  76. }
  77. }
  78. }
  79. //add
  80. /* emit(type){
  81. this.dispatchEvent({type, arguments: Array.from(arguments).slice(1, arguments.length) })
  82. }
  83. on(type, fun){
  84. this.addEventListener(type,(ev)=>{
  85. fun.apply(this, ev.arguments)
  86. })
  87. }
  88. off(type, fun){
  89. this.removeEventListener(type,)
  90. }
  91. once(type, fun) {
  92. function callback() {
  93. this.removeEventListener(type, callback),
  94. n || (n = !0, fun.apply(this, arguments))
  95. }
  96. var n = !1;
  97. return callback.listener = fun,
  98. this.on(type, callback),
  99. this
  100. } */
  101. removeAllListeners(){
  102. this._listeners = {};
  103. }
  104. }