2018-05-09 12:01:36 +05:30
|
|
|
import $ from 'jquery';
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
let instanceCount = 0;
|
|
|
|
|
|
|
|
class AutoWidthDropdownSelect {
|
|
|
|
constructor(selectElement) {
|
|
|
|
this.$selectElement = $(selectElement);
|
|
|
|
this.dropdownClass = `js-auto-width-select-dropdown-${instanceCount}`;
|
|
|
|
instanceCount += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
2018-11-08 19:23:39 +05:30
|
|
|
const { dropdownClass } = this;
|
2017-08-17 22:00:37 +05:30
|
|
|
this.$selectElement.select2({
|
|
|
|
dropdownCssClass: dropdownClass,
|
2018-03-17 18:26:18 +05:30
|
|
|
...AutoWidthDropdownSelect.selectOptions(this.dropdownClass),
|
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
static selectOptions(dropdownClass) {
|
|
|
|
return {
|
2017-08-17 22:00:37 +05:30
|
|
|
dropdownCss() {
|
|
|
|
let resultantWidth = 'auto';
|
|
|
|
const $dropdown = $(`.${dropdownClass}`);
|
|
|
|
|
|
|
|
// We have to look at the parent because
|
|
|
|
// `offsetParent` on a `display: none;` is `null`
|
|
|
|
const offsetParentWidth = $(this).parent().offsetParent().width();
|
|
|
|
// Reset any width to let it naturally flow
|
|
|
|
$dropdown.css('width', 'auto');
|
|
|
|
if ($dropdown.outerWidth(false) > offsetParentWidth) {
|
|
|
|
resultantWidth = offsetParentWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
width: resultantWidth,
|
|
|
|
maxWidth: offsetParentWidth,
|
|
|
|
};
|
|
|
|
},
|
2018-03-17 18:26:18 +05:30
|
|
|
};
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default AutoWidthDropdownSelect;
|