json-viewer.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. define(function (require, exports, module) {
  7. const { render } = require("devtools/client/shared/vendor/react-dom");
  8. const { createFactories } = require("devtools/client/shared/components/reps/rep-utils");
  9. const { MainTabbedArea } = createFactories(require("./components/main-tabbed-area"));
  10. const json = document.getElementById("json");
  11. // Application state object.
  12. let input = {
  13. jsonText: json.textContent,
  14. jsonPretty: null,
  15. headers: window.headers,
  16. tabActive: 0,
  17. prettified: false
  18. };
  19. // Remove BOM, if present.
  20. if (input.jsonText.startsWith("\ufeff")) {
  21. input.jsonText = input.jsonText.slice(1);
  22. }
  23. try {
  24. input.json = JSON.parse(input.jsonText);
  25. } catch (err) {
  26. input.json = err;
  27. }
  28. json.remove();
  29. /**
  30. * Application actions/commands. This list implements all commands
  31. * available for the JSON viewer.
  32. */
  33. input.actions = {
  34. onCopyJson: function () {
  35. dispatchEvent("copy", input.prettified ? input.jsonPretty : input.jsonText);
  36. },
  37. onSaveJson: function () {
  38. dispatchEvent("save", input.prettified ? input.jsonPretty : input.jsonText);
  39. },
  40. onCopyHeaders: function () {
  41. dispatchEvent("copy-headers", input.headers);
  42. },
  43. onSearch: function (value) {
  44. theApp.setState({searchFilter: value});
  45. },
  46. onPrettify: function (data) {
  47. if (input.prettified) {
  48. theApp.setState({jsonText: input.jsonText});
  49. } else {
  50. if (!input.jsonPretty) {
  51. input.jsonPretty = JSON.stringify(input.json, null, " ");
  52. }
  53. theApp.setState({jsonText: input.jsonPretty});
  54. }
  55. input.prettified = !input.prettified;
  56. },
  57. };
  58. /**
  59. * Helper for dispatching an event. It's handled in chrome scope.
  60. *
  61. * @param {String} type Event detail type
  62. * @param {Object} value Event detail value
  63. */
  64. function dispatchEvent(type, value) {
  65. let data = {
  66. detail: {
  67. type,
  68. value,
  69. }
  70. };
  71. let contentMessageEvent = new CustomEvent("contentMessage", data);
  72. window.dispatchEvent(contentMessageEvent);
  73. }
  74. /**
  75. * Render the main application component. It's the main tab bar displayed
  76. * at the top of the window. This component also represents ReacJS root.
  77. */
  78. let content = document.getElementById("content");
  79. let theApp = render(MainTabbedArea(input), content);
  80. let onResize = event => {
  81. window.document.body.style.height = window.innerHeight + "px";
  82. window.document.body.style.width = window.innerWidth + "px";
  83. };
  84. window.addEventListener("resize", onResize);
  85. onResize();
  86. // Send notification event to the window. Can be useful for
  87. // tests as well as extensions.
  88. let event = new CustomEvent("JSONViewInitialized", {});
  89. window.jsonViewInitialized = true;
  90. window.dispatchEvent(event);
  91. });