diff options
| author | fat <[email protected]> | 2015-05-07 16:34:28 -0700 |
|---|---|---|
| committer | fat <[email protected]> | 2015-05-07 16:34:28 -0700 |
| commit | c3a79b1a8c2fa8d7fc8edcd3e626dad8b45d5dc3 (patch) | |
| tree | 06a02f216b801efdba5d920da89b2cad47fd2408 /js/src | |
| parent | 0724bd91ff81b5eca0addce0c336c72b3ec10be5 (diff) | |
| download | bootstrap-c3a79b1a8c2fa8d7fc8edcd3e626dad8b45d5dc3.tar.xz bootstrap-c3a79b1a8c2fa8d7fc8edcd3e626dad8b45d5dc3.zip | |
change the export pattern to protect against leaking globals
Diffstat (limited to 'js/src')
| -rw-r--r-- | js/src/alert.js | 238 | ||||
| -rw-r--r-- | js/src/util.js | 166 |
2 files changed, 210 insertions, 194 deletions
diff --git a/js/src/alert.js b/js/src/alert.js index bd12b1a55..d69ad8910 100644 --- a/js/src/alert.js +++ b/js/src/alert.js @@ -1,3 +1,6 @@ +import Util from './util' + + /** * -------------------------------------------------------------------------- * Bootstrap (v4.0.0): alert.js @@ -5,164 +8,171 @@ * -------------------------------------------------------------------------- */ -import Util from 'util' +const Alert = (() => { -/** - * -------------------------------------------------------------------------- - * Constants - * -------------------------------------------------------------------------- - */ + /** + * ------------------------------------------------------------------------ + * Constants + * ------------------------------------------------------------------------ + */ -const NAME = 'alert' -const VERSION = '4.0.0' -const DATA_KEY = 'bs.alert' -const JQUERY_NO_CONFLICT = $.fn[NAME] -const TRANSITION_DURATION = 150 + const NAME = 'alert' + const VERSION = '4.0.0' + const DATA_KEY = 'bs.alert' + const JQUERY_NO_CONFLICT = $.fn[NAME] + const TRANSITION_DURATION = 150 -const Selector = { - DISMISS : '[data-dismiss="alert"]' -} + const Selector = { + DISMISS : '[data-dismiss="alert"]' + } -const Event = { - CLOSE : 'close.bs.alert', - CLOSED : 'closed.bs.alert', - CLICK : 'click.bs.alert.data-api' -} + const Event = { + CLOSE : 'close.bs.alert', + CLOSED : 'closed.bs.alert', + CLICK : 'click.bs.alert.data-api' + } -const ClassName = { - ALERT : 'alert', - FADE : 'fade', - IN : 'in' -} + const ClassName = { + ALERT : 'alert', + FADE : 'fade', + IN : 'in' + } -/** - * -------------------------------------------------------------------------- - * Class Definition - * -------------------------------------------------------------------------- - */ + /** + * ------------------------------------------------------------------------ + * Class Definition + * ------------------------------------------------------------------------ + */ -export class Alert { + class Alert { - constructor(element) { - if (element) { - this.element = element + constructor(element) { + if (element) { + this.element = element + } } - } - // public + // public + + close(element) { + let rootElement = this._getRootElement(element) + let customEvent = this._triggerCloseEvent(rootElement) - close(element) { - let rootElement = this._getRootElement(element) - let customEvent = this._triggerCloseEvent(rootElement) + if (customEvent.isDefaultPrevented()) { + return + } - if (customEvent.isDefaultPrevented()) { - return + this._removeElement(rootElement) } - this._removeElement(rootElement) - } + // private - // private + _getRootElement(element) { + let parent = false + let selector = Util.getSelectorFromElement(element) - _getRootElement(element) { - let parent = false - let selector = Util.getSelectorFromElement(element) + if (selector) { + parent = $(selector)[0] + } - if (selector) { - parent = $(selector)[0] + if (!parent) { + parent = $(element).closest('.' + ClassName.ALERT)[0] + } + + return parent } - if (!parent) { - parent = $(element).closest('.' + ClassName.ALERT)[0] + _triggerCloseEvent(element) { + var closeEvent = $.Event(Event.CLOSE) + $(element).trigger(closeEvent) + return closeEvent } - return parent - } + _removeElement(element) { + $(element).removeClass(ClassName.IN) - _triggerCloseEvent(element) { - var closeEvent = $.Event(Event.CLOSE) - $(element).trigger(closeEvent) - return closeEvent - } + if (!Util.supportsTransitionEnd() || + !$(element).hasClass(ClassName.FADE)) { + this._destroyElement(element) + return + } - _removeElement(element) { - $(element).removeClass(ClassName.IN) + $(element) + .one(Util.TRANSITION_END, this._destroyElement.bind(this, element)) + .emulateTransitionEnd(TRANSITION_DURATION) + } - if (!Util.supportsTransitionEnd() || !$(element).hasClass(ClassName.FADE)) { - this._destroyElement(element) - return + _destroyElement(element) { + $(element) + .detach() + .trigger(Event.CLOSED) + .remove() } - $(element) - .one(Util.TRANSITION_END, this._destroyElement.bind(this, element)) - .emulateTransitionEnd(TRANSITION_DURATION) - } - _destroyElement(element) { - $(element) - .detach() - .trigger(Event.CLOSED) - .remove() - } + // static + static _jQueryInterface(config) { + return this.each(function () { + let $element = $(this) + let data = $element.data(DATA_KEY) - // static + if (!data) { + data = new Alert(this) + $element.data(DATA_KEY, data) + } - static _jQueryInterface(config) { - return this.each(function () { - let $element = $(this) - let data = $element.data(DATA_KEY) + if (config === 'close') { + data[config](this) + } + }) + } - if (!data) { - data = new Alert(this) - $element.data(DATA_KEY, data) - } + static _handleDismiss(alertInstance) { + return function (event) { + if (event) { + event.preventDefault() + } - if (config === 'close') { - data[config](this) + alertInstance.close(this) } - }) + } + } - static _handleDismiss(alertInstance) { - return function (event) { - if (event) { - event.preventDefault() - } - alertInstance.close(this) - } - } + /** + * ------------------------------------------------------------------------ + * Data Api implementation + * ------------------------------------------------------------------------ + */ -} + $(document).on( + Event.CLICK, + Selector.DISMISS, + Alert._handleDismiss(new Alert()) + ) -/** - * -------------------------------------------------------------------------- - * Data Api implementation - * -------------------------------------------------------------------------- - */ + /** + * ------------------------------------------------------------------------ + * jQuery + * ------------------------------------------------------------------------ + */ -$(document).on( - Event.CLICK, - Selector.DISMISS, - Alert._handleDismiss(new Alert()) -) + $.fn[NAME] = Alert._jQueryInterface + $.fn[NAME].Constructor = Alert + $.fn[NAME].noConflict = function () { + $.fn[NAME] = Alert._JQUERY_NO_CONFLICT + return Alert._jQueryInterface + } + return Alert -/** - * -------------------------------------------------------------------------- - * jQuery - * -------------------------------------------------------------------------- - */ +})() -$.fn[NAME] = Alert._jQueryInterface -$.fn[NAME].Constructor = Alert -$.fn[NAME].noConflict = function () { - $.fn[NAME] = Alert._JQUERY_NO_CONFLICT - return Alert._jQueryInterface -} +export default Alert diff --git a/js/src/util.js b/js/src/util.js index e9542149e..68205edef 100644 --- a/js/src/util.js +++ b/js/src/util.js @@ -5,114 +5,120 @@ * -------------------------------------------------------------------------- */ +const Util = (() => { -/** - * -------------------------------------------------------------------------- - * Public Util Api - * -------------------------------------------------------------------------- - */ -var Util = { + /** + * ------------------------------------------------------------------------ + * Private TransitionEnd Helpers + * ------------------------------------------------------------------------ + */ - TRANSITION_END: 'bsTransitionEnd', + let transition = false - getUID(prefix) { - do prefix += ~~(Math.random() * 1000000) - while (document.getElementById(prefix)) - return prefix - }, + const TransitionEndEvent = { + WebkitTransition : 'webkitTransitionEnd', + MozTransition : 'transitionend', + OTransition : 'oTransitionEnd otransitionend', + transition : 'transitionend' + } - getSelectorFromElement(element) { - let selector = element.getAttribute('data-target') + function getSpecialTransitionEndEvent() { + return { + bindType: transition.end, + delegateType: transition.end, + handle: function (event) { + if ($(event.target).is(this)) { + return event.handleObj.handler.apply(this, arguments) + } + } + } + } - if (!selector) { - selector = element.getAttribute('href') || '' - selector = /^#[a-z]/i.test(selector) ? selector : null + function transitionEndTest() { + if (window.QUnit) { + return false } - return selector - }, + let el = document.createElement('bootstrap') - reflow(element) { - new Function('bs', 'return bs')(element.offsetHeight) - }, + for (var name in TransitionEndEvent) { + if (el.style[name] !== undefined) { + return { end: TransitionEndEvent[name] } + } + } - supportsTransitionEnd() { - return !!transition + return false } -} - -export default Util - + function transitionEndEmulator(duration) { + let called = false -/** - * -------------------------------------------------------------------------- - * Private TransitionEnd Helpers - * -------------------------------------------------------------------------- - */ + $(this).one(Util.TRANSITION_END, function () { + called = true + }) -let transition = false - -const TransitionEndEvent = { - WebkitTransition : 'webkitTransitionEnd', - MozTransition : 'transitionend', - OTransition : 'oTransitionEnd otransitionend', - transition : 'transitionend' -} - -function getSpecialTransitionEndEvent() { - return { - bindType: transition.end, - delegateType: transition.end, - handle: function (event) { - if ($(event.target).is(this)) { - return event.handleObj.handler.apply(this, arguments) + setTimeout(() => { + if (!called) { + $(this).trigger(transition.end) } - } - } -} + }, duration) -function transitionEndTest() { - if (window.QUnit) { - return false + return this } - let el = document.createElement('bootstrap') + function setTransitionEndSupport() { + transition = transitionEndTest() + + $.fn.emulateTransitionEnd = transitionEndEmulator - for (var name in TransitionEndEvent) { - if (el.style[name] !== undefined) { - return { end: TransitionEndEvent[name] } + if (Util.supportsTransitionEnd()) { + $.event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent() } } - return false -} -function transitionEndEmulator(duration) { - let called = false + /** + * -------------------------------------------------------------------------- + * Public Util Api + * -------------------------------------------------------------------------- + */ - $(this).one(Util.TRANSITION_END, function () { - called = true - }) + let Util = { - setTimeout(() => { - if (!called) { - $(this).trigger(transition.end) - } - }, duration) + TRANSITION_END: 'bsTransitionEnd', + + getUID(prefix) { + do prefix += ~~(Math.random() * 1000000) + while (document.getElementById(prefix)) + return prefix + }, - return this -} + getSelectorFromElement(element) { + let selector = element.getAttribute('data-target') -function setTransitionEndSupport() { - transition = transitionEndTest() + if (!selector) { + selector = element.getAttribute('href') || '' + selector = /^#[a-z]/i.test(selector) ? selector : null + } + + return selector + }, - $.fn.emulateTransitionEnd = transitionEndEmulator + reflow(element) { + new Function('bs', 'return bs')(element.offsetHeight) + }, + + supportsTransitionEnd() { + return !!transition + } - if (Util.supportsTransitionEnd()) { - $.event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent() } -} -setTransitionEndSupport() + setTransitionEndSupport() + + return Util + +})() + +export default Util |
