preventdefault.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module ui/bindings/preventdefault
  7. */
  8. /**
  9. * A helper which executes a native `Event.preventDefault()` if the target of an event equals the
  10. * {@link module:ui/view~View#element element of the view}. It shortens the definition of a
  11. * {@link module:ui/view~View#template template}.
  12. *
  13. * // In a class extending View.
  14. * import preventDefault from '@ckeditor/ckeditor5-ui/src/bindings/preventdefault';
  15. *
  16. * // ...
  17. *
  18. * this.setTemplate( {
  19. * tag: 'div',
  20. *
  21. * on: {
  22. * // Prevent the default mousedown action on this view.
  23. * mousedown: preventDefault( this )
  24. * }
  25. * } );
  26. *
  27. * @param {module:ui/view~View} view View instance that defines the template.
  28. * @returns {module:ui/template~TemplateToBinding}
  29. */
  30. export default function preventDefault( view ) {
  31. return view.bindTemplate.to( evt => {
  32. if ( evt.target === view.element ) {
  33. evt.preventDefault();
  34. }
  35. } );
  36. }