adjust-engine
Advanced tools
Comparing version 2.0.0 to 2.0.1
2.0.1 / 2015-08-25 | ||
================== | ||
* pass attachment orientation as a result | ||
2.0.0 / 2015-08-24 | ||
@@ -3,0 +8,0 @@ ================== |
19
index.js
@@ -5,3 +5,3 @@ /** | ||
var expression = require('./lib/expression') | ||
var expr = require('./lib/expression') | ||
var assign = require('object-assign') | ||
@@ -37,8 +37,12 @@ var mirror = require('./lib/mirror') | ||
options.flip = undefined === options.flip ? true : options.flip | ||
options.attachment = options.attachment || 'center middle' | ||
options.offset = options.offset || {} | ||
var attachment = expression(options.attachment) | ||
if (!options.attachment && !options.target) { | ||
throw new Error('adjust requires either an attachment or a target') | ||
} | ||
var offset = assign({}, offsets, options.offset) | ||
var target = options.target ? expression(options.target) : mirror(attachment) | ||
var attachment = options.attachment ? expr(options.attachment) : mirror(expr(options.target)) | ||
var target = options.target ? expr(options.target) : mirror(expr(options.attachment)) | ||
var orientation = attachment; | ||
@@ -72,2 +76,3 @@ return function adjust(attachment_position, target_position, viewport_position) { | ||
right = left + width + offset.left - offset.right | ||
orientation.x = mirror(orientation.x) | ||
} else | ||
@@ -78,2 +83,3 @@ if (right > viewport_position.right && target_position.left - width >= viewport_position.left) { | ||
left = right - width + offset.right - offset.left | ||
orientation.x = mirror(orientation.x) | ||
} | ||
@@ -87,2 +93,3 @@ | ||
bottom = top + height + offset.top - offset.bottom | ||
orientation.y = mirror(orientation.y) | ||
} else | ||
@@ -93,2 +100,3 @@ if (bottom > viewport_position.bottom && target_position.top - height >= viewport_position.top) { | ||
top = bottom - height + offset.bottom - offset.top | ||
orientation.y = mirror(orientation.y) | ||
} | ||
@@ -103,5 +111,6 @@ } | ||
right: left + width, | ||
bottom: top + height | ||
bottom: top + height, | ||
orientation: expr(orientation) | ||
} | ||
} | ||
} |
/** | ||
* Export `expression` | ||
* | ||
* @param {String|Object} expr | ||
* @return {Object|String} | ||
*/ | ||
module.exports = expression | ||
module.exports = function expr (expr) { | ||
return typeof expr === 'string' | ||
? parse(expr) | ||
: compile(expr) | ||
} | ||
@@ -14,3 +21,3 @@ /** | ||
function expression(expr) { | ||
function parse(expr) { | ||
var tokens = expr.split(/\s+/) | ||
@@ -51,2 +58,29 @@ var out = {} | ||
/** | ||
* Compile an object into a string | ||
* the reverse of parse | ||
* | ||
* @param {Object} n | ||
* @return {String} | ||
*/ | ||
function compile (expr) { | ||
var out = [] | ||
switch (expr.x) { | ||
case 0: out.push('left'); break | ||
case 0.5: out.push('center'); break | ||
case 1: out.push('right'); break | ||
default: out.push(expr.x * 100 + '%') | ||
} | ||
switch (expr.y) { | ||
case 0: out.push('top'); break | ||
case 0.5: out.push('middle'); break | ||
case 1: out.push('bottom'); break | ||
default: out.push((expr.y * 100) + '%') | ||
} | ||
return out.join(' ') | ||
} | ||
/** | ||
* To percentage | ||
@@ -53,0 +87,0 @@ * |
@@ -10,3 +10,3 @@ /** | ||
* | ||
* @param {Object} | ||
* @param {Number|Object} | ||
* @return {Object} | ||
@@ -16,2 +16,6 @@ */ | ||
function mirror (p) { | ||
if (typeof p === 'number') { | ||
return round(Math.abs(1 - p)) | ||
} | ||
return { | ||
@@ -18,0 +22,0 @@ x: round(Math.abs(1 - p.x)), |
{ | ||
"name": "adjust-engine", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "adjust an element relative to another element", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
8549
202