textbox.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * textbox.js - textbox 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 Textarea = require('./textarea');
  11. /**
  12. * Textbox
  13. */
  14. function Textbox(options) {
  15. if (!(this instanceof Node)) {
  16. return new Textbox(options);
  17. }
  18. options = options || {};
  19. options.scrollable = false;
  20. Textarea.call(this, options);
  21. this.secret = options.secret;
  22. this.censor = options.censor;
  23. }
  24. Textbox.prototype.__proto__ = Textarea.prototype;
  25. Textbox.prototype.type = 'textbox';
  26. Textbox.prototype.__olistener = Textbox.prototype._listener;
  27. Textbox.prototype._listener = function(ch, key) {
  28. if (key.name === 'enter') {
  29. this._done(null, this.value);
  30. return;
  31. }
  32. return this.__olistener(ch, key);
  33. };
  34. Textbox.prototype.setValue = function(value) {
  35. var visible, val;
  36. if (value == null) {
  37. value = this.value;
  38. }
  39. if (this._value !== value) {
  40. value = value.replace(/\n/g, '');
  41. this.value = value;
  42. this._value = value;
  43. if (this.secret) {
  44. this.setContent('');
  45. } else if (this.censor) {
  46. this.setContent(Array(this.value.length + 1).join('*'));
  47. } else {
  48. visible = -(this.width - this.iwidth - 1);
  49. val = this.value.replace(/\t/g, this.screen.tabc);
  50. this.setContent(val.slice(visible));
  51. }
  52. this._updateCursor();
  53. }
  54. };
  55. Textbox.prototype.submit = function() {
  56. if (!this.__listener) return;
  57. return this.__listener('\r', { name: 'enter' });
  58. };
  59. /**
  60. * Expose
  61. */
  62. module.exports = Textbox;