From f5c597a9e351b8dfb84f0fe3f09046e772482fc6 Mon Sep 17 00:00:00 2001
From: Jason <jason.daurus@gmail.com>
Date: Wed, 15 Jun 2016 00:46:59 +0800
Subject: [web] Editor & Prompt

---
 web/src/js/components/Prompt.jsx | 75 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
 create mode 100755 web/src/js/components/Prompt.jsx

(limited to 'web/src/js/components/Prompt.jsx')

diff --git a/web/src/js/components/Prompt.jsx b/web/src/js/components/Prompt.jsx
new file mode 100755
index 00000000..878aae29
--- /dev/null
+++ b/web/src/js/components/Prompt.jsx
@@ -0,0 +1,75 @@
+import React, { PropTypes } from 'react'
+import ReactDOM from 'react-dom'
+import _ from 'lodash'
+
+import {Key} from '../utils.js'
+
+Prompt.contextTypes = {
+    returnFocus: PropTypes.func
+}
+
+Prompt.propTypes = {
+    options: PropTypes.array.isRequired,
+    done: PropTypes.func.isRequired,
+    prompt: PropTypes.string,
+}
+
+Prompt.componentDidMount = function() {
+    ReactDOM.findDOMNode(this).focus()
+}
+
+export default function Prompt({ prompt, done, options }, context) {
+    const opts = []
+
+    function keyTaken(k) {
+        return _.map(opts, 'key').includes(k)
+    }
+
+    for (let i = 0; i < options.length; i++) {
+        let opt = options[i]
+        if (_.isString(opt)) {
+            let str = opt
+            while (str.length > 0 && keyTaken(str[0])) {
+                str = str.substr(1)
+            }
+            opt = { text: opt, key: str[0] }
+        }
+        if (!opt.text || !opt.key || keyTaken(opt.key)) {
+            throw 'invalid options'
+        }
+        opts.push(opt)
+    }
+
+    return (
+        <div tabIndex="0" onKeyDown={onKeyDown} onClick={onClick} className="prompt-dialog">
+            <div className="prompt-content">
+            {prompt || <strong>Select: </strong> }
+            {opts.map(opt => {
+                const idx = opt.text.indexOf(opt.key)
+                function onClick(event) {
+                    done(opt.key)
+                    event.stopPropagation()
+                }
+                return (
+                    <span key={opt.key} className="option" onClick={onClick}>
+                        {idx !== -1 ? opt.text.substring(0, idx) : opt.text + '('}
+                        {prefix}<strong className="text-primary">{opt.key}</strong>
+                        {idx !== -1 ? opt.text.substring(idx + 1) : ')'}
+                    </span>
+                )
+            })}
+            </div>
+        </div>
+    )
+
+    function onKeyDown(event) {
+        event.stopPropagation()
+        event.preventDefault()
+        const key = opts.find(opt => Key[opt.key.toUpperCase()] === event.keyCode)
+        if (!key && event.keyCode !== Key.ESC && event.keyCode !== Key.ENTER) {
+            return
+        }
+        done(k || false)
+        context.returnFocus()
+    }
+}
-- 
cgit v1.2.3


From 034287edcf00eb734cb67e62de58c3bfebf6bb44 Mon Sep 17 00:00:00 2001
From: Jason <jason.daurus@gmail.com>
Date: Fri, 17 Jun 2016 07:57:06 +0800
Subject: [web] clean up

---
 web/src/js/components/Prompt.jsx | 4 ----
 1 file changed, 4 deletions(-)

(limited to 'web/src/js/components/Prompt.jsx')

diff --git a/web/src/js/components/Prompt.jsx b/web/src/js/components/Prompt.jsx
index 878aae29..6b19b3b3 100755
--- a/web/src/js/components/Prompt.jsx
+++ b/web/src/js/components/Prompt.jsx
@@ -14,10 +14,6 @@ Prompt.propTypes = {
     prompt: PropTypes.string,
 }
 
-Prompt.componentDidMount = function() {
-    ReactDOM.findDOMNode(this).focus()
-}
-
 export default function Prompt({ prompt, done, options }, context) {
     const opts = []
 
-- 
cgit v1.2.3

