forked from phcode-dev/phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSearchField.js
More file actions
1 lines (1 loc) · 6.01 KB
/
QuickSearchField.js
File metadata and controls
1 lines (1 loc) · 6.01 KB
1
define(function(require,exports,module){const KeyEvent=require("utils/KeyEvent"),PopUpManager=require("widgets/PopUpManager");function QuickSearchField($input,options){this.$input=$input,this.options=options||{},this.$positionEl=options.$positionEl,options.maxResults=options.maxResults||10,this._handleInput=this._handleInput.bind(this),this._handleKeyDown=this._handleKeyDown.bind(this),void 0!==options.highlightZeroResults?this._highlightZeroResults=options.highlightZeroResults:this._highlightZeroResults=!0,$input.on("input",this._handleInput),$input.on("keydown",this._handleKeyDown),this._firstHighlightIndex=options.firstHighlightIndex,this._dropdownTop=$input.offset().top+$input.height()+(options.verticalAdjust||0)}QuickSearchField.prototype.options=null,QuickSearchField.prototype._pending=null,QuickSearchField.prototype._commitPending=!1,QuickSearchField.prototype._displayedQuery=null,QuickSearchField.prototype._displayedResults=null,QuickSearchField.prototype._highlightIndex=null,QuickSearchField.prototype._$dropdown=null,QuickSearchField.prototype.$input=null,QuickSearchField.prototype.$positionEl=null,QuickSearchField.prototype._handleInput=function(){this._pending=null;var valueAtEvent=this.$input.val(),self=this;setTimeout(function(){self.$input&&self.$input.val()===valueAtEvent&&self.updateResults()},0)},QuickSearchField.prototype._handleKeyDown=function(event){let popupVisible=!1;this._$dropdown&&this._$dropdown.is(":visible")&&(popupVisible=!0),event.keyCode===KeyEvent.DOM_VK_RETURN?this._displayedQuery===this.$input.val()?(event.stopPropagation(),event.preventDefault(),this._doCommit()):this._commitPending=!0:event.keyCode===KeyEvent.DOM_VK_DELETE&&popupVisible?this.options.onDelete&&this._$dropdown&&null!==this._highlightIndex&&(this.options.onDelete(this._highlightIndex),this.updateResults(),event.stopPropagation(),event.preventDefault()):event.keyCode===KeyEvent.DOM_VK_DOWN&&popupVisible?(this._displayedResults&&this._displayedResults.length&&(null===this._highlightIndex||this._highlightIndex===this._displayedResults.length-1?this._highlightIndex=0:this._highlightIndex++,this._updateHighlight(!0)),event.stopPropagation(),event.preventDefault()):event.keyCode===KeyEvent.DOM_VK_UP&&popupVisible&&(this._displayedResults&&this._displayedResults.length&&(null===this._highlightIndex||0===this._highlightIndex?this._highlightIndex=this._displayedResults.length-1:this._highlightIndex--,this._updateHighlight(!0)),event.preventDefault(),event.stopPropagation())},QuickSearchField.prototype._doCommit=function(index){var item;this._displayedResults&&this._displayedResults.length&&(index>=0?item=this._displayedResults[index]:this._highlightIndex>=0&&(item=this._displayedResults[this._highlightIndex])),this.options.onCommit(item,this._displayedQuery,this._highlightIndex)},QuickSearchField.prototype._updateHighlight=function(explicit){if(this._$dropdown){var $items=this._$dropdown.find("li");$items.removeClass("highlight"),null!==this._highlightIndex&&($items.eq(this._highlightIndex).addClass("highlight"),this.options.onHighlight(this._displayedResults[this._highlightIndex],this.$input.val(),explicit))}},QuickSearchField.prototype.updateResults=function(){this._pending=null;var query=this.$input.val(),results=this.options.resultProvider(query);if(results.done&&results.fail){this._pending=results;var self=this;this._pending.done(function(realResults){self._pending===results&&(self._render(realResults,query),this._pending=null)}),this._pending&&this._pending.fail(function(){self._pending===results&&(self._render([],query),this._pending=null)})}else this._render(results,query)},QuickSearchField.prototype._closeDropdown=function(){this._$dropdown&&(this._$dropdown.remove(),this._$dropdown=null),this.options.focusLastActiveElementOnClose&&this._$currentlyFocusedElement&&this._$currentlyFocusedElement.is(":visible")&&this._$currentlyFocusedElement.focus()},QuickSearchField.prototype._openDropdown=function(htmlContent){const self=this;if(this._$currentlyFocusedElement=$(document.activeElement),!this._$dropdown){let $positioningElement=this.$positionEl?this.$positionEl:this.$input;this._$dropdown=$("<ol class='quick-search-container'/>").appendTo("body").css({position:"absolute",top:this._dropdownTop,left:$positioningElement.offset().left,width:$positioningElement.outerWidth()}).click(function(event){var $item=$(event.target).closest("li");$item.length&&self._doCommit($item.index())})}this._$dropdown.html(htmlContent),PopUpManager.addPopUp(this._$dropdown,()=>{self.destroy(),self.options.onDismiss&&self.options.onDismiss()},!0,{popupManagesFocus:this.options.focusLastActiveElementOnClose})},QuickSearchField.prototype._render=function(results,query){if(this._displayedQuery=query,this._displayedResults=results,this._highlightIndex?this._highlightIndex>=results.length&&(this._highlightIndex=results.length-1):this._firstHighlightIndex>=0?this._highlightIndex=this._firstHighlightIndex:this._highlightIndex=null,results.error||0===results.length)this._closeDropdown(),this._highlightZeroResults&&this.$input.addClass("no-results");else if(results.hasOwnProperty("error"))this._closeDropdown(),this._highlightZeroResults&&this.$input.removeClass("no-results");else{this._highlightZeroResults&&this.$input.removeClass("no-results");const createdJqObj=$();let count=Math.min(results.length,this.options.maxResults),i;for(i=0;i<count;i++){const result=this.options.formatter(results[i],query);"string"==typeof result?createdJqObj.push($(result).get(0)):result instanceof $?createdJqObj.push(result.get(0)):console.error("QuickSearchFiled formatter should return a string html/jquery object; but got",result)}this._openDropdown(createdJqObj),this._updateHighlight(!1)}this._commitPending&&(this._commitPending=!1,this._doCommit())},QuickSearchField.prototype.setText=function(value){this.$input.val(value),this.updateResults()},QuickSearchField.prototype.destroy=function(){this._pending=null,this._closeDropdown(),this.$input&&(this.$input.off("input",this._handleInput),this.$input.off("keydown",this._handleKeyDown),this.$input=null)},exports.QuickSearchField=QuickSearchField});