checkbox.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. 'use strict';
  2. /**
  3. * `list` type prompt
  4. */
  5. var _ = require('lodash');
  6. var chalk = require('chalk');
  7. var cliCursor = require('cli-cursor');
  8. var figures = require('figures');
  9. var Base = require('./base');
  10. var observe = require('../utils/events');
  11. var Paginator = require('../utils/paginator');
  12. class CheckboxPrompt extends Base {
  13. constructor(questions, rl, answers) {
  14. super(questions, rl, answers);
  15. if (!this.opt.choices) {
  16. this.throwParamError('choices');
  17. }
  18. if (_.isArray(this.opt.default)) {
  19. this.opt.choices.forEach(function(choice) {
  20. if (this.opt.default.indexOf(choice.value) >= 0) {
  21. choice.checked = true;
  22. }
  23. }, this);
  24. }
  25. this.pointer = 0;
  26. this.firstRender = true;
  27. // Make sure no default is set (so it won't be printed)
  28. this.opt.default = null;
  29. this.paginator = new Paginator(this.screen);
  30. }
  31. /**
  32. * Start the Inquiry session
  33. * @param {Function} cb Callback when prompt is done
  34. * @return {this}
  35. */
  36. _run(cb) {
  37. this.done = cb;
  38. var events = observe(this.rl);
  39. var validation = this.handleSubmitEvents(
  40. events.line.map(this.getCurrentValue.bind(this))
  41. );
  42. validation.success.forEach(this.onEnd.bind(this));
  43. validation.error.forEach(this.onError.bind(this));
  44. events.normalizedUpKey.takeUntil(validation.success).forEach(this.onUpKey.bind(this));
  45. events.normalizedDownKey
  46. .takeUntil(validation.success)
  47. .forEach(this.onDownKey.bind(this));
  48. events.numberKey.takeUntil(validation.success).forEach(this.onNumberKey.bind(this));
  49. events.spaceKey.takeUntil(validation.success).forEach(this.onSpaceKey.bind(this));
  50. events.aKey.takeUntil(validation.success).forEach(this.onAllKey.bind(this));
  51. events.iKey.takeUntil(validation.success).forEach(this.onInverseKey.bind(this));
  52. // Init the prompt
  53. cliCursor.hide();
  54. this.render();
  55. this.firstRender = false;
  56. return this;
  57. }
  58. /**
  59. * Render the prompt to screen
  60. * @return {CheckboxPrompt} self
  61. */
  62. render(error) {
  63. // Render question
  64. var message = this.getQuestion();
  65. var bottomContent = '';
  66. if (this.firstRender) {
  67. message +=
  68. '(Press ' +
  69. chalk.cyan.bold('<space>') +
  70. ' to select, ' +
  71. chalk.cyan.bold('<a>') +
  72. ' to toggle all, ' +
  73. chalk.cyan.bold('<i>') +
  74. ' to invert selection)';
  75. }
  76. // Render choices or answer depending on the state
  77. if (this.status === 'answered') {
  78. message += chalk.cyan(this.selection.join(', '));
  79. } else {
  80. var choicesStr = renderChoices(this.opt.choices, this.pointer);
  81. var indexPosition = this.opt.choices.indexOf(
  82. this.opt.choices.getChoice(this.pointer)
  83. );
  84. message +=
  85. '\n' + this.paginator.paginate(choicesStr, indexPosition, this.opt.pageSize);
  86. }
  87. if (error) {
  88. bottomContent = chalk.red('>> ') + error;
  89. }
  90. this.screen.render(message, bottomContent);
  91. }
  92. /**
  93. * When user press `enter` key
  94. */
  95. onEnd(state) {
  96. this.status = 'answered';
  97. // Rerender prompt (and clean subline error)
  98. this.render();
  99. this.screen.done();
  100. cliCursor.show();
  101. this.done(state.value);
  102. }
  103. onError(state) {
  104. this.render(state.isValid);
  105. }
  106. getCurrentValue() {
  107. var choices = this.opt.choices.filter(function(choice) {
  108. return Boolean(choice.checked) && !choice.disabled;
  109. });
  110. this.selection = _.map(choices, 'short');
  111. return _.map(choices, 'value');
  112. }
  113. onUpKey() {
  114. var len = this.opt.choices.realLength;
  115. this.pointer = this.pointer > 0 ? this.pointer - 1 : len - 1;
  116. this.render();
  117. }
  118. onDownKey() {
  119. var len = this.opt.choices.realLength;
  120. this.pointer = this.pointer < len - 1 ? this.pointer + 1 : 0;
  121. this.render();
  122. }
  123. onNumberKey(input) {
  124. if (input <= this.opt.choices.realLength) {
  125. this.pointer = input - 1;
  126. this.toggleChoice(this.pointer);
  127. }
  128. this.render();
  129. }
  130. onSpaceKey() {
  131. this.toggleChoice(this.pointer);
  132. this.render();
  133. }
  134. onAllKey() {
  135. var shouldBeChecked = Boolean(
  136. this.opt.choices.find(function(choice) {
  137. return choice.type !== 'separator' && !choice.checked;
  138. })
  139. );
  140. this.opt.choices.forEach(function(choice) {
  141. if (choice.type !== 'separator') {
  142. choice.checked = shouldBeChecked;
  143. }
  144. });
  145. this.render();
  146. }
  147. onInverseKey() {
  148. this.opt.choices.forEach(function(choice) {
  149. if (choice.type !== 'separator') {
  150. choice.checked = !choice.checked;
  151. }
  152. });
  153. this.render();
  154. }
  155. toggleChoice(index) {
  156. var item = this.opt.choices.getChoice(index);
  157. if (item !== undefined) {
  158. this.opt.choices.getChoice(index).checked = !item.checked;
  159. }
  160. }
  161. }
  162. /**
  163. * Function for rendering checkbox choices
  164. * @param {Number} pointer Position of the pointer
  165. * @return {String} Rendered content
  166. */
  167. function renderChoices(choices, pointer) {
  168. var output = '';
  169. var separatorOffset = 0;
  170. choices.forEach(function(choice, i) {
  171. if (choice.type === 'separator') {
  172. separatorOffset++;
  173. output += ' ' + choice + '\n';
  174. return;
  175. }
  176. if (choice.disabled) {
  177. separatorOffset++;
  178. output += ' - ' + choice.name;
  179. output += ' (' + (_.isString(choice.disabled) ? choice.disabled : 'Disabled') + ')';
  180. } else {
  181. var isSelected = i - separatorOffset === pointer;
  182. output += isSelected ? chalk.cyan(figures.pointer) : ' ';
  183. output += getCheckbox(choice.checked) + ' ' + choice.name;
  184. }
  185. output += '\n';
  186. });
  187. return output.replace(/\n$/, '');
  188. }
  189. /**
  190. * Get the checkbox
  191. * @param {Boolean} checked - add a X or not to the checkbox
  192. * @return {String} Composited checkbox string
  193. */
  194. function getCheckbox(checked) {
  195. return checked ? chalk.green(figures.radioOn) : figures.radioOff;
  196. }
  197. module.exports = CheckboxPrompt;