2018-10-09 11:25:53 -04:00
|
|
|
/* 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 */
|
2016-12-14 00:26:26 -05:00
|
|
|
/* global fuzzaldrinPlus */
|
2018-03-09 15:18:59 -05:00
|
|
|
|
|
|
|
import $ from 'jquery';
|
2017-08-03 16:31:53 -04:00
|
|
|
import _ from 'underscore';
|
2017-11-01 04:58:41 -04:00
|
|
|
import fuzzaldrinPlus from 'fuzzaldrin-plus';
|
2018-01-29 07:05:04 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2017-12-11 07:28:11 -05:00
|
|
|
import { visitUrl } from './lib/utils/url_utility';
|
2017-05-10 12:17:31 -04:00
|
|
|
import { isObject } from './lib/utils/type_utility';
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2017-07-28 19:33:56 -04:00
|
|
|
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-08-07 05:59:01 -04:00
|
|
|
$clearButton.on(
|
|
|
|
'click',
|
|
|
|
(function(_this) {
|
|
|
|
// Clear click
|
|
|
|
return function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return _this.input
|
|
|
|
.val('')
|
|
|
|
.trigger('input')
|
|
|
|
.focus();
|
|
|
|
};
|
|
|
|
})(this),
|
|
|
|
);
|
2017-07-28 19:33:56 -04:00
|
|
|
|
|
|
|
this.input
|
2018-08-07 05:59:01 -04:00
|
|
|
.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-07-28 19:33:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
GitLabDropdownInput.prototype.onInput = function(cb) {
|
|
|
|
this.cb = cb;
|
2017-08-01 09:41:24 -04:00
|
|
|
};
|
2017-07-28 19:33:56 -04:00
|
|
|
|
|
|
|
return GitLabDropdownInput;
|
|
|
|
})();
|
2017-03-11 02:30:44 -05:00
|
|
|
|
|
|
|
GitLabDropdownFilter = (function() {
|
|
|
|
var ARROW_KEY_CODES, BLUR_KEYCODES, HAS_VALUE_CLASS;
|
|
|
|
|
|
|
|
BLUR_KEYCODES = [27, 40];
|
|
|
|
|
|
|
|
ARROW_KEY_CODES = [38, 40];
|
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
HAS_VALUE_CLASS = 'has-value';
|
2017-03-11 02:30:44 -05:00
|
|
|
|
|
|
|
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-08-07 05:59:01 -04:00
|
|
|
$clearButton.on(
|
|
|
|
'click',
|
|
|
|
(function(_this) {
|
|
|
|
// Clear click
|
|
|
|
return function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return _this.input
|
|
|
|
.val('')
|
|
|
|
.trigger('input')
|
|
|
|
.focus();
|
|
|
|
};
|
|
|
|
})(this),
|
|
|
|
);
|
2017-03-11 02:30:44 -05:00
|
|
|
// Key events
|
2018-08-07 05:59:01 -04:00
|
|
|
timeout = '';
|
2017-03-11 02:30:44 -05:00
|
|
|
this.input
|
2018-08-07 05:59:01 -04:00
|
|
|
.on('keydown', function(e) {
|
2017-03-11 02:30:44 -05:00
|
|
|
var keyCode = e.which;
|
|
|
|
if (keyCode === 13 && !options.elIsInput) {
|
2016-07-24 16:45:11 -04:00
|
|
|
e.preventDefault();
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
})
|
2018-08-07 05:59:01 -04:00
|
|
|
.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-03-11 02:30:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GitLabDropdownFilter.prototype.shouldBlur = function(keyCode) {
|
|
|
|
return BLUR_KEYCODES.indexOf(keyCode) !== -1;
|
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
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-08-07 05:59:01 -04:00
|
|
|
if (data != null && !this.options.filterByText) {
|
2017-03-11 02:30:44 -05:00
|
|
|
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-08-07 05:59:01 -04:00
|
|
|
key: this.options.keys,
|
2017-03-11 02:30:44 -05:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// If data is grouped therefore an [object Object]. e.g.
|
|
|
|
// {
|
|
|
|
// groupName1: [
|
|
|
|
// { prop: 'foo' },
|
|
|
|
// { prop: 'baz' }
|
|
|
|
// ],
|
|
|
|
// groupName2: [
|
|
|
|
// { prop: 'abc' },
|
|
|
|
// { prop: 'def' }
|
|
|
|
// ]
|
|
|
|
// }
|
2017-05-10 12:17:31 -04:00
|
|
|
if (isObject(data)) {
|
2017-03-11 02:30:44 -05:00
|
|
|
results = {};
|
|
|
|
for (key in data) {
|
|
|
|
group = data[key];
|
|
|
|
tmp = fuzzaldrinPlus.filter(group, search_text, {
|
2018-08-07 05:59:01 -04:00
|
|
|
key: this.options.keys,
|
2017-03-11 02:30:44 -05:00
|
|
|
});
|
|
|
|
if (tmp.length) {
|
|
|
|
results[key] = tmp.map(function(item) {
|
|
|
|
return item;
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
return this.options.callback(results);
|
|
|
|
} else {
|
|
|
|
elements = this.options.elements();
|
|
|
|
if (search_text) {
|
2017-07-28 06:54:51 -04:00
|
|
|
elements.each(function() {
|
2017-03-11 02:30:44 -05:00
|
|
|
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-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2017-07-28 06:54:51 -04:00
|
|
|
elements.show().removeClass('option-hidden');
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-07-28 06:54:51 -04:00
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
elements
|
|
|
|
.parent()
|
|
|
|
.find('.dropdown-menu-empty-item')
|
|
|
|
.toggleClass('hidden', elements.is(':visible'));
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
return GitLabDropdownFilter;
|
|
|
|
})();
|
|
|
|
|
|
|
|
GitLabDropdownRemote = (function() {
|
|
|
|
function GitLabDropdownRemote(dataEndpoint, options) {
|
|
|
|
this.dataEndpoint = dataEndpoint;
|
|
|
|
this.options = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
GitLabDropdownRemote.prototype.execute = function() {
|
2018-08-07 05:59:01 -04:00
|
|
|
if (typeof this.dataEndpoint === 'string') {
|
2017-03-11 02:30:44 -05:00
|
|
|
return this.fetchData();
|
2018-08-07 05:59:01 -04:00
|
|
|
} else if (typeof this.dataEndpoint === 'function') {
|
2017-03-11 02:30:44 -05:00
|
|
|
if (this.options.beforeSend) {
|
|
|
|
this.options.beforeSend();
|
|
|
|
}
|
2018-08-07 05:59:01 -04:00
|
|
|
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),
|
|
|
|
);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GitLabDropdownRemote.prototype.fetchData = function() {
|
2018-01-29 07:05:04 -05:00
|
|
|
if (this.options.beforeSend) {
|
|
|
|
this.options.beforeSend();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the data through ajax if the data is a string
|
2018-08-07 05:59:01 -04:00
|
|
|
return axios.get(this.dataEndpoint).then(({ data }) => {
|
|
|
|
if (this.options.success) {
|
|
|
|
return this.options.success(data);
|
|
|
|
}
|
|
|
|
});
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
return GitLabDropdownRemote;
|
|
|
|
})();
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GitLabDropdown = (function() {
|
2018-08-07 05:59:01 -04:00
|
|
|
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-07-24 16:45:11 -04:00
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
LOADING_CLASS = 'is-loading';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
PAGE_TWO_CLASS = 'is-page-two';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
ACTIVE_CLASS = 'is-active';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
INDETERMINATE_CLASS = 'is-indeterminate';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
currentIndex = -1;
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-08-29 12:55:47 -04:00
|
|
|
NON_SELECTABLE_CLASSES = '.divider, .separator, .dropdown-header, .dropdown-menu-empty-item';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
SELECTABLE_CLASSES = '.dropdown-content li:not(' + NON_SELECTABLE_CLASSES + ', .option-hidden)';
|
2017-03-11 02:30:44 -05:00
|
|
|
|
|
|
|
CURSOR_SELECT_SCROLL_PADDING = 5;
|
|
|
|
|
2017-07-28 13:36:07 -04:00
|
|
|
FILTER_INPUT = '.dropdown-input .dropdown-input-field:not(.dropdown-no-filter)';
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-07-28 19:33:56 -04:00
|
|
|
NO_FILTER_INPUT = '.dropdown-input .dropdown-input-field.dropdown-no-filter';
|
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
function GitLabDropdown(el1, options) {
|
|
|
|
var searchFields, selector, self;
|
|
|
|
this.el = el1;
|
|
|
|
this.options = options;
|
2017-05-08 14:57:58 -04:00
|
|
|
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-03-11 02:30:44 -05:00
|
|
|
self = this;
|
2018-08-07 05:59:01 -04:00
|
|
|
selector = $(this.el).data('target');
|
2017-03-11 02:30:44 -05:00
|
|
|
this.dropdown = selector != null ? $(selector) : $(this.el).parent();
|
|
|
|
// Set Defaults
|
|
|
|
this.filterInput = this.options.filterInput || this.getElement(FILTER_INPUT);
|
2017-07-28 19:33:56 -04:00
|
|
|
this.noFilterInput = this.options.noFilterInput || this.getElement(NO_FILTER_INPUT);
|
2019-06-03 18:51:02 -04:00
|
|
|
this.highlight = Boolean(this.options.highlight);
|
|
|
|
this.icon = Boolean(this.options.icon);
|
2018-08-07 05:59:01 -04:00
|
|
|
this.filterInputBlur =
|
|
|
|
this.options.filterInputBlur != null ? this.options.filterInputBlur : true;
|
2017-03-11 02:30:44 -05:00
|
|
|
// 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-07-24 16:45:11 -04:00
|
|
|
return function(data) {
|
2017-03-11 02:30:44 -05:00
|
|
|
_this.fullData = data;
|
|
|
|
_this.parseData(_this.fullData);
|
2017-12-19 05:46:13 -05:00
|
|
|
_this.focusTextInput();
|
2019-05-20 22:50:59 -04:00
|
|
|
|
|
|
|
// Update dropdown position since remote data may have changed dropdown size
|
|
|
|
_this.dropdown.find('.dropdown-menu-toggle').dropdown('update');
|
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
if (
|
|
|
|
_this.options.filterable &&
|
|
|
|
_this.filter &&
|
|
|
|
_this.filter.input &&
|
|
|
|
_this.filter.input.val() &&
|
|
|
|
_this.filter.input.val().trim() !== ''
|
|
|
|
) {
|
2017-03-11 02:30:44 -05:00
|
|
|
return _this.filter.input.trigger('input');
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
};
|
2018-08-07 05:59:01 -04:00
|
|
|
// Remote data
|
2017-05-04 08:11:15 -04:00
|
|
|
})(this),
|
|
|
|
instance: this,
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2017-08-01 09:41:24 -04:00
|
|
|
if (this.noFilterInput.length) {
|
2017-07-28 19:33:56 -04:00
|
|
|
this.plainInput = new GitLabDropdownInput(this.noFilterInput, this.options);
|
2017-08-01 09:41:24 -04:00
|
|
|
this.plainInput.onInput(this.addInput.bind(this));
|
2017-07-28 19:33:56 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
// 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,
|
2017-05-04 08:11:15 -04:00
|
|
|
instance: this,
|
2017-03-11 02:30:44 -05:00
|
|
|
elements: (function(_this) {
|
|
|
|
return function() {
|
|
|
|
selector = '.dropdown-content li:not(' + NON_SELECTABLE_CLASSES + ')';
|
|
|
|
if (_this.dropdown.find('.dropdown-toggle-page').length) {
|
2018-08-07 05:59:01 -04:00
|
|
|
selector = '.dropdown-page-one ' + selector;
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2017-11-07 09:16:49 -05:00
|
|
|
return $(selector, this.instance.dropdown);
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
|
|
|
})(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-08-07 05:59:01 -04:00
|
|
|
selector = '.dropdown-page-one ' + selector;
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
if ($(_this.el).is('input')) {
|
|
|
|
currentIndex = -1;
|
|
|
|
} else {
|
2018-08-07 05:59:01 -04:00
|
|
|
$(selector, _this.dropdown)
|
|
|
|
.first()
|
|
|
|
.find('a')
|
|
|
|
.addClass('is-focused');
|
2017-03-11 02:30:44 -05:00
|
|
|
currentIndex = 0;
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
2018-08-07 05:59:01 -04:00
|
|
|
})(this),
|
2017-03-11 02:30:44 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
// Event listeners
|
2018-08-07 05:59:01 -04:00
|
|
|
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-07-24 16:45:11 -04:00
|
|
|
}
|
2018-08-07 05:59:01 -04:00
|
|
|
};
|
|
|
|
})(this),
|
|
|
|
);
|
|
|
|
this.dropdown.on(
|
|
|
|
'blur',
|
|
|
|
'a',
|
|
|
|
(function(_this) {
|
2017-03-11 02:30:44 -05:00
|
|
|
return function(e) {
|
2018-08-07 05:59:01 -04:00
|
|
|
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-07-24 16:45:11 -04:00
|
|
|
};
|
2018-08-07 05:59:01 -04:00
|
|
|
})(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-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
if (this.options.selectable) {
|
2018-08-07 05:59:01 -04:00
|
|
|
selector = '.dropdown-content a';
|
|
|
|
if (this.dropdown.find('.dropdown-toggle-page').length) {
|
|
|
|
selector = '.dropdown-page-one .dropdown-content a';
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2018-08-07 05:59:01 -04:00
|
|
|
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-06 14:57:03 -04:00
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
// Update label right after all modifications in dropdown has been done
|
|
|
|
if (this.options.toggleLabel) {
|
|
|
|
this.updateLabel(selectedObj, $el, this);
|
|
|
|
}
|
2016-09-06 14:57:03 -04:00
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
$el.trigger('blur');
|
|
|
|
}.bind(this),
|
|
|
|
);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
// Finds an element inside wrapper element
|
|
|
|
GitLabDropdown.prototype.getElement = function(selector) {
|
|
|
|
return this.dropdown.find(selector);
|
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GitLabDropdown.prototype.toggleLoading = function() {
|
|
|
|
return $('.dropdown-menu', this.dropdown).toggleClass(LOADING_CLASS);
|
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GitLabDropdown.prototype.togglePage = function() {
|
|
|
|
var menu;
|
|
|
|
menu = $('.dropdown-menu', this.dropdown);
|
|
|
|
if (menu.hasClass(PAGE_TWO_CLASS)) {
|
|
|
|
if (this.remote) {
|
|
|
|
this.remote.execute();
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
menu.toggleClass(PAGE_TWO_CLASS);
|
|
|
|
// Focus first visible input on active page
|
|
|
|
return this.dropdown.find('[class^="dropdown-page-"]:visible :text:visible:first').focus();
|
|
|
|
};
|
|
|
|
|
|
|
|
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-05-10 12:17:31 -04:00
|
|
|
if (isObject(data)) {
|
2017-03-11 02:30:44 -05:00
|
|
|
html = [];
|
|
|
|
for (name in data) {
|
|
|
|
groupData = data[name];
|
2018-08-07 05:59:01 -04:00
|
|
|
html.push(
|
|
|
|
this.renderItem(
|
|
|
|
{
|
|
|
|
header: name,
|
|
|
|
// Add header for each group
|
|
|
|
},
|
|
|
|
name,
|
|
|
|
),
|
|
|
|
);
|
2017-03-11 02:30:44 -05:00
|
|
|
this.renderData(groupData, name).map(function(item) {
|
|
|
|
return html.push(item);
|
|
|
|
});
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
} else {
|
|
|
|
// Render each row
|
|
|
|
html = this.renderData(data);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
// Render the full menu
|
|
|
|
full_html = this.renderMenu(html);
|
|
|
|
return this.appendMenu(full_html);
|
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GitLabDropdown.prototype.renderData = function(data, group) {
|
|
|
|
if (group == null) {
|
|
|
|
group = false;
|
|
|
|
}
|
2018-08-07 05:59:01 -04:00
|
|
|
return data.map(
|
|
|
|
(function(_this) {
|
|
|
|
return function(obj, index) {
|
|
|
|
return _this.renderItem(obj, group, index);
|
|
|
|
};
|
|
|
|
})(this),
|
|
|
|
);
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
2016-10-04 15:21:06 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GitLabDropdown.prototype.shouldPropagate = function(e) {
|
|
|
|
var $target;
|
2017-08-14 03:26:19 -04:00
|
|
|
if (this.options.multiSelect || this.options.shouldPropagate === false) {
|
2017-03-11 02:30:44 -05:00
|
|
|
$target = $(e.target);
|
2018-08-07 05:59:01 -04:00
|
|
|
if (
|
|
|
|
$target &&
|
|
|
|
!$target.hasClass('dropdown-menu-close') &&
|
|
|
|
!$target.hasClass('dropdown-menu-close-icon') &&
|
|
|
|
!$target.data('isLink')
|
|
|
|
) {
|
2017-03-11 02:30:44 -05:00
|
|
|
e.stopPropagation();
|
2019-06-05 07:04:33 -04:00
|
|
|
|
|
|
|
// This prevents automatic scrolling to the top
|
2019-06-11 11:51:57 -04:00
|
|
|
if ($target.closest('a').length) {
|
2019-06-05 07:04:33 -04:00
|
|
|
return false;
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2019-04-05 11:50:21 -04:00
|
|
|
|
|
|
|
return true;
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
};
|
2016-10-04 15:21:06 -04:00
|
|
|
|
2017-05-04 08:11:15 -04:00
|
|
|
GitLabDropdown.prototype.filteredFullData = function() {
|
2018-08-07 05:59:01 -04:00
|
|
|
return this.fullData.filter(
|
|
|
|
r =>
|
|
|
|
typeof r === 'object' &&
|
|
|
|
!Object.prototype.hasOwnProperty.call(r, 'beforeDivider') &&
|
|
|
|
!Object.prototype.hasOwnProperty.call(r, 'header'),
|
2017-05-04 08:11:15 -04:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GitLabDropdown.prototype.opened = function(e) {
|
|
|
|
var contentHtml;
|
|
|
|
this.resetRows();
|
|
|
|
this.addArrowKeyEvent();
|
2016-10-04 15:21:06 -04:00
|
|
|
|
2017-05-04 08:11:15 -04:00
|
|
|
const dropdownToggle = this.dropdown.find('.dropdown-menu-toggle');
|
|
|
|
const hasFilterBulkUpdate = dropdownToggle.hasClass('js-filter-bulk-update');
|
2017-12-03 21:43:10 -05:00
|
|
|
const shouldRefreshOnOpen = dropdownToggle.hasClass('js-gl-dropdown-refresh-on-open');
|
2017-05-04 08:11:15 -04:00
|
|
|
const hasMultiSelect = dropdownToggle.hasClass('js-multiselect');
|
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
// Makes indeterminate items effective
|
2017-12-03 21:43:10 -05:00
|
|
|
if (this.fullData && (shouldRefreshOnOpen || hasFilterBulkUpdate)) {
|
2017-03-11 02:30:44 -05:00
|
|
|
this.parseData(this.fullData);
|
|
|
|
}
|
2017-05-04 08:11:15 -04:00
|
|
|
|
|
|
|
// Process the data to make sure rendered data
|
|
|
|
// matches the correct layout
|
2017-05-26 17:27:30 -04:00
|
|
|
const inputValue = this.filterInput.val();
|
|
|
|
if (this.fullData && hasMultiSelect && this.options.processData && inputValue.length === 0) {
|
2018-08-07 05:59:01 -04:00
|
|
|
this.options.processData.call(
|
|
|
|
this.options,
|
|
|
|
inputValue,
|
|
|
|
this.filteredFullData(),
|
|
|
|
this.parseData.bind(this),
|
|
|
|
);
|
2017-05-04 08:11:15 -04:00
|
|
|
}
|
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
contentHtml = $('.dropdown-content', this.dropdown).html();
|
2018-08-07 05:59:01 -04:00
|
|
|
if (this.remote && contentHtml === '') {
|
2017-03-11 02:30:44 -05:00
|
|
|
this.remote.execute();
|
|
|
|
} else {
|
|
|
|
this.focusTextInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.options.showMenuAbove) {
|
|
|
|
this.positionMenuAbove();
|
|
|
|
}
|
2017-01-04 08:16:14 -05:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
if (this.options.opened) {
|
2018-08-07 15:29:53 -04:00
|
|
|
if (this.options.preserveContext) {
|
|
|
|
this.options.opened(e);
|
|
|
|
} else {
|
|
|
|
this.options.opened.call(this, e);
|
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
return this.dropdown.trigger('shown.gl.dropdown');
|
|
|
|
};
|
2016-10-04 15:21:06 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GitLabDropdown.prototype.positionMenuAbove = function() {
|
|
|
|
var $menu = this.dropdown.find('.dropdown-menu');
|
2016-10-04 15:21:06 -04:00
|
|
|
|
2017-09-27 14:07:49 -04:00
|
|
|
$menu.addClass('dropdown-open-top');
|
2017-08-14 03:26:19 -04:00
|
|
|
$menu.css('top', 'initial');
|
|
|
|
$menu.css('bottom', '100%');
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GitLabDropdown.prototype.hidden = function(e) {
|
|
|
|
var $input;
|
|
|
|
this.resetRows();
|
|
|
|
this.removeArrayKeyEvent();
|
2018-08-07 05:59:01 -04:00
|
|
|
$input = this.dropdown.find('.dropdown-input-field');
|
2017-03-11 02:30:44 -05:00
|
|
|
if (this.options.filterable) {
|
|
|
|
$input.blur();
|
|
|
|
}
|
2018-08-07 05:59:01 -04:00
|
|
|
if (this.dropdown.find('.dropdown-toggle-page').length) {
|
2017-03-11 02:30:44 -05:00
|
|
|
$('.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');
|
|
|
|
};
|
2016-09-06 06:55:26 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
// Render the full menu
|
|
|
|
GitLabDropdown.prototype.renderMenu = function(html) {
|
|
|
|
if (this.options.renderMenu) {
|
|
|
|
return this.options.renderMenu(html);
|
|
|
|
} else {
|
2019-02-14 23:17:03 -05:00
|
|
|
return $('<ul>').append(html);
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Append the menu into the dropdown
|
|
|
|
GitLabDropdown.prototype.appendMenu = function(html) {
|
|
|
|
return this.clearMenu().append(html);
|
|
|
|
};
|
2017-01-12 10:22:51 -05:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GitLabDropdown.prototype.clearMenu = function() {
|
|
|
|
var selector;
|
|
|
|
selector = '.dropdown-content';
|
2018-08-07 05:59:01 -04:00
|
|
|
if (this.dropdown.find('.dropdown-toggle-page').length) {
|
2018-06-07 16:54:24 -04:00
|
|
|
if (this.options.containerSelector) {
|
|
|
|
selector = this.options.containerSelector;
|
|
|
|
} else {
|
|
|
|
selector = '.dropdown-page-one .dropdown-content';
|
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2017-01-12 10:22:51 -05:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
return $(selector, this.dropdown).empty();
|
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GitLabDropdown.prototype.renderItem = function(data, group, index) {
|
2018-06-16 17:50:13 -04:00
|
|
|
var field, html, selected, text, url, value, rowHidden;
|
2018-02-21 10:17:03 -05:00
|
|
|
|
|
|
|
if (!this.options.renderRow) {
|
|
|
|
value = this.options.id ? this.options.id(data) : data.id;
|
|
|
|
|
|
|
|
if (value) {
|
2018-08-07 05:59:01 -04:00
|
|
|
value = value.toString().replace(/'/g, "\\'");
|
2018-02-21 10:17:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hide element
|
|
|
|
if (this.options.hideRow && this.options.hideRow(value)) {
|
|
|
|
rowHidden = true;
|
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
if (group == null) {
|
|
|
|
group = false;
|
|
|
|
}
|
|
|
|
if (index == null) {
|
|
|
|
// Render the row
|
|
|
|
index = false;
|
|
|
|
}
|
|
|
|
html = document.createElement('li');
|
2018-02-21 10:17:03 -05:00
|
|
|
|
2019-05-28 04:15:23 -04:00
|
|
|
if (rowHidden) {
|
|
|
|
html.style.display = 'none';
|
|
|
|
}
|
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
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-06-16 17:50:13 -04:00
|
|
|
const { fieldName } = this.options;
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
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-03-11 02:30:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
// Set URL
|
|
|
|
if (this.options.url != null) {
|
|
|
|
url = this.options.url(data);
|
|
|
|
} else {
|
|
|
|
url = data.url != null ? data.url : '#';
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
// Set Text
|
|
|
|
if (this.options.text != null) {
|
|
|
|
text = this.options.text(data);
|
|
|
|
} else {
|
|
|
|
text = data.text != null ? data.text : '';
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
if (this.highlight) {
|
2018-08-07 05:59:01 -04:00
|
|
|
text = data.template
|
|
|
|
? this.highlightTemplate(text, data.template)
|
|
|
|
: this.highlightTextMatches(text, this.filterInput.val());
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
// Create the list item & the link
|
|
|
|
var link = document.createElement('a');
|
2016-09-06 06:55:26 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
link.href = url;
|
2017-05-09 02:57:13 -04:00
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
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) {
|
2017-05-09 02:57:13 -04:00
|
|
|
link.innerHTML = text;
|
|
|
|
} else {
|
|
|
|
link.textContent = text;
|
|
|
|
}
|
2016-09-06 06:55:26 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
if (selected) {
|
2018-08-07 05:59:01 -04:00
|
|
|
link.classList.add('is-active');
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
|
|
|
|
if (group) {
|
|
|
|
link.dataset.group = group;
|
|
|
|
link.dataset.index = index;
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2016-10-06 04:03:10 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
html.appendChild(link);
|
|
|
|
}
|
|
|
|
return html;
|
|
|
|
};
|
2016-10-06 04:03:10 -04:00
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
GitLabDropdown.prototype.highlightTemplate = function(text, template) {
|
|
|
|
return `"<b>${_.escape(text)}</b>" ${template}`;
|
|
|
|
};
|
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GitLabDropdown.prototype.highlightTextMatches = function(text, term) {
|
2017-05-08 15:25:12 -04:00
|
|
|
const occurrences = fuzzaldrinPlus.match(text, term);
|
2018-06-16 17:50:13 -04:00
|
|
|
const { indexOf } = [];
|
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
return text
|
|
|
|
.split('')
|
|
|
|
.map(function(character, i) {
|
|
|
|
if (indexOf.call(occurrences, i) !== -1) {
|
|
|
|
return '<b>' + character + '</b>';
|
|
|
|
} else {
|
|
|
|
return character;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.join('');
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
2016-10-06 04:03:10 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GitLabDropdown.prototype.noResults = function() {
|
|
|
|
var html;
|
2017-08-29 12:55:47 -04:00
|
|
|
return '<li class="dropdown-menu-empty-item"><a>No matching results</a></li>';
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
GitLabDropdown.prototype.rowClicked = function(el) {
|
2018-06-16 17:50:13 -04:00
|
|
|
var field, groupName, isInput, selectedIndex, selectedObject, value, isMarking;
|
2016-12-07 10:27:14 -05:00
|
|
|
|
2018-06-16 17:50:13 -04:00
|
|
|
const { fieldName } = this.options;
|
2017-03-11 02:30:44 -05:00
|
|
|
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-12-07 10:27:14 -05:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2016-12-07 10:27:14 -05:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
if (this.options.vue) {
|
2016-09-20 17:39:03 -04:00
|
|
|
if (el.hasClass(ACTIVE_CLASS)) {
|
2016-07-24 16:45:11 -04:00
|
|
|
el.removeClass(ACTIVE_CLASS);
|
|
|
|
} else {
|
|
|
|
el.addClass(ACTIVE_CLASS);
|
|
|
|
}
|
2016-08-24 20:29:25 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
return [selectedObject];
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
field = [];
|
2018-08-07 05:59:01 -04:00
|
|
|
value = this.options.id ? this.options.id(selectedObject, el) : selectedObject.id;
|
2017-03-11 02:30:44 -05:00
|
|
|
if (isInput) {
|
|
|
|
field = $(this.el);
|
2017-09-20 14:24:24 -04:00
|
|
|
} else if (value != null) {
|
2018-08-07 05:59:01 -04:00
|
|
|
field = this.dropdown
|
|
|
|
.parent()
|
|
|
|
.find(
|
|
|
|
"input[name='" + fieldName + "'][value='" + value.toString().replace(/'/g, "\\'") + "']",
|
|
|
|
);
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2016-10-22 11:46:53 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
if (this.options.isSelectable && !this.options.isSelectable(selectedObject, el)) {
|
2018-03-27 14:51:11 -04:00
|
|
|
return [selectedObject];
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
|
2017-09-20 14:24:24 -04:00
|
|
|
if (el.hasClass(ACTIVE_CLASS) && value !== 0) {
|
2017-03-11 02:30:44 -05:00
|
|
|
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-08-07 05:59:01 -04:00
|
|
|
this.dropdown.find('.' + ACTIVE_CLASS).removeClass(ACTIVE_CLASS);
|
2017-03-11 02:30:44 -05:00
|
|
|
if (!isInput) {
|
2018-08-07 05:59:01 -04:00
|
|
|
this.dropdown
|
|
|
|
.parent()
|
|
|
|
.find("input[name='" + fieldName + "']")
|
|
|
|
.remove();
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2016-08-22 12:41:51 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
if (field && field.length && value == null) {
|
|
|
|
this.clearField(field, isInput);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
// 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-08-19 04:45:58 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
return [selectedObject, isMarking];
|
|
|
|
};
|
|
|
|
|
2017-12-19 05:46:13 -05:00
|
|
|
GitLabDropdown.prototype.focusTextInput = function() {
|
2017-06-08 11:48:29 -04:00
|
|
|
if (this.options.filterable) {
|
2017-12-19 05:46:13 -05:00
|
|
|
const initialScrollTop = $(window).scrollTop();
|
2017-07-27 10:30:57 -04:00
|
|
|
|
2018-05-21 19:25:16 -04:00
|
|
|
if (this.dropdown.is('.show') && !this.filterInput.is(':focus')) {
|
2017-12-19 05:46:13 -05:00
|
|
|
this.filterInput.focus();
|
|
|
|
}
|
2017-06-08 11:48:29 -04:00
|
|
|
|
2017-12-19 05:46:13 -05:00
|
|
|
if ($(window).scrollTop() < initialScrollTop) {
|
|
|
|
$(window).scrollTop(initialScrollTop);
|
2017-06-08 11:48:29 -04:00
|
|
|
}
|
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
|
|
|
|
2017-07-28 19:33:56 -04:00
|
|
|
GitLabDropdown.prototype.addInput = function(fieldName, value, selectedObject, single) {
|
2017-03-11 02:30:44 -05:00
|
|
|
var $input;
|
|
|
|
// Create hidden input for form
|
2017-08-01 09:41:24 -04:00
|
|
|
if (single) {
|
|
|
|
$('input[name="' + fieldName + '"]').remove();
|
2017-07-28 19:33:56 -04:00
|
|
|
}
|
2017-08-01 09:41:24 -04:00
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
$input = $('<input>')
|
|
|
|
.attr('type', 'hidden')
|
|
|
|
.attr('name', fieldName)
|
|
|
|
.val(value);
|
2017-03-11 02:30:44 -05:00
|
|
|
if (this.options.inputId != null) {
|
|
|
|
$input.attr('id', this.options.inputId);
|
|
|
|
}
|
2017-05-04 08:11:15 -04:00
|
|
|
|
2017-05-26 17:27:30 -04:00
|
|
|
if (this.options.multiSelect) {
|
2018-08-07 05:59:01 -04:00
|
|
|
Object.keys(selectedObject).forEach(attribute => {
|
2017-05-26 17:27:30 -04:00
|
|
|
$input.attr(`data-${attribute}`, selectedObject[attribute]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-04 08:11:15 -04:00
|
|
|
if (this.options.inputMeta) {
|
|
|
|
$input.attr('data-meta', selectedObject[this.options.inputMeta]);
|
|
|
|
}
|
|
|
|
|
2017-07-28 19:33:56 -04:00
|
|
|
this.dropdown.before($input).trigger('change');
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
GitLabDropdown.prototype.selectRowAtIndex = function(index) {
|
|
|
|
var $el, selector;
|
|
|
|
// If we pass an option index
|
2018-08-07 05:59:01 -04:00
|
|
|
if (typeof index !== 'undefined') {
|
|
|
|
selector = SELECTABLE_CLASSES + ':eq(' + index + ') a';
|
2017-03-11 02:30:44 -05:00
|
|
|
} else {
|
2018-08-07 05:59:01 -04:00
|
|
|
selector = '.dropdown-content .is-focused';
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2018-08-07 05:59:01 -04:00
|
|
|
if (this.dropdown.find('.dropdown-toggle-page').length) {
|
|
|
|
selector = '.dropdown-page-one ' + selector;
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
// simulate a click on the first link
|
|
|
|
$el = $(selector, this.dropdown);
|
|
|
|
if ($el.length) {
|
|
|
|
var href = $el.attr('href');
|
|
|
|
if (href && href !== '#') {
|
2017-12-11 07:28:11 -05:00
|
|
|
visitUrl(href);
|
2017-03-11 02:30:44 -05:00
|
|
|
} else {
|
2017-09-20 14:24:24 -04:00
|
|
|
$el.trigger('click');
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
GitLabDropdown.prototype.addArrowKeyEvent = function() {
|
|
|
|
var $input, ARROW_KEY_CODES, selector;
|
|
|
|
ARROW_KEY_CODES = [38, 40];
|
2018-08-07 05:59:01 -04:00
|
|
|
$input = this.dropdown.find('.dropdown-input-field');
|
2017-03-11 02:30:44 -05:00
|
|
|
selector = SELECTABLE_CLASSES;
|
2018-08-07 05:59:01 -04:00
|
|
|
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-07-24 16:45:11 -04:00
|
|
|
}
|
2018-08-07 05:59:01 -04:00
|
|
|
if (currentIndex !== PREV_INDEX) {
|
|
|
|
_this.highlightRowAtIndex($listItems, currentIndex);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2018-08-07 05:59:01 -04:00
|
|
|
return false;
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2018-08-07 05:59:01 -04:00
|
|
|
if (currentKeyCode === 13 && currentIndex !== -1) {
|
|
|
|
e.preventDefault();
|
|
|
|
_this.selectRowAtIndex();
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2018-08-07 05:59:01 -04:00
|
|
|
};
|
|
|
|
})(this),
|
|
|
|
);
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GitLabDropdown.prototype.removeArrayKeyEvent = function() {
|
|
|
|
return $('body').off('keydown');
|
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GitLabDropdown.prototype.resetRows = function resetRows() {
|
|
|
|
currentIndex = -1;
|
|
|
|
$('.is-focused', this.dropdown).removeClass('is-focused');
|
|
|
|
};
|
2017-01-12 12:12:46 -05:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GitLabDropdown.prototype.highlightRowAtIndex = function($listItems, index) {
|
2018-08-07 05:59:01 -04:00
|
|
|
var $dropdownContent,
|
|
|
|
$listItem,
|
|
|
|
dropdownContentBottom,
|
|
|
|
dropdownContentHeight,
|
|
|
|
dropdownContentTop,
|
|
|
|
dropdownScrollTop,
|
|
|
|
listItemBottom,
|
|
|
|
listItemHeight,
|
|
|
|
listItemTop;
|
|
|
|
|
|
|
|
if (!$listItems) {
|
|
|
|
$listItems = $(SELECTABLE_CLASSES, this.dropdown);
|
|
|
|
}
|
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
// 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-08-07 05:59:01 -04:00
|
|
|
$listItem.find('a:first-child').addClass('is-focused');
|
2017-03-11 02:30:44 -05:00
|
|
|
// 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-08-07 05:59:01 -04:00
|
|
|
} else if (index === $listItems.length - 1) {
|
2017-03-11 02:30:44 -05:00
|
|
|
// Scroll the dropdown content to the bottom
|
|
|
|
$dropdownContent.scrollTop($dropdownContent.prop('scrollHeight'));
|
2018-08-07 05:59:01 -04:00
|
|
|
} else if (listItemBottom > dropdownContentBottom + dropdownScrollTop) {
|
2017-03-11 02:30:44 -05:00
|
|
|
// Scroll the dropdown content down
|
2018-08-07 05:59:01 -04:00
|
|
|
$dropdownContent.scrollTop(
|
|
|
|
listItemBottom - dropdownContentBottom + CURSOR_SELECT_SCROLL_PADDING,
|
|
|
|
);
|
|
|
|
} else if (listItemTop < dropdownContentTop + dropdownScrollTop) {
|
2017-03-11 02:30:44 -05:00
|
|
|
// Scroll the dropdown content up
|
2018-08-07 05:59:01 -04:00
|
|
|
return $dropdownContent.scrollTop(
|
|
|
|
listItemTop - dropdownContentTop - CURSOR_SELECT_SCROLL_PADDING,
|
|
|
|
);
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GitLabDropdown.prototype.updateLabel = function(selected, el, instance) {
|
|
|
|
if (selected == null) {
|
|
|
|
selected = null;
|
|
|
|
}
|
|
|
|
if (el == null) {
|
|
|
|
el = null;
|
|
|
|
}
|
|
|
|
if (instance == null) {
|
|
|
|
instance = null;
|
|
|
|
}
|
2017-05-04 08:11:15 -04:00
|
|
|
|
|
|
|
let toggleText = this.options.toggleLabel(selected, el, instance);
|
|
|
|
if (this.options.updateLabel) {
|
|
|
|
// Option to override the dropdown label text
|
|
|
|
toggleText = this.options.updateLabel;
|
|
|
|
}
|
|
|
|
|
2018-08-07 05:59:01 -04:00
|
|
|
return $(this.el)
|
|
|
|
.find('.dropdown-toggle-text')
|
|
|
|
.text(toggleText);
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
GitLabDropdown.prototype.clearField = function(field, isInput) {
|
|
|
|
return isInput ? field.val('') : field.remove();
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
2017-03-11 02:30:44 -05:00
|
|
|
|
|
|
|
return GitLabDropdown;
|
|
|
|
})();
|
|
|
|
|
|
|
|
$.fn.glDropdown = function(opts) {
|
|
|
|
return this.each(function() {
|
|
|
|
if (!$.data(this, 'glDropdown')) {
|
|
|
|
return $.data(this, 'glDropdown', new GitLabDropdown(this, opts));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|