From 30d88810d06f07f057dbbdf177aacf74d51d65e1 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Sun, 30 Nov 2014 14:10:27 -0500 Subject: [PATCH] Creating Base module and extracting placeholder rendering --- src/js/parser/javascript/base.js | 41 ++++++++++++++++++++++++++++++ src/js/parser/javascript/regexp.js | 7 +++-- src/js/parser/javascript/root.js | 31 +++------------------- 3 files changed, 50 insertions(+), 29 deletions(-) create mode 100644 src/js/parser/javascript/base.js diff --git a/src/js/parser/javascript/base.js b/src/js/parser/javascript/base.js new file mode 100644 index 0000000..f05a360 --- /dev/null +++ b/src/js/parser/javascript/base.js @@ -0,0 +1,41 @@ +export default { + render_label(container, text) { + var group = container.group(); + + group.rect().attr({ + rx: 10, + ry: 10 + }); + + group.text().attr({ + text: text + }); + + return group; + }, + + position_label(group) { + var text = group.select('text'), + rect = group.select('rect'), + box = text.getBBox(), + margin = 5; + + text.transform(Snap.matrix() + .translate(margin, box.height + margin)); + + rect.attr({ + width: box.width + 2 * margin, + height: box.height + 2 * margin + }); + }, + + render(container) { + container.attr({ 'class': 'placeholder' }); + + this.label = this.render_label(container, this.textValue); + }, + + position() { + this.position_label(this.label); + } +}; diff --git a/src/js/parser/javascript/regexp.js b/src/js/parser/javascript/regexp.js index a560227..eda5eda 100644 --- a/src/js/parser/javascript/regexp.js +++ b/src/js/parser/javascript/regexp.js @@ -1,4 +1,7 @@ -export default { +import _ from 'lodash'; +import Base from './base.js'; + +export default _.extend({}, Base, { matches() { if (this.elements[1].regexp) { return [this.match].concat(this.elements[1].regexp.matches()); @@ -6,4 +9,4 @@ export default { return [this.match]; } } -}; +}); diff --git a/src/js/parser/javascript/root.js b/src/js/parser/javascript/root.js index 8f5e6fc..2c53ff9 100644 --- a/src/js/parser/javascript/root.js +++ b/src/js/parser/javascript/root.js @@ -1,30 +1,7 @@ -export default { - render(container) { - container.attr({ 'class': 'placeholder' }); - - this.rect = container.rect().attr({ - rx: 10, - ry: 10 - }); - - this.text = container.text().attr({ - text: this.textValue - }); - }, - - position() { - var box = this.text.getBBox(), - margin = 5; - - this.text.transform(Snap.matrix() - .translate(margin, box.height + margin)); - - this.rect.attr({ - width: box.width + 2 * margin, - height: box.height + 2 * margin - }); - }, +import _ from 'lodash'; +import Base from './base.js'; +export default _.extend({}, Base, { flags() { var flags; @@ -48,4 +25,4 @@ export default { return this; } } -}; +});