From c5c98ad62fab7118b76b6274f14371b86b18a2e2 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Wed, 17 Dec 2014 15:12:04 -0500 Subject: [PATCH] Simplifying node proxy code --- src/js/parser/javascript/match.js | 56 +++++++-------- src/js/parser/javascript/match_fragment.js | 80 +++++++++++----------- src/js/parser/javascript/node.js | 23 +++---- src/js/parser/javascript/regexp.js | 68 +++++++++--------- src/js/parser/javascript/subexp.js | 16 ++--- 5 files changed, 120 insertions(+), 123 deletions(-) diff --git a/src/js/parser/javascript/match.js b/src/js/parser/javascript/match.js index 5e5e9e3..0291299 100644 --- a/src/js/parser/javascript/match.js +++ b/src/js/parser/javascript/match.js @@ -18,37 +18,33 @@ export default { .invoke('addClass', 'anchor'); } - if (start || end || this.parts.length !== 1) { - partPromises = _.map(this.parts, part => { - return part.render(this.container.group()); - }); + partPromises = _.map(this.parts, part => { + return part.render(this.container.group()); + }); - return Q.all(_([start, partPromises, end]).flatten().compact().value()) - .then(items => { - var prev, next, paths; + return Q.all(_([start, partPromises, end]).flatten().compact().value()) + .then(items => { + var prev, next, paths; - this.items = items; - this.spaceHorizontally(items, { - padding: 10 - }); - - prev = this.normalizeBBox(_.first(items).getBBox()); - paths = _.map(items.slice(1), item => { - var path; - - next = this.normalizeBBox(item.getBBox()); - path = `M${prev.ax2},${prev.ay}H${next.ax}`; - prev = next; - - return path; - }); - - this.container.prepend( - this.container.path(paths.join(''))); + this.items = items; + this.spaceHorizontally(items, { + padding: 10 }); - } else { - return this.proxy(this.parts[0]); - } + + prev = this.normalizeBBox(_.first(items).getBBox()); + paths = _.map(items.slice(1), item => { + var path; + + next = this.normalizeBBox(item.getBBox()); + path = `M${prev.ax2},${prev.ay}H${next.ax}`; + prev = next; + + return path; + }); + + this.container.prepend( + this.container.path(paths.join(''))); + }); }, _getAnchor() { @@ -79,5 +75,9 @@ export default { this.anchorStart = this.properties.anchor_start.textValue !== ''; this.anchorEnd = this.properties.anchor_end.textValue !== ''; + + if (!this.anchorStart && !this.anchorEnd && this.parts.length === 1) { + this.proxy = this.parts[0]; + } } }; diff --git a/src/js/parser/javascript/match_fragment.js b/src/js/parser/javascript/match_fragment.js index c4fa9e8..5c2d280 100644 --- a/src/js/parser/javascript/match_fragment.js +++ b/src/js/parser/javascript/match_fragment.js @@ -4,59 +4,55 @@ export default { type: 'match-fragment', _render() { - if (!this.repeat) { - return this.proxy(this.content); - } else { - return this.content.render(this.container.group()) - .then(() => { - var box, paths = []; + return this.content.render(this.container.group()) + .then(() => { + var box, paths = []; - this.content.transform(this.repeat.contentPosition); + this.content.transform(this.repeat.contentPosition); - box = this.content.getBBox(); + box = this.content.getBBox(); - if (this.repeat.hasSkip) { - let vert = Math.max(0, box.ay - box.y - 10), - horiz = box.width - 10; + if (this.repeat.hasSkip) { + let vert = Math.max(0, box.ay - box.y - 10), + horiz = box.width - 10; - paths.push(`M0,${box.ay}q10,0 10,-10v${-vert}q0,-10 10,-10h${horiz}q10,0 10,10v${vert}q0,10 10,10`); + paths.push(`M0,${box.ay}q10,0 10,-10v${-vert}q0,-10 10,-10h${horiz}q10,0 10,10v${vert}q0,10 10,10`); - if (!this.repeat.greedy) { - paths.push(`M10,${box.ay - 15}l5,5m-5,-5l-5,5`); - } + if (!this.repeat.greedy) { + paths.push(`M10,${box.ay - 15}l5,5m-5,-5l-5,5`); } + } - if (this.repeat.hasLoop) { - let vert = box.y2 - box.ay - 10; + if (this.repeat.hasLoop) { + let vert = box.y2 - box.ay - 10; - paths.push(`M${box.x},${box.ay}q-10,0 -10,10v${vert}q0,10 10,10h${box.width}q10,0 10,-10v${-vert}q0,-10 -10,-10`); + paths.push(`M${box.x},${box.ay}q-10,0 -10,10v${vert}q0,10 10,10h${box.width}q10,0 10,-10v${-vert}q0,-10 -10,-10`); - if (this.repeat.greedy) { - paths.push(`M${box.x2 + 10},${box.ay + 15}l5,-5m-5,5l-5,-5`); - } + if (this.repeat.greedy) { + paths.push(`M${box.x2 + 10},${box.ay + 15}l5,-5m-5,5l-5,-5`); } + } - if (paths.length) { - this.container.prepend( - this.container.path(paths.join(''))); - } - }) - .then(() => { - var labelStr = this.repeat.label, - label, - labelBox, - labelShift = (this.repeat.hasSkip ? 5 : 0), - box = this.getBBox(); + if (paths.length) { + this.container.prepend( + this.container.path(paths.join(''))); + } + }) + .then(() => { + var labelStr = this.repeat.label, + label, + labelBox, + labelShift = (this.repeat.hasSkip ? 5 : 0), + box = this.getBBox(); - if (labelStr) { - label = this.container.text(0, 0, labelStr) - .addClass('repeat-label'); - labelBox = label.getBBox(); - label.transform(Snap.matrix() - .translate(box.x2 - labelBox.width - labelShift, box.y2 + labelBox.height)); - } - }); - } + if (labelStr) { + label = this.container.text(0, 0, labelStr) + .addClass('repeat-label'); + labelBox = label.getBBox(); + label.transform(Snap.matrix() + .translate(box.x2 - labelBox.width - labelShift, box.y2 + labelBox.height)); + } + }); }, _getAnchor() { @@ -76,6 +72,8 @@ export default { if (this.properties.repeat.textValue !== '') { this.repeat = this.properties.repeat; + } else { + this.proxy = this.content; } } }; diff --git a/src/js/parser/javascript/node.js b/src/js/parser/javascript/node.js index 5b4e90f..19a83c3 100644 --- a/src/js/parser/javascript/node.js +++ b/src/js/parser/javascript/node.js @@ -26,8 +26,8 @@ export default class Node { } getAnchor() { - if (this._proxy) { - return this._proxy.getAnchor(); + if (this.proxy) { + return this.proxy.getAnchor(); } else { return this._getAnchor(); } @@ -119,16 +119,15 @@ export default class Node { this.setContainer(container); } - this.startRender(); - return this._render() - .then(this.doneRender.bind(this)) - .then(_.constant(this)); - } - - proxy(node) { - this.anchorDebug = false; - this._proxy = node; - return node.render(this.container); + if (this.proxy) { + return this.proxy.render(this.container) + .then(_.constant(this)); + } else { + this.startRender(); + return this._render() + .then(this.doneRender.bind(this)) + .then(_.constant(this)); + } } spaceHorizontally(items, options) { diff --git a/src/js/parser/javascript/regexp.js b/src/js/parser/javascript/regexp.js index f27ec2e..8facc50 100644 --- a/src/js/parser/javascript/regexp.js +++ b/src/js/parser/javascript/regexp.js @@ -7,43 +7,39 @@ export default { _render() { var matchContainer; - if (this.matches.length === 1) { - return this.proxy(this.matches[0]); - } else { - matchContainer = this.container.group() - .addClass('regexp-matches') - .transform(Snap.matrix() - .translate(20, 0)); + matchContainer = this.container.group() + .addClass('regexp-matches') + .transform(Snap.matrix() + .translate(20, 0)); - return Q.all(_.map(this.matches, match => { - return match.render(matchContainer.group()); - })) - .then(() => { - var containerBox, - paths; + return Q.all(_.map(this.matches, match => { + return match.render(matchContainer.group()); + })) + .then(() => { + var containerBox, + paths; - this.spaceVertically(this.matches, { - padding: 5 - }); - - containerBox = this.getBBox(); - paths = _.map(this.matches, this.makeConnectorLine.bind(this, containerBox)); - - paths.push(this.makeSideLine(containerBox, _.first(this.matches))); - paths.push(this.makeSideLine(containerBox, _.last(this.matches))); - - this.container.prepend( - this.container.path(paths.join(''))); - - matchContainer.prepend( - matchContainer.path(_.map(this.matches, match => { - var box = match.getBBox(), - container = matchContainer.getBBox(); - - return `M0,${box.ay}h${box.ax}M${box.ax2},${box.ay}H${container.width}`; - }).join(''))); + this.spaceVertically(this.matches, { + padding: 5 }); - } + + containerBox = this.getBBox(); + paths = _.map(this.matches, this.makeConnectorLine.bind(this, containerBox)); + + paths.push(this.makeSideLine(containerBox, _.first(this.matches))); + paths.push(this.makeSideLine(containerBox, _.last(this.matches))); + + this.container.prepend( + this.container.path(paths.join(''))); + + matchContainer.prepend( + matchContainer.path(_.map(this.matches, match => { + var box = match.getBBox(), + container = matchContainer.getBBox(); + + return `M0,${box.ay}h${box.ax}M${box.ax2},${box.ay}H${container.width}`; + }).join(''))); + }); }, makeSideLine(containerBox, match) { @@ -91,5 +87,9 @@ export default { .concat(_.map(this.properties.alternates.elements, element => { return element.properties.match; })); + + if (this.matches.length === 1) { + return this.proxy = this.matches[0]; + } } }; diff --git a/src/js/parser/javascript/subexp.js b/src/js/parser/javascript/subexp.js index 2296cc6..4932528 100644 --- a/src/js/parser/javascript/subexp.js +++ b/src/js/parser/javascript/subexp.js @@ -12,14 +12,10 @@ export default { }, _render() { - if (this.label) { - return this.regexp.render(this.container.group()) - .then(this.renderLabeledBox.bind(this, this.label, this.regexp, { - padding: 10 - })); - } else { - return this.proxy(this.regexp); - } + return this.regexp.render(this.container.group()) + .then(this.renderLabeledBox.bind(this, this.label, this.regexp, { + padding: 10 + })); }, resetCounter() { @@ -45,5 +41,9 @@ export default { } this.regexp = this.properties.regexp; + + if (!this.label) { + this.proxy = this.regexp; + } } };