RendererRegistryEventDispatcher.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Components.Rendering;
  6. using Microsoft.JSInterop;
  7. namespace Microsoft.AspNetCore.Components.Browser
  8. {
  9. /// <summary>
  10. /// Provides mechanisms for dispatching events to components in a <see cref="Renderer"/>.
  11. /// </summary>
  12. public static class RendererRegistryEventDispatcher
  13. {
  14. /// <summary>
  15. /// For framework use only.
  16. /// </summary>
  17. [JSInvokable(nameof(DispatchEvent))]
  18. public static Task DispatchEvent(
  19. BrowserEventDescriptor eventDescriptor, string eventArgsJson)
  20. {
  21. var eventArgs = ParseEventArgsJson(eventDescriptor.EventArgsType, eventArgsJson);
  22. var renderer = RendererRegistry.Current.Find(eventDescriptor.BrowserRendererId);
  23. return renderer.DispatchEventAsync(
  24. eventDescriptor.ComponentId,
  25. eventDescriptor.EventHandlerId,
  26. eventArgs);
  27. }
  28. private static UIEventArgs ParseEventArgsJson(string eventArgsType, string eventArgsJson)
  29. {
  30. switch (eventArgsType)
  31. {
  32. case "change":
  33. return Json.Deserialize<UIChangeEventArgs>(eventArgsJson);
  34. case "clipboard":
  35. return Json.Deserialize<UIClipboardEventArgs>(eventArgsJson);
  36. case "drag":
  37. return Json.Deserialize<UIDragEventArgs>(eventArgsJson);
  38. case "error":
  39. return Json.Deserialize<UIErrorEventArgs>(eventArgsJson);
  40. case "focus":
  41. return Json.Deserialize<UIFocusEventArgs>(eventArgsJson);
  42. case "keyboard":
  43. return Json.Deserialize<UIKeyboardEventArgs>(eventArgsJson);
  44. case "mouse":
  45. return Json.Deserialize<UIMouseEventArgs>(eventArgsJson);
  46. case "pointer":
  47. return Json.Deserialize<UIPointerEventArgs>(eventArgsJson);
  48. case "progress":
  49. return Json.Deserialize<UIProgressEventArgs>(eventArgsJson);
  50. case "touch":
  51. return Json.Deserialize<UITouchEventArgs>(eventArgsJson);
  52. case "unknown":
  53. return Json.Deserialize<UIEventArgs>(eventArgsJson);
  54. case "wheel":
  55. return Json.Deserialize<UIWheelEventArgs>(eventArgsJson);
  56. default:
  57. throw new ArgumentException($"Unsupported value '{eventArgsType}'.", nameof(eventArgsType));
  58. }
  59. }
  60. /// <summary>
  61. /// For framework use only.
  62. /// </summary>
  63. public class BrowserEventDescriptor
  64. {
  65. /// <summary>
  66. /// For framework use only.
  67. /// </summary>
  68. public int BrowserRendererId { get; set; }
  69. /// <summary>
  70. /// For framework use only.
  71. /// </summary>
  72. public int ComponentId { get; set; }
  73. /// <summary>
  74. /// For framework use only.
  75. /// </summary>
  76. public int EventHandlerId { get; set; }
  77. /// <summary>
  78. /// For framework use only.
  79. /// </summary>
  80. public string EventArgsType { get; set; }
  81. }
  82. }
  83. }