grip-provider.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. const { fetchProperties } = require("./actions/grips");
  7. const { Property } = require("./reducers/grips");
  8. // Implementation
  9. function GripProvider(grips, dispatch) {
  10. this.grips = grips;
  11. this.dispatch = dispatch;
  12. }
  13. /**
  14. * This object provides data for the tree displayed in the tooltip
  15. * content.
  16. */
  17. GripProvider.prototype = {
  18. /**
  19. * Fetches properties from the backend. These properties might be
  20. * displayed as child objects in e.g. a tree UI widget.
  21. */
  22. getChildren: function (object) {
  23. let grip = object;
  24. if (object instanceof Property) {
  25. grip = this.getValue(object);
  26. }
  27. if (!grip || !grip.actor) {
  28. return [];
  29. }
  30. let props = this.grips.get(grip.actor);
  31. if (!props) {
  32. // Fetch missing data from the backend. Returning a promise
  33. // from data provider causes the tree to show a spinner.
  34. return this.dispatch(fetchProperties(grip));
  35. }
  36. return props;
  37. },
  38. hasChildren: function (object) {
  39. if (object instanceof Property) {
  40. let value = this.getValue(object);
  41. if (!value) {
  42. return false;
  43. }
  44. let hasChildren = value.ownPropertyLength > 0;
  45. if (value.preview) {
  46. hasChildren = hasChildren || value.preview.ownPropertiesLength > 0;
  47. }
  48. if (value.preview) {
  49. let preview = value.preview;
  50. let k = preview.kind;
  51. let objectsWithProps = ["DOMNode", "ObjectWithURL"];
  52. hasChildren = hasChildren || (objectsWithProps.indexOf(k) != -1);
  53. hasChildren = hasChildren || (k == "ArrayLike" && preview.length > 0);
  54. }
  55. return (value.type == "object" && hasChildren);
  56. }
  57. return null;
  58. },
  59. getValue: function (object) {
  60. if (object instanceof Property) {
  61. let value = object.value;
  62. return (typeof value.value != "undefined") ? value.value :
  63. value.getterValue;
  64. }
  65. return object;
  66. },
  67. getLabel: function (object) {
  68. return (object instanceof Property) ? object.name : null;
  69. },
  70. getKey: function (object) {
  71. return (object instanceof Property) ? object.key : null;
  72. },
  73. getType: function (object) {
  74. return object.class ? object.class : "";
  75. },
  76. };
  77. // Exports from this module
  78. exports.GripProvider = GripProvider;