event.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. "use strict";
  6. // Make this available to both AMD and CJS environments
  7. define(function (require, exports, module) {
  8. // ReactJS
  9. const React = require("devtools/client/shared/vendor/react");
  10. // Reps
  11. const { createFactories, isGrip } = require("./rep-utils");
  12. const { rep } = createFactories(require("./grip").Grip);
  13. /**
  14. * Renders DOM event objects.
  15. */
  16. let Event = React.createClass({
  17. displayName: "event",
  18. propTypes: {
  19. object: React.PropTypes.object.isRequired
  20. },
  21. render: function () {
  22. // Use `Object.assign` to keep `this.props` without changes because:
  23. // 1. JSON.stringify/JSON.parse is slow.
  24. // 2. Immutable.js is planned for the future.
  25. let props = Object.assign({}, this.props);
  26. props.object = Object.assign({}, this.props.object);
  27. props.object.preview = Object.assign({}, this.props.object.preview);
  28. props.object.preview.ownProperties = props.object.preview.properties;
  29. delete props.object.preview.properties;
  30. props.object.ownPropertyLength =
  31. Object.keys(props.object.preview.ownProperties).length;
  32. switch (props.object.class) {
  33. case "MouseEvent":
  34. props.isInterestingProp = (type, value, name) => {
  35. return (name == "clientX" ||
  36. name == "clientY" ||
  37. name == "layerX" ||
  38. name == "layerY");
  39. };
  40. break;
  41. case "KeyboardEvent":
  42. props.isInterestingProp = (type, value, name) => {
  43. return (name == "key" ||
  44. name == "charCode" ||
  45. name == "keyCode");
  46. };
  47. break;
  48. case "MessageEvent":
  49. props.isInterestingProp = (type, value, name) => {
  50. return (name == "isTrusted" ||
  51. name == "data");
  52. };
  53. break;
  54. }
  55. return rep(props);
  56. }
  57. });
  58. // Registration
  59. function supportsObject(grip, type) {
  60. if (!isGrip(grip)) {
  61. return false;
  62. }
  63. return (grip.preview && grip.preview.kind == "DOMEvent");
  64. }
  65. // Exports from this module
  66. exports.Event = {
  67. rep: Event,
  68. supportsObject: supportsObject
  69. };
  70. });