Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/extract withGlobalDefaults #1045

Merged
merged 3 commits into from
Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 0 additions & 9 deletions src/SweetAlert.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,4 @@ SweetAlert.noop = () => { }

SweetAlert.version = version

SweetAlert.default = SweetAlert

/**
* Set default params if `window._swalDefaults` is an object
*/
if (typeof window !== 'undefined' && typeof window._swalDefaults === 'object') {
SweetAlert.setDefaults(window._swalDefaults)
}

export default SweetAlert
1 change: 1 addition & 0 deletions src/extensions.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './extensions/withNoNewKeyword'
export * from './extensions/withGlobalDefaults'
41 changes: 41 additions & 0 deletions src/extensions/withGlobalDefaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { warnOnce } from '../utils/utils'
import {showWarningsForParams} from '../utils/params'

const deprecationWarning = `\
"setDefaults" & "resetDefaults" methods are deprecated in favor of "mixin" method and will be removed in the next major release. \
For new projects, use "mixin". For past projects already using "setDefaults", support will be provided through an additional package.`
let defaults = {}

export function withGlobalDefaults (ParentSwal) {
class SwalWithGlobalDefaults extends ParentSwal {
_main (params) {
return super._main(Object.assign({}, defaults, params))
}

static setDefaults (params) {
warnOnce(deprecationWarning)
if (!params || typeof params !== 'object') {
throw new TypeError('SweetAlert2: The argument for setDefaults() is required and has to be a object')
}
showWarningsForParams(params)
// assign valid params from `params` to `defaults`
Object.keys(params).forEach(param => {
if (ParentSwal.isValidParameter(param)) {
defaults[param] = params[param]
}
})
}

static resetDefaults () {
warnOnce(deprecationWarning)
defaults = {}
}
}

// Set default params if `window._swalDefaults` is an object
if (typeof window !== 'undefined' && typeof window._swalDefaults === 'object') {
SwalWithGlobalDefaults.setDefaults(window._swalDefaults)
}

return SwalWithGlobalDefaults
}
6 changes: 1 addition & 5 deletions src/globalState.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
import defaultParams from './utils/params.js'

export default {
popupParams: Object.assign({}, defaultParams)
}
export default {}
4 changes: 2 additions & 2 deletions src/instanceMethods/_main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {showWarningsForParams} from '../utils/params'
import defaultParams, {showWarningsForParams} from '../utils/params'
import * as dom from '../utils/dom/index'
import { swalClasses } from '../utils/classes'
import { formatInputOptions, error, callIfFunction, isThenable } from '../utils/utils'
Expand All @@ -9,7 +9,7 @@ import { openPopup } from '../utils/openPopup'
export function _main (userParams) {
showWarningsForParams(userParams)

const params = this.params = Object.assign({}, globalState.popupParams, userParams)
const params = this.params = Object.assign({}, defaultParams, userParams)
setParameters(params)
Object.freeze(params)

Expand Down
1 change: 0 additions & 1 deletion src/staticMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export * from './staticMethods/close'
export * from './staticMethods/dom'
export * from './staticMethods/mixin'
export * from './staticMethods/queue'
export * from './staticMethods/set-reset-defaults'
export * from './staticMethods/showLoading'
export * from './staticMethods/fire'
export {
Expand Down
29 changes: 0 additions & 29 deletions src/staticMethods/set-reset-defaults.js

This file was deleted.

7 changes: 5 additions & 2 deletions src/sweetalert2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import SweetAlert from './SweetAlert'
import { withNoNewKeyword } from './extensions'
import { withGlobalDefaults, withNoNewKeyword } from './extensions'

export default withNoNewKeyword(SweetAlert)
const Swal = withNoNewKeyword(withGlobalDefaults(SweetAlert))
Swal.default = Swal

export default Swal