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
|
|
|
import Base from './base.js';
|
|
|
|
|
|
|
|
export default _.extend({}, Base, {
|
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());
|
|
|
|
}))
|
|
|
|
.then((() => {
|
|
|
|
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-13 17:55:03 +00:00
|
|
|
paths.push(Snap.format([
|
|
|
|
'M0,{box.cy}q10,0 10,-10V{top}',
|
|
|
|
'M0,{box.cy}q10,0 10,10V{bottom}',
|
|
|
|
'M{box.width},{box.cy}m40,0q-10,0 -10,-10V{top}',
|
|
|
|
'M{box.width},{box.cy}m40,0q-10,0 -10,10V{bottom}'
|
|
|
|
].join(''), {
|
|
|
|
box: containerBox,
|
2014-12-14 22:56:33 +00:00
|
|
|
top: _.first(matches).getBBox().ay + 10,
|
|
|
|
bottom: _.last(matches).getBBox().ay - 10
|
2014-12-13 17:55:03 +00:00
|
|
|
}));
|
2014-12-01 02:20:03 +00:00
|
|
|
|
2014-12-13 17:55:03 +00:00
|
|
|
this.container.prepend(
|
|
|
|
this.container.path(paths.join('')));
|
|
|
|
}).bind(this));
|
|
|
|
}
|
2014-11-30 22:54:12 +00:00
|
|
|
},
|
|
|
|
|
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,
|
|
|
|
distance = Math.abs(box.ay - containerBox.cy),
|
2014-12-14 22:03:40 +00:00
|
|
|
pathStr;
|
|
|
|
|
|
|
|
if (distance >= 15) {
|
|
|
|
pathStr = [
|
2014-12-14 22:56:33 +00:00
|
|
|
'M10,{box.ay}m0,{shift}q0,{curve} 10,{curve}',
|
|
|
|
'M{containerBox.width},{box.ay}m30,{shift}q0,{curve} -10,{curve}'
|
2014-12-14 22:03:40 +00:00
|
|
|
].join('');
|
|
|
|
} else {
|
|
|
|
pathStr = [
|
|
|
|
'M0,{containerBox.cy}c10,0 10,{anchor.y} 20,{anchor.y}',
|
|
|
|
'M{containerBox.width},{containerBox.cy}m40,0c-10,0 -10,{anchor.y} -20,{anchor.y}'
|
|
|
|
].join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
return Snap.format(pathStr, {
|
|
|
|
containerBox,
|
|
|
|
box,
|
|
|
|
shift: -10 * direction,
|
|
|
|
curve: 10 * direction,
|
|
|
|
anchor: {
|
|
|
|
x: box.x + 20,
|
2014-12-14 22:56:33 +00:00
|
|
|
y: box.ay - containerBox.cy
|
2014-12-14 22:03:40 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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-11-30 19:10:27 +00:00
|
|
|
});
|