2020-11-24 15:15:51 +05:30
|
|
|
/* eslint-disable consistent-return */
|
2018-05-09 12:01:36 +05:30
|
|
|
import $ from 'jquery';
|
2020-05-24 23:13:21 +05:30
|
|
|
import { escape } from 'lodash';
|
2018-03-17 18:26:18 +05:30
|
|
|
import fuzzaldrinPlus from 'fuzzaldrin-plus';
|
2020-06-23 00:09:42 +05:30
|
|
|
import { visitUrl } from '~/lib/utils/url_utility';
|
2020-11-24 15:15:51 +05:30
|
|
|
import { isObject } from '~/lib/utils/type_utility';
|
|
|
|
import renderItem from './render';
|
|
|
|
import { GitLabDropdownRemote } from './gl_dropdown_remote';
|
|
|
|
import { GitLabDropdownInput } from './gl_dropdown_input';
|
|
|
|
import { GitLabDropdownFilter } from './gl_dropdown_filter';
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
const LOADING_CLASS = 'is-loading';
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
const PAGE_TWO_CLASS = 'is-page-two';
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
const ACTIVE_CLASS = 'is-active';
|
|
|
|
|
|
|
|
const INDETERMINATE_CLASS = 'is-indeterminate';
|
|
|
|
|
|
|
|
let currentIndex = -1;
|
|
|
|
|
|
|
|
const NON_SELECTABLE_CLASSES = '.divider, .separator, .dropdown-header, .dropdown-menu-empty-item';
|
|
|
|
|
|
|
|
const SELECTABLE_CLASSES = `.dropdown-content li:not(${NON_SELECTABLE_CLASSES}, .option-hidden)`;
|
|
|
|
|
|
|
|
const CURSOR_SELECT_SCROLL_PADDING = 5;
|
|
|
|
|
|
|
|
const FILTER_INPUT = '.dropdown-input .dropdown-input-field:not(.dropdown-no-filter)';
|
|
|
|
|
|
|
|
const NO_FILTER_INPUT = '.dropdown-input .dropdown-input-field.dropdown-no-filter';
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
export class GitLabDropdown {
|
2020-03-13 15:44:24 +05:30
|
|
|
constructor(el1, options) {
|
2020-11-24 15:15:51 +05:30
|
|
|
let selector;
|
|
|
|
let self;
|
2020-03-13 15:44:24 +05:30
|
|
|
this.el = el1;
|
|
|
|
this.options = options;
|
|
|
|
this.updateLabel = this.updateLabel.bind(this);
|
|
|
|
this.hidden = this.hidden.bind(this);
|
|
|
|
this.opened = this.opened.bind(this);
|
|
|
|
this.shouldPropagate = this.shouldPropagate.bind(this);
|
|
|
|
self = this;
|
|
|
|
selector = $(this.el).data('target');
|
|
|
|
this.dropdown = selector != null ? $(selector) : $(this.el).parent();
|
|
|
|
// Set Defaults
|
|
|
|
this.filterInput = this.options.filterInput || this.getElement(FILTER_INPUT);
|
|
|
|
this.noFilterInput = this.options.noFilterInput || this.getElement(NO_FILTER_INPUT);
|
|
|
|
this.highlight = Boolean(this.options.highlight);
|
|
|
|
this.icon = Boolean(this.options.icon);
|
|
|
|
this.filterInputBlur =
|
|
|
|
this.options.filterInputBlur != null ? this.options.filterInputBlur : true;
|
|
|
|
// If no input is passed create a default one
|
|
|
|
self = this;
|
|
|
|
// If selector was passed
|
2020-05-24 23:13:21 +05:30
|
|
|
if (typeof this.filterInput === 'string') {
|
2020-03-13 15:44:24 +05:30
|
|
|
this.filterInput = this.getElement(this.filterInput);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
const searchFields = this.options.search ? this.options.search.fields : [];
|
|
|
|
if (this.options.data) {
|
|
|
|
// If we provided data
|
|
|
|
// data could be an array of objects or a group of arrays
|
2020-05-24 23:13:21 +05:30
|
|
|
if (typeof this.options.data === 'object' && !(this.options.data instanceof Function)) {
|
2020-03-13 15:44:24 +05:30
|
|
|
this.fullData = this.options.data;
|
|
|
|
currentIndex = -1;
|
|
|
|
this.parseData(this.options.data);
|
|
|
|
this.focusTextInput();
|
|
|
|
} else {
|
|
|
|
this.remote = new GitLabDropdownRemote(this.options.data, {
|
|
|
|
dataType: this.options.dataType,
|
|
|
|
beforeSend: this.toggleLoading.bind(this),
|
|
|
|
success: data => {
|
|
|
|
this.fullData = data;
|
|
|
|
this.parseData(this.fullData);
|
|
|
|
this.focusTextInput();
|
|
|
|
|
|
|
|
// Update dropdown position since remote data may have changed dropdown size
|
|
|
|
this.dropdown.find('.dropdown-menu-toggle').dropdown('update');
|
|
|
|
|
|
|
|
if (
|
|
|
|
this.options.filterable &&
|
|
|
|
this.filter &&
|
|
|
|
this.filter.input &&
|
|
|
|
this.filter.input.val() &&
|
|
|
|
this.filter.input.val().trim() !== ''
|
|
|
|
) {
|
|
|
|
return this.filter.input.trigger('input');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
instance: this,
|
|
|
|
});
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
if (this.noFilterInput.length) {
|
|
|
|
this.plainInput = new GitLabDropdownInput(this.noFilterInput, this.options);
|
|
|
|
this.plainInput.onInput(this.addInput.bind(this));
|
|
|
|
}
|
|
|
|
// Init filterable
|
|
|
|
if (this.options.filterable) {
|
|
|
|
this.filter = new GitLabDropdownFilter(this.filterInput, {
|
|
|
|
elIsInput: $(this.el).is('input'),
|
|
|
|
filterInputBlur: this.filterInputBlur,
|
|
|
|
filterByText: this.options.filterByText,
|
|
|
|
onFilter: this.options.onFilter,
|
|
|
|
remote: this.options.filterRemote,
|
|
|
|
query: this.options.data,
|
|
|
|
keys: searchFields,
|
|
|
|
instance: this,
|
|
|
|
elements: () => {
|
|
|
|
selector = `.dropdown-content li:not(${NON_SELECTABLE_CLASSES})`;
|
2020-01-01 13:55:28 +05:30
|
|
|
if (this.dropdown.find('.dropdown-toggle-page').length) {
|
|
|
|
selector = `.dropdown-page-one ${selector}`;
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
return $(selector, this.dropdown);
|
|
|
|
},
|
|
|
|
data: () => this.fullData,
|
|
|
|
callback: data => {
|
|
|
|
this.parseData(data);
|
|
|
|
if (this.filterInput.val() !== '') {
|
|
|
|
selector = SELECTABLE_CLASSES;
|
|
|
|
if (this.dropdown.find('.dropdown-toggle-page').length) {
|
|
|
|
selector = `.dropdown-page-one ${selector}`;
|
|
|
|
}
|
|
|
|
if ($(this.el).is('input')) {
|
|
|
|
currentIndex = -1;
|
|
|
|
} else {
|
|
|
|
$(selector, this.dropdown)
|
|
|
|
.first()
|
|
|
|
.find('a')
|
|
|
|
.addClass('is-focused');
|
|
|
|
currentIndex = 0;
|
|
|
|
}
|
2018-11-18 11:00:15 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
},
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
// Event listeners
|
|
|
|
this.dropdown.on('shown.bs.dropdown', this.opened);
|
|
|
|
this.dropdown.on('hidden.bs.dropdown', this.hidden);
|
|
|
|
$(this.el).on('update.label', this.updateLabel);
|
|
|
|
this.dropdown.on('click', '.dropdown-menu, .dropdown-menu-close', this.shouldPropagate);
|
|
|
|
this.dropdown.on('keyup', e => {
|
|
|
|
// Escape key
|
|
|
|
if (e.which === 27) {
|
|
|
|
return $('.dropdown-menu-close', this.dropdown).trigger('click');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.dropdown.on('blur', 'a', e => {
|
2020-11-24 15:15:51 +05:30
|
|
|
let $dropdownMenu;
|
|
|
|
let $relatedTarget;
|
2020-03-13 15:44:24 +05:30
|
|
|
if (e.relatedTarget != null) {
|
|
|
|
$relatedTarget = $(e.relatedTarget);
|
|
|
|
$dropdownMenu = $relatedTarget.closest('.dropdown-menu');
|
|
|
|
if ($dropdownMenu.length === 0) {
|
|
|
|
return this.dropdown.removeClass('show');
|
|
|
|
}
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
|
|
|
});
|
|
|
|
if (this.dropdown.find('.dropdown-toggle-page').length) {
|
2020-03-13 15:44:24 +05:30
|
|
|
this.dropdown.find('.dropdown-toggle-page, .dropdown-menu-back').on('click', e => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return this.togglePage();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.options.selectable) {
|
|
|
|
selector = '.dropdown-content a';
|
|
|
|
if (this.dropdown.find('.dropdown-toggle-page').length) {
|
|
|
|
selector = '.dropdown-page-one .dropdown-content a';
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
this.dropdown.on('click', selector, e => {
|
|
|
|
const $el = $(e.currentTarget);
|
|
|
|
const selected = self.rowClicked($el);
|
|
|
|
const selectedObj = selected ? selected[0] : null;
|
|
|
|
const isMarking = selected ? selected[1] : null;
|
|
|
|
if (this.options.clicked) {
|
|
|
|
this.options.clicked.call(this, {
|
|
|
|
selectedObj,
|
|
|
|
$el,
|
|
|
|
e,
|
|
|
|
isMarking,
|
|
|
|
});
|
|
|
|
}
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
// Update label right after all modifications in dropdown has been done
|
|
|
|
if (this.options.toggleLabel) {
|
|
|
|
this.updateLabel(selectedObj, $el, this);
|
|
|
|
}
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
$el.trigger('blur');
|
|
|
|
});
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
// Finds an element inside wrapper element
|
|
|
|
getElement(selector) {
|
|
|
|
return this.dropdown.find(selector);
|
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
toggleLoading() {
|
|
|
|
return $('.dropdown-menu', this.dropdown).toggleClass(LOADING_CLASS);
|
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
togglePage() {
|
|
|
|
const menu = $('.dropdown-menu', this.dropdown);
|
|
|
|
if (menu.hasClass(PAGE_TWO_CLASS)) {
|
|
|
|
if (this.remote) {
|
|
|
|
this.remote.execute();
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
menu.toggleClass(PAGE_TWO_CLASS);
|
|
|
|
// Focus first visible input on active page
|
|
|
|
return this.dropdown.find('[class^="dropdown-page-"]:visible :text:visible:first').focus();
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
parseData(data) {
|
2020-11-24 15:15:51 +05:30
|
|
|
let groupData;
|
|
|
|
let html;
|
2020-03-13 15:44:24 +05:30
|
|
|
this.renderedData = data;
|
|
|
|
if (this.options.filterable && data.length === 0) {
|
|
|
|
// render no matching results
|
|
|
|
html = [this.noResults()];
|
|
|
|
}
|
2020-01-01 13:55:28 +05:30
|
|
|
// Handle array groups
|
2020-03-13 15:44:24 +05:30
|
|
|
else if (isObject(data)) {
|
2020-01-01 13:55:28 +05:30
|
|
|
html = [];
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
Object.keys(data).forEach(name => {
|
2020-01-01 13:55:28 +05:30
|
|
|
groupData = data[name];
|
|
|
|
html.push(
|
|
|
|
this.renderItem(
|
|
|
|
{
|
|
|
|
content: name,
|
|
|
|
type: 'header',
|
|
|
|
},
|
|
|
|
name,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
this.renderData(groupData, name).map(item => html.push(item));
|
2020-03-13 15:44:24 +05:30
|
|
|
});
|
2020-01-01 13:55:28 +05:30
|
|
|
} else {
|
|
|
|
// Render each row
|
|
|
|
html = this.renderData(data);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
// Render the full menu
|
|
|
|
const fullHtml = this.renderMenu(html);
|
|
|
|
return this.appendMenu(fullHtml);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderData(data, group) {
|
|
|
|
return data.map((obj, index) => this.renderItem(obj, group || false, index));
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldPropagate(e) {
|
|
|
|
let $target;
|
|
|
|
if (this.options.multiSelect || this.options.shouldPropagate === false) {
|
|
|
|
$target = $(e.target);
|
|
|
|
if (
|
|
|
|
$target &&
|
|
|
|
!$target.hasClass('dropdown-menu-close') &&
|
|
|
|
!$target.hasClass('dropdown-menu-close-icon') &&
|
|
|
|
!$target.data('isLink')
|
|
|
|
) {
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
// This prevents automatic scrolling to the top
|
|
|
|
if ($target.closest('a').length) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
return true;
|
|
|
|
}
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
filteredFullData() {
|
|
|
|
return this.fullData.filter(
|
|
|
|
r =>
|
|
|
|
typeof r === 'object' &&
|
|
|
|
!Object.prototype.hasOwnProperty.call(r, 'beforeDivider') &&
|
|
|
|
!Object.prototype.hasOwnProperty.call(r, 'header'),
|
2020-01-01 13:55:28 +05:30
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
opened(e) {
|
|
|
|
this.resetRows();
|
|
|
|
this.addArrowKeyEvent();
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
const dropdownToggle = this.dropdown.find('.dropdown-menu-toggle');
|
|
|
|
const hasFilterBulkUpdate = dropdownToggle.hasClass('js-filter-bulk-update');
|
|
|
|
const shouldRefreshOnOpen = dropdownToggle.hasClass('js-gl-dropdown-refresh-on-open');
|
|
|
|
const hasMultiSelect = dropdownToggle.hasClass('js-multiselect');
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
// Makes indeterminate items effective
|
|
|
|
if (this.fullData && (shouldRefreshOnOpen || hasFilterBulkUpdate)) {
|
|
|
|
this.parseData(this.fullData);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
// Process the data to make sure rendered data
|
|
|
|
// matches the correct layout
|
|
|
|
const inputValue = this.filterInput.val();
|
|
|
|
if (this.fullData && hasMultiSelect && this.options.processData && inputValue.length === 0) {
|
|
|
|
this.options.processData.call(
|
|
|
|
this.options,
|
|
|
|
inputValue,
|
|
|
|
this.filteredFullData(),
|
|
|
|
this.parseData.bind(this),
|
|
|
|
);
|
|
|
|
}
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
const contentHtml = $('.dropdown-content', this.dropdown).html();
|
|
|
|
if (this.remote && contentHtml === '') {
|
|
|
|
this.remote.execute();
|
|
|
|
} else {
|
|
|
|
this.focusTextInput();
|
|
|
|
}
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
if (this.options.showMenuAbove) {
|
|
|
|
this.positionMenuAbove();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.options.opened) {
|
|
|
|
if (this.options.preserveContext) {
|
|
|
|
this.options.opened(e);
|
|
|
|
} else {
|
|
|
|
this.options.opened.call(this, e);
|
|
|
|
}
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
return this.dropdown.trigger('shown.gl.dropdown');
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
positionMenuAbove() {
|
|
|
|
const $menu = this.dropdown.find('.dropdown-menu');
|
|
|
|
|
|
|
|
$menu.addClass('dropdown-open-top');
|
|
|
|
$menu.css('top', 'initial');
|
|
|
|
$menu.css('bottom', '100%');
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
hidden(e) {
|
|
|
|
this.resetRows();
|
|
|
|
this.removeArrowKeyEvent();
|
|
|
|
const $input = this.dropdown.find('.dropdown-input-field');
|
|
|
|
if (this.options.filterable) {
|
|
|
|
$input.blur();
|
|
|
|
}
|
|
|
|
if (this.dropdown.find('.dropdown-toggle-page').length) {
|
|
|
|
$('.dropdown-menu', this.dropdown).removeClass(PAGE_TWO_CLASS);
|
|
|
|
}
|
|
|
|
if (this.options.hidden) {
|
|
|
|
this.options.hidden.call(this, e);
|
|
|
|
}
|
|
|
|
return this.dropdown.trigger('hidden.gl.dropdown');
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
// Render the full menu
|
|
|
|
renderMenu(html) {
|
|
|
|
if (this.options.renderMenu) {
|
|
|
|
return this.options.renderMenu(html);
|
|
|
|
}
|
2020-01-01 13:55:28 +05:30
|
|
|
return $('<ul>').append(html);
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
// Append the menu into the dropdown
|
|
|
|
appendMenu(html) {
|
|
|
|
return this.clearMenu().append(html);
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
clearMenu() {
|
|
|
|
let selector = '.dropdown-content';
|
|
|
|
if (this.dropdown.find('.dropdown-toggle-page').length) {
|
|
|
|
if (this.options.containerSelector) {
|
|
|
|
selector = this.options.containerSelector;
|
|
|
|
} else {
|
|
|
|
selector = '.dropdown-page-one .dropdown-content';
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
return $(selector, this.dropdown).empty();
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
renderItem(data, group, index) {
|
|
|
|
let parent;
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
if (this.dropdown && this.dropdown[0]) {
|
|
|
|
parent = this.dropdown[0].parentNode;
|
|
|
|
}
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
return renderItem({
|
|
|
|
instance: this,
|
2020-05-24 23:13:21 +05:30
|
|
|
options: {
|
|
|
|
...this.options,
|
2020-03-13 15:44:24 +05:30
|
|
|
icon: this.icon,
|
|
|
|
highlight: this.highlight,
|
|
|
|
highlightText: text => this.highlightTextMatches(text, this.filterInput.val()),
|
|
|
|
highlightTemplate: this.highlightTemplate.bind(this),
|
|
|
|
parent,
|
2020-05-24 23:13:21 +05:30
|
|
|
},
|
2020-03-13 15:44:24 +05:30
|
|
|
data,
|
|
|
|
group,
|
|
|
|
index,
|
|
|
|
});
|
|
|
|
}
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
highlightTemplate(text, template) {
|
2020-05-24 23:13:21 +05:30
|
|
|
return `"<b>${escape(text)}</b>" ${template}`;
|
2020-03-13 15:44:24 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
highlightTextMatches(text, term) {
|
|
|
|
const occurrences = fuzzaldrinPlus.match(text, term);
|
|
|
|
const { indexOf } = [];
|
|
|
|
|
|
|
|
return text
|
|
|
|
.split('')
|
|
|
|
.map((character, i) => {
|
|
|
|
if (indexOf.call(occurrences, i) !== -1) {
|
|
|
|
return `<b>${character}</b>`;
|
|
|
|
}
|
2020-01-01 13:55:28 +05:30
|
|
|
return character;
|
2020-03-13 15:44:24 +05:30
|
|
|
})
|
|
|
|
.join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
noResults() {
|
|
|
|
return '<li class="dropdown-menu-empty-item"><a>No matching results</a></li>';
|
|
|
|
}
|
|
|
|
|
|
|
|
rowClicked(el) {
|
2020-11-24 15:15:51 +05:30
|
|
|
let field;
|
|
|
|
let groupName;
|
|
|
|
let selectedIndex;
|
|
|
|
let selectedObject;
|
|
|
|
let isMarking;
|
2020-03-13 15:44:24 +05:30
|
|
|
const { fieldName } = this.options;
|
|
|
|
const isInput = $(this.el).is('input');
|
|
|
|
if (this.renderedData) {
|
|
|
|
groupName = el.data('group');
|
|
|
|
if (groupName) {
|
|
|
|
selectedIndex = el.data('index');
|
|
|
|
selectedObject = this.renderedData[groupName][selectedIndex];
|
|
|
|
} else {
|
|
|
|
selectedIndex = el.closest('li').index();
|
|
|
|
this.selectedIndex = selectedIndex;
|
|
|
|
selectedObject = this.renderedData[selectedIndex];
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
}
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
if (this.options.vue) {
|
|
|
|
if (el.hasClass(ACTIVE_CLASS)) {
|
|
|
|
el.removeClass(ACTIVE_CLASS);
|
|
|
|
} else {
|
|
|
|
el.addClass(ACTIVE_CLASS);
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
return [selectedObject];
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
field = [];
|
|
|
|
const value = this.options.id ? this.options.id(selectedObject, el) : selectedObject.id;
|
|
|
|
if (isInput) {
|
|
|
|
field = $(this.el);
|
|
|
|
} else if (value != null) {
|
|
|
|
field = this.dropdown
|
|
|
|
.parent()
|
|
|
|
.find(`input[name='${fieldName}'][value='${value.toString().replace(/'/g, "\\'")}']`);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
if (this.options.isSelectable && !this.options.isSelectable(selectedObject, el)) {
|
|
|
|
return [selectedObject];
|
|
|
|
}
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
if (el.hasClass(ACTIVE_CLASS) && value !== 0) {
|
|
|
|
isMarking = false;
|
|
|
|
el.removeClass(ACTIVE_CLASS);
|
|
|
|
if (field && field.length) {
|
|
|
|
this.clearField(field, isInput);
|
|
|
|
}
|
|
|
|
} else if (el.hasClass(INDETERMINATE_CLASS)) {
|
|
|
|
isMarking = true;
|
|
|
|
el.addClass(ACTIVE_CLASS);
|
|
|
|
el.removeClass(INDETERMINATE_CLASS);
|
|
|
|
if (field && field.length && value == null) {
|
|
|
|
this.clearField(field, isInput);
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
2020-01-01 13:55:28 +05:30
|
|
|
if ((!field || !field.length) && fieldName) {
|
|
|
|
this.addInput(fieldName, value, selectedObject);
|
2020-03-13 15:44:24 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
isMarking = true;
|
|
|
|
if (!this.options.multiSelect || el.hasClass('dropdown-clear-active')) {
|
|
|
|
this.dropdown.find(`.${ACTIVE_CLASS}`).removeClass(ACTIVE_CLASS);
|
|
|
|
if (!isInput) {
|
|
|
|
this.dropdown
|
|
|
|
.parent()
|
|
|
|
.find(`input[name='${fieldName}']`)
|
|
|
|
.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (field && field.length && value == null) {
|
|
|
|
this.clearField(field, isInput);
|
|
|
|
}
|
|
|
|
// Toggle active class for the tick mark
|
|
|
|
el.addClass(ACTIVE_CLASS);
|
|
|
|
if (value != null) {
|
|
|
|
if ((!field || !field.length) && fieldName) {
|
|
|
|
this.addInput(fieldName, value, selectedObject);
|
|
|
|
} else if (field && field.length) {
|
|
|
|
field.val(value).trigger('change');
|
|
|
|
}
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
return [selectedObject, isMarking];
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
focusTextInput() {
|
|
|
|
if (this.options.filterable) {
|
|
|
|
const initialScrollTop = $(window).scrollTop();
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
if (this.dropdown.is('.show') && !this.filterInput.is(':focus')) {
|
|
|
|
this.filterInput.focus();
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
if ($(window).scrollTop() < initialScrollTop) {
|
|
|
|
$(window).scrollTop(initialScrollTop);
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
addInput(fieldName, value, selectedObject, single) {
|
|
|
|
// Create hidden input for form
|
|
|
|
if (single) {
|
|
|
|
$(`input[name="${fieldName}"]`).remove();
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
const $input = $('<input>')
|
|
|
|
.attr('type', 'hidden')
|
|
|
|
.attr('name', fieldName)
|
|
|
|
.val(value);
|
|
|
|
if (this.options.inputId != null) {
|
|
|
|
$input.attr('id', this.options.inputId);
|
|
|
|
}
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
if (this.options.multiSelect) {
|
|
|
|
Object.keys(selectedObject).forEach(attribute => {
|
|
|
|
$input.attr(`data-${attribute}`, selectedObject[attribute]);
|
|
|
|
});
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
if (this.options.inputMeta) {
|
|
|
|
$input.attr('data-meta', selectedObject[this.options.inputMeta]);
|
|
|
|
}
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
this.dropdown.before($input).trigger('change');
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
selectRowAtIndex(index) {
|
|
|
|
// If we pass an option index
|
|
|
|
let selector;
|
|
|
|
if (typeof index !== 'undefined') {
|
|
|
|
selector = `${SELECTABLE_CLASSES}:eq(${index}) a`;
|
2017-08-17 22:00:37 +05:30
|
|
|
} else {
|
2020-03-13 15:44:24 +05:30
|
|
|
selector = '.dropdown-content .is-focused';
|
|
|
|
}
|
|
|
|
if (this.dropdown.find('.dropdown-toggle-page').length) {
|
|
|
|
selector = `.dropdown-page-one ${selector}`;
|
|
|
|
}
|
|
|
|
// simulate a click on the first link
|
|
|
|
const $el = $(selector, this.dropdown);
|
|
|
|
if ($el.length) {
|
|
|
|
const href = $el.attr('href');
|
|
|
|
if (href && href !== '#') {
|
|
|
|
visitUrl(href);
|
|
|
|
} else {
|
|
|
|
$el.trigger('click');
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
addArrowKeyEvent() {
|
|
|
|
const ARROW_KEY_CODES = [38, 40];
|
|
|
|
let selector = SELECTABLE_CLASSES;
|
|
|
|
if (this.dropdown.find('.dropdown-toggle-page').length) {
|
|
|
|
selector = `.dropdown-page-one ${selector}`;
|
|
|
|
}
|
|
|
|
return $('body').on('keydown', e => {
|
2020-11-24 15:15:51 +05:30
|
|
|
let $listItems;
|
|
|
|
let PREV_INDEX;
|
2020-03-13 15:44:24 +05:30
|
|
|
const currentKeyCode = e.which;
|
|
|
|
if (ARROW_KEY_CODES.indexOf(currentKeyCode) !== -1) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
PREV_INDEX = currentIndex;
|
|
|
|
$listItems = $(selector, this.dropdown);
|
|
|
|
// if @options.filterable
|
|
|
|
// $input.blur()
|
|
|
|
if (currentKeyCode === 40) {
|
|
|
|
// Move down
|
|
|
|
if (currentIndex < $listItems.length - 1) {
|
|
|
|
currentIndex += 1;
|
|
|
|
}
|
|
|
|
} else if (currentKeyCode === 38) {
|
|
|
|
// Move up
|
|
|
|
if (currentIndex > 0) {
|
|
|
|
currentIndex -= 1;
|
|
|
|
}
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
if (currentIndex !== PREV_INDEX) {
|
|
|
|
this.highlightRowAtIndex($listItems, currentIndex);
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
return false;
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
if (currentKeyCode === 13 && currentIndex !== -1) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.selectRowAtIndex();
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
});
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
removeArrowKeyEvent() {
|
|
|
|
return $('body').off('keydown');
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
resetRows() {
|
|
|
|
currentIndex = -1;
|
|
|
|
$('.is-focused', this.dropdown).removeClass('is-focused');
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
highlightRowAtIndex($listItems, index) {
|
|
|
|
if (!$listItems) {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
$listItems = $(SELECTABLE_CLASSES, this.dropdown);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the class for the previously focused row
|
|
|
|
$('.is-focused', this.dropdown).removeClass('is-focused');
|
|
|
|
// Update the class for the row at the specific index
|
|
|
|
const $listItem = $listItems.eq(index);
|
|
|
|
$listItem.find('a:first-child').addClass('is-focused');
|
|
|
|
// Dropdown content scroll area
|
|
|
|
const $dropdownContent = $listItem.closest('.dropdown-content');
|
|
|
|
const dropdownScrollTop = $dropdownContent.scrollTop();
|
|
|
|
const dropdownContentHeight = $dropdownContent.outerHeight();
|
|
|
|
const dropdownContentTop = $dropdownContent.prop('offsetTop');
|
|
|
|
const dropdownContentBottom = dropdownContentTop + dropdownContentHeight;
|
|
|
|
// Get the offset bottom of the list item
|
|
|
|
const listItemHeight = $listItem.outerHeight();
|
|
|
|
const listItemTop = $listItem.prop('offsetTop');
|
|
|
|
const listItemBottom = listItemTop + listItemHeight;
|
|
|
|
if (!index) {
|
|
|
|
// Scroll the dropdown content to the top
|
|
|
|
$dropdownContent.scrollTop(0);
|
|
|
|
} else if (index === $listItems.length - 1) {
|
|
|
|
// Scroll the dropdown content to the bottom
|
|
|
|
$dropdownContent.scrollTop($dropdownContent.prop('scrollHeight'));
|
|
|
|
} else if (listItemBottom > dropdownContentBottom + dropdownScrollTop) {
|
|
|
|
// Scroll the dropdown content down
|
|
|
|
$dropdownContent.scrollTop(
|
|
|
|
listItemBottom - dropdownContentBottom + CURSOR_SELECT_SCROLL_PADDING,
|
|
|
|
);
|
|
|
|
} else if (listItemTop < dropdownContentTop + dropdownScrollTop) {
|
|
|
|
// Scroll the dropdown content up
|
|
|
|
return $dropdownContent.scrollTop(
|
|
|
|
listItemTop - dropdownContentTop - CURSOR_SELECT_SCROLL_PADDING,
|
|
|
|
);
|
|
|
|
}
|
2020-01-01 13:55:28 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
updateLabel(selected = null, el = null, instance = null) {
|
|
|
|
let toggleText = this.options.toggleLabel(selected, el, instance);
|
|
|
|
if (this.options.updateLabel) {
|
|
|
|
// Option to override the dropdown label text
|
|
|
|
toggleText = this.options.updateLabel;
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
return $(this.el)
|
|
|
|
.find('.dropdown-toggle-text')
|
|
|
|
.text(toggleText);
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
clearField(field, isInput) {
|
|
|
|
return isInput ? field.val('') : field.remove();
|
|
|
|
}
|
|
|
|
}
|