From 0724bd91ff81b5eca0addce0c336c72b3ec10be5 Mon Sep 17 00:00:00 2001 From: fat Date: Thu, 7 May 2015 12:48:22 -0700 Subject: es6 alert :| --- js/src/util.js | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 js/src/util.js (limited to 'js/src/util.js') diff --git a/js/src/util.js b/js/src/util.js new file mode 100644 index 000000000..e9542149e --- /dev/null +++ b/js/src/util.js @@ -0,0 +1,118 @@ +/** + * -------------------------------------------------------------------------- + * Bootstrap (v4.0.0): util.js + * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) + * -------------------------------------------------------------------------- + */ + + +/** + * -------------------------------------------------------------------------- + * Public Util Api + * -------------------------------------------------------------------------- + */ + +var Util = { + + TRANSITION_END: 'bsTransitionEnd', + + getUID(prefix) { + do prefix += ~~(Math.random() * 1000000) + while (document.getElementById(prefix)) + return prefix + }, + + getSelectorFromElement(element) { + let selector = element.getAttribute('data-target') + + if (!selector) { + selector = element.getAttribute('href') || '' + selector = /^#[a-z]/i.test(selector) ? selector : null + } + + return selector + }, + + reflow(element) { + new Function('bs', 'return bs')(element.offsetHeight) + }, + + supportsTransitionEnd() { + return !!transition + } + +} + +export default Util + + +/** + * -------------------------------------------------------------------------- + * Private TransitionEnd Helpers + * -------------------------------------------------------------------------- + */ + +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) + } + } + } +} + +function transitionEndTest() { + if (window.QUnit) { + return false + } + + let el = document.createElement('bootstrap') + + for (var name in TransitionEndEvent) { + if (el.style[name] !== undefined) { + return { end: TransitionEndEvent[name] } + } + } + + return false +} + +function transitionEndEmulator(duration) { + let called = false + + $(this).one(Util.TRANSITION_END, function () { + called = true + }) + + setTimeout(() => { + if (!called) { + $(this).trigger(transition.end) + } + }, duration) + + return this +} + +function setTransitionEndSupport() { + transition = transitionEndTest() + + $.fn.emulateTransitionEnd = transitionEndEmulator + + if (Util.supportsTransitionEnd()) { + $.event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent() + } +} + +setTransitionEndSupport() -- cgit v1.2.3 From c3a79b1a8c2fa8d7fc8edcd3e626dad8b45d5dc3 Mon Sep 17 00:00:00 2001 From: fat Date: Thu, 7 May 2015 16:34:28 -0700 Subject: change the export pattern to protect against leaking globals --- js/src/util.js | 166 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 86 insertions(+), 80 deletions(-) (limited to 'js/src/util.js') 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 -- cgit v1.2.3 From 660505188241418ffda53b5eb848defecd5f57e1 Mon Sep 17 00:00:00 2001 From: fat Date: Thu, 7 May 2015 17:07:38 -0700 Subject: button -> es6 --- js/src/util.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'js/src/util.js') diff --git a/js/src/util.js b/js/src/util.js index 68205edef..abc548a45 100644 --- a/js/src/util.js +++ b/js/src/util.js @@ -5,7 +5,7 @@ * -------------------------------------------------------------------------- */ -const Util = (() => { +const Util = (($) => { /** @@ -119,6 +119,6 @@ const Util = (() => { return Util -})() +})(jQuery) export default Util -- cgit v1.2.3 From 1b183e2ff796fc22aba8a8cac074a306c083d018 Mon Sep 17 00:00:00 2001 From: fat Date: Thu, 7 May 2015 22:26:40 -0700 Subject: carousel -> es6 --- js/src/util.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'js/src/util.js') diff --git a/js/src/util.js b/js/src/util.js index abc548a45..c9ffbe555 100644 --- a/js/src/util.js +++ b/js/src/util.js @@ -60,7 +60,7 @@ const Util = (($) => { setTimeout(() => { if (!called) { - $(this).trigger(transition.end) + Util.triggerTransitionEnd(this) } }, duration) @@ -109,6 +109,10 @@ const Util = (($) => { new Function('bs', 'return bs')(element.offsetHeight) }, + triggerTransitionEnd(element) { + $(element).trigger(transition.end) + }, + supportsTransitionEnd() { return !!transition } -- cgit v1.2.3 From eaab1def7af7d7e1ab32ff69d043b46e2815ca22 Mon Sep 17 00:00:00 2001 From: fat Date: Wed, 13 May 2015 14:46:50 -0700 Subject: add simple type checker implementation --- js/src/util.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'js/src/util.js') diff --git a/js/src/util.js b/js/src/util.js index c9ffbe555..86bea6578 100644 --- a/js/src/util.js +++ b/js/src/util.js @@ -23,6 +23,15 @@ const Util = (($) => { transition : 'transitionend' } + // shoutout AngusCroll (https://goo.gl/pxwQGp) + function toType(obj) { + return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase() + } + + function isElement(obj) { + return (obj[0] || obj).nodeType; + } + function getSpecialTransitionEndEvent() { return { bindType: transition.end, @@ -115,6 +124,25 @@ const Util = (($) => { supportsTransitionEnd() { return !!transition + }, + + typeCheckConfig(componentName, config, configTypes) { + + for (let property in configTypes) { + let expectedTypes = configTypes[property] + let value = config[property] + let valueType + + if (value && isElement(value)) valueType = 'element' + else valueType = toType(value) + + if (!new RegExp(expectedTypes).test(valueType)) { + throw new Error( + `${componentName.toUpperCase()}: ` + + `Option "${property}" provided type "${valueType}" ` + + `but expected type "${expectedTypes}".`) + } + } } } -- cgit v1.2.3