ToggleButtonViewModel.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*global define*/
  2. define([
  3. '../Core/defaultValue',
  4. '../Core/defined',
  5. '../Core/defineProperties',
  6. '../Core/DeveloperError',
  7. '../ThirdParty/knockout'
  8. ], function(
  9. defaultValue,
  10. defined,
  11. defineProperties,
  12. DeveloperError,
  13. knockout) {
  14. "use strict";
  15. /**
  16. * A view model which exposes the properties of a toggle button.
  17. * @alias ToggleButtonViewModel
  18. * @constructor
  19. *
  20. * @param {Command} command The command which will be executed when the button is toggled.
  21. * @param {Object} [options] Object with the following properties:
  22. * @param {Boolean} [options.toggled=false] A boolean indicating whether the button should be initially toggled.
  23. * @param {String} [options.tooltip=''] A string containing the button's tooltip.
  24. */
  25. var ToggleButtonViewModel = function(command, options) {
  26. //>>includeStart('debug', pragmas.debug);
  27. if (!defined(command)) {
  28. throw new DeveloperError('command is required.');
  29. }
  30. //>>includeEnd('debug');
  31. this._command = command;
  32. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  33. /**
  34. * Gets or sets whether the button is currently toggled. This property is observable.
  35. * @type {Boolean}
  36. * @default false
  37. */
  38. this.toggled = defaultValue(options.toggled, false);
  39. /**
  40. * Gets or sets the button's tooltip. This property is observable.
  41. * @type {String}
  42. * @default ''
  43. */
  44. this.tooltip = defaultValue(options.tooltip, '');
  45. knockout.track(this, ['toggled', 'tooltip']);
  46. };
  47. defineProperties(ToggleButtonViewModel.prototype, {
  48. /**
  49. * Gets the command which will be executed when the button is toggled.
  50. * @memberof ToggleButtonViewModel.prototype
  51. * @type {Command}
  52. */
  53. command : {
  54. get : function() {
  55. return this._command;
  56. }
  57. }
  58. });
  59. return ToggleButtonViewModel;
  60. });