0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-01 13:24:25 +01:00

Use named constants for magic numbers (#19992)

Mostly KeyboardEvent.which and MouseEvent.which values.

[skip validator]
This commit is contained in:
Chris Rebert 2016-05-30 23:42:00 -07:00
parent 301dc70e1b
commit 5a35ad6d74
4 changed files with 25 additions and 16 deletions

View File

@ -24,6 +24,8 @@ const Carousel = (($) => {
const DATA_API_KEY = '.data-api'
const JQUERY_NO_CONFLICT = $.fn[NAME]
const TRANSITION_DURATION = 600
const ARROW_LEFT_KEYCODE = 37 // KeyboardEvent.which value for left arrow key
const ARROW_RIGHT_KEYCODE = 39 // KeyboardEvent.which value for right arrow key
const Default = {
interval : 5000,
@ -236,8 +238,8 @@ const Carousel = (($) => {
}
switch (event.which) {
case 37: this.prev(); break
case 39: this.next(); break
case ARROW_LEFT_KEYCODE: this.prev(); break
case ARROW_RIGHT_KEYCODE: this.next(); break
default: return
}
}

View File

@ -17,12 +17,16 @@ const Dropdown = (($) => {
* ------------------------------------------------------------------------
*/
const NAME = 'dropdown'
const VERSION = '4.0.0-alpha.2'
const DATA_KEY = 'bs.dropdown'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
const JQUERY_NO_CONFLICT = $.fn[NAME]
const NAME = 'dropdown'
const VERSION = '4.0.0-alpha.2'
const DATA_KEY = 'bs.dropdown'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
const JQUERY_NO_CONFLICT = $.fn[NAME]
const ESCAPE_KEYCODE = 27 // KeyboardEvent.which value for Escape (Esc) key
const ARROW_UP_KEYCODE = 38 // KeyboardEvent.which value for up arrow key
const ARROW_DOWN_KEYCODE = 40 // KeyboardEvent.which value for down arrow key
const RIGHT_MOUSE_BUTTON_WHICH = 3 // MouseEvent.which value for the right button (assuming a right-handed mouse)
const Event = {
HIDE : `hide${EVENT_KEY}`,
@ -152,7 +156,7 @@ const Dropdown = (($) => {
}
static _clearMenus(event) {
if (event && event.which === 3) {
if (event && event.which === RIGHT_MOUSE_BUTTON_WHICH) {
return
}
@ -218,10 +222,10 @@ const Dropdown = (($) => {
let parent = Dropdown._getParentFromElement(this)
let isActive = $(parent).hasClass(ClassName.OPEN)
if ((!isActive && event.which !== 27) ||
(isActive && event.which === 27)) {
if ((!isActive && event.which !== ESCAPE_KEYCODE) ||
(isActive && event.which === ESCAPE_KEYCODE)) {
if (event.which === 27) {
if (event.which === ESCAPE_KEYCODE) {
let toggle = $(parent).find(Selector.DATA_TOGGLE)[0]
$(toggle).trigger('focus')
}
@ -242,11 +246,11 @@ const Dropdown = (($) => {
let index = items.indexOf(event.target)
if (event.which === 38 && index > 0) { // up
if (event.which === ARROW_UP_KEYCODE && index > 0) { // up
index--
}
if (event.which === 40 && index < items.length - 1) { // down
if (event.which === ARROW_DOWN_KEYCODE && index < items.length - 1) { // down
index++
}

View File

@ -25,6 +25,7 @@ const Modal = (($) => {
const JQUERY_NO_CONFLICT = $.fn[NAME]
const TRANSITION_DURATION = 300
const BACKDROP_TRANSITION_DURATION = 150
const ESCAPE_KEYCODE = 27 // Escape (Esc) key
const Default = {
backdrop : true,
@ -271,7 +272,7 @@ const Modal = (($) => {
_setEscapeEvent() {
if (this._isShown && this._config.keyboard) {
$(this._element).on(Event.KEYDOWN_DISMISS, (event) => {
if (event.which === 27) {
if (event.which === ESCAPE_KEYCODE) {
this.hide()
}
})

View File

@ -16,6 +16,8 @@ const Util = (($) => {
let transition = false
const MAX_UID = 1000000
const TransitionEndEvent = {
WebkitTransition : 'webkitTransitionEnd',
MozTransition : 'transitionend',
@ -100,7 +102,7 @@ const Util = (($) => {
getUID(prefix) {
do {
/* eslint-disable no-bitwise */
prefix += ~~(Math.random() * 1000000) // "~~" acts like a faster Math.floor() here
prefix += ~~(Math.random() * MAX_UID) // "~~" acts like a faster Math.floor() here
/* eslint-enable no-bitwise */
} while (document.getElementById(prefix))
return prefix