| 1 |
import isFunction from './isFunction'; |
| 2 |
import findIndex from './findIndex'; |
| 3 |
import getClientRect from '../utils/getClientRect'; |
| 4 |
|
| 5 |
/** |
| 6 |
* Loop trough the list of modifiers and run them in order, |
| 7 |
* each of them will then edit the data object. |
| 8 |
* @method |
| 9 |
* @memberof Popper.Utils |
| 10 |
* @param {dataObject} data |
| 11 |
* @param {Array} modifiers |
| 12 |
* @param {String} ends - Optional modifier name used as stopper |
| 13 |
* @returns {dataObject} |
| 14 |
*/ |
| 15 |
export default function runModifiers(modifiers, data, ends) { |
| 16 |
const modifiersToRun = ends === undefined |
| 17 |
? modifiers |
| 18 |
: modifiers.slice(0, findIndex(modifiers, 'name', ends)); |
| 19 |
|
| 20 |
modifiersToRun.forEach(modifier => { |
| 21 |
if (modifier['function']) { // eslint-disable-line dot-notation |
| 22 |
console.warn('`modifier.function` is deprecated, use `modifier.fn`!'); |
| 23 |
} |
| 24 |
const fn = modifier['function'] || modifier.fn; // eslint-disable-line dot-notation |
| 25 |
if (modifier.enabled && isFunction(fn)) { |
| 26 |
// Add properties to offsets to make them a complete clientRect object |
| 27 |
// we do this before each modifier to make sure the previous one doesn't |
| 28 |
// mess with these values |
| 29 |
data.offsets.popper = getClientRect(data.offsets.popper); |
| 30 |
data.offsets.reference = getClientRect(data.offsets.reference); |
| 31 |
|
| 32 |
data = fn(data, modifier); |
| 33 |
} |
| 34 |
}); |
| 35 |
|
| 36 |
return data; |
| 37 |
} |