adjust-engine
Advanced tools
Comparing version 1.0.1 to 2.0.0
2.0.0 / 2015-08-24 | ||
================== | ||
* BREAKING: changed offset calculation to move object, but not affect the height or width | ||
* better defaults | ||
* supports flipping offsets. | ||
1.0.1 / 2015-08-19 | ||
@@ -3,0 +10,0 @@ ================== |
78
index.js
@@ -5,3 +5,5 @@ /** | ||
var expression = require('./lib/expression') | ||
var assign = require('object-assign') | ||
var mirror = require('./lib/mirror') | ||
@@ -36,8 +38,7 @@ /** | ||
options.attachment = options.attachment || 'center middle' | ||
options.target = options.target || options.attachment | ||
options.offset = options.offset || {} | ||
var attachment = expression(options.attachment) | ||
var offset = assign(offsets, options.offset) | ||
var target = expression(options.target) | ||
var offset = assign({}, offsets, options.offset) | ||
var target = options.target ? expression(options.target) : mirror(attachment) | ||
@@ -58,8 +59,6 @@ return function adjust(attachment_position, target_position, viewport_position) { | ||
// update the position with the offsets | ||
var left = target_position.left + offset_x + offset.left | ||
var top = target_position.top + offset_y + offset.top | ||
var height = height - offset.top - offset.bottom | ||
var width = width - offset.left - offset.right | ||
var bottom = top + height + offset.bottom | ||
var right = left + width + offset.right | ||
var left = target_position.left + offset_x + offset.left - offset.right | ||
var top = target_position.top + offset_y + offset.top - offset.bottom | ||
var bottom = top + height + offset.bottom - offset.top | ||
var right = left + width + offset.right - offset.left | ||
@@ -71,8 +70,10 @@ // check if we need to flip | ||
if (left < viewport_position.left && target_position.right + width <= viewport_position.right) { | ||
left = target_position.right | ||
right = left + width | ||
// flip right | ||
left = target_position.right + offset.right - offset.left | ||
right = left + width + offset.left - offset.right | ||
} else | ||
if (right > viewport_position.right && target_position.left - width >= viewport_position.left) { | ||
right = target_position.left | ||
left = right - width | ||
// flip left | ||
right = target_position.left + offset.left - offset.right | ||
left = right - width + offset.right - offset.left | ||
} | ||
@@ -83,8 +84,10 @@ | ||
if (top < viewport_position.top && target_position.bottom + height <= viewport_position.bottom) { | ||
top = target_position.bottom | ||
bottom = top + height | ||
// flip bottom | ||
top = target_position.bottom + offset.bottom - offset.top | ||
bottom = top + height + offset.top - offset.bottom | ||
} else | ||
if (bottom > viewport_position.bottom && target_position.top - height >= viewport_position.top) { | ||
bottom = target_position.top | ||
top = bottom - height | ||
// flip top | ||
bottom = target_position.top + offset.top - offset.bottom | ||
top = bottom - height + offset.bottom - offset.top | ||
} | ||
@@ -103,42 +106,1 @@ } | ||
} | ||
/** | ||
* Parse the expression | ||
* | ||
* @param {String} expr | ||
* @return {Object} | ||
*/ | ||
function expression(expr) { | ||
var tokens = expr.split(/\s+/) | ||
var out = {} | ||
tokens.forEach(function(token, i) { | ||
switch (token) { | ||
case 'center': return out.x = 0.5 | ||
case 'middle': return out.y = 0.5 | ||
case 'bottom': return out.y = 1 | ||
case 'right': return out.x = 1 | ||
case 'left': return out.x = 0 | ||
case 'top': return out.y = 0 | ||
default: | ||
return i % 2 | ||
? out.y = percentage(token) | ||
: out.x = percentage(token) | ||
} | ||
}) | ||
return out | ||
} | ||
/** | ||
* To percentage | ||
* | ||
* @param {String} val | ||
* @return {Number} | ||
*/ | ||
function percentage (val) { | ||
var float = parseFloat(val) | ||
return isNaN(float) ? 0 : float / 100 | ||
} |
{ | ||
"name": "adjust-engine", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "adjust an element relative to another element", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
# adjust-engine | ||
Position an element relative to another element. This repository is just | ||
the calculation engine behind the adjustments. | ||
Position an element relative to another element. This repository is the workhorse behind [adjust](https://github.com/matthewmueller/adjust). | ||
@@ -7,0 +6,0 @@ ## Installation |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7339
8
161
87