Adding documentation to match_fragment.js

This commit is contained in:
Jeff Avallone 2015-04-19 21:16:54 -04:00
parent 424eca0a2a
commit ce41796f8b

View File

@ -1,9 +1,16 @@
// MatchFragment nodes are part of a [Match](./match.html) followed by an
// optional [Repeat](./repeat.html) node. If no repeat is applied, then
// rendering is proxied to the content node.
import _ from 'lodash'; import _ from 'lodash';
export default { export default {
type: 'match-fragment', type: 'match-fragment',
definedProperties: { definedProperties: {
// Default anchor is overridden to apply an transforms from the fragment
// to its content's anchor. Essentially, the fragment inherits the anchor
// of its content.
_anchor: { _anchor: {
get: function() { get: function() {
var anchor = this.content.getBBox(), var anchor = this.content.getBBox(),
@ -18,15 +25,18 @@ export default {
} }
}, },
// Renders the fragment into the currently set container.
_render() { _render() {
return this.content.render(this.container.group()) return this.content.render(this.container.group())
.then(() => { .then(() => {
var box, paths; var box, paths;
// Contents must be transformed based on the repeat that is applied.
this.content.transform(this.repeat.contentPosition); this.content.transform(this.repeat.contentPosition);
box = this.content.getBBox(); box = this.content.getBBox();
// Add skip or repeat paths to the container.
paths = _.flatten([ paths = _.flatten([
this.skipPath(box), this.skipPath(box),
this.loopPath(box) this.loopPath(box)
@ -39,6 +49,8 @@ export default {
}); });
}, },
// Returns the path spec to render the line that skips over the content for
// fragments that are optionally matched.
skipPath(box) { skipPath(box) {
var paths = []; var paths = [];
@ -48,6 +60,7 @@ export default {
paths.push(`M0,${box.ay}q10,0 10,-10v${-vert}q0,-10 10,-10h${horiz}q10,0 10,10v${vert}q0,10 10,10`); paths.push(`M0,${box.ay}q10,0 10,-10v${-vert}q0,-10 10,-10h${horiz}q10,0 10,10v${vert}q0,10 10,10`);
// When the repeat is not greedy, the skip path gets a preference arrow.
if (!this.repeat.greedy) { if (!this.repeat.greedy) {
paths.push(`M10,${box.ay - 15}l5,5m-5,-5l-5,5`); paths.push(`M10,${box.ay - 15}l5,5m-5,-5l-5,5`);
} }
@ -56,6 +69,8 @@ export default {
return paths; return paths;
}, },
// Returns the path spec to render the line that repeats the content for
// fragments that are matched more than once.
loopPath(box) { loopPath(box) {
var paths = []; var paths = [];
@ -64,6 +79,7 @@ export default {
paths.push(`M${box.x},${box.ay}q-10,0 -10,10v${vert}q0,10 10,10h${box.width}q10,0 10,-10v${-vert}q0,-10 -10,-10`); paths.push(`M${box.x},${box.ay}q-10,0 -10,10v${vert}q0,10 10,10h${box.width}q10,0 10,-10v${-vert}q0,-10 -10,-10`);
// When the repeat is greedy, the loop path gets the preference arrow.
if (this.repeat.greedy) { if (this.repeat.greedy) {
paths.push(`M${box.x2 + 10},${box.ay + 15}l5,-5m-5,5l-5,-5`); paths.push(`M${box.x2 + 10},${box.ay + 15}l5,-5m-5,5l-5,-5`);
} }
@ -72,6 +88,8 @@ export default {
return paths; return paths;
}, },
// Renders label for the loop path indicating how many times the content may
// be matched.
loopLabel() { loopLabel() {
var labelStr = this.repeat.label, var labelStr = this.repeat.label,
label, labelBox, box; label, labelBox, box;
@ -89,13 +107,19 @@ export default {
}, },
setup() { setup() {
// Then content of the fragment.
this.content = this.properties.content; this.content = this.properties.content;
// The repetition rule for the fragment.
this.repeat = this.properties.repeat; this.repeat = this.properties.repeat;
if (!this.repeat.hasLoop && !this.repeat.hasSkip) { if (!this.repeat.hasLoop && !this.repeat.hasSkip) {
// For fragments without a skip or loop, rendering is proxied to the
// content. Also set flag indicating that contents can be merged if the
// content is a literal node.
this.canMerge = (this.content.type === 'literal'); this.canMerge = (this.content.type === 'literal');
this.proxy = this.content; this.proxy = this.content;
} else { } else {
// Fragments that have skip or loop lines cannot be merged with others.
this.canMerge = false; this.canMerge = false;
} }
} }