object-provider.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. /**
  9. * Implementation of the default data provider. A provider is state less
  10. * object responsible for transformation data (usually a state) to
  11. * a structure that can be directly consumed by the tree-view component.
  12. */
  13. let ObjectProvider = {
  14. getChildren: function (object) {
  15. let children = [];
  16. if (object instanceof ObjectProperty) {
  17. object = object.value;
  18. }
  19. if (!object) {
  20. return [];
  21. }
  22. if (typeof (object) == "string") {
  23. return [];
  24. }
  25. for (let prop in object) {
  26. try {
  27. children.push(new ObjectProperty(prop, object[prop]));
  28. } catch (e) {
  29. console.error(e);
  30. }
  31. }
  32. return children;
  33. },
  34. hasChildren: function (object) {
  35. if (object instanceof ObjectProperty) {
  36. object = object.value;
  37. }
  38. if (!object) {
  39. return false;
  40. }
  41. if (typeof object == "string") {
  42. return false;
  43. }
  44. if (typeof object !== "object") {
  45. return false;
  46. }
  47. return Object.keys(object).length > 0;
  48. },
  49. getLabel: function (object) {
  50. return (object instanceof ObjectProperty) ?
  51. object.name : null;
  52. },
  53. getValue: function (object) {
  54. return (object instanceof ObjectProperty) ?
  55. object.value : null;
  56. },
  57. getKey: function (object) {
  58. return (object instanceof ObjectProperty) ?
  59. object.name : null;
  60. },
  61. getType: function (object) {
  62. return (object instanceof ObjectProperty) ?
  63. typeof object.value : typeof object;
  64. }
  65. };
  66. function ObjectProperty(name, value) {
  67. this.name = name;
  68. this.value = value;
  69. }
  70. // Exports from this module
  71. exports.ObjectProperty = ObjectProperty;
  72. exports.ObjectProvider = ObjectProvider;
  73. });