1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-08-25 14:30:46 +02:00

add simple type checker implementation

This commit is contained in:
fat
2015-05-13 14:46:50 -07:00
parent c2ced2292a
commit eaab1def7a
25 changed files with 258 additions and 28 deletions

19
js/dist/carousel.js vendored
View File

@@ -35,6 +35,14 @@ var Carousel = (function ($) {
wrap: true
};
var DefaultType = {
interval: '(number|boolean)',
keyboard: 'boolean',
slide: '(boolean|string)',
pause: 'string',
wrap: 'boolean'
};
var Direction = {
NEXT: 'next',
PREVIOUS: 'prev'
@@ -86,7 +94,7 @@ var Carousel = (function ($) {
this._isPaused = false;
this._isSliding = false;
this._config = config;
this._config = this._getConfig(config);
this._element = $(element)[0];
this._indicatorsElement = $(this._element).find(Selector.INDICATORS)[0];
@@ -187,10 +195,17 @@ var Carousel = (function ($) {
this._indicatorsElement = null;
}
}, {
key: '_addEventListeners',
key: '_getConfig',
// private
value: function _getConfig(config) {
config = $.extend({}, Default, config);
Util.typeCheckConfig(NAME, config, DefaultType);
return config;
}
}, {
key: '_addEventListeners',
value: function _addEventListeners() {
if (this._config.keyboard) {
$(this._element).on(Event.KEYDOWN, $.proxy(this._keydown, this));

File diff suppressed because one or more lines are too long

17
js/dist/collapse.js vendored
View File

@@ -32,6 +32,11 @@ var Collapse = (function ($) {
parent: null
};
var DefaultType = {
toggle: 'boolean',
parent: '(string|null)'
};
var Event = {
SHOW: 'show' + EVENT_KEY,
SHOWN: 'shown' + EVENT_KEY,
@@ -69,7 +74,7 @@ var Collapse = (function ($) {
this._isTransitioning = false;
this._element = element;
this._config = $.extend({}, Default, config);
this._config = this._getConfig(config);
this._triggerArray = $.makeArray($('[data-toggle="collapse"][href="#' + element.id + '"],' + ('[data-toggle="collapse"][data-target="#' + element.id + '"]')));
this._parent = this._config.parent ? this._getParent() : null;
@@ -230,10 +235,18 @@ var Collapse = (function ($) {
this._isTransitioning = null;
}
}, {
key: '_getDimension',
key: '_getConfig',
// private
value: function _getConfig(config) {
config = $.extend({}, Default, config);
config.toggle = !!config.toggle;
Util.typeCheckConfig(NAME, config, DefaultType);
return config;
}
}, {
key: '_getDimension',
value: function _getDimension() {
var hasWidth = $(this._element).hasClass(Dimension.WIDTH);
return hasWidth ? Dimension.WIDTH : Dimension.HEIGHT;

File diff suppressed because one or more lines are too long

18
js/dist/modal.js vendored
View File

@@ -35,6 +35,13 @@ var Modal = (function ($) {
show: true
};
var DefaultType = {
backdrop: '(boolean|string)',
keyboard: 'boolean',
focus: 'boolean',
show: 'boolean'
};
var Event = {
HIDE: 'hide' + EVENT_KEY,
HIDDEN: 'hidden' + EVENT_KEY,
@@ -73,7 +80,7 @@ var Modal = (function ($) {
function Modal(element, config) {
_classCallCheck(this, Modal);
this._config = config;
this._config = this._getConfig(config);
this._element = element;
this._dialog = $(element).find(Selector.DIALOG)[0];
this._backdrop = null;
@@ -184,10 +191,17 @@ var Modal = (function ($) {
this._scrollbarWidth = null;
}
}, {
key: '_showElement',
key: '_getConfig',
// private
value: function _getConfig(config) {
config = $.extend({}, Default, config);
Util.typeCheckConfig(NAME, config, DefaultType);
return config;
}
}, {
key: '_showElement',
value: function _showElement(relatedTarget) {
var _this2 = this;

File diff suppressed because one or more lines are too long

9
js/dist/popover.js vendored
View File

@@ -34,6 +34,10 @@ var Popover = (function ($) {
template: '<div class="popover" role="tooltip">' + '<div class="popover-arrow"></div>' + '<h3 class="popover-title"></h3>' + '<div class="popover-content"></div></div>'
});
var DefaultType = $.extend({}, Tooltip.DefaultType, {
content: '(string|function)'
});
var ClassName = {
FADE: 'fade',
IN: 'in'
@@ -148,6 +152,11 @@ var Popover = (function ($) {
get: function () {
return EVENT_KEY;
}
}, {
key: 'DefaultType',
get: function () {
return DefaultType;
}
}, {
key: '_jQueryInterface',

File diff suppressed because one or more lines are too long

View File

@@ -32,6 +32,12 @@ var ScrollSpy = (function ($) {
target: ''
};
var DefaultType = {
offset: 'number',
method: 'string',
target: '(string|element)'
};
var Event = {
ACTIVATE: 'activate' + EVENT_KEY,
SCROLL: 'scroll' + EVENT_KEY,
@@ -155,6 +161,8 @@ var ScrollSpy = (function ($) {
config.target = '#' + id;
}
Util.typeCheckConfig(NAME, config, DefaultType);
return config;
}
}, {

File diff suppressed because one or more lines are too long

5
js/dist/tab.js vendored
View File

@@ -219,11 +219,6 @@ var Tab = (function ($) {
get: function () {
return VERSION;
}
}, {
key: 'Default',
get: function () {
return Default;
}
}, {
key: '_jQueryInterface',

2
js/dist/tab.js.map vendored

File diff suppressed because one or more lines are too long

22
js/dist/tooltip.js vendored
View File

@@ -37,7 +37,20 @@ var Tooltip = (function ($) {
selector: false,
placement: 'top',
offset: '0 0',
constraints: null
constraints: []
};
var DefaultType = {
animation: 'boolean',
template: 'string',
title: '(string|function)',
trigger: 'string',
delay: '(number|object)',
html: 'boolean',
selector: '(string|boolean)',
placement: '(string|function)',
offset: 'string',
constraints: 'array'
};
var AttachmentMap = {
@@ -476,6 +489,8 @@ var Tooltip = (function ($) {
};
}
Util.typeCheckConfig(NAME, config, this.constructor.DefaultType);
return config;
}
}, {
@@ -527,6 +542,11 @@ var Tooltip = (function ($) {
get: function () {
return EVENT_KEY;
}
}, {
key: 'DefaultType',
get: function () {
return DefaultType;
}
}, {
key: '_jQueryInterface',

File diff suppressed because one or more lines are too long

24
js/dist/util.js vendored
View File

@@ -24,6 +24,15 @@ var Util = (function ($) {
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,
@@ -116,6 +125,21 @@ var Util = (function ($) {
supportsTransitionEnd: function supportsTransitionEnd() {
return !!transition;
},
typeCheckConfig: function typeCheckConfig(componentName, config, configTypes) {
for (var property in configTypes) {
var expectedTypes = configTypes[property];
var value = config[property];
var valueType = undefined;
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 + '".'));
}
}
}
};

2
js/dist/util.js.map vendored

File diff suppressed because one or more lines are too long