12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337 |
- goog.provide('goog.ui.ac.InputHandler');
- goog.require('goog.Disposable');
- goog.require('goog.Timer');
- goog.require('goog.a11y.aria');
- goog.require('goog.a11y.aria.Role');
- goog.require('goog.a11y.aria.State');
- goog.require('goog.dom');
- goog.require('goog.dom.selection');
- goog.require('goog.events.EventHandler');
- goog.require('goog.events.EventType');
- goog.require('goog.events.KeyCodes');
- goog.require('goog.events.KeyHandler');
- goog.require('goog.string');
- goog.require('goog.userAgent');
- goog.require('goog.userAgent.product');
- goog.ui.ac.InputHandler = function(
- opt_separators, opt_literals, opt_multi, opt_throttleTime) {
- goog.Disposable.call(this);
- var throttleTime = opt_throttleTime || 150;
-
- this.multi_ = opt_multi != null ? opt_multi : true;
-
- this.setSeparators(
- opt_separators || goog.ui.ac.InputHandler.STANDARD_LIST_SEPARATORS);
-
- this.literals_ = opt_literals || '';
-
- this.preventSelectionOnTab_ = false;
-
- this.preventDefaultOnTab_ = this.multi_;
-
- this.timer_ = throttleTime > 0 ? new goog.Timer(throttleTime) : null;
-
- this.eh_ = new goog.events.EventHandler(this);
-
- this.activateHandler_ = new goog.events.EventHandler(this);
-
- this.keyHandler_ = new goog.events.KeyHandler();
-
- this.lastKeyCode_ = -1;
- };
- goog.inherits(goog.ui.ac.InputHandler, goog.Disposable);
- goog.tagUnsealableClass(goog.ui.ac.InputHandler);
- goog.ui.ac.InputHandler.REQUIRES_ASYNC_BLUR_ =
- (goog.userAgent.product.IPHONE || goog.userAgent.product.IPAD) &&
-
- !goog.userAgent.isVersionOrHigher('533.17.9');
- goog.ui.ac.InputHandler.STANDARD_LIST_SEPARATORS = ',;';
- goog.ui.ac.InputHandler.QUOTE_LITERALS = '"';
- goog.ui.ac.InputHandler.prototype.ac_;
- goog.ui.ac.InputHandler.prototype.separators_;
- goog.ui.ac.InputHandler.prototype.defaultSeparator_;
- goog.ui.ac.InputHandler.prototype.trimmer_;
- goog.ui.ac.InputHandler.prototype.separatorCheck_;
- goog.ui.ac.InputHandler.prototype.whitespaceWrapEntries_ = true;
- goog.ui.ac.InputHandler.prototype.generateNewTokenOnLiteral_ = true;
- goog.ui.ac.InputHandler.prototype.upsideDown_ = false;
- goog.ui.ac.InputHandler.prototype.separatorUpdates_ = true;
- goog.ui.ac.InputHandler.prototype.separatorSelects_ = true;
- goog.ui.ac.InputHandler.prototype.activeTimeoutId_ = null;
- goog.ui.ac.InputHandler.prototype.activeElement_ = null;
- goog.ui.ac.InputHandler.prototype.lastValue_ = '';
- goog.ui.ac.InputHandler.prototype.waitingForIme_ = false;
- goog.ui.ac.InputHandler.prototype.rowJustSelected_ = false;
- goog.ui.ac.InputHandler.prototype.updateDuringTyping_ = true;
- goog.ui.ac.InputHandler.prototype.attachAutoComplete = function(ac) {
- this.ac_ = ac;
- };
- goog.ui.ac.InputHandler.prototype.getAutoComplete = function() {
- return this.ac_;
- };
- goog.ui.ac.InputHandler.prototype.getActiveElement = function() {
- return this.activeElement_;
- };
- goog.ui.ac.InputHandler.prototype.getValue = function() {
- return this.activeElement_.value;
- };
- goog.ui.ac.InputHandler.prototype.setValue = function(value) {
- this.activeElement_.value = value;
- };
- goog.ui.ac.InputHandler.prototype.getCursorPosition = function() {
- return goog.dom.selection.getStart(this.activeElement_);
- };
- goog.ui.ac.InputHandler.prototype.setCursorPosition = function(pos) {
- goog.dom.selection.setStart(this.activeElement_, pos);
- goog.dom.selection.setEnd(this.activeElement_, pos);
- };
- goog.ui.ac.InputHandler.prototype.attachInput = function(target) {
- if (goog.dom.isElement(target)) {
- var el = (target);
- goog.a11y.aria.setRole(el, goog.a11y.aria.Role.COMBOBOX);
- goog.a11y.aria.setState(el, goog.a11y.aria.State.AUTOCOMPLETE, 'list');
- }
- this.eh_.listen(target, goog.events.EventType.FOCUS, this.handleFocus);
- this.eh_.listen(target, goog.events.EventType.BLUR, this.handleBlur);
- if (!this.activeElement_) {
- this.activateHandler_.listen(
- target, goog.events.EventType.KEYDOWN,
- this.onKeyDownOnInactiveElement_);
-
- if (goog.dom.isElement(target)) {
- var ownerDocument = goog.dom.getOwnerDocument(
- (target));
- if (goog.dom.getActiveElement(ownerDocument) == target) {
- this.processFocus( (target));
- }
- }
- }
- };
- goog.ui.ac.InputHandler.prototype.detachInput = function(target) {
- if (goog.dom.isElement(target)) {
- var el = (target);
- goog.a11y.aria.removeRole(el);
- goog.a11y.aria.removeState(el, goog.a11y.aria.State.AUTOCOMPLETE);
- }
- if (target == this.activeElement_) {
- this.handleBlur();
- }
- this.eh_.unlisten(target, goog.events.EventType.FOCUS, this.handleFocus);
- this.eh_.unlisten(target, goog.events.EventType.BLUR, this.handleBlur);
- if (!this.activeElement_) {
- this.activateHandler_.unlisten(
- target, goog.events.EventType.KEYDOWN,
- this.onKeyDownOnInactiveElement_);
- }
- };
- goog.ui.ac.InputHandler.prototype.attachInputs = function(var_args) {
- for (var i = 0; i < arguments.length; i++) {
- this.attachInput(arguments[i]);
- }
- };
- goog.ui.ac.InputHandler.prototype.detachInputs = function(var_args) {
- for (var i = 0; i < arguments.length; i++) {
- this.detachInput(arguments[i]);
- }
- };
- goog.ui.ac.InputHandler.prototype.selectRow = function(row, opt_multi) {
- if (this.activeElement_) {
- this.setTokenText(row.toString(), opt_multi);
- }
- return false;
- };
- goog.ui.ac.InputHandler.prototype.setTokenText = function(
- tokenText, opt_multi) {
- if (goog.isDef(opt_multi) ? opt_multi : this.multi_) {
- var index = this.getTokenIndex_(this.getValue(), this.getCursorPosition());
-
- var entries = this.splitInput_(this.getValue());
-
- var replaceValue = tokenText;
-
- if (!this.separatorCheck_.test(replaceValue)) {
- replaceValue =
- goog.string.trimRight(replaceValue) + this.defaultSeparator_;
- }
-
-
- if (this.whitespaceWrapEntries_) {
- if (index != 0 && !goog.string.isEmptyOrWhitespace(entries[index - 1])) {
- replaceValue = ' ' + replaceValue;
- }
-
-
- if (index == entries.length - 1) {
- replaceValue = replaceValue + ' ';
- }
- }
-
-
- if (replaceValue != entries[index]) {
-
- entries[index] = replaceValue;
- var el = this.activeElement_;
-
-
-
-
-
-
-
-
-
- if (goog.userAgent.GECKO ||
- (goog.userAgent.IE && goog.userAgent.isVersionOrHigher('9'))) {
- el.blur();
- }
-
- el.value = entries.join('');
-
- var pos = 0;
- for (var i = 0; i <= index; i++) {
- pos += entries[i].length;
- }
-
- el.focus();
- this.setCursorPosition(pos);
- }
- } else {
- this.setValue(tokenText);
- }
-
- this.rowJustSelected_ = true;
- };
- goog.ui.ac.InputHandler.prototype.disposeInternal = function() {
- goog.ui.ac.InputHandler.superClass_.disposeInternal.call(this);
- if (this.activeTimeoutId_ != null) {
-
- window.clearTimeout(this.activeTimeoutId_);
- }
- this.eh_.dispose();
- delete this.eh_;
- this.activateHandler_.dispose();
- this.keyHandler_.dispose();
- goog.dispose(this.timer_);
- };
- goog.ui.ac.InputHandler.prototype.setSeparators = function(
- separators, opt_defaultSeparators) {
- this.separators_ = separators;
- this.defaultSeparator_ = goog.isDefAndNotNull(opt_defaultSeparators) ?
- opt_defaultSeparators :
- this.separators_.substring(0, 1);
- var wspaceExp = this.multi_ ? '[\\s' + this.separators_ + ']+' : '[\\s]+';
- this.trimmer_ = new RegExp('^' + wspaceExp + '|' + wspaceExp + '$', 'g');
- this.separatorCheck_ = new RegExp('\\s*[' + this.separators_ + ']$');
- };
- goog.ui.ac.InputHandler.prototype.setUpsideDown = function(upsideDown) {
- this.upsideDown_ = upsideDown;
- };
- goog.ui.ac.InputHandler.prototype.setWhitespaceWrapEntries = function(
- newValue) {
- this.whitespaceWrapEntries_ = newValue;
- };
- goog.ui.ac.InputHandler.prototype.setGenerateNewTokenOnLiteral = function(
- newValue) {
- this.generateNewTokenOnLiteral_ = newValue;
- };
- goog.ui.ac.InputHandler.prototype.setTrimmingRegExp = function(trimmer) {
- this.trimmer_ = trimmer;
- };
- goog.ui.ac.InputHandler.prototype.setPreventDefaultOnTab = function(newValue) {
- this.preventDefaultOnTab_ = newValue;
- };
- goog.ui.ac.InputHandler.prototype.setPreventSelectionOnTab = function(
- newValue) {
- this.preventSelectionOnTab_ = newValue;
- };
- goog.ui.ac.InputHandler.prototype.setSeparatorCompletes = function(newValue) {
- this.separatorUpdates_ = newValue;
- this.separatorSelects_ = newValue;
- };
- goog.ui.ac.InputHandler.prototype.setSeparatorSelects = function(newValue) {
- this.separatorSelects_ = newValue;
- };
- goog.ui.ac.InputHandler.prototype.getThrottleTime = function() {
- return this.timer_ ? this.timer_.getInterval() : -1;
- };
- goog.ui.ac.InputHandler.prototype.setRowJustSelected = function(justSelected) {
- this.rowJustSelected_ = justSelected;
- };
- goog.ui.ac.InputHandler.prototype.setThrottleTime = function(time) {
- if (time < 0) {
- this.timer_.dispose();
- this.timer_ = null;
- return;
- }
- if (this.timer_) {
- this.timer_.setInterval(time);
- } else {
- this.timer_ = new goog.Timer(time);
- }
- };
- goog.ui.ac.InputHandler.prototype.getUpdateDuringTyping = function() {
- return this.updateDuringTyping_;
- };
- goog.ui.ac.InputHandler.prototype.setUpdateDuringTyping = function(value) {
- this.updateDuringTyping_ = value;
- };
- goog.ui.ac.InputHandler.prototype.handleKeyEvent = function(e) {
- switch (e.keyCode) {
-
-
-
- case goog.events.KeyCodes.DOWN:
- if (this.ac_.isOpen()) {
- this.moveDown_();
- e.preventDefault();
- return true;
- } else if (!this.multi_) {
- this.update(true);
- e.preventDefault();
- return true;
- }
- break;
-
-
- case goog.events.KeyCodes.UP:
- if (this.ac_.isOpen()) {
- this.moveUp_();
- e.preventDefault();
- return true;
- }
- break;
-
-
-
- case goog.events.KeyCodes.TAB:
- if (this.ac_.isOpen() && !e.shiftKey && !this.preventSelectionOnTab_) {
-
- this.update();
- if (this.ac_.selectHilited() && this.preventDefaultOnTab_) {
- e.preventDefault();
- return true;
- }
- } else {
- this.ac_.dismiss();
- }
- break;
-
- case goog.events.KeyCodes.ENTER:
- if (this.ac_.isOpen()) {
-
- this.update();
- if (this.ac_.selectHilited()) {
- e.preventDefault();
- e.stopPropagation();
- return true;
- }
- } else {
- this.ac_.dismiss();
- }
- break;
-
- case goog.events.KeyCodes.ESC:
- if (this.ac_.isOpen()) {
- this.ac_.dismiss();
- e.preventDefault();
- e.stopPropagation();
- return true;
- }
- break;
-
-
- case goog.events.KeyCodes.WIN_IME:
- if (!this.waitingForIme_) {
- this.startWaitingForIme_();
- return true;
- }
- break;
- default:
- if (this.timer_ && !this.updateDuringTyping_) {
-
- this.timer_.stop();
- this.timer_.start();
- }
- }
- return this.handleSeparator_(e);
- };
- goog.ui.ac.InputHandler.prototype.handleSeparator_ = function(e) {
- var isSeparatorKey = this.multi_ && e.charCode &&
- this.separators_.indexOf(String.fromCharCode(e.charCode)) != -1;
- if (this.separatorUpdates_ && isSeparatorKey) {
- this.update();
- }
- if (this.separatorSelects_ && isSeparatorKey) {
- if (this.ac_.selectHilited()) {
- e.preventDefault();
- return true;
- }
- }
- return false;
- };
- goog.ui.ac.InputHandler.prototype.needKeyUpListener = function() {
- return false;
- };
- goog.ui.ac.InputHandler.prototype.handleKeyUp = function(e) {
- return false;
- };
- goog.ui.ac.InputHandler.prototype.addEventHandlers_ = function() {
- this.keyHandler_.attach(this.activeElement_);
- this.eh_.listen(
- this.keyHandler_, goog.events.KeyHandler.EventType.KEY, this.onKey_);
- if (this.needKeyUpListener()) {
- this.eh_.listen(
- this.activeElement_, goog.events.EventType.KEYUP, this.handleKeyUp);
- }
- this.eh_.listen(
- this.activeElement_, goog.events.EventType.MOUSEDOWN, this.onMouseDown_);
-
- if (goog.userAgent.IE) {
- this.eh_.listen(
- this.activeElement_, goog.events.EventType.KEYPRESS,
- this.onIeKeyPress_);
- }
- };
- goog.ui.ac.InputHandler.prototype.removeEventHandlers_ = function() {
- this.eh_.unlisten(
- this.keyHandler_, goog.events.KeyHandler.EventType.KEY, this.onKey_);
- this.keyHandler_.detach();
- this.eh_.unlisten(
- this.activeElement_, goog.events.EventType.KEYUP, this.handleKeyUp);
- this.eh_.unlisten(
- this.activeElement_, goog.events.EventType.MOUSEDOWN, this.onMouseDown_);
- if (goog.userAgent.IE) {
- this.eh_.unlisten(
- this.activeElement_, goog.events.EventType.KEYPRESS,
- this.onIeKeyPress_);
- }
- if (this.waitingForIme_) {
- this.stopWaitingForIme_();
- }
- };
- goog.ui.ac.InputHandler.prototype.handleFocus = function(e) {
- this.processFocus( (e.target || null));
- };
- goog.ui.ac.InputHandler.prototype.processFocus = function(target) {
- this.activateHandler_.removeAll();
- if (this.ac_) {
- this.ac_.cancelDelayedDismiss();
- }
-
-
- if (target != this.activeElement_) {
- this.activeElement_ = target;
- if (this.timer_) {
- this.timer_.start();
- this.eh_.listen(this.timer_, goog.Timer.TICK, this.onTick_);
- }
- this.lastValue_ = this.getValue();
- this.addEventHandlers_();
- }
- };
- goog.ui.ac.InputHandler.prototype.handleBlur = function(opt_e) {
-
- if (goog.ui.ac.InputHandler.REQUIRES_ASYNC_BLUR_) {
-
-
-
-
-
- this.activeTimeoutId_ =
- window.setTimeout(goog.bind(this.processBlur, this), 0);
- return;
- } else {
- this.processBlur();
- }
- };
- goog.ui.ac.InputHandler.prototype.processBlur = function() {
-
-
-
- if (this.activeElement_) {
- this.removeEventHandlers_();
- this.activeElement_ = null;
- if (this.timer_) {
- this.timer_.stop();
- this.eh_.unlisten(this.timer_, goog.Timer.TICK, this.onTick_);
- }
- if (this.ac_) {
-
-
- this.ac_.dismissOnDelay();
- }
- }
- };
- goog.ui.ac.InputHandler.prototype.onTick_ = function(e) {
- this.update();
- };
- goog.ui.ac.InputHandler.prototype.onKeyDownOnInactiveElement_ = function(e) {
- this.handleFocus(e);
- };
- goog.ui.ac.InputHandler.prototype.onKey_ = function(e) {
- this.lastKeyCode_ = e.keyCode;
- if (this.ac_) {
- this.handleKeyEvent(e);
- }
- };
- goog.ui.ac.InputHandler.prototype.onKeyPress_ = function(e) {
- if (this.waitingForIme_ &&
- this.lastKeyCode_ != goog.events.KeyCodes.WIN_IME) {
- this.stopWaitingForIme_();
- }
- };
- goog.ui.ac.InputHandler.prototype.onKeyUp_ = function(e) {
- if (this.waitingForIme_ &&
- (e.keyCode == goog.events.KeyCodes.ENTER ||
- (e.keyCode == goog.events.KeyCodes.M && e.ctrlKey))) {
- this.stopWaitingForIme_();
- }
- };
- goog.ui.ac.InputHandler.prototype.onMouseDown_ = function(e) {
- if (this.ac_) {
- this.handleMouseDown(e);
- }
- };
- goog.ui.ac.InputHandler.prototype.handleMouseDown = function(e) {};
- goog.ui.ac.InputHandler.prototype.startWaitingForIme_ = function() {
- if (this.waitingForIme_) {
- return;
- }
- this.eh_.listen(
- this.activeElement_, goog.events.EventType.KEYUP, this.onKeyUp_);
- this.eh_.listen(
- this.activeElement_, goog.events.EventType.KEYPRESS, this.onKeyPress_);
- this.waitingForIme_ = true;
- };
- goog.ui.ac.InputHandler.prototype.stopWaitingForIme_ = function() {
- if (!this.waitingForIme_) {
- return;
- }
- this.waitingForIme_ = false;
- this.eh_.unlisten(
- this.activeElement_, goog.events.EventType.KEYPRESS, this.onKeyPress_);
- this.eh_.unlisten(
- this.activeElement_, goog.events.EventType.KEYUP, this.onKeyUp_);
- };
- goog.ui.ac.InputHandler.prototype.onIeKeyPress_ = function(e) {
- this.handleSeparator_(e);
- };
- goog.ui.ac.InputHandler.prototype.update = function(opt_force) {
- if (this.activeElement_ &&
- (opt_force || this.getValue() != this.lastValue_)) {
- if (opt_force || !this.rowJustSelected_) {
- var token = this.parseToken();
- if (this.ac_) {
- this.ac_.setTarget(this.activeElement_);
- this.ac_.setToken(token, this.getValue());
- }
- }
- this.lastValue_ = this.getValue();
- }
- this.rowJustSelected_ = false;
- };
- goog.ui.ac.InputHandler.prototype.parseToken = function() {
- return this.parseToken_();
- };
- goog.ui.ac.InputHandler.prototype.moveUp_ = function() {
- return this.upsideDown_ ? this.ac_.hiliteNext() : this.ac_.hilitePrev();
- };
- goog.ui.ac.InputHandler.prototype.moveDown_ = function() {
- return this.upsideDown_ ? this.ac_.hilitePrev() : this.ac_.hiliteNext();
- };
- goog.ui.ac.InputHandler.prototype.parseToken_ = function() {
- var caret = this.getCursorPosition();
- var text = this.getValue();
- return this.trim_(this.splitInput_(text)[this.getTokenIndex_(text, caret)]);
- };
- goog.ui.ac.InputHandler.prototype.trim_ = function(text) {
- return this.trimmer_ ? String(text).replace(this.trimmer_, '') : text;
- };
- goog.ui.ac.InputHandler.prototype.getTokenIndex_ = function(text, caret) {
-
- var entries = this.splitInput_(text);
-
- if (caret == text.length) return entries.length - 1;
-
- var current = 0;
- for (var i = 0, pos = 0; i < entries.length && pos <= caret; i++) {
- pos += entries[i].length;
- current = i;
- }
-
- return current;
- };
- goog.ui.ac.InputHandler.prototype.splitInput_ = function(text) {
- if (!this.multi_) {
- return [text];
- }
- var arr = String(text).split('');
- var parts = [];
- var cache = [];
- for (var i = 0, inLiteral = false; i < arr.length; i++) {
- if (this.literals_ && this.literals_.indexOf(arr[i]) != -1) {
- if (this.generateNewTokenOnLiteral_ && !inLiteral) {
- parts.push(cache.join(''));
- cache.length = 0;
- }
- cache.push(arr[i]);
- inLiteral = !inLiteral;
- } else if (!inLiteral && this.separators_.indexOf(arr[i]) != -1) {
- cache.push(arr[i]);
- parts.push(cache.join(''));
- cache.length = 0;
- } else {
- cache.push(arr[i]);
- }
- }
- parts.push(cache.join(''));
- return parts;
- };
|