debian-mirror-gitlab/app/assets/javascripts/search_autocomplete.js.es6

428 lines
13 KiB
JavaScript
Raw Normal View History

2016-11-03 12:29:30 +05:30
((global) => {
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
const KEYCODE = {
ESCAPE: 27,
BACKSPACE: 8,
ENTER: 13,
UP: 38,
DOWN: 40
};
class SearchAutocomplete {
constructor({ wrap, optsEl, autocompletePath, projectId, projectRef } = {}) {
this.bindEventContext();
this.wrap = wrap || $('.search');
this.optsEl = optsEl || this.wrap.find('.search-autocomplete-opts');
this.autocompletePath = autocompletePath || this.optsEl.data('autocomplete-path');
this.projectId = projectId || (this.optsEl.data('autocomplete-project-id') || '');
this.projectRef = projectRef || (this.optsEl.data('autocomplete-project-ref') || '');
2016-09-13 17:45:13 +05:30
this.dropdown = this.wrap.find('.dropdown');
this.dropdownContent = this.dropdown.find('.dropdown-content');
this.locationBadgeEl = this.getElement('.location-badge');
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');
this.saveOriginalState();
2016-09-29 09:46:39 +05:30
// Only when user is logged in
2016-09-13 17:45:13 +05:30
if (gon.current_user_id) {
this.createAutocomplete();
}
this.searchInput.addClass('disabled');
this.saveTextLength();
this.bindEvents();
}
2016-09-29 09:46:39 +05:30
// Finds an element inside wrapper element
2016-11-03 12:29:30 +05:30
bindEventContext() {
this.onSearchInputBlur = this.onSearchInputBlur.bind(this);
this.onClearInputClick = this.onClearInputClick.bind(this);
this.onSearchInputFocus = this.onSearchInputFocus.bind(this);
this.onSearchInputClick = this.onSearchInputClick.bind(this);
this.onSearchInputKeyUp = this.onSearchInputKeyUp.bind(this);
this.onSearchInputKeyDown = this.onSearchInputKeyDown.bind(this);
}
getElement(selector) {
2016-09-13 17:45:13 +05:30
return this.wrap.find(selector);
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
saveOriginalState() {
2016-09-13 17:45:13 +05:30
return this.originalState = this.serializeState();
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
saveTextLength() {
2016-09-13 17:45:13 +05:30
return this.lastTextLength = this.searchInput.val().length;
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
createAutocomplete() {
2016-09-13 17:45:13 +05:30
return this.searchInput.glDropdown({
filterInputBlur: false,
filterable: true,
filterRemote: true,
highlight: true,
enterCallback: false,
filterInput: 'input#search',
search: {
fields: ['text']
},
data: this.getData.bind(this),
selectable: true,
clicked: this.onClick.bind(this)
});
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
getData(term, callback) {
2016-09-13 17:45:13 +05:30
var _this, contents, jqXHR;
_this = this;
if (!term) {
if (contents = this.getCategoryContents()) {
this.searchInput.data('glDropdown').filter.options.callback(contents);
this.enableAutocomplete();
}
return;
}
2016-09-29 09:46:39 +05:30
// Prevent multiple ajax calls
2016-09-13 17:45:13 +05:30
if (this.loadingSuggestions) {
return;
}
this.loadingSuggestions = true;
return jqXHR = $.get(this.autocompletePath, {
project_id: this.projectId,
project_ref: this.projectRef,
term: term
}, function(response) {
var data, firstCategory, i, lastCategory, len, suggestion;
2016-09-29 09:46:39 +05:30
// Hide dropdown menu if no suggestions returns
2016-09-13 17:45:13 +05:30
if (!response.length) {
_this.disableAutocomplete();
return;
}
data = [];
2016-09-29 09:46:39 +05:30
// List results
2016-09-13 17:45:13 +05:30
firstCategory = true;
for (i = 0, len = response.length; i < len; i++) {
suggestion = response[i];
2016-09-29 09:46:39 +05:30
// Add group header before list each group
2016-09-13 17:45:13 +05:30
if (lastCategory !== suggestion.category) {
if (!firstCategory) {
data.push('separator');
}
if (firstCategory) {
firstCategory = false;
}
data.push({
header: suggestion.category
});
lastCategory = suggestion.category;
}
data.push({
id: (suggestion.category.toLowerCase()) + "-" + suggestion.id,
category: suggestion.category,
text: suggestion.label,
url: suggestion.url
});
}
2016-09-29 09:46:39 +05:30
// Add option to proceed with the search
2016-09-13 17:45:13 +05:30
if (data.length) {
data.push('separator');
data.push({
text: "Result name contains \"" + term + "\"",
url: "/search?search=" + term + "&project_id=" + (_this.projectInputEl.val()) + "&group_id=" + (_this.groupInputEl.val())
});
}
return callback(data);
}).always(function() {
return _this.loadingSuggestions = false;
});
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
getCategoryContents() {
2016-09-13 17:45:13 +05:30
var dashboardOptions, groupOptions, issuesPath, items, mrPath, name, options, projectOptions, userId, utils;
userId = gon.current_user_id;
utils = gl.utils, projectOptions = gl.projectOptions, groupOptions = gl.groupOptions, dashboardOptions = gl.dashboardOptions;
if (utils.isInGroupsPage() && groupOptions) {
options = groupOptions[utils.getGroupSlug()];
} else if (utils.isInProjectPage() && projectOptions) {
options = projectOptions[utils.getProjectSlug()];
} else if (dashboardOptions) {
options = dashboardOptions;
}
issuesPath = options.issuesPath, mrPath = options.mrPath, name = options.name;
items = [
{
header: "" + name
}, {
text: 'Issues assigned to me',
url: issuesPath + "/?assignee_id=" + userId
}, {
text: "Issues I've created",
url: issuesPath + "/?author_id=" + userId
}, 'separator', {
text: 'Merge requests assigned to me',
url: mrPath + "/?assignee_id=" + userId
}, {
text: "Merge requests I've created",
url: mrPath + "/?author_id=" + userId
}
];
if (!name) {
items.splice(0, 1);
}
return items;
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
serializeState() {
2016-09-13 17:45:13 +05:30
return {
2016-09-29 09:46:39 +05:30
// Search Criteria
2016-09-13 17:45:13 +05:30
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(),
2016-09-29 09:46:39 +05:30
// Location badge
2016-09-13 17:45:13 +05:30
_location: this.locationBadgeEl.text()
};
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
bindEvents() {
2016-09-13 17:45:13 +05:30
this.searchInput.on('keydown', this.onSearchInputKeyDown);
this.searchInput.on('keyup', this.onSearchInputKeyUp);
this.searchInput.on('click', this.onSearchInputClick);
this.searchInput.on('focus', this.onSearchInputFocus);
this.searchInput.on('blur', this.onSearchInputBlur);
this.clearInput.on('click', this.onClearInputClick);
return this.locationBadgeEl.on('click', (function(_this) {
return function() {
return _this.searchInput.focus();
};
})(this));
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
enableAutocomplete() {
2016-09-13 17:45:13 +05:30
var _this;
2016-09-29 09:46:39 +05:30
// No need to enable anything if user is not logged in
2016-09-13 17:45:13 +05:30
if (!gon.current_user_id) {
return;
}
if (!this.dropdown.hasClass('open')) {
_this = this;
this.loadingSuggestions = false;
this.dropdown.addClass('open').trigger('shown.bs.dropdown');
return this.searchInput.removeClass('disabled');
}
};
2016-09-29 09:46:39 +05:30
// Saves last length of the entered text
2016-11-03 12:29:30 +05:30
onSearchInputKeyDown() {
2016-09-13 17:45:13 +05:30
return this.saveTextLength();
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
onSearchInputKeyUp(e) {
2016-09-13 17:45:13 +05:30
switch (e.keyCode) {
case KEYCODE.BACKSPACE:
2016-09-29 09:46:39 +05:30
// when trying to remove the location badge
2016-09-13 17:45:13 +05:30
if (this.lastTextLength === 0 && this.badgePresent()) {
this.removeLocationBadge();
}
2016-09-29 09:46:39 +05:30
// When removing the last character and no badge is present
2016-09-13 17:45:13 +05:30
if (this.lastTextLength === 1) {
this.disableAutocomplete();
}
2016-09-29 09:46:39 +05:30
// When removing any character from existin value
2016-09-13 17:45:13 +05:30
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:
2016-09-29 09:46:39 +05:30
// Handle the case when deleting the input value other than backspace
// e.g. Pressing ctrl + backspace or ctrl + x
2016-09-13 17:45:13 +05:30
if (this.searchInput.val() === '') {
this.disableAutocomplete();
} else {
2016-09-29 09:46:39 +05:30
// We should display the menu only when input is not empty
2016-09-13 17:45:13 +05:30
if (e.keyCode !== KEYCODE.ENTER) {
this.enableAutocomplete();
}
}
}
this.wrap.toggleClass('has-value', !!e.target.value);
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-09-29 09:46:39 +05:30
// Avoid falsy value to be returned
2016-11-03 12:29:30 +05:30
onSearchInputClick(e) {
2016-09-13 17:45:13 +05:30
return e.stopImmediatePropagation();
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
onSearchInputFocus() {
2016-09-13 17:45:13 +05:30
this.isFocused = true;
this.wrap.addClass('search-active');
if (this.getValue() === '') {
return this.getData();
}
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
getValue() {
2016-09-13 17:45:13 +05:30
return this.searchInput.val();
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
onClearInputClick(e) {
2016-09-13 17:45:13 +05:30
e.preventDefault();
return this.searchInput.val('').focus();
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
onSearchInputBlur(e) {
2016-09-13 17:45:13 +05:30
this.isFocused = false;
this.wrap.removeClass('search-active');
2016-09-29 09:46:39 +05:30
// If input is blank then restore state
2016-09-13 17:45:13 +05:30
if (this.searchInput.val() === '') {
return this.restoreOriginalState();
}
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
addLocationBadge(item) {
2016-09-13 17:45:13 +05:30
var badgeText, category, value;
category = item.category != null ? item.category + ": " : '';
value = item.value != null ? item.value : '';
badgeText = "" + category + value;
this.locationBadgeEl.text(badgeText).show();
return this.wrap.addClass('has-location-badge');
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
hasLocationBadge() {
2016-09-13 17:45:13 +05:30
return this.wrap.is('.has-location-badge');
};
2016-11-03 12:29:30 +05:30
restoreOriginalState() {
2016-09-13 17:45:13 +05:30
var i, input, inputs, len;
inputs = Object.keys(this.originalState);
for (i = 0, len = inputs.length; i < len; i++) {
input = inputs[i];
this.getElement("#" + input).val(this.originalState[input]);
}
if (this.originalState._location === '') {
return this.locationBadgeEl.hide();
} else {
return this.addLocationBadge({
value: this.originalState._location
});
}
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
badgePresent() {
2016-09-13 17:45:13 +05:30
return this.locationBadgeEl.length;
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
resetSearchState() {
2016-09-13 17:45:13 +05:30
var i, input, inputs, len, results;
inputs = Object.keys(this.originalState);
results = [];
for (i = 0, len = inputs.length; i < len; i++) {
input = inputs[i];
2016-09-29 09:46:39 +05:30
// _location isnt a input
2016-09-13 17:45:13 +05:30
if (input === '_location') {
break;
}
results.push(this.getElement("#" + input).val(''));
}
return results;
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
removeLocationBadge() {
2016-09-13 17:45:13 +05:30
this.locationBadgeEl.hide();
this.resetSearchState();
this.wrap.removeClass('has-location-badge');
return this.disableAutocomplete();
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
disableAutocomplete() {
2016-09-13 17:45:13 +05:30
if (!this.searchInput.hasClass('disabled') && this.dropdown.hasClass('open')) {
this.searchInput.addClass('disabled');
this.dropdown.removeClass('open').trigger('hidden.bs.dropdown');
this.restoreMenu();
}
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
restoreMenu() {
2016-09-13 17:45:13 +05:30
var html;
html = "<ul> <li><a class='dropdown-menu-empty-link is-focused'>Loading...</a></li> </ul>";
return this.dropdownContent.html(html);
};
2016-11-03 12:29:30 +05:30
onClick(item, $el, e) {
2016-09-13 17:45:13 +05:30
if (location.pathname.indexOf(item.url) !== -1) {
e.preventDefault();
if (!this.badgePresent) {
if (item.category === 'Projects') {
this.projectInputEl.val(item.id);
this.addLocationBadge({
value: 'This project'
});
}
if (item.category === 'Groups') {
this.groupInputEl.val(item.id);
this.addLocationBadge({
value: 'This group'
});
}
}
$el.removeClass('is-active');
this.disableAutocomplete();
return this.searchInput.val('').focus();
}
};
2016-11-03 12:29:30 +05:30
}
global.SearchAutocomplete = SearchAutocomplete;
$(function() {
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 || {};
var projectPath = $projectOptionsDataEl.data('project-path');
gl.projectOptions[projectPath] = {
name: $projectOptionsDataEl.data('name'),
issuesPath: $projectOptionsDataEl.data('issues-path'),
mrPath: $projectOptionsDataEl.data('mr-path')
};
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
if ($groupOptionsDataEl.length) {
gl.groupOptions = gl.groupOptions || {};
var groupPath = $groupOptionsDataEl.data('group-path');
gl.groupOptions[groupPath] = {
name: $groupOptionsDataEl.data('name'),
issuesPath: $groupOptionsDataEl.data('issues-path'),
mrPath: $groupOptionsDataEl.data('mr-path')
};
}
if ($dashboardOptionsDataEl.length) {
gl.dashboardOptions = {
issuesPath: $dashboardOptionsDataEl.data('issues-path'),
mrPath: $dashboardOptionsDataEl.data('mr-path')
};
}
});
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
})(window.gl || (window.gl = {}));