Commit 2d0d3ef5 authored by Phil Hughes's avatar Phil Hughes

Merge branch 'Remove-var-from-gl_dropdown.js' into 'master'

Remove var from gl dropdown.js

See merge request gitlab-org/gitlab!20166
parents 707b3d84 184acdfc
/* eslint-disable func-names, no-underscore-dangle, no-var, one-var, vars-on-top, no-cond-assign, no-return-assign, no-else-return, camelcase, no-lonely-if, guard-for-in, no-restricted-syntax, consistent-return, no-param-reassign, no-loop-func */ /* eslint-disable func-names, no-underscore-dangle, one-var, no-cond-assign, no-return-assign, no-else-return, camelcase, no-lonely-if, guard-for-in, no-restricted-syntax, consistent-return, no-param-reassign, no-loop-func */
import $ from 'jquery'; import $ from 'jquery';
import _ from 'underscore'; import _ from 'underscore';
...@@ -33,13 +33,12 @@ const FILTER_INPUT = '.dropdown-input .dropdown-input-field:not(.dropdown-no-fil ...@@ -33,13 +33,12 @@ const FILTER_INPUT = '.dropdown-input .dropdown-input-field:not(.dropdown-no-fil
const NO_FILTER_INPUT = '.dropdown-input .dropdown-input-field.dropdown-no-filter'; const NO_FILTER_INPUT = '.dropdown-input .dropdown-input-field.dropdown-no-filter';
function GitLabDropdownInput(input, options) { function GitLabDropdownInput(input, options) {
var $inputContainer, $clearButton; const _this = this;
var _this = this;
this.input = input; this.input = input;
this.options = options; this.options = options;
this.fieldName = this.options.fieldName || 'field-name'; this.fieldName = this.options.fieldName || 'field-name';
$inputContainer = this.input.parent(); const $inputContainer = this.input.parent();
$clearButton = $inputContainer.find('.js-dropdown-input-clear'); const $clearButton = $inputContainer.find('.js-dropdown-input-clear');
$clearButton.on('click', e => { $clearButton.on('click', e => {
// Clear click // Clear click
e.preventDefault(); e.preventDefault();
...@@ -52,13 +51,13 @@ function GitLabDropdownInput(input, options) { ...@@ -52,13 +51,13 @@ function GitLabDropdownInput(input, options) {
this.input this.input
.on('keydown', e => { .on('keydown', e => {
var keyCode = e.which; const keyCode = e.which;
if (keyCode === 13 && !options.elIsInput) { if (keyCode === 13 && !options.elIsInput) {
e.preventDefault(); e.preventDefault();
} }
}) })
.on('input', e => { .on('input', e => {
var val = e.currentTarget.value || _this.options.inputFieldName; let val = e.currentTarget.value || _this.options.inputFieldName;
val = val val = val
.split(' ') .split(' ')
.join('-') // replaces space with dash .join('-') // replaces space with dash
...@@ -78,12 +77,12 @@ GitLabDropdownInput.prototype.onInput = function(cb) { ...@@ -78,12 +77,12 @@ GitLabDropdownInput.prototype.onInput = function(cb) {
}; };
function GitLabDropdownFilter(input, options) { function GitLabDropdownFilter(input, options) {
var $clearButton, $inputContainer, ref, timeout; let ref, timeout;
this.input = input; this.input = input;
this.options = options; this.options = options;
this.filterInputBlur = (ref = this.options.filterInputBlur) != null ? ref : true; this.filterInputBlur = (ref = this.options.filterInputBlur) != null ? ref : true;
$inputContainer = this.input.parent(); const $inputContainer = this.input.parent();
$clearButton = $inputContainer.find('.js-dropdown-input-clear'); const $clearButton = $inputContainer.find('.js-dropdown-input-clear');
$clearButton.on('click', e => { $clearButton.on('click', e => {
// Clear click // Clear click
e.preventDefault(); e.preventDefault();
...@@ -97,7 +96,7 @@ function GitLabDropdownFilter(input, options) { ...@@ -97,7 +96,7 @@ function GitLabDropdownFilter(input, options) {
timeout = ''; timeout = '';
this.input this.input
.on('keydown', e => { .on('keydown', e => {
var keyCode = e.which; const keyCode = e.which;
if (keyCode === 13 && !options.elIsInput) { if (keyCode === 13 && !options.elIsInput) {
e.preventDefault(); e.preventDefault();
} }
...@@ -130,11 +129,11 @@ GitLabDropdownFilter.prototype.shouldBlur = function(keyCode) { ...@@ -130,11 +129,11 @@ GitLabDropdownFilter.prototype.shouldBlur = function(keyCode) {
}; };
GitLabDropdownFilter.prototype.filter = function(search_text) { GitLabDropdownFilter.prototype.filter = function(search_text) {
var data, elements, group, key, results, tmp; let elements, group, key, results, tmp;
if (this.options.onFilter) { if (this.options.onFilter) {
this.options.onFilter(search_text); this.options.onFilter(search_text);
} }
data = this.options.data(); const data = this.options.data();
if (data != null && !this.options.filterByText) { if (data != null && !this.options.filterByText) {
results = data; results = data;
if (search_text !== '') { if (search_text !== '') {
...@@ -178,9 +177,8 @@ GitLabDropdownFilter.prototype.filter = function(search_text) { ...@@ -178,9 +177,8 @@ GitLabDropdownFilter.prototype.filter = function(search_text) {
elements = this.options.elements(); elements = this.options.elements();
if (search_text) { if (search_text) {
elements.each(function() { elements.each(function() {
var $el, matches; const $el = $(this);
$el = $(this); const matches = fuzzaldrinPlus.match($el.text().trim(), search_text);
matches = fuzzaldrinPlus.match($el.text().trim(), search_text);
if (!$el.is('.dropdown-header')) { if (!$el.is('.dropdown-header')) {
if (matches.length) { if (matches.length) {
return $el.show().removeClass('option-hidden'); return $el.show().removeClass('option-hidden');
...@@ -238,7 +236,7 @@ GitLabDropdownRemote.prototype.fetchData = function() { ...@@ -238,7 +236,7 @@ GitLabDropdownRemote.prototype.fetchData = function() {
}; };
function GitLabDropdown(el1, options) { function GitLabDropdown(el1, options) {
var searchFields, selector, self; let selector, self;
this.el = el1; this.el = el1;
this.options = options; this.options = options;
this.updateLabel = this.updateLabel.bind(this); this.updateLabel = this.updateLabel.bind(this);
...@@ -260,7 +258,7 @@ function GitLabDropdown(el1, options) { ...@@ -260,7 +258,7 @@ function GitLabDropdown(el1, options) {
if (_.isString(this.filterInput)) { if (_.isString(this.filterInput)) {
this.filterInput = this.getElement(this.filterInput); this.filterInput = this.getElement(this.filterInput);
} }
searchFields = this.options.search ? this.options.search.fields : []; const searchFields = this.options.search ? this.options.search.fields : [];
if (this.options.data) { if (this.options.data) {
// If we provided data // If we provided data
// data could be an array of objects or a group of arrays // data could be an array of objects or a group of arrays
...@@ -350,7 +348,7 @@ function GitLabDropdown(el1, options) { ...@@ -350,7 +348,7 @@ function GitLabDropdown(el1, options) {
} }
}); });
this.dropdown.on('blur', 'a', e => { this.dropdown.on('blur', 'a', e => {
var $dropdownMenu, $relatedTarget; let $dropdownMenu, $relatedTarget;
if (e.relatedTarget != null) { if (e.relatedTarget != null) {
$relatedTarget = $(e.relatedTarget); $relatedTarget = $(e.relatedTarget);
$dropdownMenu = $relatedTarget.closest('.dropdown-menu'); $dropdownMenu = $relatedTarget.closest('.dropdown-menu');
...@@ -372,11 +370,10 @@ function GitLabDropdown(el1, options) { ...@@ -372,11 +370,10 @@ function GitLabDropdown(el1, options) {
selector = '.dropdown-page-one .dropdown-content a'; selector = '.dropdown-page-one .dropdown-content a';
} }
this.dropdown.on('click', selector, e => { this.dropdown.on('click', selector, e => {
var $el, selected, selectedObj, isMarking; const $el = $(e.currentTarget);
$el = $(e.currentTarget); const selected = self.rowClicked($el);
selected = self.rowClicked($el); const selectedObj = selected ? selected[0] : null;
selectedObj = selected ? selected[0] : null; const isMarking = selected ? selected[1] : null;
isMarking = selected ? selected[1] : null;
if (this.options.clicked) { if (this.options.clicked) {
this.options.clicked.call(this, { this.options.clicked.call(this, {
selectedObj, selectedObj,
...@@ -406,8 +403,7 @@ GitLabDropdown.prototype.toggleLoading = function() { ...@@ -406,8 +403,7 @@ GitLabDropdown.prototype.toggleLoading = function() {
}; };
GitLabDropdown.prototype.togglePage = function() { GitLabDropdown.prototype.togglePage = function() {
var menu; const menu = $('.dropdown-menu', this.dropdown);
menu = $('.dropdown-menu', this.dropdown);
if (menu.hasClass(PAGE_TWO_CLASS)) { if (menu.hasClass(PAGE_TWO_CLASS)) {
if (this.remote) { if (this.remote) {
this.remote.execute(); this.remote.execute();
...@@ -419,7 +415,7 @@ GitLabDropdown.prototype.togglePage = function() { ...@@ -419,7 +415,7 @@ GitLabDropdown.prototype.togglePage = function() {
}; };
GitLabDropdown.prototype.parseData = function(data) { GitLabDropdown.prototype.parseData = function(data) {
var full_html, groupData, html, name; let groupData, html, name;
this.renderedData = data; this.renderedData = data;
if (this.options.filterable && data.length === 0) { if (this.options.filterable && data.length === 0) {
// render no matching results // render no matching results
...@@ -447,7 +443,7 @@ GitLabDropdown.prototype.parseData = function(data) { ...@@ -447,7 +443,7 @@ GitLabDropdown.prototype.parseData = function(data) {
} }
} }
// Render the full menu // Render the full menu
full_html = this.renderMenu(html); const full_html = this.renderMenu(html);
return this.appendMenu(full_html); return this.appendMenu(full_html);
}; };
...@@ -456,7 +452,7 @@ GitLabDropdown.prototype.renderData = function(data, group) { ...@@ -456,7 +452,7 @@ GitLabDropdown.prototype.renderData = function(data, group) {
}; };
GitLabDropdown.prototype.shouldPropagate = function(e) { GitLabDropdown.prototype.shouldPropagate = function(e) {
var $target; let $target;
if (this.options.multiSelect || this.options.shouldPropagate === false) { if (this.options.multiSelect || this.options.shouldPropagate === false) {
$target = $(e.target); $target = $(e.target);
if ( if (
...@@ -487,7 +483,6 @@ GitLabDropdown.prototype.filteredFullData = function() { ...@@ -487,7 +483,6 @@ GitLabDropdown.prototype.filteredFullData = function() {
}; };
GitLabDropdown.prototype.opened = function(e) { GitLabDropdown.prototype.opened = function(e) {
var contentHtml;
this.resetRows(); this.resetRows();
this.addArrowKeyEvent(); this.addArrowKeyEvent();
...@@ -513,7 +508,7 @@ GitLabDropdown.prototype.opened = function(e) { ...@@ -513,7 +508,7 @@ GitLabDropdown.prototype.opened = function(e) {
); );
} }
contentHtml = $('.dropdown-content', this.dropdown).html(); const contentHtml = $('.dropdown-content', this.dropdown).html();
if (this.remote && contentHtml === '') { if (this.remote && contentHtml === '') {
this.remote.execute(); this.remote.execute();
} else { } else {
...@@ -536,7 +531,7 @@ GitLabDropdown.prototype.opened = function(e) { ...@@ -536,7 +531,7 @@ GitLabDropdown.prototype.opened = function(e) {
}; };
GitLabDropdown.prototype.positionMenuAbove = function() { GitLabDropdown.prototype.positionMenuAbove = function() {
var $menu = this.dropdown.find('.dropdown-menu'); const $menu = this.dropdown.find('.dropdown-menu');
$menu.addClass('dropdown-open-top'); $menu.addClass('dropdown-open-top');
$menu.css('top', 'initial'); $menu.css('top', 'initial');
...@@ -544,10 +539,9 @@ GitLabDropdown.prototype.positionMenuAbove = function() { ...@@ -544,10 +539,9 @@ GitLabDropdown.prototype.positionMenuAbove = function() {
}; };
GitLabDropdown.prototype.hidden = function(e) { GitLabDropdown.prototype.hidden = function(e) {
var $input;
this.resetRows(); this.resetRows();
this.removeArrowKeyEvent(); this.removeArrowKeyEvent();
$input = this.dropdown.find('.dropdown-input-field'); const $input = this.dropdown.find('.dropdown-input-field');
if (this.options.filterable) { if (this.options.filterable) {
$input.blur(); $input.blur();
} }
...@@ -575,7 +569,7 @@ GitLabDropdown.prototype.appendMenu = function(html) { ...@@ -575,7 +569,7 @@ GitLabDropdown.prototype.appendMenu = function(html) {
}; };
GitLabDropdown.prototype.clearMenu = function() { GitLabDropdown.prototype.clearMenu = function() {
var selector; let selector;
selector = '.dropdown-content'; selector = '.dropdown-content';
if (this.dropdown.find('.dropdown-toggle-page').length) { if (this.dropdown.find('.dropdown-toggle-page').length) {
if (this.options.containerSelector) { if (this.options.containerSelector) {
...@@ -635,10 +629,9 @@ GitLabDropdown.prototype.noResults = function() { ...@@ -635,10 +629,9 @@ GitLabDropdown.prototype.noResults = function() {
}; };
GitLabDropdown.prototype.rowClicked = function(el) { GitLabDropdown.prototype.rowClicked = function(el) {
var field, groupName, isInput, selectedIndex, selectedObject, value, isMarking; let field, groupName, selectedIndex, selectedObject, isMarking;
const { fieldName } = this.options; const { fieldName } = this.options;
isInput = $(this.el).is('input'); const isInput = $(this.el).is('input');
if (this.renderedData) { if (this.renderedData) {
groupName = el.data('group'); groupName = el.data('group');
if (groupName) { if (groupName) {
...@@ -662,7 +655,7 @@ GitLabDropdown.prototype.rowClicked = function(el) { ...@@ -662,7 +655,7 @@ GitLabDropdown.prototype.rowClicked = function(el) {
} }
field = []; field = [];
value = this.options.id ? this.options.id(selectedObject, el) : selectedObject.id; const value = this.options.id ? this.options.id(selectedObject, el) : selectedObject.id;
if (isInput) { if (isInput) {
field = $(this.el); field = $(this.el);
} else if (value != null) { } else if (value != null) {
...@@ -734,13 +727,12 @@ GitLabDropdown.prototype.focusTextInput = function() { ...@@ -734,13 +727,12 @@ GitLabDropdown.prototype.focusTextInput = function() {
}; };
GitLabDropdown.prototype.addInput = function(fieldName, value, selectedObject, single) { GitLabDropdown.prototype.addInput = function(fieldName, value, selectedObject, single) {
var $input;
// Create hidden input for form // Create hidden input for form
if (single) { if (single) {
$(`input[name="${fieldName}"]`).remove(); $(`input[name="${fieldName}"]`).remove();
} }
$input = $('<input>') const $input = $('<input>')
.attr('type', 'hidden') .attr('type', 'hidden')
.attr('name', fieldName) .attr('name', fieldName)
.val(value); .val(value);
...@@ -762,7 +754,7 @@ GitLabDropdown.prototype.addInput = function(fieldName, value, selectedObject, s ...@@ -762,7 +754,7 @@ GitLabDropdown.prototype.addInput = function(fieldName, value, selectedObject, s
}; };
GitLabDropdown.prototype.selectRowAtIndex = function(index) { GitLabDropdown.prototype.selectRowAtIndex = function(index) {
var $el, selector; let selector;
// If we pass an option index // If we pass an option index
if (typeof index !== 'undefined') { if (typeof index !== 'undefined') {
selector = `${SELECTABLE_CLASSES}:eq(${index}) a`; selector = `${SELECTABLE_CLASSES}:eq(${index}) a`;
...@@ -773,9 +765,9 @@ GitLabDropdown.prototype.selectRowAtIndex = function(index) { ...@@ -773,9 +765,9 @@ GitLabDropdown.prototype.selectRowAtIndex = function(index) {
selector = `.dropdown-page-one ${selector}`; selector = `.dropdown-page-one ${selector}`;
} }
// simulate a click on the first link // simulate a click on the first link
$el = $(selector, this.dropdown); const $el = $(selector, this.dropdown);
if ($el.length) { if ($el.length) {
var href = $el.attr('href'); const href = $el.attr('href');
if (href && href !== '#') { if (href && href !== '#') {
visitUrl(href); visitUrl(href);
} else { } else {
...@@ -785,15 +777,15 @@ GitLabDropdown.prototype.selectRowAtIndex = function(index) { ...@@ -785,15 +777,15 @@ GitLabDropdown.prototype.selectRowAtIndex = function(index) {
}; };
GitLabDropdown.prototype.addArrowKeyEvent = function() { GitLabDropdown.prototype.addArrowKeyEvent = function() {
var ARROW_KEY_CODES, selector; let selector;
ARROW_KEY_CODES = [38, 40]; const ARROW_KEY_CODES = [38, 40];
selector = SELECTABLE_CLASSES; selector = SELECTABLE_CLASSES;
if (this.dropdown.find('.dropdown-toggle-page').length) { if (this.dropdown.find('.dropdown-toggle-page').length) {
selector = `.dropdown-page-one ${selector}`; selector = `.dropdown-page-one ${selector}`;
} }
return $('body').on('keydown', e => { return $('body').on('keydown', e => {
var $listItems, PREV_INDEX, currentKeyCode; let $listItems, PREV_INDEX;
currentKeyCode = e.which; const currentKeyCode = e.which;
if (ARROW_KEY_CODES.indexOf(currentKeyCode) !== -1) { if (ARROW_KEY_CODES.indexOf(currentKeyCode) !== -1) {
e.preventDefault(); e.preventDefault();
e.stopImmediatePropagation(); e.stopImmediatePropagation();
...@@ -834,16 +826,6 @@ GitLabDropdown.prototype.resetRows = function resetRows() { ...@@ -834,16 +826,6 @@ GitLabDropdown.prototype.resetRows = function resetRows() {
}; };
GitLabDropdown.prototype.highlightRowAtIndex = function($listItems, index) { GitLabDropdown.prototype.highlightRowAtIndex = function($listItems, index) {
var $dropdownContent,
$listItem,
dropdownContentBottom,
dropdownContentHeight,
dropdownContentTop,
dropdownScrollTop,
listItemBottom,
listItemHeight,
listItemTop;
if (!$listItems) { if (!$listItems) {
$listItems = $(SELECTABLE_CLASSES, this.dropdown); $listItems = $(SELECTABLE_CLASSES, this.dropdown);
} }
...@@ -851,18 +833,18 @@ GitLabDropdown.prototype.highlightRowAtIndex = function($listItems, index) { ...@@ -851,18 +833,18 @@ GitLabDropdown.prototype.highlightRowAtIndex = function($listItems, index) {
// Remove the class for the previously focused row // Remove the class for the previously focused row
$('.is-focused', this.dropdown).removeClass('is-focused'); $('.is-focused', this.dropdown).removeClass('is-focused');
// Update the class for the row at the specific index // Update the class for the row at the specific index
$listItem = $listItems.eq(index); const $listItem = $listItems.eq(index);
$listItem.find('a:first-child').addClass('is-focused'); $listItem.find('a:first-child').addClass('is-focused');
// Dropdown content scroll area // Dropdown content scroll area
$dropdownContent = $listItem.closest('.dropdown-content'); const $dropdownContent = $listItem.closest('.dropdown-content');
dropdownScrollTop = $dropdownContent.scrollTop(); const dropdownScrollTop = $dropdownContent.scrollTop();
dropdownContentHeight = $dropdownContent.outerHeight(); const dropdownContentHeight = $dropdownContent.outerHeight();
dropdownContentTop = $dropdownContent.prop('offsetTop'); const dropdownContentTop = $dropdownContent.prop('offsetTop');
dropdownContentBottom = dropdownContentTop + dropdownContentHeight; const dropdownContentBottom = dropdownContentTop + dropdownContentHeight;
// Get the offset bottom of the list item // Get the offset bottom of the list item
listItemHeight = $listItem.outerHeight(); const listItemHeight = $listItem.outerHeight();
listItemTop = $listItem.prop('offsetTop'); const listItemTop = $listItem.prop('offsetTop');
listItemBottom = listItemTop + listItemHeight; const listItemBottom = listItemTop + listItemHeight;
if (!index) { if (!index) {
// Scroll the dropdown content to the top // Scroll the dropdown content to the top
$dropdownContent.scrollTop(0); $dropdownContent.scrollTop(0);
......
---
title: replace var gl_dropdown.js
merge_request: 20166
author: nuwe1
type: other
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment