2018-12-13 13:39:08 +05:30
|
|
|
/* eslint-disable func-names, no-underscore-dangle, no-var, one-var, vars-on-top, no-unused-vars, no-shadow, no-cond-assign, prefer-arrow-callback, no-return-assign, no-else-return, camelcase, no-lonely-if, guard-for-in, no-restricted-syntax, consistent-return, prefer-template, no-param-reassign, no-loop-func */
|
2017-08-17 22:00:37 +05:30
|
|
|
/* global fuzzaldrinPlus */
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
import $ from 'jquery';
|
2017-09-10 17:25:29 +05:30
|
|
|
import _ from 'underscore';
|
2018-03-17 18:26:18 +05:30
|
|
|
import fuzzaldrinPlus from 'fuzzaldrin-plus';
|
|
|
|
import axios from './lib/utils/axios_utils';
|
|
|
|
import { visitUrl } from './lib/utils/url_utility';
|
2017-09-10 17:25:29 +05:30
|
|
|
import { isObject } from './lib/utils/type_utility';
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
var GitLabDropdown, GitLabDropdownFilter, GitLabDropdownRemote, GitLabDropdownInput;
|
|
|
|
|
|
|
|
GitLabDropdownInput = (function() {
|
|
|
|
function GitLabDropdownInput(input, options) {
|
|
|
|
var $inputContainer, $clearButton;
|
|
|
|
var _this = this;
|
|
|
|
this.input = input;
|
|
|
|
this.options = options;
|
|
|
|
this.fieldName = this.options.fieldName || 'field-name';
|
|
|
|
$inputContainer = this.input.parent();
|
|
|
|
$clearButton = $inputContainer.find('.js-dropdown-input-clear');
|
2018-11-18 11:00:15 +05:30
|
|
|
$clearButton.on(
|
|
|
|
'click',
|
|
|
|
(function(_this) {
|
|
|
|
// Clear click
|
|
|
|
return function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return _this.input
|
|
|
|
.val('')
|
|
|
|
.trigger('input')
|
|
|
|
.focus();
|
|
|
|
};
|
|
|
|
})(this),
|
|
|
|
);
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
this.input
|
2018-11-18 11:00:15 +05:30
|
|
|
.on('keydown', function(e) {
|
|
|
|
var keyCode = e.which;
|
|
|
|
if (keyCode === 13 && !options.elIsInput) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on('input', function(e) {
|
|
|
|
var val = e.currentTarget.value || _this.options.inputFieldName;
|
|
|
|
val = val
|
|
|
|
.split(' ')
|
|
|
|
.join('-') // replaces space with dash
|
|
|
|
.replace(/[^a-zA-Z0-9 -]/g, '')
|
|
|
|
.toLowerCase() // replace non alphanumeric
|
|
|
|
.replace(/(-)\1+/g, '-'); // replace repeated dashes
|
|
|
|
_this.cb(_this.options.fieldName, val, {}, true);
|
|
|
|
_this.input
|
|
|
|
.closest('.dropdown')
|
|
|
|
.find('.dropdown-toggle-text')
|
|
|
|
.text(val);
|
|
|
|
});
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
GitLabDropdownInput.prototype.onInput = function(cb) {
|
|
|
|
this.cb = cb;
|
|
|
|
};
|
|
|
|
|
|
|
|
return GitLabDropdownInput;
|
|
|
|
})();
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
GitLabDropdownFilter = (function() {
|
|
|
|
var ARROW_KEY_CODES, BLUR_KEYCODES, HAS_VALUE_CLASS;
|
|
|
|
|
|
|
|
BLUR_KEYCODES = [27, 40];
|
|
|
|
|
|
|
|
ARROW_KEY_CODES = [38, 40];
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
HAS_VALUE_CLASS = 'has-value';
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
function GitLabDropdownFilter(input, options) {
|
|
|
|
var $clearButton, $inputContainer, ref, timeout;
|
|
|
|
this.input = input;
|
|
|
|
this.options = options;
|
|
|
|
this.filterInputBlur = (ref = this.options.filterInputBlur) != null ? ref : true;
|
|
|
|
$inputContainer = this.input.parent();
|
|
|
|
$clearButton = $inputContainer.find('.js-dropdown-input-clear');
|
2018-11-18 11:00:15 +05:30
|
|
|
$clearButton.on(
|
|
|
|
'click',
|
|
|
|
(function(_this) {
|
|
|
|
// Clear click
|
|
|
|
return function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return _this.input
|
|
|
|
.val('')
|
|
|
|
.trigger('input')
|
|
|
|
.focus();
|
|
|
|
};
|
|
|
|
})(this),
|
|
|
|
);
|
2017-08-17 22:00:37 +05:30
|
|
|
// Key events
|
2018-11-18 11:00:15 +05:30
|
|
|
timeout = '';
|
2017-08-17 22:00:37 +05:30
|
|
|
this.input
|
2018-11-18 11:00:15 +05:30
|
|
|
.on('keydown', function(e) {
|
2017-08-17 22:00:37 +05:30
|
|
|
var keyCode = e.which;
|
|
|
|
if (keyCode === 13 && !options.elIsInput) {
|
2016-09-13 17:45:13 +05:30
|
|
|
e.preventDefault();
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
})
|
2018-11-18 11:00:15 +05:30
|
|
|
.on(
|
|
|
|
'input',
|
|
|
|
function() {
|
|
|
|
if (this.input.val() !== '' && !$inputContainer.hasClass(HAS_VALUE_CLASS)) {
|
|
|
|
$inputContainer.addClass(HAS_VALUE_CLASS);
|
|
|
|
} else if (this.input.val() === '' && $inputContainer.hasClass(HAS_VALUE_CLASS)) {
|
|
|
|
$inputContainer.removeClass(HAS_VALUE_CLASS);
|
|
|
|
}
|
|
|
|
// Only filter asynchronously only if option remote is set
|
|
|
|
if (this.options.remote) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
return (timeout = setTimeout(
|
|
|
|
function() {
|
|
|
|
$inputContainer.parent().addClass('is-loading');
|
|
|
|
|
|
|
|
return this.options.query(
|
|
|
|
this.input.val(),
|
|
|
|
function(data) {
|
|
|
|
$inputContainer.parent().removeClass('is-loading');
|
|
|
|
return this.options.callback(data);
|
|
|
|
}.bind(this),
|
|
|
|
);
|
|
|
|
}.bind(this),
|
|
|
|
250,
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
return this.filter(this.input.val());
|
|
|
|
}
|
|
|
|
}.bind(this),
|
|
|
|
);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
GitLabDropdownFilter.prototype.shouldBlur = function(keyCode) {
|
|
|
|
return BLUR_KEYCODES.indexOf(keyCode) !== -1;
|
|
|
|
};
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
GitLabDropdownFilter.prototype.filter = function(search_text) {
|
|
|
|
var data, elements, group, key, results, tmp;
|
|
|
|
if (this.options.onFilter) {
|
|
|
|
this.options.onFilter(search_text);
|
|
|
|
}
|
|
|
|
data = this.options.data();
|
2018-11-18 11:00:15 +05:30
|
|
|
if (data != null && !this.options.filterByText) {
|
2017-08-17 22:00:37 +05:30
|
|
|
results = data;
|
|
|
|
if (search_text !== '') {
|
|
|
|
// When data is an array of objects therefore [object Array] e.g.
|
|
|
|
// [
|
|
|
|
// { prop: 'foo' },
|
|
|
|
// { prop: 'baz' }
|
|
|
|
// ]
|
|
|
|
if (_.isArray(data)) {
|
|
|
|
results = fuzzaldrinPlus.filter(data, search_text, {
|
2018-11-18 11:00:15 +05:30
|
|
|
key: this.options.keys,
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// If data is grouped therefore an [object Object]. e.g.
|
|
|
|
// {
|
|
|
|
// groupName1: [
|
|
|
|
// { prop: 'foo' },
|
|
|
|
// { prop: 'baz' }
|
|
|
|
// ],
|
|
|
|
// groupName2: [
|
|
|
|
// { prop: 'abc' },
|
|
|
|
// { prop: 'def' }
|
|
|
|
// ]
|
|
|
|
// }
|
2017-09-10 17:25:29 +05:30
|
|
|
if (isObject(data)) {
|
2017-08-17 22:00:37 +05:30
|
|
|
results = {};
|
|
|
|
for (key in data) {
|
|
|
|
group = data[key];
|
|
|
|
tmp = fuzzaldrinPlus.filter(group, search_text, {
|
2018-11-18 11:00:15 +05:30
|
|
|
key: this.options.keys,
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
if (tmp.length) {
|
|
|
|
results[key] = tmp.map(function(item) {
|
|
|
|
return item;
|
2016-09-13 17:45:13 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
return this.options.callback(results);
|
|
|
|
} else {
|
|
|
|
elements = this.options.elements();
|
|
|
|
if (search_text) {
|
2017-09-10 17:25:29 +05:30
|
|
|
elements.each(function() {
|
2017-08-17 22:00:37 +05:30
|
|
|
var $el, matches;
|
|
|
|
$el = $(this);
|
|
|
|
matches = fuzzaldrinPlus.match($el.text().trim(), search_text);
|
|
|
|
if (!$el.is('.dropdown-header')) {
|
|
|
|
if (matches.length) {
|
|
|
|
return $el.show().removeClass('option-hidden');
|
|
|
|
} else {
|
|
|
|
return $el.hide().addClass('option-hidden');
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2017-09-10 17:25:29 +05:30
|
|
|
elements.show().removeClass('option-hidden');
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
elements
|
|
|
|
.parent()
|
|
|
|
.find('.dropdown-menu-empty-item')
|
|
|
|
.toggleClass('hidden', elements.is(':visible'));
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
};
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
return GitLabDropdownFilter;
|
|
|
|
})();
|
|
|
|
|
|
|
|
GitLabDropdownRemote = (function() {
|
|
|
|
function GitLabDropdownRemote(dataEndpoint, options) {
|
|
|
|
this.dataEndpoint = dataEndpoint;
|
|
|
|
this.options = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
GitLabDropdownRemote.prototype.execute = function() {
|
2018-11-18 11:00:15 +05:30
|
|
|
if (typeof this.dataEndpoint === 'string') {
|
2017-08-17 22:00:37 +05:30
|
|
|
return this.fetchData();
|
2018-11-18 11:00:15 +05:30
|
|
|
} else if (typeof this.dataEndpoint === 'function') {
|
2017-08-17 22:00:37 +05:30
|
|
|
if (this.options.beforeSend) {
|
|
|
|
this.options.beforeSend();
|
|
|
|
}
|
2018-11-18 11:00:15 +05:30
|
|
|
return this.dataEndpoint(
|
|
|
|
'',
|
|
|
|
(function(_this) {
|
|
|
|
// Fetch the data by calling the data funcfion
|
|
|
|
return function(data) {
|
|
|
|
if (_this.options.success) {
|
|
|
|
_this.options.success(data);
|
|
|
|
}
|
|
|
|
if (_this.options.beforeSend) {
|
|
|
|
return _this.options.beforeSend();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})(this),
|
|
|
|
);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
};
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
GitLabDropdownRemote.prototype.fetchData = function() {
|
2018-03-17 18:26:18 +05:30
|
|
|
if (this.options.beforeSend) {
|
|
|
|
this.options.beforeSend();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the data through ajax if the data is a string
|
2018-11-18 11:00:15 +05:30
|
|
|
return axios.get(this.dataEndpoint).then(({ data }) => {
|
|
|
|
if (this.options.success) {
|
|
|
|
return this.options.success(data);
|
|
|
|
}
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
};
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
return GitLabDropdownRemote;
|
|
|
|
})();
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
GitLabDropdown = (function() {
|
2018-11-18 11:00:15 +05:30
|
|
|
var ACTIVE_CLASS,
|
|
|
|
FILTER_INPUT,
|
|
|
|
NO_FILTER_INPUT,
|
|
|
|
INDETERMINATE_CLASS,
|
|
|
|
LOADING_CLASS,
|
|
|
|
PAGE_TWO_CLASS,
|
|
|
|
NON_SELECTABLE_CLASSES,
|
|
|
|
SELECTABLE_CLASSES,
|
|
|
|
CURSOR_SELECT_SCROLL_PADDING,
|
|
|
|
currentIndex;
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
LOADING_CLASS = 'is-loading';
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
PAGE_TWO_CLASS = 'is-page-two';
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
ACTIVE_CLASS = 'is-active';
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
INDETERMINATE_CLASS = 'is-indeterminate';
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
currentIndex = -1;
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
NON_SELECTABLE_CLASSES = '.divider, .separator, .dropdown-header, .dropdown-menu-empty-item';
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
SELECTABLE_CLASSES = '.dropdown-content li:not(' + NON_SELECTABLE_CLASSES + ', .option-hidden)';
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
CURSOR_SELECT_SCROLL_PADDING = 5;
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
FILTER_INPUT = '.dropdown-input .dropdown-input-field:not(.dropdown-no-filter)';
|
|
|
|
|
|
|
|
NO_FILTER_INPUT = '.dropdown-input .dropdown-input-field.dropdown-no-filter';
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
function GitLabDropdown(el1, options) {
|
|
|
|
var searchFields, selector, self;
|
|
|
|
this.el = el1;
|
|
|
|
this.options = options;
|
2017-09-10 17:25:29 +05:30
|
|
|
this.updateLabel = this.updateLabel.bind(this);
|
|
|
|
this.hidden = this.hidden.bind(this);
|
|
|
|
this.opened = this.opened.bind(this);
|
|
|
|
this.shouldPropagate = this.shouldPropagate.bind(this);
|
2017-08-17 22:00:37 +05:30
|
|
|
self = this;
|
2018-11-18 11:00:15 +05:30
|
|
|
selector = $(this.el).data('target');
|
2017-08-17 22:00:37 +05:30
|
|
|
this.dropdown = selector != null ? $(selector) : $(this.el).parent();
|
|
|
|
// Set Defaults
|
|
|
|
this.filterInput = this.options.filterInput || this.getElement(FILTER_INPUT);
|
2017-09-10 17:25:29 +05:30
|
|
|
this.noFilterInput = this.options.noFilterInput || this.getElement(NO_FILTER_INPUT);
|
2019-09-04 21:01:54 +05:30
|
|
|
this.highlight = Boolean(this.options.highlight);
|
|
|
|
this.icon = Boolean(this.options.icon);
|
2018-11-18 11:00:15 +05:30
|
|
|
this.filterInputBlur =
|
|
|
|
this.options.filterInputBlur != null ? this.options.filterInputBlur : true;
|
2017-08-17 22:00:37 +05:30
|
|
|
// If no input is passed create a default one
|
|
|
|
self = this;
|
|
|
|
// If selector was passed
|
|
|
|
if (_.isString(this.filterInput)) {
|
|
|
|
this.filterInput = this.getElement(this.filterInput);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
if (_.isObject(this.options.data) && !_.isFunction(this.options.data)) {
|
|
|
|
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: (function(_this) {
|
2016-09-13 17:45:13 +05:30
|
|
|
return function(data) {
|
2017-08-17 22:00:37 +05:30
|
|
|
_this.fullData = data;
|
|
|
|
_this.parseData(_this.fullData);
|
2018-03-17 18:26:18 +05:30
|
|
|
_this.focusTextInput();
|
2019-09-04 21:01:54 +05:30
|
|
|
|
|
|
|
// Update dropdown position since remote data may have changed dropdown size
|
|
|
|
_this.dropdown.find('.dropdown-menu-toggle').dropdown('update');
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
if (
|
|
|
|
_this.options.filterable &&
|
|
|
|
_this.filter &&
|
|
|
|
_this.filter.input &&
|
|
|
|
_this.filter.input.val() &&
|
|
|
|
_this.filter.input.val().trim() !== ''
|
|
|
|
) {
|
2017-08-17 22:00:37 +05:30
|
|
|
return _this.filter.input.trigger('input');
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
|
|
|
};
|
2018-11-18 11:00:15 +05:30
|
|
|
// Remote data
|
2017-08-17 22:00:37 +05:30
|
|
|
})(this),
|
|
|
|
instance: this,
|
2016-09-13 17:45:13 +05:30
|
|
|
});
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2017-09-10 17:25:29 +05:30
|
|
|
if (this.noFilterInput.length) {
|
|
|
|
this.plainInput = new GitLabDropdownInput(this.noFilterInput, this.options);
|
|
|
|
this.plainInput.onInput(this.addInput.bind(this));
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
// 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: (function(_this) {
|
|
|
|
return function() {
|
|
|
|
selector = '.dropdown-content li:not(' + NON_SELECTABLE_CLASSES + ')';
|
|
|
|
if (_this.dropdown.find('.dropdown-toggle-page').length) {
|
2018-11-18 11:00:15 +05:30
|
|
|
selector = '.dropdown-page-one ' + selector;
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2018-03-17 18:26:18 +05:30
|
|
|
return $(selector, this.instance.dropdown);
|
2017-08-17 22:00:37 +05:30
|
|
|
};
|
|
|
|
})(this),
|
|
|
|
data: (function(_this) {
|
|
|
|
return function() {
|
|
|
|
return _this.fullData;
|
|
|
|
};
|
|
|
|
})(this),
|
|
|
|
callback: (function(_this) {
|
|
|
|
return function(data) {
|
|
|
|
_this.parseData(data);
|
|
|
|
if (_this.filterInput.val() !== '') {
|
|
|
|
selector = SELECTABLE_CLASSES;
|
|
|
|
if (_this.dropdown.find('.dropdown-toggle-page').length) {
|
2018-11-18 11:00:15 +05:30
|
|
|
selector = '.dropdown-page-one ' + selector;
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
if ($(_this.el).is('input')) {
|
|
|
|
currentIndex = -1;
|
|
|
|
} else {
|
2018-11-18 11:00:15 +05:30
|
|
|
$(selector, _this.dropdown)
|
|
|
|
.first()
|
|
|
|
.find('a')
|
|
|
|
.addClass('is-focused');
|
2017-08-17 22:00:37 +05:30
|
|
|
currentIndex = 0;
|
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
};
|
2018-11-18 11:00:15 +05:30
|
|
|
})(this),
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
// Event listeners
|
2018-11-18 11:00:15 +05:30
|
|
|
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',
|
|
|
|
(function(_this) {
|
|
|
|
return function(e) {
|
|
|
|
// Escape key
|
|
|
|
if (e.which === 27) {
|
|
|
|
return $('.dropdown-menu-close', _this.dropdown).trigger('click');
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2018-11-18 11:00:15 +05:30
|
|
|
};
|
|
|
|
})(this),
|
|
|
|
);
|
|
|
|
this.dropdown.on(
|
|
|
|
'blur',
|
|
|
|
'a',
|
|
|
|
(function(_this) {
|
2017-08-17 22:00:37 +05:30
|
|
|
return function(e) {
|
2018-11-18 11:00:15 +05:30
|
|
|
var $dropdownMenu, $relatedTarget;
|
|
|
|
if (e.relatedTarget != null) {
|
|
|
|
$relatedTarget = $(e.relatedTarget);
|
|
|
|
$dropdownMenu = $relatedTarget.closest('.dropdown-menu');
|
|
|
|
if ($dropdownMenu.length === 0) {
|
|
|
|
return _this.dropdown.removeClass('show');
|
|
|
|
}
|
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
};
|
2018-11-18 11:00:15 +05:30
|
|
|
})(this),
|
|
|
|
);
|
|
|
|
if (this.dropdown.find('.dropdown-toggle-page').length) {
|
|
|
|
this.dropdown.find('.dropdown-toggle-page, .dropdown-menu-back').on(
|
|
|
|
'click',
|
|
|
|
(function(_this) {
|
|
|
|
return function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return _this.togglePage();
|
|
|
|
};
|
|
|
|
})(this),
|
|
|
|
);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
if (this.options.selectable) {
|
2018-11-18 11:00:15 +05:30
|
|
|
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
|
|
|
}
|
2018-11-18 11:00:15 +05:30
|
|
|
this.dropdown.on(
|
|
|
|
'click',
|
|
|
|
selector,
|
|
|
|
function(e) {
|
|
|
|
var $el, selected, selectedObj, isMarking;
|
|
|
|
$el = $(e.currentTarget);
|
|
|
|
selected = self.rowClicked($el);
|
|
|
|
selectedObj = selected ? selected[0] : null;
|
|
|
|
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
|
|
|
|
2018-11-18 11:00:15 +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
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
$el.trigger('blur');
|
|
|
|
}.bind(this),
|
|
|
|
);
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
// Finds an element inside wrapper element
|
|
|
|
GitLabDropdown.prototype.getElement = function(selector) {
|
|
|
|
return this.dropdown.find(selector);
|
|
|
|
};
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
GitLabDropdown.prototype.toggleLoading = function() {
|
|
|
|
return $('.dropdown-menu', this.dropdown).toggleClass(LOADING_CLASS);
|
|
|
|
};
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
GitLabDropdown.prototype.togglePage = function() {
|
|
|
|
var menu;
|
|
|
|
menu = $('.dropdown-menu', this.dropdown);
|
|
|
|
if (menu.hasClass(PAGE_TWO_CLASS)) {
|
|
|
|
if (this.remote) {
|
|
|
|
this.remote.execute();
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +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();
|
|
|
|
};
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
GitLabDropdown.prototype.parseData = function(data) {
|
|
|
|
var full_html, groupData, html, name;
|
|
|
|
this.renderedData = data;
|
|
|
|
if (this.options.filterable && data.length === 0) {
|
|
|
|
// render no matching results
|
|
|
|
html = [this.noResults()];
|
|
|
|
} else {
|
|
|
|
// Handle array groups
|
2017-09-10 17:25:29 +05:30
|
|
|
if (isObject(data)) {
|
2017-08-17 22:00:37 +05:30
|
|
|
html = [];
|
|
|
|
for (name in data) {
|
|
|
|
groupData = data[name];
|
2018-11-18 11:00:15 +05:30
|
|
|
html.push(
|
|
|
|
this.renderItem(
|
|
|
|
{
|
|
|
|
header: name,
|
|
|
|
// Add header for each group
|
|
|
|
},
|
|
|
|
name,
|
|
|
|
),
|
|
|
|
);
|
2017-08-17 22:00:37 +05:30
|
|
|
this.renderData(groupData, name).map(function(item) {
|
|
|
|
return html.push(item);
|
|
|
|
});
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
} else {
|
|
|
|
// Render each row
|
|
|
|
html = this.renderData(data);
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
// Render the full menu
|
|
|
|
full_html = this.renderMenu(html);
|
|
|
|
return this.appendMenu(full_html);
|
|
|
|
};
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
GitLabDropdown.prototype.renderData = function(data, group) {
|
|
|
|
if (group == null) {
|
|
|
|
group = false;
|
|
|
|
}
|
2018-11-18 11:00:15 +05:30
|
|
|
return data.map(
|
|
|
|
(function(_this) {
|
|
|
|
return function(obj, index) {
|
|
|
|
return _this.renderItem(obj, group, index);
|
|
|
|
};
|
|
|
|
})(this),
|
|
|
|
);
|
2017-08-17 22:00:37 +05:30
|
|
|
};
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
GitLabDropdown.prototype.shouldPropagate = function(e) {
|
|
|
|
var $target;
|
2018-03-17 18:26:18 +05:30
|
|
|
if (this.options.multiSelect || this.options.shouldPropagate === false) {
|
2017-08-17 22:00:37 +05:30
|
|
|
$target = $(e.target);
|
2018-11-18 11:00:15 +05:30
|
|
|
if (
|
|
|
|
$target &&
|
|
|
|
!$target.hasClass('dropdown-menu-close') &&
|
|
|
|
!$target.hasClass('dropdown-menu-close-icon') &&
|
|
|
|
!$target.data('isLink')
|
|
|
|
) {
|
2017-08-17 22:00:37 +05:30
|
|
|
e.stopPropagation();
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
// This prevents automatic scrolling to the top
|
|
|
|
if ($target.closest('a').length) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
return true;
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
};
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
GitLabDropdown.prototype.filteredFullData = function() {
|
2018-11-18 11:00:15 +05:30
|
|
|
return this.fullData.filter(
|
|
|
|
r =>
|
|
|
|
typeof r === 'object' &&
|
|
|
|
!Object.prototype.hasOwnProperty.call(r, 'beforeDivider') &&
|
|
|
|
!Object.prototype.hasOwnProperty.call(r, 'header'),
|
2017-08-17 22:00:37 +05:30
|
|
|
);
|
|
|
|
};
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
GitLabDropdown.prototype.opened = function(e) {
|
|
|
|
var contentHtml;
|
|
|
|
this.resetRows();
|
|
|
|
this.addArrowKeyEvent();
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
const dropdownToggle = this.dropdown.find('.dropdown-menu-toggle');
|
|
|
|
const hasFilterBulkUpdate = dropdownToggle.hasClass('js-filter-bulk-update');
|
2018-03-17 18:26:18 +05:30
|
|
|
const shouldRefreshOnOpen = dropdownToggle.hasClass('js-gl-dropdown-refresh-on-open');
|
2017-08-17 22:00:37 +05:30
|
|
|
const hasMultiSelect = dropdownToggle.hasClass('js-multiselect');
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
// Makes indeterminate items effective
|
2018-03-17 18:26:18 +05:30
|
|
|
if (this.fullData && (shouldRefreshOnOpen || hasFilterBulkUpdate)) {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.parseData(this.fullData);
|
|
|
|
}
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-08-17 22:00:37 +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) {
|
2018-11-18 11:00:15 +05:30
|
|
|
this.options.processData.call(
|
|
|
|
this.options,
|
|
|
|
inputValue,
|
|
|
|
this.filteredFullData(),
|
|
|
|
this.parseData.bind(this),
|
|
|
|
);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
contentHtml = $('.dropdown-content', this.dropdown).html();
|
2018-11-18 11:00:15 +05:30
|
|
|
if (this.remote && contentHtml === '') {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.remote.execute();
|
|
|
|
} else {
|
|
|
|
this.focusTextInput();
|
|
|
|
}
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
if (this.options.showMenuAbove) {
|
|
|
|
this.positionMenuAbove();
|
|
|
|
}
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
if (this.options.opened) {
|
2018-11-18 11:00:15 +05:30
|
|
|
if (this.options.preserveContext) {
|
|
|
|
this.options.opened(e);
|
|
|
|
} else {
|
|
|
|
this.options.opened.call(this, e);
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
return this.dropdown.trigger('shown.gl.dropdown');
|
|
|
|
};
|
|
|
|
|
|
|
|
GitLabDropdown.prototype.positionMenuAbove = function() {
|
|
|
|
var $menu = this.dropdown.find('.dropdown-menu');
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
$menu.addClass('dropdown-open-top');
|
|
|
|
$menu.css('top', 'initial');
|
|
|
|
$menu.css('bottom', '100%');
|
2017-08-17 22:00:37 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
GitLabDropdown.prototype.hidden = function(e) {
|
|
|
|
var $input;
|
|
|
|
this.resetRows();
|
|
|
|
this.removeArrayKeyEvent();
|
2018-11-18 11:00:15 +05:30
|
|
|
$input = this.dropdown.find('.dropdown-input-field');
|
2017-08-17 22:00:37 +05:30
|
|
|
if (this.options.filterable) {
|
|
|
|
$input.blur();
|
|
|
|
}
|
2018-11-18 11:00:15 +05:30
|
|
|
if (this.dropdown.find('.dropdown-toggle-page').length) {
|
2017-08-17 22:00:37 +05:30
|
|
|
$('.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');
|
|
|
|
};
|
|
|
|
|
|
|
|
// Render the full menu
|
|
|
|
GitLabDropdown.prototype.renderMenu = function(html) {
|
|
|
|
if (this.options.renderMenu) {
|
|
|
|
return this.options.renderMenu(html);
|
|
|
|
} else {
|
2019-07-07 11:18:12 +05:30
|
|
|
return $('<ul>').append(html);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Append the menu into the dropdown
|
|
|
|
GitLabDropdown.prototype.appendMenu = function(html) {
|
|
|
|
return this.clearMenu().append(html);
|
|
|
|
};
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
GitLabDropdown.prototype.clearMenu = function() {
|
|
|
|
var selector;
|
|
|
|
selector = '.dropdown-content';
|
2018-11-18 11:00:15 +05:30
|
|
|
if (this.dropdown.find('.dropdown-toggle-page').length) {
|
2018-11-08 19:23:39 +05:30
|
|
|
if (this.options.containerSelector) {
|
|
|
|
selector = this.options.containerSelector;
|
|
|
|
} else {
|
|
|
|
selector = '.dropdown-page-one .dropdown-content';
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return $(selector, this.dropdown).empty();
|
|
|
|
};
|
|
|
|
|
|
|
|
GitLabDropdown.prototype.renderItem = function(data, group, index) {
|
2018-11-08 19:23:39 +05:30
|
|
|
var field, html, selected, text, url, value, rowHidden;
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
if (!this.options.renderRow) {
|
|
|
|
value = this.options.id ? this.options.id(data) : data.id;
|
|
|
|
|
|
|
|
if (value) {
|
2018-11-18 11:00:15 +05:30
|
|
|
value = value.toString().replace(/'/g, "\\'");
|
2018-03-27 19:54:05 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hide element
|
|
|
|
if (this.options.hideRow && this.options.hideRow(value)) {
|
|
|
|
rowHidden = true;
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
if (group == null) {
|
|
|
|
group = false;
|
|
|
|
}
|
|
|
|
if (index == null) {
|
|
|
|
// Render the row
|
|
|
|
index = false;
|
|
|
|
}
|
|
|
|
html = document.createElement('li');
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
if (rowHidden) {
|
|
|
|
html.style.display = 'none';
|
|
|
|
}
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
if (data === 'divider' || data === 'separator') {
|
|
|
|
html.className = data;
|
|
|
|
return html;
|
|
|
|
}
|
|
|
|
// Header
|
|
|
|
if (data.header != null) {
|
|
|
|
html.className = 'dropdown-header';
|
|
|
|
html.innerHTML = data.header;
|
|
|
|
return html;
|
|
|
|
}
|
|
|
|
if (this.options.renderRow) {
|
|
|
|
// Call the render function
|
|
|
|
html = this.options.renderRow.call(this.options, data, this);
|
|
|
|
} else {
|
|
|
|
if (!selected) {
|
2018-11-08 19:23:39 +05:30
|
|
|
const { fieldName } = this.options;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
if (value) {
|
|
|
|
field = this.dropdown.parent().find(`input[name='${fieldName}'][value='${value}']`);
|
|
|
|
if (field.length) {
|
|
|
|
selected = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
field = this.dropdown.parent().find(`input[name='${fieldName}']`);
|
|
|
|
selected = !field.length;
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
// Set URL
|
|
|
|
if (this.options.url != null) {
|
|
|
|
url = this.options.url(data);
|
|
|
|
} else {
|
|
|
|
url = data.url != null ? data.url : '#';
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
// Set Text
|
|
|
|
if (this.options.text != null) {
|
|
|
|
text = this.options.text(data);
|
|
|
|
} else {
|
|
|
|
text = data.text != null ? data.text : '';
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
if (this.highlight) {
|
2018-11-18 11:00:15 +05:30
|
|
|
text = data.template
|
|
|
|
? this.highlightTemplate(text, data.template)
|
|
|
|
: this.highlightTextMatches(text, this.filterInput.val());
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
// Create the list item & the link
|
|
|
|
var link = document.createElement('a');
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
link.href = url;
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
if (this.icon) {
|
|
|
|
text = `<span>${text}</span>`;
|
|
|
|
link.classList.add('d-flex', 'align-items-center');
|
|
|
|
link.innerHTML = data.icon ? data.icon + text : text;
|
|
|
|
} else if (this.highlight) {
|
2016-11-03 12:29:30 +05:30
|
|
|
link.innerHTML = text;
|
2017-08-17 22:00:37 +05:30
|
|
|
} else {
|
|
|
|
link.textContent = text;
|
|
|
|
}
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
if (selected) {
|
2018-11-18 11:00:15 +05:30
|
|
|
link.classList.add('is-active');
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
if (group) {
|
|
|
|
link.dataset.group = group;
|
|
|
|
link.dataset.index = index;
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
html.appendChild(link);
|
|
|
|
}
|
|
|
|
return html;
|
|
|
|
};
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
GitLabDropdown.prototype.highlightTemplate = function(text, template) {
|
|
|
|
return `"<b>${_.escape(text)}</b>" ${template}`;
|
|
|
|
};
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
GitLabDropdown.prototype.highlightTextMatches = function(text, term) {
|
2017-09-10 17:25:29 +05:30
|
|
|
const occurrences = fuzzaldrinPlus.match(text, term);
|
2018-11-08 19:23:39 +05:30
|
|
|
const { indexOf } = [];
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
return text
|
|
|
|
.split('')
|
|
|
|
.map(function(character, i) {
|
|
|
|
if (indexOf.call(occurrences, i) !== -1) {
|
|
|
|
return '<b>' + character + '</b>';
|
|
|
|
} else {
|
|
|
|
return character;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.join('');
|
2017-08-17 22:00:37 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
GitLabDropdown.prototype.noResults = function() {
|
|
|
|
var html;
|
2018-03-17 18:26:18 +05:30
|
|
|
return '<li class="dropdown-menu-empty-item"><a>No matching results</a></li>';
|
2017-08-17 22:00:37 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
GitLabDropdown.prototype.rowClicked = function(el) {
|
2018-11-08 19:23:39 +05:30
|
|
|
var field, groupName, isInput, selectedIndex, selectedObject, value, isMarking;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
const { fieldName } = this.options;
|
2017-08-17 22:00:37 +05:30
|
|
|
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();
|
|
|
|
selectedObject = this.renderedData[selectedIndex];
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (this.options.vue) {
|
2016-09-13 17:45:13 +05:30
|
|
|
if (el.hasClass(ACTIVE_CLASS)) {
|
|
|
|
el.removeClass(ACTIVE_CLASS);
|
|
|
|
} else {
|
|
|
|
el.addClass(ACTIVE_CLASS);
|
|
|
|
}
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
return [selectedObject];
|
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
field = [];
|
2018-11-18 11:00:15 +05:30
|
|
|
value = this.options.id ? this.options.id(selectedObject, el) : selectedObject.id;
|
2017-08-17 22:00:37 +05:30
|
|
|
if (isInput) {
|
|
|
|
field = $(this.el);
|
2018-03-17 18:26:18 +05:30
|
|
|
} else if (value != null) {
|
2018-11-18 11:00:15 +05:30
|
|
|
field = this.dropdown
|
|
|
|
.parent()
|
|
|
|
.find(
|
|
|
|
"input[name='" + fieldName + "'][value='" + value.toString().replace(/'/g, "\\'") + "']",
|
|
|
|
);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (this.options.isSelectable && !this.options.isSelectable(selectedObject, el)) {
|
2018-05-09 12:01:36 +05:30
|
|
|
return [selectedObject];
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
if (el.hasClass(ACTIVE_CLASS) && value !== 0) {
|
2017-08-17 22:00:37 +05:30
|
|
|
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);
|
|
|
|
}
|
|
|
|
if ((!field || !field.length) && fieldName) {
|
|
|
|
this.addInput(fieldName, value, selectedObject);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
isMarking = true;
|
|
|
|
if (!this.options.multiSelect || el.hasClass('dropdown-clear-active')) {
|
2018-11-18 11:00:15 +05:30
|
|
|
this.dropdown.find('.' + ACTIVE_CLASS).removeClass(ACTIVE_CLASS);
|
2017-08-17 22:00:37 +05:30
|
|
|
if (!isInput) {
|
2018-11-18 11:00:15 +05:30
|
|
|
this.dropdown
|
|
|
|
.parent()
|
|
|
|
.find("input[name='" + fieldName + "']")
|
|
|
|
.remove();
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
if (field && field.length && value == null) {
|
|
|
|
this.clearField(field, isInput);
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
// 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');
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
return [selectedObject, isMarking];
|
|
|
|
};
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
GitLabDropdown.prototype.focusTextInput = function() {
|
2017-09-10 17:25:29 +05:30
|
|
|
if (this.options.filterable) {
|
2018-03-17 18:26:18 +05:30
|
|
|
const initialScrollTop = $(window).scrollTop();
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
if (this.dropdown.is('.show') && !this.filterInput.is(':focus')) {
|
2018-03-17 18:26:18 +05:30
|
|
|
this.filterInput.focus();
|
|
|
|
}
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
if ($(window).scrollTop() < initialScrollTop) {
|
|
|
|
$(window).scrollTop(initialScrollTop);
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
};
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
GitLabDropdown.prototype.addInput = function(fieldName, value, selectedObject, single) {
|
2017-08-17 22:00:37 +05:30
|
|
|
var $input;
|
|
|
|
// Create hidden input for form
|
2017-09-10 17:25:29 +05:30
|
|
|
if (single) {
|
|
|
|
$('input[name="' + fieldName + '"]').remove();
|
|
|
|
}
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
$input = $('<input>')
|
|
|
|
.attr('type', 'hidden')
|
|
|
|
.attr('name', fieldName)
|
|
|
|
.val(value);
|
2017-08-17 22:00:37 +05:30
|
|
|
if (this.options.inputId != null) {
|
|
|
|
$input.attr('id', this.options.inputId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.options.multiSelect) {
|
2018-11-18 11:00:15 +05:30
|
|
|
Object.keys(selectedObject).forEach(attribute => {
|
2017-08-17 22:00:37 +05:30
|
|
|
$input.attr(`data-${attribute}`, selectedObject[attribute]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.options.inputMeta) {
|
|
|
|
$input.attr('data-meta', selectedObject[this.options.inputMeta]);
|
|
|
|
}
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
this.dropdown.before($input).trigger('change');
|
2017-08-17 22:00:37 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
GitLabDropdown.prototype.selectRowAtIndex = function(index) {
|
|
|
|
var $el, selector;
|
|
|
|
// If we pass an option index
|
2018-11-18 11:00:15 +05:30
|
|
|
if (typeof index !== 'undefined') {
|
|
|
|
selector = SELECTABLE_CLASSES + ':eq(' + index + ') a';
|
2017-08-17 22:00:37 +05:30
|
|
|
} else {
|
2018-11-18 11:00:15 +05:30
|
|
|
selector = '.dropdown-content .is-focused';
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2018-11-18 11:00:15 +05:30
|
|
|
if (this.dropdown.find('.dropdown-toggle-page').length) {
|
|
|
|
selector = '.dropdown-page-one ' + selector;
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
// simulate a click on the first link
|
|
|
|
$el = $(selector, this.dropdown);
|
|
|
|
if ($el.length) {
|
|
|
|
var href = $el.attr('href');
|
|
|
|
if (href && href !== '#') {
|
2018-03-17 18:26:18 +05:30
|
|
|
visitUrl(href);
|
2017-08-17 22:00:37 +05:30
|
|
|
} else {
|
2018-03-17 18:26:18 +05:30
|
|
|
$el.trigger('click');
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
GitLabDropdown.prototype.addArrowKeyEvent = function() {
|
|
|
|
var $input, ARROW_KEY_CODES, selector;
|
|
|
|
ARROW_KEY_CODES = [38, 40];
|
2018-11-18 11:00:15 +05:30
|
|
|
$input = this.dropdown.find('.dropdown-input-field');
|
2017-08-17 22:00:37 +05:30
|
|
|
selector = SELECTABLE_CLASSES;
|
2018-11-18 11:00:15 +05:30
|
|
|
if (this.dropdown.find('.dropdown-toggle-page').length) {
|
|
|
|
selector = '.dropdown-page-one ' + selector;
|
|
|
|
}
|
|
|
|
return $('body').on(
|
|
|
|
'keydown',
|
|
|
|
(function(_this) {
|
|
|
|
return function(e) {
|
|
|
|
var $listItems, PREV_INDEX, currentKeyCode;
|
|
|
|
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;
|
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2018-11-18 11:00:15 +05:30
|
|
|
if (currentIndex !== PREV_INDEX) {
|
|
|
|
_this.highlightRowAtIndex($listItems, currentIndex);
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2018-11-18 11:00:15 +05:30
|
|
|
return false;
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2018-11-18 11:00:15 +05:30
|
|
|
if (currentKeyCode === 13 && currentIndex !== -1) {
|
|
|
|
e.preventDefault();
|
|
|
|
_this.selectRowAtIndex();
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2018-11-18 11:00:15 +05:30
|
|
|
};
|
|
|
|
})(this),
|
|
|
|
);
|
2017-08-17 22:00:37 +05:30
|
|
|
};
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
GitLabDropdown.prototype.removeArrayKeyEvent = function() {
|
|
|
|
return $('body').off('keydown');
|
|
|
|
};
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
GitLabDropdown.prototype.resetRows = function resetRows() {
|
|
|
|
currentIndex = -1;
|
|
|
|
$('.is-focused', this.dropdown).removeClass('is-focused');
|
|
|
|
};
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
GitLabDropdown.prototype.highlightRowAtIndex = function($listItems, index) {
|
2018-11-18 11:00:15 +05:30
|
|
|
var $dropdownContent,
|
|
|
|
$listItem,
|
|
|
|
dropdownContentBottom,
|
|
|
|
dropdownContentHeight,
|
|
|
|
dropdownContentTop,
|
|
|
|
dropdownScrollTop,
|
|
|
|
listItemBottom,
|
|
|
|
listItemHeight,
|
|
|
|
listItemTop;
|
|
|
|
|
|
|
|
if (!$listItems) {
|
|
|
|
$listItems = $(SELECTABLE_CLASSES, this.dropdown);
|
|
|
|
}
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
// 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
|
|
|
|
$listItem = $listItems.eq(index);
|
2018-11-18 11:00:15 +05:30
|
|
|
$listItem.find('a:first-child').addClass('is-focused');
|
2017-08-17 22:00:37 +05:30
|
|
|
// Dropdown content scroll area
|
|
|
|
$dropdownContent = $listItem.closest('.dropdown-content');
|
|
|
|
dropdownScrollTop = $dropdownContent.scrollTop();
|
|
|
|
dropdownContentHeight = $dropdownContent.outerHeight();
|
|
|
|
dropdownContentTop = $dropdownContent.prop('offsetTop');
|
|
|
|
dropdownContentBottom = dropdownContentTop + dropdownContentHeight;
|
|
|
|
// Get the offset bottom of the list item
|
|
|
|
listItemHeight = $listItem.outerHeight();
|
|
|
|
listItemTop = $listItem.prop('offsetTop');
|
|
|
|
listItemBottom = listItemTop + listItemHeight;
|
|
|
|
if (!index) {
|
|
|
|
// Scroll the dropdown content to the top
|
|
|
|
$dropdownContent.scrollTop(0);
|
2018-11-18 11:00:15 +05:30
|
|
|
} else if (index === $listItems.length - 1) {
|
2017-08-17 22:00:37 +05:30
|
|
|
// Scroll the dropdown content to the bottom
|
|
|
|
$dropdownContent.scrollTop($dropdownContent.prop('scrollHeight'));
|
2018-11-18 11:00:15 +05:30
|
|
|
} else if (listItemBottom > dropdownContentBottom + dropdownScrollTop) {
|
2017-08-17 22:00:37 +05:30
|
|
|
// Scroll the dropdown content down
|
2018-11-18 11:00:15 +05:30
|
|
|
$dropdownContent.scrollTop(
|
|
|
|
listItemBottom - dropdownContentBottom + CURSOR_SELECT_SCROLL_PADDING,
|
|
|
|
);
|
|
|
|
} else if (listItemTop < dropdownContentTop + dropdownScrollTop) {
|
2017-08-17 22:00:37 +05:30
|
|
|
// Scroll the dropdown content up
|
2018-11-18 11:00:15 +05:30
|
|
|
return $dropdownContent.scrollTop(
|
|
|
|
listItemTop - dropdownContentTop - CURSOR_SELECT_SCROLL_PADDING,
|
|
|
|
);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
};
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
GitLabDropdown.prototype.updateLabel = function(selected, el, instance) {
|
|
|
|
if (selected == null) {
|
|
|
|
selected = null;
|
|
|
|
}
|
|
|
|
if (el == null) {
|
|
|
|
el = null;
|
|
|
|
}
|
|
|
|
if (instance == 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;
|
|
|
|
}
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
return $(this.el)
|
|
|
|
.find('.dropdown-toggle-text')
|
|
|
|
.text(toggleText);
|
2016-09-13 17:45:13 +05:30
|
|
|
};
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
GitLabDropdown.prototype.clearField = function(field, isInput) {
|
|
|
|
return isInput ? field.val('') : field.remove();
|
|
|
|
};
|
|
|
|
|
|
|
|
return GitLabDropdown;
|
|
|
|
})();
|
|
|
|
|
|
|
|
$.fn.glDropdown = function(opts) {
|
|
|
|
return this.each(function() {
|
|
|
|
if (!$.data(this, 'glDropdown')) {
|
|
|
|
return $.data(this, 'glDropdown', new GitLabDropdown(this, opts));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|