|
| 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, ' '); |
| 122 | + var cursor = text.substr(cursorIdx, 1); |
| 123 | + var right = _.escape(text.substr(cursorIdx + 1)).replace(/ /g, ' '); |
| 124 | + $left.html(left); |
| 125 | + if (!cursor) { |
| 126 | + $cursor.html(' ').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