Commit 40a04bc7 authored by Tim Zallmann's avatar Tim Zallmann

Wraps Select 2 Import into its own webpack bundle

Wraps all imports for select 2 to deferred imports,
especially in the main.js we are actually checking if there 
is any select 2 element on the page or not.
parent 2bb0d089
...@@ -7,4 +7,3 @@ import 'vendor/jquery.caret'; ...@@ -7,4 +7,3 @@ import 'vendor/jquery.caret';
import 'vendor/jquery.atwho'; import 'vendor/jquery.atwho';
import 'vendor/jquery.scrollTo'; import 'vendor/jquery.scrollTo';
import 'jquery.waitforimages'; import 'jquery.waitforimages';
import 'select2/select2';
...@@ -4,6 +4,8 @@ import Api from './api'; ...@@ -4,6 +4,8 @@ import Api from './api';
import { normalizeHeaders } from './lib/utils/common_utils'; import { normalizeHeaders } from './lib/utils/common_utils';
export default function groupsSelect() { export default function groupsSelect() {
import(/* webpackChunkName: 'select2' */ 'select2/select2')
.then(() => {
// Needs to be accessible in rspec // Needs to be accessible in rspec
window.GROUP_SELECT_PER_PAGE = 20; window.GROUP_SELECT_PER_PAGE = 20;
$('.ajax-groups-select').each(function setAjaxGroupsSelect2() { $('.ajax-groups-select').each(function setAjaxGroupsSelect2() {
...@@ -93,4 +95,6 @@ export default function groupsSelect() { ...@@ -93,4 +95,6 @@ export default function groupsSelect() {
dropdown.style.height = `${Math.floor(dropdown.scrollHeight)}px`; dropdown.style.height = `${Math.floor(dropdown.scrollHeight)}px`;
}); });
}); });
})
.catch(() => {});
} }
...@@ -11,10 +11,14 @@ class AutoWidthDropdownSelect { ...@@ -11,10 +11,14 @@ class AutoWidthDropdownSelect {
init() { init() {
const { dropdownClass } = this; const { dropdownClass } = this;
import(/* webpackChunkName: 'select2' */ 'select2/select2')
.then(() => {
this.$selectElement.select2({ this.$selectElement.select2({
dropdownCssClass: dropdownClass, dropdownCssClass: dropdownClass,
...AutoWidthDropdownSelect.selectOptions(this.dropdownClass), ...AutoWidthDropdownSelect.selectOptions(this.dropdownClass),
}); });
})
.catch(() => {});
return this; return this;
} }
......
...@@ -7,10 +7,14 @@ export default class IssuableContext { ...@@ -7,10 +7,14 @@ export default class IssuableContext {
constructor(currentUser) { constructor(currentUser) {
this.userSelect = new UsersSelect(currentUser); this.userSelect = new UsersSelect(currentUser);
import(/* webpackChunkName: 'select2' */ 'select2/select2')
.then(() => {
$('select.select2').select2({ $('select.select2').select2({
width: 'resolve', width: 'resolve',
dropdownAutoWidth: true, dropdownAutoWidth: true,
}); });
})
.catch(() => {});
$('.issuable-sidebar .inline-update').on('change', 'select', function onClickSelect() { $('.issuable-sidebar .inline-update').on('change', 'select', function onClickSelect() {
return $(this).submit(); return $(this).submit();
......
...@@ -120,6 +120,8 @@ export default class IssuableForm { ...@@ -120,6 +120,8 @@ export default class IssuableForm {
} }
initTargetBranchDropdown() { initTargetBranchDropdown() {
import(/* webpackChunkName: 'select2' */ 'select2/select2')
.then(() => {
this.$targetBranchSelect.select2({ this.$targetBranchSelect.select2({
...AutoWidthDropdownSelect.selectOptions('js-target-branch-select'), ...AutoWidthDropdownSelect.selectOptions('js-target-branch-select'),
ajax: { ajax: {
...@@ -150,5 +152,7 @@ export default class IssuableForm { ...@@ -150,5 +152,7 @@ export default class IssuableForm {
}); });
}, },
}); });
})
.catch(() => {});
} }
} }
...@@ -100,6 +100,9 @@ function deferredInitialisation() { ...@@ -100,6 +100,9 @@ function deferredInitialisation() {
}); });
// Initialize select2 selects // Initialize select2 selects
if ($('select.select2').length) {
import(/* webpackChunkName: 'select2' */ 'select2/select2')
.then(() => {
$('select.select2').select2({ $('select.select2').select2({
width: 'resolve', width: 'resolve',
dropdownAutoWidth: true, dropdownAutoWidth: true,
...@@ -112,6 +115,9 @@ function deferredInitialisation() { ...@@ -112,6 +115,9 @@ function deferredInitialisation() {
$(':focus').blur(); $(':focus').blur();
}, 1); }, 1);
}); });
})
.catch(() => {});
}
// Initialize tooltips // Initialize tooltips
$body.tooltip({ $body.tooltip({
......
...@@ -5,6 +5,8 @@ import Api from './api'; ...@@ -5,6 +5,8 @@ import Api from './api';
import ProjectSelectComboButton from './project_select_combo_button'; import ProjectSelectComboButton from './project_select_combo_button';
export default function projectSelect() { export default function projectSelect() {
import(/* webpackChunkName: 'select2' */ 'select2/select2')
.then(() => {
$('.ajax-project-select').each(function(i, select) { $('.ajax-project-select').each(function(i, select) {
var placeholder; var placeholder;
const simpleFilter = $(select).data('simpleFilter') || false; const simpleFilter = $(select).data('simpleFilter') || false;
...@@ -98,4 +100,6 @@ export default function projectSelect() { ...@@ -98,4 +100,6 @@ export default function projectSelect() {
if (simpleFilter) return select; if (simpleFilter) return select;
return new ProjectSelectComboButton(select); return new ProjectSelectComboButton(select);
}); });
})
.catch(() => {});
} }
...@@ -44,9 +44,13 @@ export default class ProjectSelectComboButton { ...@@ -44,9 +44,13 @@ export default class ProjectSelectComboButton {
// eslint-disable-next-line class-methods-use-this // eslint-disable-next-line class-methods-use-this
openDropdown(event) { openDropdown(event) {
import(/* webpackChunkName: 'select2' */ 'select2/select2')
.then(() => {
$(event.currentTarget) $(event.currentTarget)
.siblings('.project-item-select') .siblings('.project-item-select')
.select2('open'); .select2('open');
})
.catch(() => {});
} }
selectProject() { selectProject() {
......
...@@ -579,6 +579,8 @@ function UsersSelect(currentUser, els, options = {}) { ...@@ -579,6 +579,8 @@ function UsersSelect(currentUser, els, options = {}) {
}; };
})(this), })(this),
); );
import(/* webpackChunkName: 'select2' */ 'select2/select2')
.then(() => {
$('.ajax-users-select').each( $('.ajax-users-select').each(
(function(_this) { (function(_this) {
return function(i, select) { return function(i, select) {
...@@ -637,7 +639,11 @@ function UsersSelect(currentUser, els, options = {}) { ...@@ -637,7 +639,11 @@ function UsersSelect(currentUser, els, options = {}) {
data.results.unshift(anyUser); data.results.unshift(anyUser);
} }
} }
if (showEmailUser && data.results.length === 0 && query.term.match(/^[^@]+@[^@]+$/)) { if (
showEmailUser &&
data.results.length === 0 &&
query.term.match(/^[^@]+@[^@]+$/)
) {
var trimmed = query.term.trim(); var trimmed = query.term.trim();
emailUser = { emailUser = {
name: 'Invite "' + trimmed + '" by email', name: 'Invite "' + trimmed + '" by email',
...@@ -674,6 +680,8 @@ function UsersSelect(currentUser, els, options = {}) { ...@@ -674,6 +680,8 @@ function UsersSelect(currentUser, els, options = {}) {
}; };
})(this), })(this),
); );
})
.catch(() => {});
} }
UsersSelect.prototype.initSelection = function(element, callback) { UsersSelect.prototype.initSelection = function(element, callback) {
......
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