2018-10-09 11:25:53 -04:00
|
|
|
/* eslint-disable no-return-assign, one-var, no-var, no-unused-vars, consistent-return, object-shorthand, prefer-template, class-methods-use-this, no-lonely-if, vars-on-top */
|
2018-03-09 15:18:59 -05:00
|
|
|
|
|
|
|
import $ from 'jquery';
|
2018-08-07 05:59:01 -04:00
|
|
|
import { escape, throttle } from 'underscore';
|
|
|
|
import { s__, sprintf } from '~/locale';
|
|
|
|
import { getIdenticonBackgroundClass, getIdenticonTitle } from '~/helpers/avatar_helper';
|
2018-02-06 07:21:17 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2018-02-21 15:22:56 -05:00
|
|
|
import DropdownUtils from './filtered_search/dropdown_utils';
|
2018-08-07 05:59:01 -04:00
|
|
|
import {
|
|
|
|
isInGroupsPage,
|
|
|
|
isInProjectPage,
|
|
|
|
getGroupSlug,
|
|
|
|
getProjectSlug,
|
|
|
|
spriteIcon,
|
|
|
|
} from './lib/utils/common_utils';
|
2016-12-13 22:01:05 -05:00
|
|
|
|
2017-12-06 11:41:24 -05:00
|
|
|
/**
|
|
|
|
* Search input in top navigation bar.
|
|
|
|
* On click, opens a dropdown
|
|
|
|
* As the user types it filters the results
|
|
|
|
* When the user clicks `x` button it cleans the input and closes the dropdown.
|
|
|
|
*/
|
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
const KEYCODE = {
|
|
|
|
ESCAPE: 27,
|
|
|
|
BACKSPACE: 8,
|
|
|
|
ENTER: 13,
|
|
|
|
UP: 38,
|
|
|
|
DOWN: 40,
|
|
|
|
};
|
|
|
|
|
|
|
|
function setSearchOptions() {
|
|
|
|
var $projectOptionsDataEl = $('.js-search-project-options');
|
|
|
|
var $groupOptionsDataEl = $('.js-search-group-options');
|
|
|
|
var $dashboardOptionsDataEl = $('.js-search-dashboard-options');
|
|
|
|
|
|
|
|
if ($projectOptionsDataEl.length) {
|
|
|
|
gl.projectOptions = gl.projectOptions || {};
|
|
|
|
|
2018-02-20 17:20:48 -05:00
|
|
|
var projectPath = $projectOptionsDataEl.data('projectPath');
|
2017-12-13 04:26:44 -05:00
|
|
|
|
|
|
|
gl.projectOptions[projectPath] = {
|
|
|
|
name: $projectOptionsDataEl.data('name'),
|
2018-02-20 17:20:48 -05:00
|
|
|
issuesPath: $projectOptionsDataEl.data('issuesPath'),
|
|
|
|
issuesDisabled: $projectOptionsDataEl.data('issuesDisabled'),
|
|
|
|
mrPath: $projectOptionsDataEl.data('mrPath'),
|
2017-12-13 04:26:44 -05:00
|
|
|
};
|
|
|
|
}
|
2017-12-06 11:41:24 -05:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
if ($groupOptionsDataEl.length) {
|
|
|
|
gl.groupOptions = gl.groupOptions || {};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2018-02-20 17:20:48 -05:00
|
|
|
var groupPath = $groupOptionsDataEl.data('groupPath');
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
gl.groupOptions[groupPath] = {
|
|
|
|
name: $groupOptionsDataEl.data('name'),
|
2018-02-20 17:20:48 -05:00
|
|
|
issuesPath: $groupOptionsDataEl.data('issuesPath'),
|
|
|
|
mrPath: $groupOptionsDataEl.data('mrPath'),
|
2017-12-13 04:26:44 -05:00
|
|
|
};
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
if ($dashboardOptionsDataEl.length) {
|
|
|
|
gl.dashboardOptions = {
|
2018-08-07 05:59:01 -04:00
|
|
|
name: s__('SearchAutocomplete|All GitLab'),
|
2018-02-20 17:20:48 -05:00
|
|
|
issuesPath: $dashboardOptionsDataEl.data('issuesPath'),
|
|
|
|
mrPath: $dashboardOptionsDataEl.data('mrPath'),
|
2017-12-13 04:26:44 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 17:59:09 -04:00
|
|
|
export class SearchAutocomplete {
|
2017-12-13 04:26:44 -05:00
|
|
|
constructor({ wrap, optsEl, autocompletePath, projectId, projectRef } = {}) {
|
|
|
|
setSearchOptions();
|
|
|
|
this.bindEventContext();
|
|
|
|
this.wrap = wrap || $('.search');
|
|
|
|
this.optsEl = optsEl || this.wrap.find('.search-autocomplete-opts');
|
2018-02-20 17:20:48 -05:00
|
|
|
this.autocompletePath = autocompletePath || this.optsEl.data('autocompletePath');
|
|
|
|
this.projectId = projectId || (this.optsEl.data('autocompleteProjectId') || '');
|
|
|
|
this.projectRef = projectRef || (this.optsEl.data('autocompleteProjectRef') || '');
|
2017-12-13 04:26:44 -05:00
|
|
|
this.dropdown = this.wrap.find('.dropdown');
|
|
|
|
this.dropdownToggle = this.wrap.find('.js-dropdown-search-toggle');
|
2018-08-07 05:59:01 -04:00
|
|
|
this.dropdownMenu = this.dropdown.find('.dropdown-menu');
|
2017-12-13 04:26:44 -05:00
|
|
|
this.dropdownContent = this.dropdown.find('.dropdown-content');
|
|
|
|
this.scopeInputEl = this.getElement('#scope');
|
|
|
|
this.searchInput = this.getElement('.search-input');
|
|
|
|
this.projectInputEl = this.getElement('#search_project_id');
|
|
|
|
this.groupInputEl = this.getElement('#group_id');
|
|
|
|
this.searchCodeInputEl = this.getElement('#search_code');
|
|
|
|
this.repositoryInputEl = this.getElement('#repository_ref');
|
|
|
|
this.clearInput = this.getElement('.js-clear-input');
|
2018-08-07 05:59:01 -04:00
|
|
|
this.scrollFadeInitialized = false;
|
2017-12-13 04:26:44 -05:00
|
|
|
this.saveOriginalState();
|
|
|
|
|
|
|
|
// Only when user is logged in
|
|
|
|
if (gon.current_user_id) {
|
|
|
|
this.createAutocomplete();
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
this.searchInput.addClass('disabled');
|
|
|
|
this.saveTextLength();
|
|
|
|
this.bindEvents();
|
|
|
|
this.dropdownToggle.dropdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finds an element inside wrapper element
|
|
|
|
bindEventContext() {
|
|
|
|
this.onSearchInputBlur = this.onSearchInputBlur.bind(this);
|
|
|
|
this.onClearInputClick = this.onClearInputClick.bind(this);
|
|
|
|
this.onSearchInputFocus = this.onSearchInputFocus.bind(this);
|
|
|
|
this.onSearchInputKeyUp = this.onSearchInputKeyUp.bind(this);
|
|
|
|
this.onSearchInputKeyDown = this.onSearchInputKeyDown.bind(this);
|
2018-08-07 05:59:01 -04:00
|
|
|
this.setScrollFade = this.setScrollFade.bind(this);
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
|
|
|
getElement(selector) {
|
|
|
|
return this.wrap.find(selector);
|
|
|
|
}
|
|
|
|
|
|
|
|
saveOriginalState() {
|
2018-08-07 05:59:01 -04:00
|
|
|
return (this.originalState = this.serializeState());
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
saveTextLength() {
|
2018-08-07 05:59:01 -04:00
|
|
|
return (this.lastTextLength = this.searchInput.val().length);
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
createAutocomplete() {
|
|
|
|
return this.searchInput.glDropdown({
|
|
|
|
filterInputBlur: false,
|
|
|
|
filterable: true,
|
|
|
|
filterRemote: true,
|
|
|
|
highlight: true,
|
2018-08-07 05:59:01 -04:00
|
|
|
icon: true,
|
2017-12-13 04:26:44 -05:00
|
|
|
enterCallback: false,
|
|
|
|
filterInput: 'input#search',
|
|
|
|
search: {
|
|
|
|
fields: ['text'],
|
|
|
|
},
|
|
|
|
id: this.getSearchText,
|
|
|
|
data: this.getData.bind(this),
|
|
|
|
selectable: true,
|
|
|
|
clicked: this.onClick.bind(this),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getSearchText(selectedObject, el) {
|
|
|
|
return selectedObject.id ? selectedObject.text : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
getData(term, callback) {
|
|
|
|
if (!term) {
|
|
|
|
const contents = this.getCategoryContents();
|
|
|
|
if (contents) {
|
2018-08-02 10:18:17 -04:00
|
|
|
const glDropdownInstance = this.searchInput.data('glDropdown');
|
|
|
|
|
|
|
|
if (glDropdownInstance) {
|
|
|
|
glDropdownInstance.filter.options.callback(contents);
|
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
this.enableAutocomplete();
|
|
|
|
}
|
|
|
|
return;
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
// Prevent multiple ajax calls
|
|
|
|
if (this.loadingSuggestions) {
|
|
|
|
return;
|
2017-01-12 12:12:46 -05:00
|
|
|
}
|
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
this.loadingSuggestions = true;
|
2017-12-06 11:41:24 -05:00
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
return axios
|
|
|
|
.get(this.autocompletePath, {
|
|
|
|
params: {
|
|
|
|
project_id: this.projectId,
|
|
|
|
project_ref: this.projectRef,
|
|
|
|
term: term,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
// Hide dropdown menu if no suggestions returns
|
|
|
|
if (!response.data.length) {
|
|
|
|
this.disableAutocomplete();
|
|
|
|
return;
|
|
|
|
}
|
2017-12-06 11:41:24 -05:00
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
const data = [];
|
|
|
|
// List results
|
|
|
|
let firstCategory = true;
|
|
|
|
let lastCategory;
|
|
|
|
for (let i = 0, len = response.data.length; i < len; i += 1) {
|
|
|
|
const suggestion = response.data[i];
|
|
|
|
// Add group header before list each group
|
|
|
|
if (lastCategory !== suggestion.category) {
|
|
|
|
if (!firstCategory) {
|
|
|
|
data.push('separator');
|
|
|
|
}
|
|
|
|
if (firstCategory) {
|
|
|
|
firstCategory = false;
|
|
|
|
}
|
|
|
|
data.push({
|
|
|
|
header: suggestion.category,
|
|
|
|
});
|
|
|
|
lastCategory = suggestion.category;
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
data.push({
|
2018-08-07 05:59:01 -04:00
|
|
|
id: `${suggestion.category.toLowerCase()}-${suggestion.id}`,
|
|
|
|
icon: this.getAvatar(suggestion),
|
|
|
|
category: suggestion.category,
|
|
|
|
text: suggestion.label,
|
|
|
|
url: suggestion.url,
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
}
|
2018-08-07 05:59:01 -04:00
|
|
|
// Add option to proceed with the search
|
|
|
|
if (data.length) {
|
|
|
|
const icon = spriteIcon('search', 's16 inline-search-icon');
|
|
|
|
let template;
|
2018-02-06 07:21:17 -05:00
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
if (this.projectInputEl.val()) {
|
|
|
|
template = s__('SearchAutocomplete|in this project');
|
|
|
|
}
|
|
|
|
if (this.groupInputEl.val()) {
|
|
|
|
template = s__('SearchAutocomplete|in this group');
|
|
|
|
}
|
2018-02-06 07:21:17 -05:00
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
data.unshift('separator');
|
|
|
|
data.unshift({
|
|
|
|
icon,
|
|
|
|
text: term,
|
|
|
|
template: s__('SearchAutocomplete|in all GitLab'),
|
2018-10-28 00:11:55 -04:00
|
|
|
url: `${gon.relative_url_root}/search?search=${term}`,
|
2018-08-07 05:59:01 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
if (template) {
|
|
|
|
data.unshift({
|
|
|
|
icon,
|
|
|
|
text: term,
|
|
|
|
template,
|
2018-10-30 16:28:31 -04:00
|
|
|
url: `${
|
|
|
|
gon.relative_url_root
|
|
|
|
}/search?search=${term}&project_id=${this.projectInputEl.val()}&group_id=${this.groupInputEl.val()}`,
|
2018-08-07 05:59:01 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(data);
|
|
|
|
|
|
|
|
this.loadingSuggestions = false;
|
|
|
|
this.highlightFirstRow();
|
|
|
|
this.setScrollFade();
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
this.loadingSuggestions = false;
|
|
|
|
});
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2017-12-06 11:41:24 -05:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
getCategoryContents() {
|
|
|
|
const userName = gon.current_username;
|
|
|
|
const { projectOptions, groupOptions, dashboardOptions } = gl;
|
|
|
|
|
|
|
|
// Get options
|
|
|
|
let options;
|
|
|
|
if (isInGroupsPage() && groupOptions) {
|
|
|
|
options = groupOptions[getGroupSlug()];
|
|
|
|
} else if (isInProjectPage() && projectOptions) {
|
|
|
|
options = projectOptions[getProjectSlug()];
|
|
|
|
} else if (dashboardOptions) {
|
|
|
|
options = dashboardOptions;
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
const { issuesPath, mrPath, name, issuesDisabled } = options;
|
|
|
|
const baseItems = [];
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
baseItems.push({
|
|
|
|
header: `${name}`,
|
|
|
|
});
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
const issueItems = [
|
|
|
|
{
|
2018-08-07 05:59:01 -04:00
|
|
|
text: s__('SearchAutocomplete|Issues assigned to me'),
|
2018-10-28 23:03:43 -04:00
|
|
|
url: `${issuesPath}/?assignee_username=${userName}`,
|
2017-12-13 04:26:44 -05:00
|
|
|
},
|
|
|
|
{
|
2018-08-07 05:59:01 -04:00
|
|
|
text: s__("SearchAutocomplete|Issues I've created"),
|
2018-10-28 23:03:43 -04:00
|
|
|
url: `${issuesPath}/?author_username=${userName}`,
|
2017-12-13 04:26:44 -05:00
|
|
|
},
|
|
|
|
];
|
|
|
|
const mergeRequestItems = [
|
|
|
|
{
|
2018-08-07 05:59:01 -04:00
|
|
|
text: s__('SearchAutocomplete|Merge requests assigned to me'),
|
2018-10-28 23:03:43 -04:00
|
|
|
url: `${mrPath}/?assignee_username=${userName}`,
|
2017-12-13 04:26:44 -05:00
|
|
|
},
|
|
|
|
{
|
2018-08-07 05:59:01 -04:00
|
|
|
text: s__("SearchAutocomplete|Merge requests I've created"),
|
2018-10-28 23:03:43 -04:00
|
|
|
url: `${mrPath}/?author_username=${userName}`,
|
2017-12-13 04:26:44 -05:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let items;
|
|
|
|
if (issuesDisabled) {
|
|
|
|
items = baseItems.concat(mergeRequestItems);
|
|
|
|
} else {
|
2018-08-07 05:59:01 -04:00
|
|
|
items = baseItems.concat(...issueItems, ...mergeRequestItems);
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
return items;
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
serializeState() {
|
|
|
|
return {
|
|
|
|
// Search Criteria
|
|
|
|
search_project_id: this.projectInputEl.val(),
|
|
|
|
group_id: this.groupInputEl.val(),
|
|
|
|
search_code: this.searchCodeInputEl.val(),
|
|
|
|
repository_ref: this.repositoryInputEl.val(),
|
|
|
|
scope: this.scopeInputEl.val(),
|
|
|
|
};
|
|
|
|
}
|
2017-12-06 11:41:24 -05:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
bindEvents() {
|
|
|
|
this.searchInput.on('keydown', this.onSearchInputKeyDown);
|
|
|
|
this.searchInput.on('keyup', this.onSearchInputKeyUp);
|
|
|
|
this.searchInput.on('focus', this.onSearchInputFocus);
|
|
|
|
this.searchInput.on('blur', this.onSearchInputBlur);
|
|
|
|
this.clearInput.on('click', this.onClearInputClick);
|
2018-08-07 05:59:01 -04:00
|
|
|
this.dropdownContent.on('scroll', throttle(this.setScrollFade, 250));
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
enableAutocomplete() {
|
2018-08-07 05:59:01 -04:00
|
|
|
this.setScrollFade();
|
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
// No need to enable anything if user is not logged in
|
|
|
|
if (!gon.current_user_id) {
|
|
|
|
return;
|
2017-01-10 17:54:56 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
// If the dropdown is closed, we'll open it
|
2018-06-27 03:00:11 -04:00
|
|
|
if (!this.dropdown.hasClass('show')) {
|
2017-12-13 04:26:44 -05:00
|
|
|
this.loadingSuggestions = false;
|
|
|
|
this.dropdownToggle.dropdown('toggle');
|
|
|
|
return this.searchInput.removeClass('disabled');
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
// Saves last length of the entered text
|
|
|
|
onSearchInputKeyDown() {
|
|
|
|
return this.saveTextLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
onSearchInputKeyUp(e) {
|
|
|
|
switch (e.keyCode) {
|
|
|
|
case KEYCODE.BACKSPACE:
|
|
|
|
// When removing the last character and no badge is present
|
|
|
|
if (this.lastTextLength === 1) {
|
|
|
|
this.disableAutocomplete();
|
|
|
|
}
|
|
|
|
// When removing any character from existin value
|
|
|
|
if (this.lastTextLength > 1) {
|
|
|
|
this.enableAutocomplete();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KEYCODE.ESCAPE:
|
|
|
|
this.restoreOriginalState();
|
|
|
|
break;
|
|
|
|
case KEYCODE.ENTER:
|
|
|
|
this.disableAutocomplete();
|
|
|
|
break;
|
|
|
|
case KEYCODE.UP:
|
|
|
|
case KEYCODE.DOWN:
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
// Handle the case when deleting the input value other than backspace
|
|
|
|
// e.g. Pressing ctrl + backspace or ctrl + x
|
|
|
|
if (this.searchInput.val() === '') {
|
2016-08-19 12:17:17 -04:00
|
|
|
this.disableAutocomplete();
|
2017-12-13 04:26:44 -05:00
|
|
|
} else {
|
|
|
|
// We should display the menu only when input is not empty
|
|
|
|
if (e.keyCode !== KEYCODE.ENTER) {
|
|
|
|
this.enableAutocomplete();
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
this.wrap.toggleClass('has-value', !!e.target.value);
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
onSearchInputFocus() {
|
|
|
|
this.isFocused = true;
|
|
|
|
this.wrap.addClass('search-active');
|
|
|
|
if (this.getValue() === '') {
|
|
|
|
return this.getData();
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
getValue() {
|
|
|
|
return this.searchInput.val();
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
onClearInputClick(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.wrap.toggleClass('has-value', !!e.target.value);
|
|
|
|
return this.searchInput.val('').focus();
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
onSearchInputBlur(e) {
|
|
|
|
this.isFocused = false;
|
|
|
|
this.wrap.removeClass('search-active');
|
|
|
|
// If input is blank then restore state
|
|
|
|
if (this.searchInput.val() === '') {
|
|
|
|
return this.restoreOriginalState();
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
restoreOriginalState() {
|
|
|
|
var i, input, inputs, len;
|
|
|
|
inputs = Object.keys(this.originalState);
|
|
|
|
for (i = 0, len = inputs.length; i < len; i += 1) {
|
|
|
|
input = inputs[i];
|
2018-08-07 05:59:01 -04:00
|
|
|
this.getElement('#' + input).val(this.originalState[input]);
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
resetSearchState() {
|
|
|
|
var i, input, inputs, len, results;
|
|
|
|
inputs = Object.keys(this.originalState);
|
|
|
|
results = [];
|
|
|
|
for (i = 0, len = inputs.length; i < len; i += 1) {
|
|
|
|
input = inputs[i];
|
2018-08-07 05:59:01 -04:00
|
|
|
results.push(this.getElement('#' + input).val(''));
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
return results;
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
disableAutocomplete() {
|
2018-06-27 03:00:11 -04:00
|
|
|
if (!this.searchInput.hasClass('disabled') && this.dropdown.hasClass('show')) {
|
2017-12-13 04:26:44 -05:00
|
|
|
this.searchInput.addClass('disabled');
|
2018-06-27 03:00:11 -04:00
|
|
|
this.dropdown.removeClass('show').trigger('hidden.bs.dropdown');
|
2017-12-13 04:26:44 -05:00
|
|
|
this.restoreMenu();
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
restoreMenu() {
|
|
|
|
var html;
|
|
|
|
html = '<ul><li class="dropdown-menu-empty-item"><a>Loading...</a></li></ul>';
|
|
|
|
return this.dropdownContent.html(html);
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
onClick(item, $el, e) {
|
2018-06-15 11:58:27 -04:00
|
|
|
if (window.location.pathname.indexOf(item.url) !== -1) {
|
2017-12-13 04:26:44 -05:00
|
|
|
if (!e.metaKey) e.preventDefault();
|
2018-08-07 05:59:01 -04:00
|
|
|
if (item.category === 'Projects') {
|
|
|
|
this.projectInputEl.val(item.id);
|
|
|
|
}
|
|
|
|
if (item.category === 'Groups') {
|
|
|
|
this.groupInputEl.val(item.id);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
$el.removeClass('is-active');
|
|
|
|
this.disableAutocomplete();
|
|
|
|
return this.searchInput.val('').focus();
|
2017-01-10 17:54:56 -05:00
|
|
|
}
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2018-08-07 05:59:01 -04:00
|
|
|
|
|
|
|
highlightFirstRow() {
|
|
|
|
this.searchInput.data('glDropdown').highlightRowAtIndex(null, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
getAvatar(item) {
|
|
|
|
if (!Object.hasOwnProperty.call(item, 'avatar_url')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { label, id } = item;
|
|
|
|
const avatarUrl = item.avatar_url;
|
|
|
|
const avatar = avatarUrl
|
|
|
|
? `<img class="search-item-avatar" src="${avatarUrl}" />`
|
|
|
|
: `<div class="s16 avatar identicon ${getIdenticonBackgroundClass(id)}">${getIdenticonTitle(
|
|
|
|
escape(label),
|
|
|
|
)}</div>`;
|
|
|
|
|
|
|
|
return avatar;
|
|
|
|
}
|
|
|
|
|
|
|
|
isScrolledUp() {
|
|
|
|
const el = this.dropdownContent[0];
|
|
|
|
const currentPosition = this.contentClientHeight + el.scrollTop;
|
|
|
|
|
|
|
|
return currentPosition < this.maxPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
initScrollFade() {
|
|
|
|
const el = this.dropdownContent[0];
|
|
|
|
this.scrollFadeInitialized = true;
|
|
|
|
|
|
|
|
this.contentClientHeight = el.clientHeight;
|
|
|
|
this.maxPosition = el.scrollHeight;
|
|
|
|
this.dropdownMenu.addClass('dropdown-content-faded-mask');
|
|
|
|
}
|
|
|
|
|
|
|
|
setScrollFade() {
|
|
|
|
this.initScrollFade();
|
|
|
|
|
|
|
|
this.dropdownMenu.toggleClass('fade-out', !this.isScrolledUp());
|
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2018-09-13 17:59:09 -04:00
|
|
|
|
|
|
|
export default function initSearchAutocomplete(opts) {
|
|
|
|
return new SearchAutocomplete(opts);
|
|
|
|
}
|