* Parse an `offset` string to extrapolate `x` and `y` numeric offsets. * @function * @memberof {modifiers~offset} * @private * @argument {String} offset * @argument {Object} popperOffsets * @argument {Object} referenceOffsets * @argument {String} basePlacement * @returns {Arr
(offset, popperOffsets, referenceOffsets, basePlacement)
| 3246 | * @returns {Array} a two cells array with x and y offsets in numbers |
| 3247 | */ |
| 3248 | function parseOffset(offset, popperOffsets, referenceOffsets, basePlacement) { |
| 3249 | var offsets = [0, 0]; |
| 3250 | |
| 3251 | // Use height if placement is left or right and index is 0 otherwise use width |
| 3252 | // in this way the first offset will use an axis and the second one |
| 3253 | // will use the other one |
| 3254 | var useHeight = ['right', 'left'].indexOf(basePlacement) !== -1; |
| 3255 | |
| 3256 | // Split the offset string to obtain a list of values and operands |
| 3257 | // The regex addresses values with the plus or minus sign in front (+10, -20, etc) |
| 3258 | var fragments = offset.split(/(\+|\-)/).map(function (frag) { |
| 3259 | return frag.trim(); |
| 3260 | }); |
| 3261 | |
| 3262 | // Detect if the offset string contains a pair of values or a single one |
| 3263 | // they could be separated by comma or space |
| 3264 | var divider = fragments.indexOf(find(fragments, function (frag) { |
| 3265 | return frag.search(/,|\s/) !== -1; |
| 3266 | })); |
| 3267 | |
| 3268 | if (fragments[divider] && fragments[divider].indexOf(',') === -1) { |
| 3269 | console.warn('Offsets separated by white space(s) are deprecated, use a comma (,) instead.'); |
| 3270 | } |
| 3271 | |
| 3272 | // If divider is found, we divide the list of values and operands to divide |
| 3273 | // them by ofset X and Y. |
| 3274 | var splitRegex = /\s*,\s*|\s+/; |
| 3275 | var ops = divider !== -1 ? [fragments.slice(0, divider).concat([fragments[divider].split(splitRegex)[0]]), [fragments[divider].split(splitRegex)[1]].concat(fragments.slice(divider + 1))] : [fragments]; |
| 3276 | |
| 3277 | // Convert the values with units to absolute pixels to allow our computations |
| 3278 | ops = ops.map(function (op, index) { |
| 3279 | // Most of the units rely on the orientation of the popper |
| 3280 | var measurement = (index === 1 ? !useHeight : useHeight) ? 'height' : 'width'; |
| 3281 | var mergeWithPrevious = false; |
| 3282 | return op |
| 3283 | // This aggregates any `+` or `-` sign that aren't considered operators |
| 3284 | // e.g.: 10 + +5 => [10, +, +5] |
| 3285 | .reduce(function (a, b) { |
| 3286 | if (a[a.length - 1] === '' && ['+', '-'].indexOf(b) !== -1) { |
| 3287 | a[a.length - 1] = b; |
| 3288 | mergeWithPrevious = true; |
| 3289 | return a; |
| 3290 | } else if (mergeWithPrevious) { |
| 3291 | a[a.length - 1] += b; |
| 3292 | mergeWithPrevious = false; |
| 3293 | return a; |
| 3294 | } else { |
| 3295 | return a.concat(b); |
| 3296 | } |
| 3297 | }, []) |
| 3298 | // Here we convert the string values into number values (in px) |
| 3299 | .map(function (str) { |
| 3300 | return toValue(str, measurement, popperOffsets, referenceOffsets); |
| 3301 | }); |
| 3302 | }); |
| 3303 | |
| 3304 | // Loop trough the offsets arrays and execute the operations |
| 3305 | ops.forEach(function (op, index) { |