mouse.js 888 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. console.log(this)
  34. this.container.addEventListener('mousedown', this.handler.bind(this));
  35. if(this.isDroppable) {
  36. this.container.addEventListener('dragover', this.cancel);
  37. this.container.addEventListener('drop', this.dropHandler.bind(this));
  38. }
  39. return this;
  40. }
  41. }