From 870b2e6ed424e3f34119bb1ec34a4de2ae3db2ba Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Fri, 19 Dec 2014 12:06:21 -0500 Subject: [PATCH] Moving normalizeBBox to util.js --- spec/parser/javascript/node_spec.js | 20 -------------------- spec/util_spec.js | 22 +++++++++++++++++++++- src/js/parser/javascript/match.js | 9 +++++---- src/js/parser/javascript/node.js | 14 +++----------- src/js/util.js | 11 +++++++++++ 5 files changed, 40 insertions(+), 36 deletions(-) diff --git a/spec/parser/javascript/node_spec.js b/spec/parser/javascript/node_spec.js index a53d809..89c4f7e 100644 --- a/spec/parser/javascript/node_spec.js +++ b/spec/parser/javascript/node_spec.js @@ -104,26 +104,6 @@ describe('parser/javascript/node.js', function() { }); - describe('#normalizeBBox', function() { - - it('defaults the anchor keys to values from the bbox', function() { - expect(this.node.normalizeBBox({ - x: 'bbox x', - x2: 'bbox x2', - cy: 'bbox cy', - ay: 'bbox ay' - })).toEqual({ - x: 'bbox x', - x2: 'bbox x2', - cy: 'bbox cy', - ax: 'bbox x', - ax2: 'bbox x2', - ay: 'bbox ay' - }); - }); - - }); - describe('#transform', function() { it('returns the result of calling transform on the container', function() { diff --git a/spec/util_spec.js b/spec/util_spec.js index e43a3f6..ec2e583 100644 --- a/spec/util_spec.js +++ b/spec/util_spec.js @@ -1,4 +1,4 @@ -import { customEvent } from 'src/js/util.js'; +import { customEvent, normalizeBBox } from 'src/js/util.js'; describe('util.js', function() { @@ -16,4 +16,24 @@ describe('util.js', function() { }); + describe('normalizeBBox', function() { + + it('defaults the anchor keys to values from the bbox', function() { + expect(normalizeBBox({ + x: 'bbox x', + x2: 'bbox x2', + cy: 'bbox cy', + ay: 'bbox ay' + })).toEqual({ + x: 'bbox x', + x2: 'bbox x2', + cy: 'bbox cy', + ax: 'bbox x', + ax2: 'bbox x2', + ay: 'bbox ay' + }); + }); + + }); + }); diff --git a/src/js/parser/javascript/match.js b/src/js/parser/javascript/match.js index 2192355..4e0c64b 100644 --- a/src/js/parser/javascript/match.js +++ b/src/js/parser/javascript/match.js @@ -1,3 +1,4 @@ +import { normalizeBBox } from '../../util.js'; import _ from 'lodash'; import Q from 'q'; @@ -7,8 +8,8 @@ export default { definedProperties: { _anchor: { get: function() { - var start = this.normalizeBBox(_.first(this.items).getBBox()), - end = this.normalizeBBox(_.last(this.items).getBBox()), + var start = normalizeBBox(_.first(this.items).getBBox()), + end = normalizeBBox(_.last(this.items).getBBox()), matrix = this.transform().localMatrix; return { @@ -47,11 +48,11 @@ export default { padding: 10 }); - prev = this.normalizeBBox(_.first(items).getBBox()); + prev = normalizeBBox(_.first(items).getBBox()); paths = _.map(items.slice(1), item => { var path; - next = this.normalizeBBox(item.getBBox()); + next = normalizeBBox(item.getBBox()); path = `M${prev.ax2},${prev.ay}H${next.ax}`; prev = next; diff --git a/src/js/parser/javascript/node.js b/src/js/parser/javascript/node.js index b94abd9..80ce726 100644 --- a/src/js/parser/javascript/node.js +++ b/src/js/parser/javascript/node.js @@ -1,4 +1,4 @@ -import { customEvent } from '../../util.js'; +import { customEvent, normalizeBBox } from '../../util.js'; import _ from 'lodash'; import Q from 'q'; @@ -54,14 +54,6 @@ export default class Node { return _.extend(this.container.getBBox(), this.anchor); } - normalizeBBox(box) { - return _.extend({ - ax: box.x, - ax2: box.x2, - ay: box.cy - }, box); - } - transform(matrix) { return this.container.transform(matrix); } @@ -150,14 +142,14 @@ export default class Node { item.transform(Snap.matrix() .translate(offset, 0)); - box = this.normalizeBBox(item.getBBox()); + box = normalizeBBox(item.getBBox()); verticalCenter = Math.max(verticalCenter, box.ay); return offset + options.padding + box.width; }, 0); for (var item of items) { - let box = this.normalizeBBox(item.getBBox()); + let box = normalizeBBox(item.getBBox()); item.transform(Snap.matrix() .add(item.transform().localMatrix) diff --git a/src/js/util.js b/src/js/util.js index f62e03d..3572e93 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -1,6 +1,17 @@ +import _ from 'lodash'; + export function customEvent(name, detail) { var evt = document.createEvent('Event'); evt.initEvent(name, true, true); evt.detail = detail; return evt; } + + +export function normalizeBBox(box) { + return _.extend({ + ax: box.x, + ax2: box.x2, + ay: box.cy + }, box); +}