regexper-static/src/js/parser/javascript/match.js

99 lines
2.2 KiB
JavaScript
Raw Normal View History

import util from '../../util.js';
import _ from 'lodash';
import Q from 'q';
export default {
type: 'match',
2014-12-17 21:04:55 +00:00
definedProperties: {
_anchor: {
get: function() {
var start = util.normalizeBBox(this.start.getBBox()),
end = util.normalizeBBox(this.end.getBBox()),
2014-12-17 21:04:55 +00:00
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,
items;
if (this.anchorStart) {
2014-12-13 18:42:55 +00:00
start = this.renderLabel('Start of line')
.invoke('addClass', 'anchor');
2014-12-11 00:58:36 +00:00
}
if (this.anchorEnd) {
2014-12-13 18:42:55 +00:00
end = this.renderLabel('End of line')
.invoke('addClass', 'anchor');
2014-12-11 00:58:36 +00:00
}
2014-12-17 20:12:04 +00:00
partPromises = _.map(this.parts, part => {
return part.render(this.container.group());
});
2014-12-15 02:37:56 +00:00
items = _([start, partPromises, end]).flatten().compact().value();
if (items.length === 0) {
items = [this.container.group()];
}
return Q.all(items)
2014-12-17 20:12:04 +00:00
.then(items => {
this.start = _.first(items);
this.end = _.last(items);
util.spaceHorizontally(items, {
2014-12-17 20:12:04 +00:00
padding: 10
});
2014-12-15 02:37:56 +00:00
2014-12-17 20:12:04 +00:00
this.container.prepend(
2014-12-26 17:48:02 +00:00
this.container.path(this.connectorPaths(items).join('')));
2014-12-17 20:12:04 +00:00
});
},
2014-12-26 17:48:02 +00:00
connectorPaths(items) {
var prev, next;
prev = util.normalizeBBox(_.first(items).getBBox());
return _.map(items.slice(1), item => {
try {
next = util.normalizeBBox(item.getBBox());
return `M${prev.ax2},${prev.ay}H${next.ax}`;
}
finally {
prev = next;
}
});
},
setup() {
this.parts = _.reduce(this.properties.parts.elements, function(result, node) {
2014-12-11 01:11:51 +00:00
var last = _.last(result);
2014-12-17 19:56:02 +00:00
if (last && node.canMerge && last.canMerge) {
2014-12-26 17:48:02 +00:00
last.content.merge(node.content);
2014-12-11 01:11:51 +00:00
} else {
result.push(node);
}
return result;
}, []);
2014-12-15 00:08:14 +00:00
2014-12-26 17:48:02 +00:00
this.anchorStart = (this.properties.anchor_start.textValue !== '');
this.anchorEnd = (this.properties.anchor_end.textValue !== '');
2014-12-17 20:12:04 +00:00
if (!this.anchorStart && !this.anchorEnd && this.parts.length === 1) {
this.proxy = this.parts[0];
}
}
};