ProviderViewModel.js 3.0 KB

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