sudo.js 1.7 KB

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