2014-11-30 19:10:27 +00:00
|
|
|
import _ from 'lodash';
|
2014-12-13 14:09:58 +00:00
|
|
|
import Q from 'q';
|
2014-11-30 19:10:27 +00:00
|
|
|
|
2014-12-17 16:53:04 +00:00
|
|
|
export default {
|
2014-12-03 01:10:56 +00:00
|
|
|
type: 'regexp',
|
|
|
|
|
2014-12-11 00:01:57 +00:00
|
|
|
_render() {
|
2014-12-13 17:55:03 +00:00
|
|
|
var matches = this.matches(),
|
|
|
|
matchContainer;
|
2014-12-10 21:52:57 +00:00
|
|
|
|
|
|
|
if (matches.length === 1) {
|
2014-12-13 14:09:58 +00:00
|
|
|
return this.proxy(matches[0]);
|
2014-12-10 21:52:57 +00:00
|
|
|
} else {
|
2014-12-13 17:55:03 +00:00
|
|
|
matchContainer = this.container.group()
|
|
|
|
.addClass('regexp-matches')
|
|
|
|
.transform(Snap.matrix()
|
|
|
|
.translate(20, 0));
|
2014-12-10 21:52:57 +00:00
|
|
|
|
2014-12-13 17:55:03 +00:00
|
|
|
return Q.all(_.map(matches, match => {
|
|
|
|
return match.render(matchContainer.group());
|
|
|
|
}))
|
2014-12-16 03:00:24 +00:00
|
|
|
.then(() => {
|
2014-12-13 17:55:03 +00:00
|
|
|
var containerBox,
|
|
|
|
paths;
|
2014-11-30 22:54:12 +00:00
|
|
|
|
2014-12-13 17:55:03 +00:00
|
|
|
this.spaceVertically(matches, {
|
|
|
|
padding: 5
|
|
|
|
});
|
2014-11-30 22:54:12 +00:00
|
|
|
|
2014-12-13 17:55:03 +00:00
|
|
|
containerBox = this.getBBox();
|
2014-12-14 22:03:40 +00:00
|
|
|
paths = _.map(matches, this.makeConnectorLine.bind(this, containerBox));
|
2014-12-01 02:20:03 +00:00
|
|
|
|
2014-12-15 00:08:14 +00:00
|
|
|
paths.push(this.makeSideLine(containerBox, _.first(matches)));
|
|
|
|
paths.push(this.makeSideLine(containerBox, _.last(matches)));
|
2014-12-01 02:20:03 +00:00
|
|
|
|
2014-12-13 17:55:03 +00:00
|
|
|
this.container.prepend(
|
|
|
|
this.container.path(paths.join('')));
|
2014-12-15 02:37:56 +00:00
|
|
|
|
|
|
|
matchContainer.prepend(
|
|
|
|
matchContainer.path(_.map(matches, match => {
|
2014-12-16 02:51:17 +00:00
|
|
|
var box = match.getBBox(),
|
|
|
|
container = matchContainer.getBBox();
|
|
|
|
|
|
|
|
return `M0,${box.ay}h${box.ax}M${box.ax2},${box.ay}H${container.width}`;
|
2014-12-15 02:37:56 +00:00
|
|
|
}).join('')));
|
2014-12-16 03:00:24 +00:00
|
|
|
});
|
2014-12-13 17:55:03 +00:00
|
|
|
}
|
2014-11-30 22:54:12 +00:00
|
|
|
},
|
|
|
|
|
2014-12-15 00:08:14 +00:00
|
|
|
makeSideLine(containerBox, match) {
|
|
|
|
var box = match.getBBox(),
|
|
|
|
direction = box.ay > containerBox.cy ? 1 : -1,
|
|
|
|
distance = Math.abs(box.ay - containerBox.cy);
|
|
|
|
|
|
|
|
if (distance >= 15) {
|
2014-12-16 02:51:17 +00:00
|
|
|
let edge = box.ay - 10 * direction,
|
|
|
|
shift = 10 * direction;
|
|
|
|
|
|
|
|
return [
|
|
|
|
`M0,${containerBox.cy}q10,0 10,${shift}V${edge}`,
|
|
|
|
`M${containerBox.width + 40},${containerBox.cy}q-10,0 -10,${shift}V${edge}`
|
|
|
|
].join('');
|
2014-12-15 00:08:14 +00:00
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-12-14 22:03:40 +00:00
|
|
|
makeConnectorLine(containerBox, match) {
|
|
|
|
var box = match.getBBox(),
|
2014-12-14 22:56:33 +00:00
|
|
|
direction = box.ay > containerBox.cy ? 1 : -1,
|
2014-12-16 02:51:17 +00:00
|
|
|
distance = Math.abs(box.ay - containerBox.cy);
|
2014-12-14 22:03:40 +00:00
|
|
|
|
|
|
|
if (distance >= 15) {
|
2014-12-16 02:51:17 +00:00
|
|
|
let curve = 10 * direction;
|
|
|
|
|
|
|
|
return [
|
|
|
|
`M10,${box.ay - curve}q0,${curve} 10,${curve}`,
|
|
|
|
`M${containerBox.width + 30},${box.ay - curve}q0,${curve} -10,${curve}`
|
2014-12-14 22:03:40 +00:00
|
|
|
].join('');
|
|
|
|
} else {
|
2014-12-16 02:51:17 +00:00
|
|
|
let anchor = box.ay - containerBox.cy;
|
|
|
|
|
|
|
|
return [
|
|
|
|
`M0,${containerBox.cy}c10,0 10,${anchor} 20,${anchor}`,
|
|
|
|
`M${containerBox.width + 40},${containerBox.cy}c-10,0 -10,${anchor} -20,${anchor}`
|
2014-12-14 22:03:40 +00:00
|
|
|
].join('');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-11-26 23:24:40 +00:00
|
|
|
matches() {
|
2014-12-03 00:59:10 +00:00
|
|
|
return [this._match].concat(_.map(this._alternates.elements, _.property('match')));
|
2014-11-26 23:24:40 +00:00
|
|
|
}
|
2014-12-17 16:53:04 +00:00
|
|
|
};
|