text: define("bitbucket/internal/widget/searchable-selector/searchable-selector","module exports @atlassian/aui jquery lodash bitbucket/internal/util/ajax bitbucket/internal/util/dom-event bitbucket/internal/util/events bitbucket/internal/util/promise bitbucket/internal/widget/keyboard-controller bitbucket/internal/widget/paged-scrollable".split(" "),function(m,l,k,n,p,q,r,t,u,v,w){function b(a,c){return this.init.apply(this,arguments)}Object.defineProperty(l,"__esModule",{value:!0});var g=babelHelpers.interopRequireDefault(k),
  d=babelHelpers.interopRequireDefault(n),f=babelHelpers.interopRequireDefault(p),x=babelHelpers.interopRequireDefault(q),y=babelHelpers.interopRequireDefault(r),z=babelHelpers.interopRequireDefault(t),A=babelHelpers.interopRequireDefault(u);k=babelHelpers.interopRequireDefault(v);var B=babelHelpers.interopRequireDefault(w),C=k.default.TabKeyboardController,D=k.default.ListKeyboardController,E=0;b.prototype.defaults={id:null,extraClasses:null,namespace:"searchable-selector",url:null,tabs:null,searchable:!0,
  searchPlaceholder:null,queryParamKey:"filter",queryParamsBuilder:null,pageSize:20,statusCodeHandlers:{},followLinks:!1,hideDialogOnSelect:!0,itemSelectedEvent:"bitbucket.internal.widget.searchableSelector.itemSelected",context:null,field:null,itemDataKey:"item",template:bitbucket.internal.widget.searchableSelector,resultsTemplate:bitbucket.internal.widget.searchableSelectorResults,noResultsText:null,noMoreResultsText:null,triggerContentTemplate:bitbucket.internal.widget.searchableSelectorTriggerContent,
  triggerPlaceholder:null,preloadData:null,alwaysShowPreload:!1,dataTransform:null,postOptionsInit:null,clearResultsOnSearch:!0,popUpOffsetX:0,popUpOffsetY:-1,width:350};b.constructDataPageFromPreloadArray=function(a){return f.default.isArray(a)?{values:a,isLastPage:!1,size:a.length,start:0,limit:a.length}:null};b.prototype.tabOptionKeys="label url resultsTemplate noResultsText noMoreResultsText dataTransform queryParamKey preloadData searchPlaceholder".split(" ");b.prototype.init=function(a,c){var e=
  this;this.instanceId=E++;this.$trigger=(0,d.default)(a);this.setOptions(c);this.scrollables=[];this.scrollableDataStores=[];this._getOptionVal("id")||(this.options.id=this._getOptionVal("namespace")+"-"+this.instanceId);this.options.tabs&&this.options.tabs.length&&(this.tabs=f.default.map(this.options.tabs,function(a){return d.default.extend(!0,{},f.default.pick(e.options,e.tabOptionKeys),a)}));a=this._getOption("postOptionsInit");f.default.isFunction(a)&&a.call(this);var b;this.blockShortcutPropagation=
  b=function(a){var c=this===document;a.keyCode===g.default.keyCode.ESCAPE?(a[c?"stopImmediatePropagation":"stopPropagation"](),e.dialog.hide()):c||a.stopPropagation()};var h=e._getOptionVal("externalSearchField");this.dialog=g.default.InlineDialog(this.$trigger,this._getOptionVal("id"),function(a,c,b){a.data("initialised")||e._initialiseDialogContent(a);b();h||f.default.defer(f.default.bind(e.setFocus,e))},{offsetX:this._getOptionVal("popUpOffsetX"),offsetY:this._getOptionVal("popUpOffsetY"),noBind:!!h,
  width:this._getOptionVal("width"),hideCallback:function(){(e.$content.is(document.activeElement)||d.default.contains(e.$content[0],document.activeElement))&&e.$trigger.focus();(0,d.default)(document).add(e.dialog).off("keydown keypress",b)},initCallback:function(){(0,d.default)(document).add(e.dialog).on("keydown keypress",b)}});this._getOptionVal("searchable")&&h&&(this.$searchField=(0,d.default)(h),this._initialiseSearchOnInput());this.$trigger.find(".name").length&&(this._selectedItem=this._getItemFromTrigger());
  g.default.bind("hide.dialog",function(a,c){e.$trigger.closest(c.dialog.popup.element).length&&e.dialog.hide()});return this};b.prototype.getSelectedItem=function(){return this._selectedItem};b.prototype.setSelectedItem=function(a){d.default.isPlainObject(a)&&null!=a.id&&(this._selectedItem&&this._selectedItem.id===a.id||this._itemSelected(a))};b.prototype.clearSelection=function(){this._selectedItem=null;this._getOptionVal("field")&&(0,d.default)(this._getOptionVal("field")).val("").trigger("change");
  this.resetTrigger()};b.prototype._getItemFromTrigger=function(){var a=this.$trigger.find(".name");return d.default.extend({},this._buildObjectFromElementDataAttributes(a),{name:a.text()})};b.prototype._buildObjectFromElementDataAttributes=function(a){return(0,d.default)(a).data(this._getOptionVal("itemDataKey"))};b.prototype._initialiseDialogContent=function(a){var c=this,e=this._getOptionVal("searchable"),b=this._getOptionVal("externalSearchField");a.append(this._getOption("template")({id:this._getOptionVal("id"),
  tabs:this.tabs,searchable:!b&&e,searchPlaceholder:this._getOptionVal("searchPlaceholder"),extraClasses:this._getOptionVal("extraClasses")}));a.closest(".aui-inline-dialog").addClass("searchable-selector-dialog");e&&!b&&(this.$searchField=a.find("input.filter"),this._initialiseSearchOnInput());this.tabs&&(b=a.find("ul.tabs-menu a"),b.on("tabSelect",function(a,b){c.currentTabId=b.tab.parent().data("tab-id");(c._getOptionVal("searchable")||0===c._getDataStoreForScrollable().length)&&c._populateScrollable();
  c._resultsKeyboardController.setListElement(c._getCurrentScrollable().$scrollElement.find("ul.results-list"));e&&c.$searchField.focus();c._updateSearchPlaceholder()}),b.on("click",function(a){(0,d.default)(this).parent().hasClass("active-tab")&&(a.stopPropagation(),a.preventDefault())}),b.on("keydown",function(a){if(a.keyCode===g.default.keyCode.ENTER){var c=(0,d.default)(this);c.parent("li").hasClass("active-tab")?a.preventDefault():c.click()}}),g.default.tabs.setup());a.find(".results").each(function(){c.scrollables.push(c._createScrollable((0,
  d.default)(this)));c.scrollableDataStores.push([])});a.on("click",".result a",function(a){c.selectItem(a,(0,d.default)(this))});this._initialiseKeyboardNavigation(a);this._initialiseMouseNavigation(a);this._populateScrollable();this.$content=a;a.data("initialised",!0)};b.prototype._initialiseSearchOnInput=function(){var a=this,c=this._getSearchTerm(),b=this._getOptionVal("externalSearchField"),d=A.default.delay(f.default.bind(this._handleSearchInput,this),350),h=f.default.bind(function(){c!==(c=this._getSearchTerm())&&
  (this._pendingSearch?this._pendingSearch.reset(c):(this._pendingSearch=d(c),this._pendingSearch.always(function(){a._pendingSearch=null})))},this);this.$searchField.on("keydown.searchable-selector input.searchable-selector",f.default.bind(function(e){b&&(e.which===g.default.keyCode.ESCAPE?this._pendingSearch&&this._pendingSearch.abort():e.which===g.default.keyCode.DOWN&&this._shouldSearch(c)?this.dialog.show():e.which!==g.default.keyCode.ENTER||this.dialog.is(":visible")||this._pendingSearch&&this._pendingSearch.done(function(){var c=
  a._getCurrentScrollable().$scrollElement.find("ul.results-list li.result:first");a._clickItem(c)}));f.default.defer(h)},this))};b.prototype._handleSearchInput=function(a){if(this._getOptionVal("externalSearchField")){if(this._shouldSearch(a))return this.dialog.is(":visible")||this.dialog.show(),this._populateScrollable();this.dialog.hide();return d.default.Deferred().reject().promise()}return this._populateScrollable()};b.prototype._initialiseKeyboardNavigation=function(a){var c=this,b=!1,d=function(a){var e=
  c.dialog.is(":visible");if(!c._getOptionVal("externalSearchField")||e)b&&!e?c.$trigger.click():c._clickItem(a)};this._resultsKeyboardController&&this._resultsKeyboardController.destroy();if(this.$searchField)var f=this.$searchField;else this.tabs?f=a.find("ul.tabs-menu"):(f=this.$trigger,b=!0);var g=c._getCurrentScrollable().$scrollElement.find("ul.results-list");this._resultsKeyboardController=new D(f,g,{wrapAround:!1,focusedClass:"focused",itemSelector:"li.result",requestMore:function(){var a=c._getCurrentScrollable().loadAfter();
  return a&&a.then(function(a){return a.isLastPage})},onSelect:function(a){c._pendingSearch?c._pendingSearch.done(function(){var a=g.find("li.result:first");a.length&&d(a)}):d(a)},focusIntoView:!0,adjacentItems:c._getOptionVal("adjacentItems")});this._tabKeyboardController&&this._tabKeyboardController.destroy();this._tabKeyboardController=new C(a)};b.prototype._initialiseMouseNavigation=function(a){a.on("mousemove","li",function(){(0,d.default)(this).find(".focused").hasClass("focused")||(a.find(".focused").removeClass("focused"),
  (0,d.default)(this).addClass("focused"))})};b.prototype._clickItem=function(a){a.children("a").each(function(){this.click()})};b.prototype.setFocus=function(){this.$searchField?this.$searchField.focus():this.tabs?this.$content.find("ul.tabs-menu a").first().focus():this.$trigger.focus()};b.prototype._createScrollable=function(a){a=new B.default(a,{pageSize:this._getOptionVal("pageSize"),bufferPixels:0,preventOverscroll:!0,paginationContext:this.options.paginationContext});a.requestData=f.default.bind(this.getResults,
  this,a);a.attachNewContent=f.default.bind(this.addResultsToList,this,a,!1);return a};b.prototype._populateScrollable=function(a){a=a||this._getCurrentScrollable();this._emptyScrollable(a);var c=this._getPreloadData(),b=0;c&&(b=this.addResultsToList(a,!0,c));a=a.init();return 0<b?d.default.Deferred().resolve().promise():a};b.prototype._getPreloadData=function(){var a=this._getOptionVal("preloadData"),c=this._getSearchTerm();if(a&&(this._getOptionVal("alwaysShowPreload")||""===c)){var b=this._getOption("dataTransform");
  f.default.isFunction(b)&&(a=b.call(this,a,c))}else a=null;return a};b.prototype._emptyScrollable=function(a){a=a||this._getCurrentScrollable();a.reset();this._getDataStoreForScrollable(a).length=0;this._getOptionVal("clearResultsOnSearch")&&a.$scrollElement.children("ul.results-list").empty()};b.prototype._getCurrentScrollable=function(){return this.scrollables[this.tabs&&this.currentTabId?this.currentTabId:0]};b.prototype._getDataStoreForScrollable=function(a){if(!a)return this.scrollableDataStores[this.tabs&&
  this.currentTabId?this.currentTabId:0];a=f.default.indexOf(this.scrollables,a);return-1!==a?this.scrollableDataStores[a]:null};b.prototype._getSearchTerm=function(){return this.$searchField?this.$searchField.val():""};b.prototype._updateSearchPlaceholder=function(){this.$searchField&&this.$searchField.attr("placeholder",this._getOptionVal("searchPlaceholder"))};b.prototype._getOptionVal=function(a){return this._getOption(a,!0)};b.prototype._getOption=function(a,c){a=this.tabs&&f.default.includes(this.tabOptionKeys,
  a)?this.tabs[this.currentTabId||0][a]:this.options[a];return c?f.default.isFunction(a)?a.call(this):a:a};b.prototype.setOptions=function(a){this.options=d.default.extend(!0,{},this.defaults,a)};b.prototype.getResults=function(a,c,b){var e=this;this.currentXHR&&this.currentXHR.abort&&this.currentXHR.abort();c=(this._getOption("queryParamsBuilder")||this._defaultQueryParamsBuilder).call(this,this._getSearchTerm(),c,b);if(null===c)return d.default.Deferred().reject().promise();b=a.$scrollElement.children("ul.results-list");
  this._showSpinner(a);b.scrollTop(b[0].scrollHeight);c=this.currentXHR=x.default.rest({url:this._getOptionVal("url"),data:c,statusCode:this._getOptionVal("statusCodeHandlers")});c.always(function(){e._hideSpinner(a);e.currentXHR=null});return c};b.prototype._showSpinner=function(a){a.$scrollElement.children(".spinner").show().spin()};b.prototype._hideSpinner=function(a){a.$scrollElement.children(".spinner").spinStop().hide()};b.prototype._shouldSearch=function(a){return null!=(this._getOption("queryParamsBuilder")||
  this._defaultQueryParamsBuilder).call(this,a,0,this._getOptionVal("pageSize"))};b.prototype._defaultQueryParamsBuilder=function(a,c,b){c={start:c,limit:b};this._getOptionVal("searchable")&&(c[this._getOptionVal("queryParamKey")]=a);return c};b.prototype.addResultsToList=function(a,c,b){var e=this._getOption("dataTransform"),h;f.default.isFunction(e)&&(b=e.call(this,b,this._getSearchTerm()));!c&&(h=this._getPreloadData())&&(b=this.dedupeData(b,h));b=d.default.extend({},b,{isPreload:c,noResultsText:this._getOptionVal("noResultsText"),
  noMoreResultsText:this._getOptionVal("noMoreResultsText")});e=(0,d.default)(this._getOption("resultsTemplate")(b));var g=this._getDataStoreForScrollable(a);g.push.apply(g,b.values);a=a.$scrollElement.children("ul.results-list");(c=0===b.start&&!(h&&0<h.size)&&(!c||0<b.size))&&a.empty();a.append(e).attr("data-last-updated",(new Date).getTime());c&&0<b.size&&this._resultsKeyboardController.moveToNext();return b.size};b.prototype.dedupeData=function(a,b){a&&a.values&&b&&b.values&&(a=d.default.extend(!0,
  {},a),a.values=f.default.reject(a.values,function(a){return f.default.find(b.values,function(b){return a.id===b.id})}));return a};b.prototype.selectItem=function(a,b){var c=b.data("id"),d=f.default.find(this._getDataStoreForScrollable(),function(a){return a.id===c});("#"===b.attr("href")||!this._getOptionVal("followLinks")&&y.default.openInSameTab)&&a.preventDefault();this._selectedItem&&this._selectedItem.id===d.id||this._itemSelected(d);this._getOptionVal("hideDialogOnSelect")&&this.dialog.hide()};
  b.prototype._itemSelected=function(a){this._selectedItem=a;this._getOptionVal("field")&&(0,d.default)(this._getOptionVal("field")).val(a.id).trigger("change");this.updateTrigger({item:a});z.default.trigger(this._getOptionVal("itemSelectedEvent"),this,a,this._getOptionVal("context"))};b.prototype.updateTrigger=function(a,b){this._getOptionVal("externalSearchField")||(this.$trigger.html(this._getOption("triggerContentTemplate")(a||{})),b&&this.$trigger.attr("title",b));this.$trigger.trigger("change")};
  b.prototype.resetTrigger=function(){this.updateTrigger({text:this._getOptionVal("triggerPlaceholder")})};b.prototype.destroy=function(){this.blockShortcutPropagation&&(0,d.default)(document).add(this.dialog).off("keydown keypress",this.blockShortcutPropagation);this._getOptionVal("externalSearchField")&&this.$searchField.off(".searchable-selector");this.dialog&&(this.dialog.hide(),this.dialog.remove(),this.dialog=null);this._resultsKeyboardController&&(this._resultsKeyboardController.destroy(),this._resultsKeyboardController=
  null);this._tabKeyboardController&&(this._tabKeyboardController.destroy(),this._tabKeyboardController=null);this.tabs=this.scrollableDataStores=this.scrollables=this.$trigger=null;this._initialiseDialogContent=d.default.noop};l.default=b;m.exports=l["default"]});