diff options
author | Armen Kesablyan | 2012-02-22 16:26:41 -0800 |
---|---|---|
committer | Armen Kesablyan | 2012-02-22 16:26:41 -0800 |
commit | 0bd1cefea2ab350fad1a891bdc926053b799aafc (patch) | |
tree | 962f559fcc02a3dfeb297d59907e40fa153453f3 /imports/codemirror/lib/util/simple-hint.js | |
parent | 695bc5082f48dddf66ce31480a4faefc067b38bd (diff) | |
parent | 2d2b1af8b5c0d506fe6a1cf65614101fec145970 (diff) | |
download | ninja-0bd1cefea2ab350fad1a891bdc926053b799aafc.tar.gz |
Merge branch 'refs/heads/master' into new-tool-icons
Diffstat (limited to 'imports/codemirror/lib/util/simple-hint.js')
-rwxr-xr-x | imports/codemirror/lib/util/simple-hint.js | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/imports/codemirror/lib/util/simple-hint.js b/imports/codemirror/lib/util/simple-hint.js new file mode 100755 index 00000000..b38f3892 --- /dev/null +++ b/imports/codemirror/lib/util/simple-hint.js | |||
@@ -0,0 +1,66 @@ | |||
1 | (function() { | ||
2 | CodeMirror.simpleHint = function(editor, getHints) { | ||
3 | // We want a single cursor position. | ||
4 | if (editor.somethingSelected()) return; | ||
5 | var result = getHints(editor); | ||
6 | if (!result || !result.list.length) return; | ||
7 | var completions = result.list; | ||
8 | function insert(str) { | ||
9 | editor.replaceRange(str, result.from, result.to); | ||
10 | } | ||
11 | // When there is only one completion, use it directly. | ||
12 | if (completions.length == 1) {insert(completions[0]); return true;} | ||
13 | |||
14 | // Build the select widget | ||
15 | var complete = document.createElement("div"); | ||
16 | complete.className = "CodeMirror-completions"; | ||
17 | var sel = complete.appendChild(document.createElement("select")); | ||
18 | // Opera doesn't move the selection when pressing up/down in a | ||
19 | // multi-select, but it does properly support the size property on | ||
20 | // single-selects, so no multi-select is necessary. | ||
21 | if (!window.opera) sel.multiple = true; | ||
22 | for (var i = 0; i < completions.length; ++i) { | ||
23 | var opt = sel.appendChild(document.createElement("option")); | ||
24 | opt.appendChild(document.createTextNode(completions[i])); | ||
25 | } | ||
26 | sel.firstChild.selected = true; | ||
27 | sel.size = Math.min(10, completions.length); | ||
28 | var pos = editor.cursorCoords(); | ||
29 | complete.style.left = pos.x + "px"; | ||
30 | complete.style.top = pos.yBot + "px"; | ||
31 | document.body.appendChild(complete); | ||
32 | // Hack to hide the scrollbar. | ||
33 | if (completions.length <= 10) | ||
34 | complete.style.width = (sel.clientWidth - 1) + "px"; | ||
35 | |||
36 | var done = false; | ||
37 | function close() { | ||
38 | if (done) return; | ||
39 | done = true; | ||
40 | complete.parentNode.removeChild(complete); | ||
41 | } | ||
42 | function pick() { | ||
43 | insert(completions[sel.selectedIndex]); | ||
44 | close(); | ||
45 | setTimeout(function(){editor.focus();}, 50); | ||
46 | } | ||
47 | CodeMirror.connect(sel, "blur", close); | ||
48 | CodeMirror.connect(sel, "keydown", function(event) { | ||
49 | var code = event.keyCode; | ||
50 | // Enter | ||
51 | if (code == 13) {CodeMirror.e_stop(event); pick();} | ||
52 | // Escape | ||
53 | else if (code == 27) {CodeMirror.e_stop(event); close(); editor.focus();} | ||
54 | else if (code != 38 && code != 40) { | ||
55 | close(); editor.focus(); | ||
56 | setTimeout(function(){CodeMirror.simpleHint(editor, getHints);}, 50); | ||
57 | } | ||
58 | }); | ||
59 | CodeMirror.connect(sel, "dblclick", pick); | ||
60 | |||
61 | sel.focus(); | ||
62 | // Opera sometimes ignores focusing a freshly created node | ||
63 | if (window.opera) setTimeout(function(){if (!done) sel.focus();}, 100); | ||
64 | return true; | ||
65 | }; | ||
66 | })(); | ||