button.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * button.js - 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 Input = require('./input');
  11. /**
  12. * Button
  13. */
  14. function Button(options) {
  15. var self = this;
  16. if (!(this instanceof Node)) {
  17. return new Button(options);
  18. }
  19. options = options || {};
  20. if (options.autoFocus == null) {
  21. options.autoFocus = false;
  22. }
  23. Input.call(this, options);
  24. this.on('keypress', function(ch, key) {
  25. if (key.name === 'enter' || key.name === 'space') {
  26. return self.press();
  27. }
  28. });
  29. if (this.options.mouse) {
  30. this.on('click', function() {
  31. return self.press();
  32. });
  33. }
  34. }
  35. Button.prototype.__proto__ = Input.prototype;
  36. Button.prototype.type = 'button';
  37. Button.prototype.press = function() {
  38. this.focus();
  39. this.value = true;
  40. var result = this.emit('press');
  41. delete this.value;
  42. return result;
  43. };
  44. /**
  45. * Expose
  46. */
  47. module.exports = Button;