From 407167a4ae6895da0aa2d8550e848d02a7f7e07a Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Sat, 13 Dec 2014 13:26:43 -0500 Subject: [PATCH] Folding labeled box positioning into rendering --- src/js/parser/javascript/base.js | 64 +++++++++++++++-------------- src/js/parser/javascript/charset.js | 8 +--- src/js/parser/javascript/subexp.js | 10 ++--- 3 files changed, 39 insertions(+), 43 deletions(-) diff --git a/src/js/parser/javascript/base.js b/src/js/parser/javascript/base.js index 9c9fa6a..ecf0ee1 100644 --- a/src/js/parser/javascript/base.js +++ b/src/js/parser/javascript/base.js @@ -120,43 +120,47 @@ export default { }); }, - renderLabeledBox(label) { - this.label = this.container.text() - .addClass([this.type, 'label'].join('-')) - .attr({ - text: label - }); - - this.box = this.container.rect() - .addClass([this.type, 'box'].join('-')) - .attr({ - rx: 3, - ry: 3 - }); - }, - - positionLabeledBox(content, options) { - var labelBox, contentBox; + renderLabeledBox(label, content, options) { + var deferred = Q.defer(), + label = this.container.text() + .addClass([this.type, 'label'].join('-')) + .attr({ + text: label + }), + box = this.container.rect() + .addClass([this.type, 'box'].join('-')) + .attr({ + rx: 3, + ry: 3 + }); _.defaults(options, { padding: 0 }); - labelBox = this.label.getBBox(); - contentBox = content.getBBox(); + this.container.prepend(label); + this.container.prepend(box); - this.label.transform(Snap.matrix() - .translate(0, labelBox.height)); + setTimeout(deferred.resolve); + deferred.promise.then(() => { + var labelBox = label.getBBox(), + contentBox = content.getBBox(); - this.box - .transform(Snap.matrix() - .translate(0, labelBox.height)) - .attr({ - width: Math.max(contentBox.width + options.padding * 2, labelBox.width), - height: contentBox.height + options.padding * 2 - }); + label.transform(Snap.matrix() + .translate(0, labelBox.height)); - content.transform(Snap.matrix() - .translate(this.box.getBBox().cx - contentBox.cx, labelBox.height + options.padding)); + box + .transform(Snap.matrix() + .translate(0, labelBox.height)) + .attr({ + width: Math.max(contentBox.width + options.padding * 2, labelBox.width), + height: contentBox.height + options.padding * 2 + }); + + content.transform(Snap.matrix() + .translate(box.getBBox().cx - contentBox.cx, labelBox.height + options.padding)); + }); + + return deferred.promise; } }; diff --git a/src/js/parser/javascript/charset.js b/src/js/parser/javascript/charset.js index 3b709d1..4b0cc85 100644 --- a/src/js/parser/javascript/charset.js +++ b/src/js/parser/javascript/charset.js @@ -6,11 +6,7 @@ export default _.extend({}, Base, { type: 'charset', _render() { - var partContainer; - - this.renderLabeledBox(this.invert() ? 'None of:' : 'One of:'); - - partContainer = this.container.group(); + var partContainer = this.container.group(); return Q.all(_.map(this.parts.elements, part => { return part.render(partContainer.group()); @@ -20,7 +16,7 @@ export default _.extend({}, Base, { padding: 5 }); - this.positionLabeledBox(partContainer, { + return this.renderLabeledBox(this.invert() ? 'None of:' : 'One of:', partContainer, { padding: 5 }); }).bind(this)); diff --git a/src/js/parser/javascript/subexp.js b/src/js/parser/javascript/subexp.js index 7df4d1a..b2b9334 100644 --- a/src/js/parser/javascript/subexp.js +++ b/src/js/parser/javascript/subexp.js @@ -16,14 +16,10 @@ export default _.extend({}, Base, { var label = this.groupLabel(); if (label) { - this.renderLabeledBox(label); - return this.regexp.render(this.container.group()) - .then((() => { - this.positionLabeledBox(this.regexp, { - padding: 10 - }); - }).bind(this)); + .then(this.renderLabeledBox.bind(this, label, this.regexp, { + padding: 10 + })); } else { return this.proxy(this.regexp); }