ProviderViewModel.js 3.6 KB

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