radiobutton.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * radiobutton.js - radio button element for blessed
  3. * Copyright (c) 2013-2015, Christopher Jeffrey and contributors (MIT License).
  4. * https://github.com/chjj/blessed
  5. */
  6. /**
  7. * Modules
  8. */
  9. var Node = require('./node');
  10. var Checkbox = require('./checkbox');
  11. /**
  12. * RadioButton
  13. */
  14. function RadioButton(options) {
  15. var self = this;
  16. if (!(this instanceof Node)) {
  17. return new RadioButton(options);
  18. }
  19. options = options || {};
  20. Checkbox.call(this, options);
  21. this.on('check', function() {
  22. var el = self;
  23. while (el = el.parent) {
  24. if (el.type === 'radio-set'
  25. || el.type === 'form') break;
  26. }
  27. el = el || self.parent;
  28. el.forDescendants(function(el) {
  29. if (el.type !== 'radio-button' || el === self) {
  30. return;
  31. }
  32. el.uncheck();
  33. });
  34. });
  35. }
  36. RadioButton.prototype.__proto__ = Checkbox.prototype;
  37. RadioButton.prototype.type = 'radio-button';
  38. RadioButton.prototype.render = function() {
  39. this.clearPos(true);
  40. this.setContent('(' + (this.checked ? '*' : ' ') + ') ' + this.text, true);
  41. return this._render();
  42. };
  43. RadioButton.prototype.toggle = RadioButton.prototype.check;
  44. /**
  45. * Expose
  46. */
  47. module.exports = RadioButton;