mouse.js 868 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import EventSystem from './EventSystem';
  2. export default class Mouse extends EventSystem{
  3. constructor(container = window){
  4. super()
  5. this.container = container;
  6. }
  7. handler(e) {
  8. switch(e.button){
  9. case 0:
  10. this.emit('LeftDown',e);
  11. break;
  12. case 2:
  13. this.emit('RightDown',e);
  14. break;
  15. default:
  16. console.log('sss')
  17. break;
  18. }
  19. }
  20. dropHandler(event) {
  21. let data = event.dataTransfer.getData('MyObject');
  22. this.emit('Drop', {
  23. event,
  24. data
  25. });
  26. }
  27. droppable(droppable = true) {
  28. this.isDroppable = droppable;
  29. return this;
  30. }
  31. cancel(e) { e.preventDefault(); }
  32. listen(){
  33. this.container.addEventListener('mousedown', this.handler.bind(this));
  34. if(this.isDroppable) {
  35. this.container.addEventListener('dragover', this.cancel);
  36. this.container.addEventListener('drop', this.dropHandler.bind(this));
  37. }
  38. return this;
  39. }
  40. }