ProviderViewModel.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import defaultValue from "../../Core/defaultValue.js";
  2. import defined from "../../Core/defined.js";
  3. import DeveloperError from "../../Core/DeveloperError.js";
  4. import knockout from "../../ThirdParty/knockout.js";
  5. import createCommand from "../createCommand.js";
  6. /**
  7. * A view model that represents each item in the {@link BaseLayerPicker}.
  8. *
  9. * @alias ProviderViewModel
  10. * @constructor
  11. *
  12. * @param {Object} options The object containing all parameters.
  13. * @param {String} options.name The name of the layer.
  14. * @param {String} options.tooltip The tooltip to show when the item is moused over.
  15. * @param {String} options.iconUrl An icon representing the layer.
  16. * @param {String} [options.category] A category for the layer.
  17. * @param {ProviderViewModel.CreationFunction|Command} options.creationFunction A function or Command
  18. * that creates one or more providers which will be added to the globe when this item is selected.
  19. *
  20. * @see BaseLayerPicker
  21. * @see ImageryProvider
  22. * @see TerrainProvider
  23. */
  24. function ProviderViewModel(options) {
  25. //>>includeStart('debug', pragmas.debug);
  26. if (!defined(options.name)) {
  27. throw new DeveloperError("options.name is required.");
  28. }
  29. if (!defined(options.tooltip)) {
  30. throw new DeveloperError("options.tooltip is required.");
  31. }
  32. if (!defined(options.iconUrl)) {
  33. throw new DeveloperError("options.iconUrl is required.");
  34. }
  35. if (typeof options.creationFunction !== "function") {
  36. throw new DeveloperError("options.creationFunction is required.");
  37. }
  38. //>>includeEnd('debug');
  39. let creationCommand = options.creationFunction;
  40. if (!defined(creationCommand.canExecute)) {
  41. creationCommand = createCommand(creationCommand);
  42. }
  43. this._creationCommand = creationCommand;
  44. /**
  45. * Gets the display name. This property is observable.
  46. * @type {String}
  47. */
  48. this.name = options.name;
  49. /**
  50. * Gets the tooltip. This property is observable.
  51. * @type {String}
  52. */
  53. this.tooltip = options.tooltip;
  54. /**
  55. * Gets the icon. This property is observable.
  56. * @type {String}
  57. */
  58. this.iconUrl = options.iconUrl;
  59. this._category = defaultValue(options.category, "");
  60. knockout.track(this, ["name", "tooltip", "iconUrl"]);
  61. }
  62. Object.defineProperties(ProviderViewModel.prototype, {
  63. /**
  64. * Gets the Command that creates one or more providers which will be added to
  65. * the globe when this item is selected.
  66. * @memberof ProviderViewModel.prototype
  67. * @memberof ProviderViewModel.prototype
  68. * @type {Command}
  69. * @readonly
  70. */
  71. creationCommand: {
  72. get: function () {
  73. return this._creationCommand;
  74. },
  75. },
  76. /**
  77. * Gets the category
  78. * @type {String}
  79. * @memberof ProviderViewModel.prototype
  80. * @readonly
  81. */
  82. category: {
  83. get: function () {
  84. return this._category;
  85. },
  86. },
  87. });
  88. /**
  89. * A function which creates one or more providers.
  90. * @callback ProviderViewModel.CreationFunction
  91. * @returns {ImageryProvider|TerrainProvider|ImageryProvider[]|TerrainProvider[]}
  92. * The ImageryProvider or TerrainProvider, or array of providers, to be added
  93. * to the globe.
  94. */
  95. export default ProviderViewModel;