sudo.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict';
  6. import React from 'react';
  7. import Frame from './frame';
  8. import './sudo.less';
  9. export default class SudoPrompt extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. this.onSuccess = null;
  13. this.state = {
  14. show: false,
  15. pswd: ''
  16. }
  17. }
  18. componentDidMount() {
  19. SH_event.on('sudo_prompt', (success) => {
  20. this.setState({show: true});
  21. this.onSuccess = success;
  22. setTimeout(() => {
  23. let el = this.refs.body;
  24. el && el.querySelector('input').focus();
  25. }, 100);
  26. });
  27. }
  28. onOK() {
  29. let pswd = this.refs.pswd.value;
  30. if (!pswd) return;
  31. this.setState({
  32. show: false,
  33. pswd: pswd
  34. });
  35. SH_event.emit('sudo_pswd', pswd);
  36. if (typeof this.onSuccess === 'function') {
  37. this.onSuccess(pswd);
  38. }
  39. this.onSuccess = null;
  40. }
  41. onCancel() {
  42. this.setState({
  43. show: false
  44. });
  45. this.onSuccess = null;
  46. }
  47. body() {
  48. return (
  49. <div ref="body">
  50. <div className="ln">
  51. <div className="title">{SH_Agent.lang.sudo_pswd}</div>
  52. <div className="cnt">
  53. <input
  54. type="password"
  55. ref="pswd"
  56. onKeyDown={(e)=>(e.keyCode === 13 && this.onOK()||e.keyCode===27 && this.onCancel())}
  57. />
  58. </div>
  59. </div>
  60. </div>
  61. )
  62. }
  63. render() {
  64. return (
  65. <Frame
  66. show={this.state.show}
  67. head={SH_Agent.lang.input_sudo_pswd}
  68. body={this.body()}
  69. onOK={() => this.onOK()}
  70. onCancel={() => this.onCancel()}
  71. />
  72. );
  73. }
  74. }