Folding _position methods into render method promises

This commit is contained in:
Jeff Avallone 2014-12-13 12:55:03 -05:00
parent 29316bb2aa
commit cb9785de91
8 changed files with 130 additions and 140 deletions

View File

@ -47,9 +47,6 @@ export default {
} }
var promise = this._render(); var promise = this._render();
if (!this._proxy) {
promise = promise.then(this._position.bind(this));
}
return promise.then(_.constant(this)); return promise.then(_.constant(this));
}, },
@ -71,8 +68,6 @@ export default {
}); });
}, },
_position() {},
spaceHorizontally(items, options) { spaceHorizontally(items, options) {
var verticalCenter = 0; var verticalCenter = 0;

View File

@ -6,23 +6,24 @@ export default _.extend({}, Base, {
type: 'charset', type: 'charset',
_render() { _render() {
var partContainer;
this.renderLabeledBox(this.invert() ? 'None of:' : 'One of:'); this.renderLabeledBox(this.invert() ? 'None of:' : 'One of:');
this.partContainer = this.container.group(); partContainer = this.container.group();
return Q.all(_.map(this.parts.elements, (part => { return Q.all(_.map(this.parts.elements, part => {
return part.render(this.partContainer.group()); return part.render(partContainer.group());
}).bind(this))); }))
}, .then((() => {
_position() {
this.spaceVertically(this.parts.elements, { this.spaceVertically(this.parts.elements, {
padding: 5 padding: 5
}); });
this.positionLabeledBox(this.partContainer, { this.positionLabeledBox(partContainer, {
padding: 5 padding: 5
}); });
}).bind(this));
}, },
invert() { invert() {

View File

@ -6,7 +6,7 @@ export default _.extend({}, Base, {
type: 'charset-range', type: 'charset-range',
_render() { _render() {
this.hyphen = this.container.text() var hyphen = this.container.text()
.attr({ .attr({
text: '-' text: '-'
}); });
@ -14,12 +14,11 @@ export default _.extend({}, Base, {
return Q.all([ return Q.all([
this.first.render(this.container.group()), this.first.render(this.container.group()),
this.last.render(this.container.group()) this.last.render(this.container.group())
]); ])
}, .then((() => {
this.spaceHorizontally([this.first, hyphen, this.last], {
_position() {
this.spaceHorizontally([this.first, this.hyphen, this.last], {
padding: 5 padding: 5
}); });
}).bind(this));
} }
}); });

View File

@ -27,16 +27,15 @@ export default _.extend({}, Base, {
return part.render(this.container.group()); return part.render(this.container.group());
}).bind(this)); }).bind(this));
return Q.all(_([start, partPromises, end]).flatten().compact().value()); return Q.all(_([start, partPromises, end]).flatten().compact().value())
} else { .then(((items) => {
return this.proxy(parts[0]);
}
},
_position(items) {
this.spaceHorizontally(items, { this.spaceHorizontally(items, {
padding: 10 padding: 10
}); });
}).bind(this));
} else {
return this.proxy(parts[0]);
}
}, },
anchorStart() { anchorStart() {

View File

@ -8,11 +8,8 @@ export default _.extend({}, Base, {
if (this._repeat.textValue === '') { if (this._repeat.textValue === '') {
return this.proxy(this._content); return this.proxy(this._content);
} else { } else {
return this._content.render(this.container.group()); return this._content.render(this.container.group())
} .then((() => {
},
_position() {
var box, paths = []; var box, paths = [];
this._content.transform(this._repeat.contentPosition()); this._content.transform(this._repeat.contentPosition());
@ -38,5 +35,7 @@ export default _.extend({}, Base, {
this.container.prepend( this.container.prepend(
this.container.path(paths.join(''))); this.container.path(paths.join('')));
} }
}).bind(this));
}
} }
}); });

View File

@ -6,32 +6,28 @@ export default _.extend({}, Base, {
type: 'regexp', type: 'regexp',
_render() { _render() {
var matches = this.matches(); var matches = this.matches(),
matchContainer;
if (matches.length === 1) { if (matches.length === 1) {
return this.proxy(matches[0]); return this.proxy(matches[0]);
} else { } else {
this.matchContainer = this.container.group() matchContainer = this.container.group()
.addClass('regexp-matches'); .addClass('regexp-matches')
.transform(Snap.matrix()
.translate(20, 0));
return Q.all(_.map(matches, (match => { return Q.all(_.map(matches, match => {
return match.render(this.matchContainer.group()); return match.render(matchContainer.group());
}).bind(this))); }))
} .then((() => {
}, var containerBox,
_position() {
var matches = this.matches(),
containerBox,
paths; paths;
this.spaceVertically(matches, { this.spaceVertically(matches, {
padding: 5 padding: 5
}); });
this.matchContainer.transform(Snap.matrix()
.translate(20, 0));
containerBox = this.getBBox(); containerBox = this.getBBox();
paths = _.map(matches, match => { paths = _.map(matches, match => {
var box = match.getBBox(), var box = match.getBBox(),
@ -76,6 +72,8 @@ export default _.extend({}, Base, {
this.container.prepend( this.container.prepend(
this.container.path(paths.join(''))); this.container.path(paths.join('')));
}).bind(this));
}
}, },
matches() { matches() {

View File

@ -5,10 +5,6 @@ export default _.extend({}, Base, {
type: 'root', type: 'root',
_render() { _render() {
var promise = this.regexp.render(this.container.group());
this.regexp.transform(Snap.matrix()
.translate(10, 0));
this.start = this.container.circle() this.start = this.container.circle()
.addClass('pin') .addClass('pin')
.attr({ r: 5 }); .attr({ r: 5 });
@ -16,16 +12,20 @@ export default _.extend({}, Base, {
.addClass('pin') .addClass('pin')
.attr({ r: 5 }); .attr({ r: 5 });
return promise; return this.regexp.render(this.container.group())
}, .then((() => {
var contentBox;
_position() { this.regexp.transform(Snap.matrix()
var contentBox = this.regexp.getBBox(); .translate(10, 0));
contentBox = this.regexp.getBBox();
this.start.transform(Snap.matrix() this.start.transform(Snap.matrix()
.translate(0, contentBox.cy)); .translate(0, contentBox.cy));
this.end.transform(Snap.matrix() this.end.transform(Snap.matrix()
.translate(contentBox.x2 + 10, contentBox.cy)); .translate(contentBox.x2 + 10, contentBox.cy));
}).bind(this));
}, },
flags() { flags() {

View File

@ -18,16 +18,15 @@ export default _.extend({}, Base, {
if (label) { if (label) {
this.renderLabeledBox(label); this.renderLabeledBox(label);
return this.regexp.render(this.container.group()); return this.regexp.render(this.container.group())
} else { .then((() => {
return this.proxy(this.regexp);
}
},
_position() {
this.positionLabeledBox(this.regexp, { this.positionLabeledBox(this.regexp, {
padding: 10 padding: 10
}); });
}).bind(this));
} else {
return this.proxy(this.regexp);
}
}, },
groupLabel() { groupLabel() {