From 17a95392a8c574e66ad8eac95508dbc9305c3dc4 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Wed, 17 Dec 2014 16:04:55 -0500 Subject: [PATCH] Refactoring property definition code --- src/js/parser/javascript/charset.js | 28 ++++++++++---------- src/js/parser/javascript/match.js | 30 ++++++++++++---------- src/js/parser/javascript/match_fragment.js | 28 ++++++++++---------- src/js/parser/javascript/node.js | 5 ++++ src/js/parser/javascript/repeat.js | 24 +++++++++-------- src/js/parser/javascript/subexp.js | 28 ++++++++++---------- 6 files changed, 79 insertions(+), 64 deletions(-) diff --git a/src/js/parser/javascript/charset.js b/src/js/parser/javascript/charset.js index 1e9c3c7..5c63e2a 100644 --- a/src/js/parser/javascript/charset.js +++ b/src/js/parser/javascript/charset.js @@ -3,6 +3,21 @@ import Q from 'q'; export default { type: 'charset', + definedProperties: { + _anchor: { + get: function() { + var box = this.container.getBBox(), + matrix = this.transform().localMatrix; + + return { + ax: box.x, + ax2: box.x2, + ay: matrix.y(0, this.partContainer.getBBox().cy) + }; + } + } + }, + _render() { this.partContainer = this.container.group(); @@ -29,18 +44,5 @@ export default { return part.textValue; } }); - - Object.defineProperty(this, '_anchor', { - get: function() { - var box = this.container.getBBox(), - matrix = this.transform().localMatrix; - - return { - ax: box.x, - ax2: box.x2, - ay: matrix.y(0, this.partContainer.getBBox().cy) - }; - } - }); } }; diff --git a/src/js/parser/javascript/match.js b/src/js/parser/javascript/match.js index 220037d..2192355 100644 --- a/src/js/parser/javascript/match.js +++ b/src/js/parser/javascript/match.js @@ -4,6 +4,22 @@ import Q from 'q'; export default { type: 'match', + definedProperties: { + _anchor: { + get: function() { + var start = this.normalizeBBox(_.first(this.items).getBBox()), + end = this.normalizeBBox(_.last(this.items).getBBox()), + matrix = this.transform().localMatrix; + + return { + ax: matrix.x(start.ax, start.ay), + ax2: matrix.x(end.ax2, end.ay), + ay: matrix.y(start.ax, start.ay) + }; + } + } + }, + _render() { var start, end, partPromises; @@ -66,19 +82,5 @@ export default { if (!this.anchorStart && !this.anchorEnd && this.parts.length === 1) { this.proxy = this.parts[0]; } - - Object.defineProperty(this, '_anchor', { - get: function() { - var start = this.normalizeBBox(_.first(this.items).getBBox()), - end = this.normalizeBBox(_.last(this.items).getBBox()), - matrix = this.transform().localMatrix; - - return { - ax: matrix.x(start.ax, start.ay), - ax2: matrix.x(end.ax2, end.ay), - ay: matrix.y(start.ax, start.ay) - }; - } - }); } }; diff --git a/src/js/parser/javascript/match_fragment.js b/src/js/parser/javascript/match_fragment.js index 0985c36..80273b3 100644 --- a/src/js/parser/javascript/match_fragment.js +++ b/src/js/parser/javascript/match_fragment.js @@ -3,6 +3,21 @@ import _ from 'lodash'; export default { type: 'match-fragment', + definedProperties: { + _anchor: { + get: function() { + var anchor = this.content.anchor, + matrix = this.transform().localMatrix; + + return _.extend(anchor, { + ax: matrix.x(anchor.ax, anchor.ay), + ax2: matrix.x(anchor.ax2, anchor.ay), + ay: matrix.y(anchor.ax, anchor.ay) + }); + } + } + }, + _render() { return this.content.render(this.container.group()) .then(() => { @@ -64,18 +79,5 @@ export default { } else { this.proxy = this.content; } - - Object.defineProperty(this, '_anchor', { - get: function() { - var anchor = this.content.anchor, - matrix = this.transform().localMatrix; - - return _.extend(anchor, { - ax: matrix.x(anchor.ax, anchor.ay), - ax2: matrix.x(anchor.ax2, anchor.ay), - ay: matrix.y(anchor.ax, anchor.ay) - }); - } - }); } }; diff --git a/src/js/parser/javascript/node.js b/src/js/parser/javascript/node.js index 9abf4b2..62f4267 100644 --- a/src/js/parser/javascript/node.js +++ b/src/js/parser/javascript/node.js @@ -15,9 +15,14 @@ export default class Node { set module(mod) { _.extend(this, mod); + if (this.setup) { this.setup(); } + + _.forOwn(this.definedProperties || {}, (methods, name) => { + Object.defineProperty(this, name, methods); + }); } set container(container) { diff --git a/src/js/parser/javascript/repeat.js b/src/js/parser/javascript/repeat.js index 84ba6c2..73c420f 100644 --- a/src/js/parser/javascript/repeat.js +++ b/src/js/parser/javascript/repeat.js @@ -7,14 +7,8 @@ function formatTimes(times) { } export default { - setup() { - this.minimum = this.properties.spec.minimum; - this.maximum = this.properties.spec.maximum; - this.greedy = (this.properties.greedy.textValue === ''); - this.hasSkip = this.minimum === 0; - this.hasLoop = this.maximum === -1 || this.maximum > 1; - - Object.defineProperty(this, 'contentPosition', { + definedProperties: { + contentPosition: { get: function() { var x = 0, y = 0; @@ -29,9 +23,9 @@ export default { return Snap.matrix().translate(x, y); } - }); + }, - Object.defineProperty(this, 'label', { + label: { get: function() { if (this.minimum >= 2 && this.maximum === -1) { return `${this.minimum - 1}+ times`; @@ -45,6 +39,14 @@ export default { } } } - }); + } + }, + + setup() { + this.minimum = this.properties.spec.minimum; + this.maximum = this.properties.spec.maximum; + this.greedy = (this.properties.greedy.textValue === ''); + this.hasSkip = this.minimum === 0; + this.hasLoop = this.maximum === -1 || this.maximum > 1; } } diff --git a/src/js/parser/javascript/subexp.js b/src/js/parser/javascript/subexp.js index 948b94b..1e16a42 100644 --- a/src/js/parser/javascript/subexp.js +++ b/src/js/parser/javascript/subexp.js @@ -5,6 +5,21 @@ var groupCounter = 1; export default { type: 'subexp', + definedProperties: { + _anchor: { + get: function() { + var anchor = this.regexp.anchor, + matrix = this.transform().localMatrix; + + return _.extend(anchor, { + ax: matrix.x(anchor.ax, anchor.ay), + ax2: matrix.x(anchor.ax2, anchor.ay), + ay: matrix.y(anchor.ax, anchor.ay) + }); + } + } + }, + labelMap: { '?:': '', '?=': 'positive lookahead', @@ -34,18 +49,5 @@ export default { if (!this.label) { this.proxy = this.regexp; } - - Object.defineProperty(this, '_anchor', { - get: function() { - var anchor = this.regexp.anchor, - matrix = this.transform().localMatrix; - - return _.extend(anchor, { - ax: matrix.x(anchor.ax, anchor.ay), - ax2: matrix.x(anchor.ax2, anchor.ay), - ay: matrix.y(anchor.ax, anchor.ay) - }); - } - }); } };