diff options
author | Valerio Virgillito | 2012-02-15 16:09:47 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-02-15 16:09:47 -0800 |
commit | d366c0bd1af6471511217ed574083e15059519b5 (patch) | |
tree | c8c9f9af761457e3c5f4c6774fb0fbba851df0c4 /imports/codemirror/lib/util/simple-hint.js | |
parent | 997ce3fb65f27b3d6f331f63b5dc22d3c7fb8f1e (diff) | |
parent | b85bfb54aaca3ccca3c1ef09115de925cd67f4e9 (diff) | |
download | ninja-d366c0bd1af6471511217ed574083e15059519b5.tar.gz |
Merge branch 'refs/heads/integration'
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 | })(); | ||