rep.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // Dependencies
  9. const React = require("devtools/client/shared/vendor/react");
  10. const { isGrip } = require("./rep-utils");
  11. // Load all existing rep templates
  12. const { Undefined } = require("./undefined");
  13. const { Null } = require("./null");
  14. const { StringRep } = require("./string");
  15. const { LongStringRep } = require("./long-string");
  16. const { Number } = require("./number");
  17. const { ArrayRep } = require("./array");
  18. const { Obj } = require("./object");
  19. const { SymbolRep } = require("./symbol");
  20. const { InfinityRep } = require("./infinity");
  21. const { NaNRep } = require("./nan");
  22. // DOM types (grips)
  23. const { Attribute } = require("./attribute");
  24. const { DateTime } = require("./date-time");
  25. const { Document } = require("./document");
  26. const { Event } = require("./event");
  27. const { Func } = require("./function");
  28. const { PromiseRep } = require("./promise");
  29. const { RegExp } = require("./regexp");
  30. const { StyleSheet } = require("./stylesheet");
  31. const { CommentNode } = require("./comment-node");
  32. const { ElementNode } = require("./element-node");
  33. const { TextNode } = require("./text-node");
  34. const { Window } = require("./window");
  35. const { ObjectWithText } = require("./object-with-text");
  36. const { ObjectWithURL } = require("./object-with-url");
  37. const { GripArray } = require("./grip-array");
  38. const { GripMap } = require("./grip-map");
  39. const { Grip } = require("./grip");
  40. // List of all registered template.
  41. // XXX there should be a way for extensions to register a new
  42. // or modify an existing rep.
  43. let reps = [
  44. RegExp,
  45. StyleSheet,
  46. Event,
  47. DateTime,
  48. CommentNode,
  49. ElementNode,
  50. TextNode,
  51. Attribute,
  52. LongStringRep,
  53. Func,
  54. PromiseRep,
  55. ArrayRep,
  56. Document,
  57. Window,
  58. ObjectWithText,
  59. ObjectWithURL,
  60. GripArray,
  61. GripMap,
  62. Grip,
  63. Undefined,
  64. Null,
  65. StringRep,
  66. Number,
  67. SymbolRep,
  68. InfinityRep,
  69. NaNRep,
  70. ];
  71. /**
  72. * Generic rep that is using for rendering native JS types or an object.
  73. * The right template used for rendering is picked automatically according
  74. * to the current value type. The value must be passed is as 'object'
  75. * property.
  76. */
  77. const Rep = React.createClass({
  78. displayName: "Rep",
  79. propTypes: {
  80. object: React.PropTypes.any,
  81. defaultRep: React.PropTypes.object,
  82. mode: React.PropTypes.string
  83. },
  84. render: function () {
  85. let rep = getRep(this.props.object, this.props.defaultRep);
  86. return rep(this.props);
  87. },
  88. });
  89. // Helpers
  90. /**
  91. * Return a rep object that is responsible for rendering given
  92. * object.
  93. *
  94. * @param object {Object} Object to be rendered in the UI. This
  95. * can be generic JS object as well as a grip (handle to a remote
  96. * debuggee object).
  97. *
  98. * @param defaultObject {React.Component} The default template
  99. * that should be used to render given object if none is found.
  100. */
  101. function getRep(object, defaultRep = Obj) {
  102. let type = typeof object;
  103. if (type == "object" && object instanceof String) {
  104. type = "string";
  105. } else if (object && type == "object" && object.type) {
  106. type = object.type;
  107. }
  108. if (isGrip(object)) {
  109. type = object.class;
  110. }
  111. for (let i = 0; i < reps.length; i++) {
  112. let rep = reps[i];
  113. try {
  114. // supportsObject could return weight (not only true/false
  115. // but a number), which would allow to priorities templates and
  116. // support better extensibility.
  117. if (rep.supportsObject(object, type)) {
  118. return React.createFactory(rep.rep);
  119. }
  120. } catch (err) {
  121. console.error(err);
  122. }
  123. }
  124. return React.createFactory(defaultRep.rep);
  125. }
  126. // Exports from this module
  127. exports.Rep = Rep;
  128. });