Refactoring property definition code

This commit is contained in:
Jeff Avallone 2014-12-17 16:04:55 -05:00
parent a0f145ab3d
commit 17a95392a8
6 changed files with 79 additions and 64 deletions

View File

@ -3,6 +3,21 @@ import Q from 'q';
export default { export default {
type: 'charset', 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() { _render() {
this.partContainer = this.container.group(); this.partContainer = this.container.group();
@ -29,18 +44,5 @@ export default {
return part.textValue; 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)
};
}
});
} }
}; };

View File

@ -4,6 +4,22 @@ import Q from 'q';
export default { export default {
type: 'match', 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() { _render() {
var start, end, var start, end,
partPromises; partPromises;
@ -66,19 +82,5 @@ export default {
if (!this.anchorStart && !this.anchorEnd && this.parts.length === 1) { if (!this.anchorStart && !this.anchorEnd && this.parts.length === 1) {
this.proxy = this.parts[0]; 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)
};
}
});
} }
}; };

View File

@ -3,6 +3,21 @@ import _ from 'lodash';
export default { export default {
type: 'match-fragment', 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() { _render() {
return this.content.render(this.container.group()) return this.content.render(this.container.group())
.then(() => { .then(() => {
@ -64,18 +79,5 @@ export default {
} else { } else {
this.proxy = this.content; 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)
});
}
});
} }
}; };

View File

@ -15,9 +15,14 @@ export default class Node {
set module(mod) { set module(mod) {
_.extend(this, mod); _.extend(this, mod);
if (this.setup) { if (this.setup) {
this.setup(); this.setup();
} }
_.forOwn(this.definedProperties || {}, (methods, name) => {
Object.defineProperty(this, name, methods);
});
} }
set container(container) { set container(container) {

View File

@ -7,14 +7,8 @@ function formatTimes(times) {
} }
export default { export default {
setup() { definedProperties: {
this.minimum = this.properties.spec.minimum; contentPosition: {
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', {
get: function() { get: function() {
var x = 0, y = 0; var x = 0, y = 0;
@ -29,9 +23,9 @@ export default {
return Snap.matrix().translate(x, y); return Snap.matrix().translate(x, y);
} }
}); },
Object.defineProperty(this, 'label', { label: {
get: function() { get: function() {
if (this.minimum >= 2 && this.maximum === -1) { if (this.minimum >= 2 && this.maximum === -1) {
return `${this.minimum - 1}+ times`; 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;
} }
} }

View File

@ -5,6 +5,21 @@ var groupCounter = 1;
export default { export default {
type: 'subexp', 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: { labelMap: {
'?:': '', '?:': '',
'?=': 'positive lookahead', '?=': 'positive lookahead',
@ -34,18 +49,5 @@ export default {
if (!this.label) { if (!this.label) {
this.proxy = this.regexp; 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)
});
}
});
} }
}; };