mirror of
https://github.com/Choices-js/Choices.git
synced 2024-05-03 14:23:12 +02:00
ESLint entire project
This commit is contained in:
parent
330eb73594
commit
9f0dc2c8dc
10
.eslintrc
10
.eslintrc
|
@ -10,5 +10,15 @@
|
|||
"rules": {
|
||||
"quotes": [2, "single"],
|
||||
"strict": [2, "never"],
|
||||
"indent": ["error", 4, {"SwitchCase": 1}],
|
||||
"eol-last": "off",
|
||||
"arrow-body-style": "off",
|
||||
"no-underscore-dangle": "off",
|
||||
"no-new": 0,
|
||||
"max-len": "off",
|
||||
"no-console": ["error", { allow: ["warn", "error"] }],
|
||||
"consistent-return": "off",
|
||||
"no-param-reassign": ["error", { "props": false }],
|
||||
"no-unused-vars": ["error", { "args": "none" }]
|
||||
},
|
||||
}
|
||||
|
|
8
.eslintrc.js
Normal file
8
.eslintrc.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
module.exports = {
|
||||
"extends": "airbnb",
|
||||
"plugins": [
|
||||
"react",
|
||||
"jsx-a11y",
|
||||
"import"
|
||||
]
|
||||
};
|
4
assets/scripts/dist/choices.min.js
vendored
4
assets/scripts/dist/choices.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -6,7 +6,7 @@ export const addItem = (value, label, id, choiceId, activateOptions) => {
|
|||
id,
|
||||
choiceId,
|
||||
activateOptions,
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export const removeItem = (id, choiceId) => {
|
||||
|
@ -14,7 +14,7 @@ export const removeItem = (id, choiceId) => {
|
|||
type: 'REMOVE_ITEM',
|
||||
id,
|
||||
choiceId,
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export const highlightItem = (id, highlighted) => {
|
||||
|
@ -22,7 +22,7 @@ export const highlightItem = (id, highlighted) => {
|
|||
type: 'HIGHLIGHT_ITEM',
|
||||
id,
|
||||
highlighted,
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export const addChoice = (value, label, id, groupId, disabled) => {
|
||||
|
@ -33,21 +33,21 @@ export const addChoice = (value, label, id, groupId, disabled) => {
|
|||
id,
|
||||
groupId,
|
||||
disabled,
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export const filterChoices = (results) => {
|
||||
return {
|
||||
type: 'FILTER_CHOICES',
|
||||
results,
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export const activateChoices = (active = true) => {
|
||||
return {
|
||||
type: 'ACTIVATE_CHOICES',
|
||||
active,
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export const addGroup = (value, id, active, disabled) => {
|
||||
|
@ -57,11 +57,11 @@ export const addGroup = (value, id, active, disabled) => {
|
|||
id,
|
||||
active,
|
||||
disabled,
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export const clearAll = () => {
|
||||
return {
|
||||
type: 'CLEAR_ALL',
|
||||
}
|
||||
};
|
||||
};
|
File diff suppressed because it is too large
Load diff
|
@ -1,3 +1,5 @@
|
|||
/* eslint-disable */
|
||||
|
||||
// Production steps of ECMA-262, Edition 6, 22.1.2.1
|
||||
// Reference: https://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.from
|
||||
if (!Array.from) {
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
export const hasClass = (elem, className) => {
|
||||
return new RegExp(' ' + className + ' ').test(' ' + elem.className + ' ');
|
||||
}
|
||||
/* eslint-disable */
|
||||
|
||||
/**
|
||||
* Capitalises the first letter of each word in a string
|
||||
* Capitalises the first letter of each word in a string
|
||||
* @param {String} str String to capitalise
|
||||
* @return {String} Capitalised string
|
||||
*/
|
||||
|
@ -31,7 +29,7 @@ export const isType = function(type, obj) {
|
|||
*/
|
||||
export const isNode = (o) => {
|
||||
return (
|
||||
typeof Node === "object" ? o instanceof Node :
|
||||
typeof Node === "object" ? o instanceof Node :
|
||||
o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName==="string"
|
||||
);
|
||||
};
|
||||
|
@ -46,7 +44,7 @@ export const isElement = (o) => {
|
|||
typeof HTMLElement === "object" ? o instanceof HTMLElement : //DOM2
|
||||
o && typeof o === "object" && o !== null && o.nodeType === 1 && typeof o.nodeName==="string"
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Merges unspecified amount of objects into new object
|
||||
|
@ -111,7 +109,7 @@ export const whichTransitionEvent = function(){
|
|||
return transitions[t];
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* CSS animation end event listener
|
||||
|
@ -136,7 +134,7 @@ export const whichAnimationEvent = function() {
|
|||
};
|
||||
|
||||
/**
|
||||
* Get the ancestors of each element in the current set of matched elements,
|
||||
* Get the ancestors of each element in the current set of matched elements,
|
||||
* up to but not including the element matched by the selector
|
||||
* @param {NodeElement} elem Element to begin search from
|
||||
* @param {NodeElement} parent Parent to find
|
||||
|
@ -243,9 +241,9 @@ export const getSiblings = function (elem) {
|
|||
return siblings;
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Find ancestor in DOM tree
|
||||
* @param {NodeElement} el Element to start search from
|
||||
* @param {NodeElement} el Element to start search from
|
||||
* @param {[type]} cls Class of parent
|
||||
* @return {NodeElement} Found parent element
|
||||
*/
|
||||
|
@ -258,7 +256,7 @@ export const findAncestor = function(el, cls) {
|
|||
* Debounce an event handler.
|
||||
* @param {Function} func Function to run after wait
|
||||
* @param {Number} wait The delay before the function is executed
|
||||
* @param {Boolean} immediate If passed, trigger the function on the leading edge, instead of the trailing.
|
||||
* @param {Boolean} immediate If passed, trigger the function on the leading edge, instead of the trailing.
|
||||
* @return {Function} A function will be called after it stops being called for a given delay
|
||||
*/
|
||||
export const debounce = function(func, wait, immediate) {
|
||||
|
@ -309,21 +307,21 @@ export const getElementOffset = function(el, offset) {
|
|||
};
|
||||
|
||||
/**
|
||||
* Get the next or previous element from a given start point
|
||||
* Get the next or previous element from a given start point
|
||||
* @param {HTMLElement} startEl Element to start position from
|
||||
* @param {String} className The class we will look through
|
||||
* @param {String} className The class we will look through
|
||||
* @param {Number} direction Positive next element, negative previous element
|
||||
* @return {[HTMLElement} Found element
|
||||
*/
|
||||
export const getAdjacentEl = (startEl, className, direction = 1) => {
|
||||
if(!startEl || !className) return;
|
||||
if(!startEl || !className) return;
|
||||
|
||||
const parent = startEl.parentNode.parentNode;
|
||||
const children = Array.from(parent.querySelectorAll(className));
|
||||
|
||||
const startPos = children.indexOf(startEl);
|
||||
const operatorDirection = direction > 0 ? 1 : -1;
|
||||
|
||||
|
||||
return children[startPos + operatorDirection];
|
||||
};
|
||||
|
||||
|
@ -354,10 +352,10 @@ export const isInView = function(el, position, offset) {
|
|||
};
|
||||
|
||||
/**
|
||||
* Determine whether an element is within
|
||||
* Determine whether an element is within
|
||||
* @param {HTMLElement} el Element to test
|
||||
* @param {HTMLElement} parent Scrolling parent
|
||||
* @param {Number} direction Whether element is visible from above or below
|
||||
* @param {Number} direction Whether element is visible from above or below
|
||||
* @return {Boolean}
|
||||
*/
|
||||
export const isScrolledIntoView = (el, parent, direction = 1) => {
|
||||
|
@ -372,9 +370,9 @@ export const isScrolledIntoView = (el, parent, direction = 1) => {
|
|||
// In view from top
|
||||
isVisible = el.offsetTop >= parent.scrollTop;
|
||||
}
|
||||
|
||||
|
||||
return isVisible;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove html tags from a string
|
||||
|
@ -387,7 +385,7 @@ export const stripHTML = function(html) {
|
|||
return el.textContent || el.innerText || "";
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Adds animation to an element and removes it upon animation completion
|
||||
* @param {Element} el Element to add animation to
|
||||
* @param {String} animation Animation class to add to element
|
||||
|
@ -414,7 +412,7 @@ export const addAnimation = (el, animation) => {
|
|||
*/
|
||||
export const getRandomNumber = function(min, max) {
|
||||
return Math.floor(Math.random() * (max - min) + min);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Turn a string into a node
|
||||
|
@ -436,7 +434,7 @@ export const strToEl = (function() {
|
|||
};
|
||||
}());
|
||||
|
||||
/**
|
||||
/**
|
||||
* Sets the width of a passed input based on its value
|
||||
* @return {Number} Width of input
|
||||
*/
|
||||
|
@ -456,13 +454,13 @@ export const getWidthOfInput = (input) => {
|
|||
document.body.appendChild(testEl);
|
||||
|
||||
if(value && testEl.offsetWidth !== input.offsetWidth) {
|
||||
width = testEl.offsetWidth + 4;
|
||||
width = testEl.offsetWidth + 4;
|
||||
}
|
||||
|
||||
document.body.removeChild(testEl);
|
||||
}
|
||||
|
||||
return `${width}px`;
|
||||
return `${width}px`;
|
||||
};
|
||||
|
||||
export const sortByAlpha = (a, b) => {
|
||||
|
|
|
@ -1,22 +1,28 @@
|
|||
const choices = (state = [], action) => {
|
||||
switch (action.type) {
|
||||
case 'ADD_CHOICE':
|
||||
case 'ADD_CHOICE': {
|
||||
/*
|
||||
A disabled choice appears in the choice dropdown but cannot be selected
|
||||
A selected choice has been added to the passed input's value (added as an item)
|
||||
An active choice appears within the choice dropdown
|
||||
*/
|
||||
return [...state, {
|
||||
id: action.id,
|
||||
groupId: action.groupId,
|
||||
value: action.value,
|
||||
label: action.label,
|
||||
disabled: action.disabled, // A disabled choice appears in the choice dropdown but cannot be selected
|
||||
selected: false, // A selected choice has been added to the passed input's value (added as an item)
|
||||
active: true, // An active choice appears within the choice dropdown
|
||||
disabled: action.disabled,
|
||||
selected: false,
|
||||
active: true,
|
||||
score: 9999,
|
||||
}];
|
||||
}
|
||||
|
||||
case 'ADD_ITEM':
|
||||
case 'ADD_ITEM': {
|
||||
let newState = state;
|
||||
|
||||
// If all choices need to be activated
|
||||
if(action.activateOptions) {
|
||||
if (action.activateOptions) {
|
||||
newState = state.map((choice) => {
|
||||
choice.active = action.active;
|
||||
return choice;
|
||||
|
@ -24,9 +30,9 @@ const choices = (state = [], action) => {
|
|||
}
|
||||
// When an item is added and it has an associated choice,
|
||||
// we want to disable it so it can't be chosen again
|
||||
if(action.choiceId > -1) {
|
||||
if (action.choiceId > -1) {
|
||||
newState = state.map((choice) => {
|
||||
if(choice.id === parseInt(action.choiceId)) {
|
||||
if (choice.id === parseInt(action.choiceId, 10)) {
|
||||
choice.selected = true;
|
||||
}
|
||||
return choice;
|
||||
|
@ -34,48 +40,54 @@ const choices = (state = [], action) => {
|
|||
}
|
||||
|
||||
return newState;
|
||||
}
|
||||
|
||||
case 'REMOVE_ITEM':
|
||||
case 'REMOVE_ITEM': {
|
||||
// When an item is removed and it has an associated choice,
|
||||
// we want to re-enable it so it can be chosen again
|
||||
if(action.choiceId > -1) {
|
||||
if (action.choiceId > -1) {
|
||||
return state.map((choice) => {
|
||||
if(choice.id === parseInt(action.choiceId)) {
|
||||
if (choice.id === parseInt(action.choiceId, 10)) {
|
||||
choice.selected = false;
|
||||
}
|
||||
return choice;
|
||||
});
|
||||
} else {
|
||||
return state;
|
||||
}
|
||||
|
||||
case 'FILTER_CHOICES':
|
||||
return state;
|
||||
}
|
||||
|
||||
case 'FILTER_CHOICES': {
|
||||
const filteredResults = action.results;
|
||||
const filteredState = state.map((choice, index) => {
|
||||
// Set active state based on whether choice is
|
||||
const filteredState = state.map((choice) => {
|
||||
// Set active state based on whether choice is
|
||||
// within filtered results
|
||||
|
||||
|
||||
choice.active = filteredResults.some((result) => {
|
||||
if(result.item.id === choice.id) {
|
||||
if (result.item.id === choice.id) {
|
||||
choice.score = result.score;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
return choice;
|
||||
});
|
||||
|
||||
return filteredState;
|
||||
}
|
||||
|
||||
case 'ACTIVATE_CHOICES':
|
||||
case 'ACTIVATE_CHOICES': {
|
||||
return state.map((choice) => {
|
||||
choice.active = action.active;
|
||||
return choice;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
default:
|
||||
default: {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default choices;
|
|
@ -1,16 +1,18 @@
|
|||
const groups = (state = [], action) => {
|
||||
switch (action.type) {
|
||||
case 'ADD_GROUP':
|
||||
case 'ADD_GROUP': {
|
||||
return [...state, {
|
||||
id: action.id,
|
||||
value: action.value,
|
||||
active: action.active,
|
||||
disabled: action.disabled,
|
||||
}];
|
||||
}
|
||||
|
||||
default:
|
||||
default: {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default groups;
|
|
@ -6,14 +6,14 @@ import choices from './choices';
|
|||
const appReducer = combineReducers({
|
||||
items,
|
||||
groups,
|
||||
choices
|
||||
choices,
|
||||
});
|
||||
|
||||
const rootReducer = (state, action) => {
|
||||
// If we are clearing all items, groups and options we reassign
|
||||
const rootReducer = (passedState, action) => {
|
||||
let state = passedState;
|
||||
// If we are clearing all items, groups and options we reassign
|
||||
// state and then pass that state to our proper reducer. This isn't
|
||||
// mutating our actual state.
|
||||
//
|
||||
// mutating our actual state
|
||||
// See: http://stackoverflow.com/a/35641992
|
||||
if (action.type === 'CLEAR_ALL') {
|
||||
state = undefined;
|
||||
|
|
|
@ -1,43 +1,47 @@
|
|||
const items = (state = [], action) => {
|
||||
switch (action.type) {
|
||||
case 'ADD_ITEM':
|
||||
case 'ADD_ITEM': {
|
||||
// Add object to items array
|
||||
let newState = [...state, {
|
||||
const newState = [...state, {
|
||||
id: action.id,
|
||||
choiceId: action.choiceId,
|
||||
value: action.value,
|
||||
label: action.label,
|
||||
active: true,
|
||||
highlighted: false
|
||||
highlighted: false,
|
||||
}];
|
||||
|
||||
return newState.map((item) => {
|
||||
if(item.highlighted) {
|
||||
if (item.highlighted) {
|
||||
item.highlighted = false;
|
||||
}
|
||||
return item;
|
||||
});
|
||||
}
|
||||
|
||||
case 'REMOVE_ITEM':
|
||||
case 'REMOVE_ITEM': {
|
||||
// Set item to inactive
|
||||
return state.map((item) => {
|
||||
if(item.id === action.id) {
|
||||
if (item.id === action.id) {
|
||||
item.active = false;
|
||||
}
|
||||
return item;
|
||||
});
|
||||
}
|
||||
|
||||
case 'HIGHLIGHT_ITEM':
|
||||
case 'HIGHLIGHT_ITEM': {
|
||||
return state.map((item) => {
|
||||
if(item.id === action.id) {
|
||||
if (item.id === action.id) {
|
||||
item.highlighted = action.highlighted;
|
||||
}
|
||||
return item;
|
||||
});
|
||||
}
|
||||
|
||||
default:
|
||||
default: {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default items;
|
|
@ -1,17 +1,14 @@
|
|||
'use strict';
|
||||
|
||||
import { createStore } from 'redux';
|
||||
import rootReducer from './../reducers/index.js';
|
||||
|
||||
|
||||
export class Store {
|
||||
export default class Store {
|
||||
constructor() {
|
||||
this.store = createStore(
|
||||
rootReducer,
|
||||
rootReducer,
|
||||
window.devToolsExtension ? window.devToolsExtension() : undefined
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get store object (wrapping Redux method)
|
||||
* @return {Object} State
|
||||
|
@ -53,7 +50,6 @@ export class Store {
|
|||
*/
|
||||
getItemsFilteredByActive() {
|
||||
const items = this.getItems();
|
||||
|
||||
const values = items.filter((item) => {
|
||||
return item.active === true;
|
||||
}, []);
|
||||
|
@ -75,12 +71,11 @@ export class Store {
|
|||
}
|
||||
|
||||
/**
|
||||
* Get choices from store
|
||||
* Get choices from store
|
||||
* @return {Array} Option objects
|
||||
*/
|
||||
getChoices() {
|
||||
const state = this.store.getState();
|
||||
|
||||
return state.choices;
|
||||
}
|
||||
|
||||
|
@ -90,10 +85,9 @@ export class Store {
|
|||
*/
|
||||
getChoicesFilteredByActive() {
|
||||
const choices = this.getChoices();
|
||||
|
||||
const values = choices.filter((choice) => {
|
||||
return choice.active === true;
|
||||
},[]);
|
||||
}, []);
|
||||
|
||||
return values;
|
||||
}
|
||||
|
@ -106,7 +100,7 @@ export class Store {
|
|||
const choices = this.getChoices();
|
||||
const values = choices.filter((choice) => {
|
||||
return choice.disabled !== true;
|
||||
},[]);
|
||||
}, []);
|
||||
|
||||
return values;
|
||||
}
|
||||
|
@ -116,11 +110,12 @@ export class Store {
|
|||
* @return {Object} Found choice
|
||||
*/
|
||||
getChoiceById(id) {
|
||||
if(!id) return;
|
||||
const choices = this.getChoicesFilteredByActive();
|
||||
const foundChoice = choices.find((choice) => choice.id === parseInt(id));
|
||||
|
||||
return foundChoice;
|
||||
if (id) {
|
||||
const choices = this.getChoicesFilteredByActive();
|
||||
const foundChoice = choices.find((choice) => choice.id === parseInt(id, 10));
|
||||
return foundChoice;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -145,11 +140,11 @@ export class Store {
|
|||
const hasActiveOptions = choices.some((choice) => {
|
||||
return choice.active === true && choice.disabled === false;
|
||||
});
|
||||
return isActive && hasActiveOptions ? true : false;
|
||||
},[]);
|
||||
return isActive && hasActiveOptions;
|
||||
}, []);
|
||||
|
||||
return values;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = Store;
|
13
package.json
13
package.json
|
@ -27,12 +27,18 @@
|
|||
"devDependencies": {
|
||||
"autoprefixer": "^6.3.3",
|
||||
"babel-core": "^6.7.2",
|
||||
"babel-eslint": "^6.1.2",
|
||||
"babel-loader": "^6.2.4",
|
||||
"babel-preset-es2015": "^6.6.0",
|
||||
"babel-preset-stage-0": "^6.5.0",
|
||||
"csso": "^1.7.0",
|
||||
"csso": "^1.8.2",
|
||||
"es6-promise": "^3.2.1",
|
||||
"fuse.js": "^2.2.0",
|
||||
"eslint": "^3.3.0",
|
||||
"eslint-config-airbnb": "^10.0.1",
|
||||
"eslint-loader": "^1.5.0",
|
||||
"eslint-plugin-import": "^1.13.0",
|
||||
"eslint-plugin-jsx-a11y": "^2.1.0",
|
||||
"eslint-plugin-react": "^6.1.0",
|
||||
"jasmine-core": "^2.4.1",
|
||||
"karma": "^1.1.0",
|
||||
"karma-coverage": "^1.0.0",
|
||||
|
@ -52,6 +58,7 @@
|
|||
"wrapper-webpack-plugin": "^0.1.7"
|
||||
},
|
||||
"dependencies": {
|
||||
"redux": "^3.3.1"
|
||||
"redux": "^3.3.1",
|
||||
"fuse.js": "^2.2.0"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,9 @@ module.exports = {
|
|||
filename: 'choices.min.js',
|
||||
publicPath: '/assets/scripts/dist/'
|
||||
},
|
||||
eslint: {
|
||||
configFile: '.eslintrc'
|
||||
},
|
||||
plugins: [
|
||||
new webpack.HotModuleReplacementPlugin(),
|
||||
new webpack.DefinePlugin({
|
||||
|
@ -24,7 +27,7 @@ module.exports = {
|
|||
loaders: [{
|
||||
test: /\.js$/,
|
||||
exclude: /(node_modules|bower_components)/,
|
||||
loaders: ['babel'],
|
||||
loaders: ['babel', 'eslint-loader'],
|
||||
include: path.join(__dirname, 'assets/scripts/src')
|
||||
}]
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue