sudo.jsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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) {
  32. let el = this.refs.body
  33. el && el.querySelector('input').focus()
  34. return
  35. }
  36. this.setState({
  37. show: false,
  38. pswd: pswd
  39. })
  40. Agent.emit('sudo_pswd', pswd)
  41. if (typeof this.onSuccess === 'function') {
  42. this.onSuccess(pswd)
  43. }
  44. this.onSuccess = null
  45. }
  46. onCancel () {
  47. Agent.emit('sudo_cancel')
  48. this.setState({
  49. show: false
  50. })
  51. this.onSuccess = null
  52. }
  53. body () {
  54. let {lang} = this.props
  55. return (
  56. <div ref="body">
  57. <div className="ln">
  58. <div className="title">{lang.sudo_pswd}</div>
  59. <div className="cnt">
  60. <input
  61. type="password"
  62. ref="pswd"
  63. onKeyDown={e => (e.keyCode === 13 && this.onOK() ||
  64. e.keyCode === 27 && this.onCancel())}
  65. />
  66. </div>
  67. </div>
  68. </div>
  69. )
  70. }
  71. render () {
  72. let {lang} = this.props
  73. return (
  74. <MyFrame
  75. show={this.state.show}
  76. head={lang.input_sudo_pswd}
  77. body={this.body()}
  78. onOK={() => this.onOK()}
  79. onCancel={() => this.onCancel()}
  80. lang={lang}
  81. />
  82. )
  83. }
  84. }