Object spread : less jQuery more ES6 (#24665)

This commit is contained in:
Johann-S 2017-11-13 11:25:36 +01:00 committed by GitHub
parent 1354a929f9
commit 9a0bba9afa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 85 additions and 51 deletions

View File

@ -10,6 +10,7 @@ module.exports = {
] ]
], ],
plugins: [ plugins: [
process.env.PLUGINS && 'transform-es2015-modules-strip' process.env.PLUGINS && 'transform-es2015-modules-strip',
'@babel/proposal-object-rest-spread'
].filter(Boolean) ].filter(Boolean)
}; };

View File

@ -15,7 +15,8 @@ const plugins = [
externalHelpersWhitelist: [ // include only required helpers externalHelpersWhitelist: [ // include only required helpers
'defineProperties', 'defineProperties',
'createClass', 'createClass',
'inheritsLoose' 'inheritsLoose',
'extends'
] ]
}) })
] ]

View File

@ -223,7 +223,10 @@ const Carousel = (($) => {
// private // private
_getConfig(config) { _getConfig(config) {
config = $.extend({}, Default, config) config = {
...Default,
...config
}
Util.typeCheckConfig(NAME, config, DefaultType) Util.typeCheckConfig(NAME, config, DefaultType)
return config return config
} }
@ -428,10 +431,16 @@ const Carousel = (($) => {
static _jQueryInterface(config) { static _jQueryInterface(config) {
return this.each(function () { return this.each(function () {
let data = $(this).data(DATA_KEY) let data = $(this).data(DATA_KEY)
const _config = $.extend({}, Default, $(this).data()) let _config = {
...Default,
...$(this).data()
}
if (typeof config === 'object') { if (typeof config === 'object') {
$.extend(_config, config) _config = {
..._config,
...config
}
} }
const action = typeof config === 'string' ? config : _config.slide const action = typeof config === 'string' ? config : _config.slide
@ -468,7 +477,10 @@ const Carousel = (($) => {
return return
} }
const config = $.extend({}, $(target).data(), $(this).data()) const config = {
...$(target).data(),
...$(this).data()
}
const slideIndex = this.getAttribute('data-slide-to') const slideIndex = this.getAttribute('data-slide-to')
if (slideIndex) { if (slideIndex) {

View File

@ -277,7 +277,10 @@ const Collapse = (($) => {
// private // private
_getConfig(config) { _getConfig(config) {
config = $.extend({}, Default, config) config = {
...Default,
...config
}
config.toggle = Boolean(config.toggle) // coerce string values config.toggle = Boolean(config.toggle) // coerce string values
Util.typeCheckConfig(NAME, config, DefaultType) Util.typeCheckConfig(NAME, config, DefaultType)
return config return config
@ -338,12 +341,11 @@ const Collapse = (($) => {
return this.each(function () { return this.each(function () {
const $this = $(this) const $this = $(this)
let data = $this.data(DATA_KEY) let data = $this.data(DATA_KEY)
const _config = $.extend( const _config = {
{}, ...Default,
Default, ...$this.data(),
$this.data(), ...typeof config === 'object' && config
typeof config === 'object' && config }
)
if (!data && _config.toggle && /show|hide/.test(config)) { if (!data && _config.toggle && /show|hide/.test(config)) {
_config.toggle = false _config.toggle = false

View File

@ -210,12 +210,11 @@ const Dropdown = (($) => {
} }
_getConfig(config) { _getConfig(config) {
config = $.extend( config = {
{}, ...this.constructor.Default,
this.constructor.Default, ...$(this._element).data(),
$(this._element).data(), ...config
config }
)
Util.typeCheckConfig( Util.typeCheckConfig(
NAME, NAME,
@ -262,7 +261,10 @@ const Dropdown = (($) => {
const offsetConf = {} const offsetConf = {}
if (typeof this._config.offset === 'function') { if (typeof this._config.offset === 'function') {
offsetConf.fn = (data) => { offsetConf.fn = (data) => {
data.offsets = $.extend({}, data.offsets, this._config.offset(data.offsets) || {}) data.offsets = {
...data.offsets,
...this._config.offset(data.offsets) || {}
}
return data return data
} }
} else { } else {

View File

@ -227,7 +227,10 @@ const Modal = (($) => {
// private // private
_getConfig(config) { _getConfig(config) {
config = $.extend({}, Default, config) config = {
...Default,
...config
}
Util.typeCheckConfig(NAME, config, DefaultType) Util.typeCheckConfig(NAME, config, DefaultType)
return config return config
} }
@ -506,12 +509,11 @@ const Modal = (($) => {
static _jQueryInterface(config, relatedTarget) { static _jQueryInterface(config, relatedTarget) {
return this.each(function () { return this.each(function () {
let data = $(this).data(DATA_KEY) let data = $(this).data(DATA_KEY)
const _config = $.extend( const _config = {
{}, ...Modal.Default,
Modal.Default, ...$(this).data(),
$(this).data(), ...typeof config === 'object' && config
typeof config === 'object' && config }
)
if (!data) { if (!data) {
data = new Modal(this, _config) data = new Modal(this, _config)
@ -547,7 +549,10 @@ const Modal = (($) => {
} }
const config = $(target).data(DATA_KEY) ? const config = $(target).data(DATA_KEY) ?
'toggle' : $.extend({}, $(target).data(), $(this).data()) 'toggle' : {
...$(target).data(),
...$(this).data()
}
if (this.tagName === 'A' || this.tagName === 'AREA') { if (this.tagName === 'A' || this.tagName === 'AREA') {
event.preventDefault() event.preventDefault()

View File

@ -26,7 +26,9 @@ const Popover = (($) => {
const CLASS_PREFIX = 'bs-popover' const CLASS_PREFIX = 'bs-popover'
const BSCLS_PREFIX_REGEX = new RegExp(`(^|\\s)${CLASS_PREFIX}\\S+`, 'g') const BSCLS_PREFIX_REGEX = new RegExp(`(^|\\s)${CLASS_PREFIX}\\S+`, 'g')
const Default = $.extend({}, Tooltip.Default, { const Default = {
...Tooltip.Default,
...{
placement : 'right', placement : 'right',
trigger : 'click', trigger : 'click',
content : '', content : '',
@ -34,11 +36,15 @@ const Popover = (($) => {
+ '<div class="arrow"></div>' + '<div class="arrow"></div>'
+ '<h3 class="popover-header"></h3>' + '<h3 class="popover-header"></h3>'
+ '<div class="popover-body"></div></div>' + '<div class="popover-body"></div></div>'
}) }
}
const DefaultType = $.extend({}, Tooltip.DefaultType, { const DefaultType = {
...Tooltip.DefaultType,
...{
content : '(string|element|function)' content : '(string|element|function)'
}) }
}
const ClassName = { const ClassName = {
FADE : 'fade', FADE : 'fade',

View File

@ -171,7 +171,10 @@ const ScrollSpy = (($) => {
// private // private
_getConfig(config) { _getConfig(config) {
config = $.extend({}, Default, config) config = {
...Default,
...config
}
if (typeof config.target !== 'string') { if (typeof config.target !== 'string') {
let id = $(config.target).attr('id') let id = $(config.target).attr('id')

View File

@ -501,10 +501,13 @@ const Tooltip = (($) => {
}) })
if (this.config.selector) { if (this.config.selector) {
this.config = $.extend({}, this.config, { this.config = {
...this.config,
...{
trigger : 'manual', trigger : 'manual',
selector : '' selector : ''
}) }
}
} else { } else {
this._fixTitle() this._fixTitle()
} }
@ -613,12 +616,11 @@ const Tooltip = (($) => {
} }
_getConfig(config) { _getConfig(config) {
config = $.extend( config = {
{}, ...this.constructor.Default,
this.constructor.Default, ...$(this.element).data(),
$(this.element).data(), ...config
config }
)
if (typeof config.delay === 'number') { if (typeof config.delay === 'number') {
config.delay = { config.delay = {