prompt.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * prompt.js - prompt 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 Box = require('./box');
  11. var Button = require('./button');
  12. var Textbox = require('./textbox');
  13. /**
  14. * Prompt
  15. */
  16. function Prompt(options) {
  17. if (!(this instanceof Node)) {
  18. return new Prompt(options);
  19. }
  20. options = options || {};
  21. options.hidden = true;
  22. Box.call(this, options);
  23. this._.input = new Textbox({
  24. parent: this,
  25. top: 3,
  26. height: 1,
  27. left: 2,
  28. right: 2,
  29. bg: 'black'
  30. });
  31. this._.okay = new Button({
  32. parent: this,
  33. top: 5,
  34. height: 1,
  35. left: 2,
  36. width: 6,
  37. content: 'Okay',
  38. align: 'center',
  39. bg: 'black',
  40. hoverBg: 'blue',
  41. autoFocus: false,
  42. mouse: true
  43. });
  44. this._.cancel = new Button({
  45. parent: this,
  46. top: 5,
  47. height: 1,
  48. shrink: true,
  49. left: 10,
  50. width: 8,
  51. content: 'Cancel',
  52. align: 'center',
  53. bg: 'black',
  54. hoverBg: 'blue',
  55. autoFocus: false,
  56. mouse: true
  57. });
  58. }
  59. Prompt.prototype.__proto__ = Box.prototype;
  60. Prompt.prototype.type = 'prompt';
  61. Prompt.prototype.input =
  62. Prompt.prototype.setInput =
  63. Prompt.prototype.readInput = function(text, value, callback) {
  64. var self = this;
  65. var okay, cancel;
  66. if (!callback) {
  67. callback = value;
  68. value = '';
  69. }
  70. // Keep above:
  71. // var parent = this.parent;
  72. // this.detach();
  73. // parent.append(this);
  74. this.show();
  75. this.setContent(' ' + text);
  76. this._.input.value = value;
  77. this.screen.saveFocus();
  78. this._.okay.on('press', okay = function() {
  79. self._.input.submit();
  80. });
  81. this._.cancel.on('press', cancel = function() {
  82. self._.input.cancel();
  83. });
  84. this._.input.readInput(function(err, data) {
  85. self.hide();
  86. self.screen.restoreFocus();
  87. self._.okay.removeListener('press', okay);
  88. self._.cancel.removeListener('press', cancel);
  89. return callback(err, data);
  90. });
  91. this.screen.render();
  92. };
  93. /**
  94. * Expose
  95. */
  96. module.exports = Prompt;