Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 23e7432

Browse filesBrowse files
author
Arne Claassen
committed
Merge pull request sdether#17 from sdether/issue_8
Issue 8 and input field related changes
2 parents 26af7ce + 6dad841 commit 23e7432
Copy full SHA for 23e7432

File tree

Expand file treeCollapse file tree

5 files changed

+456
-210
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+456
-210
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ By implementing the functions `getNode` and `getChildNodes`, this library adds p
134134

135135
## Changelog
136136

137+
**0.2.9** -- 2013/08/31
138+
* Added ability to bind ReadLine/Shell to an element.
139+
* Added ability to bind/unbind keys (could be used to replace emacs bindings of readline, but primarily added to unbind some keys for using readline on input elements.
140+
* Created input.js for easy binding of readline to either an input element or a span behaving like an input.
141+
137142
**0.2.8** -- 2013/03/13
138143
* Added handling of . and .. in Josh.PathHandler.pathcompletionhandler, so that a trailing .. completes to ../ and . to ./
139144
* Removed the hardcoded **strong** in the input template, making it a span instead so it can be styled via css instead.

‎index.html

Copy file name to clipboard
+62-36Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,75 @@
11
<!doctype html>
22
<html>
33
<head>
4-
<meta charset="utf-8">
5-
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
6-
<title>Shell testbed</title>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
6+
<title>Shell testbed</title>
77

8-
<link href='http://fonts.googleapis.com/css?family=Source+Code+Pro' rel='stylesheet' type='text/css'>
9-
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
10-
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
11-
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
12-
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js"></script>
13-
<script>Josh = {Debug: true };</script>
14-
<script src="js/history.js"></script>
15-
<script src="js/killring.js"></script>
16-
<script src="js/readline.js"></script>
17-
<script src="js/shell.js"></script>
18-
<script src="js/pathhandler.js"></script>
19-
<script src="js/example.js"></script>
20-
<style type="text/css">
21-
#shell-panel {
22-
display: none;
23-
height: 400px;
24-
width: 97.0%;
25-
opacity: 0.9;
26-
background-color: #002f05;
27-
color: #00fe00;
28-
position: fixed;
29-
padding: 20px 20px 20px 20px;
30-
top: 0;
31-
left: 0;
32-
z-index: 1000;
33-
font-family: 'Source Code Pro', sans-serif;
34-
font-size: 0.9em;
35-
overflow:scroll;
36-
overflow-x:hidden;
37-
overflow-y: scroll;
38-
}</style>
8+
<link href='http://fonts.googleapis.com/css?family=Source+Code+Pro' rel='stylesheet' type='text/css'>
9+
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
10+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
11+
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
12+
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js"></script>
13+
<script>Josh = {Debug: true };</script>
14+
<script src="js/history.js"></script>
15+
<script src="js/killring.js"></script>
16+
<script src="js/readline.js"></script>
17+
<script src="js/input.js"></script>
18+
<script src="js/shell.js"></script>
19+
<script src="js/pathhandler.js"></script>
20+
<script src="js/example.js"></script>
21+
<style type="text/css">
22+
#shell-panel {
23+
display: none;
24+
height: 400px;
25+
width: 97.0%;
26+
opacity: 0.9;
27+
background-color: #002f05;
28+
color: #00fe00;
29+
position: fixed;
30+
padding: 20px 20px 20px 20px;
31+
top: 0;
32+
left: 0;
33+
z-index: 1000;
34+
font-family: 'Source Code Pro', sans-serif;
35+
font-size: 0.9em;
36+
overflow: scroll;
37+
overflow-x: hidden;
38+
overflow-y: scroll;
39+
}
40+
41+
#cmd1 {
42+
border-style: solid;
43+
border-width: 1px;
44+
display: inline-block;
45+
width: 200px;
46+
height: 20px;
47+
}
48+
49+
</style>
3950
</head>
4051
<body>
4152
<div id="shell-panel">
42-
<div id="shell-view"></div>
53+
<div id="shell-view"></div>
4354
</div>
4455
<div>
45-
<p>Press <strong>~</strong> to activate console.</p>
56+
<p>Press <strong>~</strong> to activate console.</p>
4657
</div>
4758

59+
<p>
60+
Cmd1: <span id="cmd1"></span>
61+
</p>
62+
63+
<p>
64+
Cmd2: <input id="cmd2">
65+
</p>
66+
<script>
67+
68+
69+
var history = new Josh.History();
70+
var killring = new Josh.KillRing();
71+
var cmd1 = new Josh.Input({id: "cmd1"});
72+
var cmd2 = new Josh.Input({id: "cmd2"});
73+
</script>
4874
</body>
4975
</html>

‎js/example.js

Copy file name to clipboardExpand all lines: js/example.js
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
// -----------------------
148148

149149
// Activation and display behavior happens at document ready time.
150-
$(document).ready(function() {
150+
$(root).ready(function() {
151151

152152
// The default name for the div the shell uses as its container is `shell-panel`, although that can be changed via
153153
// the shell config parameter `shell-panel-id`. The `Shell` display model relies on a 'panel' to contain a 'view'.
@@ -167,10 +167,10 @@
167167
}
168168

169169
// Mimicking *Quake*-style dropdown consoles, we activate and show on `~`.
170-
_console.log("activating shell");
171170
if(event.keyCode == 126) {
171+
_console.log("activating shell");
172172
event.preventDefault();
173-
shell.activate();
173+
shell.activate();
174174
$consolePanel.slideDown();
175175
$consolePanel.focus();
176176
}

‎js/input.js

Copy file name to clipboard
+172Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/* ------------------------------------------------------------------------*
2+
* Copyright 2013 Arne F. Claassen
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*-------------------------------------------------------------------------*/
16+
17+
var Josh = Josh || {};
18+
(function (root, $, _) {
19+
$.fn.josh_caretTo = function (index) {
20+
return this.queue(function (next) {
21+
if (this.createTextRange) {
22+
var range = this.createTextRange();
23+
range.move("character", index);
24+
range.select();
25+
} else if (this.selectionStart != null) {
26+
this.setSelectionRange(index, index);
27+
}
28+
next();
29+
});
30+
};
31+
$.fn.josh_caretPosition = function () {
32+
var el = this.get(0);
33+
if (el.createTextRange) {
34+
var range = el.createTextRange();
35+
range.moveStart('character', -el.value.length);
36+
return range.text.length;
37+
} else if (el.selectionStart != null) {
38+
return el.selectionStart;
39+
}
40+
return 0;
41+
};
42+
43+
var history = Josh.History();
44+
var killring = new Josh.KillRing();
45+
46+
Josh.Input = function (config) {
47+
config = config || {};
48+
49+
// instance fields
50+
var _console = config.console || (Josh.Debug && root.console ? root.console : {
51+
log: function () {
52+
}
53+
});
54+
55+
var _id = "#" + config.id;
56+
var _blinktime = config.blinktime || 500;
57+
var _active = false;
58+
var _cursor_visible = false;
59+
var _isInput = false;
60+
var _history = config.history || history;
61+
var _killring = config.killring || killring;
62+
var _text;
63+
var self = {
64+
templates: {
65+
span: _.template('<span class="input"><span class="left"/><span class="cursor"/><span class="right"/></span>')
66+
},
67+
history: _history,
68+
killring: _killring
69+
};
70+
71+
$(document).ready(function () {
72+
var $input = $(_id);
73+
var el = $input.get(0);
74+
var readline = new Josh.ReadLine({
75+
history: _history,
76+
killring: _killring,
77+
console: _console
78+
});
79+
self.readline = readline;
80+
readline.attach(el);
81+
var activate = null;
82+
_isInput = $input.is('input');
83+
if (_isInput) {
84+
85+
_console.log(_id + ' is an input');
86+
87+
function renderInput(line) {
88+
var text = line ? line.text : '';
89+
_text = text;
90+
$input.val(text);
91+
$input.josh_caretTo(line.cursor);
92+
}
93+
readline.onChange(renderInput);
94+
$input.click(function() {
95+
var line = readline.getLine();
96+
line.cursor = $input.josh_caretPosition();
97+
readline.setLine(line);
98+
});
99+
100+
activate = function() {
101+
// Note: have to re-render with a setTimeout, because on focus, but after the onfocus event is processed,
102+
// the input will select all, invalidating our render
103+
setTimeout(function() {
104+
renderInput(readline.getLine());
105+
}, 0);
106+
};
107+
} else {
108+
_console.log(_id + ' is a non-input element');
109+
$input.html(self.templates.span());
110+
if(typeof $input.attr('tabindex') === 'undefined') {
111+
$input.attr('tabindex',0);
112+
}
113+
var $left = $input.find('.left');
114+
var $right = $input.find('.right');
115+
var $cursor = $input.find('.cursor');
116+
117+
function renderSpan(line) {
118+
var text = line.text || '';
119+
_text = text;
120+
var cursorIdx = line.cursor || 0;
121+
var left = _.escape(text.substr(0, cursorIdx)).replace(/ /g, '&nbsp;');
122+
var cursor = text.substr(cursorIdx, 1);
123+
var right = _.escape(text.substr(cursorIdx + 1)).replace(/ /g, '&nbsp;');
124+
$left.html(left);
125+
if (!cursor) {
126+
$cursor.html('&nbsp;').css('textDecoration', 'underline');
127+
} else {
128+
$cursor.text(cursor).css('textDecoration', 'underline');
129+
}
130+
$right.html(right);
131+
}
132+
133+
function blinkCursor() {
134+
if (!_active) {
135+
return;
136+
}
137+
root.setTimeout(function () {
138+
if (!_active) {
139+
return;
140+
}
141+
_cursor_visible = !_cursor_visible;
142+
if (_cursor_visible) {
143+
$cursor.css('textDecoration', 'underline');
144+
} else {
145+
$cursor.css('textDecoration', '');
146+
}
147+
blinkCursor();
148+
}, _blinktime);
149+
}
150+
151+
activate = function () {
152+
blinkCursor();
153+
}
154+
readline.onChange(renderSpan);
155+
}
156+
readline.unbind({keyCode: Josh.Keys.Special.Tab});
157+
readline.unbind({char: 'R', ctrlKey: true});
158+
readline.onActivate(function () {
159+
_active = true;
160+
activate();
161+
});
162+
readline.onDeactivate(function () {
163+
_active = false;
164+
if (_text) {
165+
_history.accept(_text);
166+
}
167+
});
168+
169+
});
170+
return self;
171+
}
172+
})(this, $, _);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.