Simplifying node proxy code

This commit is contained in:
Jeff Avallone 2014-12-17 15:12:04 -05:00
parent dd6eac2b4e
commit c5c98ad62f
5 changed files with 120 additions and 123 deletions

View File

@ -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];
}
}
};

View File

@ -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;
}
}
};

View File

@ -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) {

View File

@ -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];
}
}
};

View File

@ -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;
}
}
};