2014-11-30 19:10:27 +00:00
|
|
|
import _ from 'lodash';
|
|
|
|
import Base from './base.js';
|
|
|
|
|
|
|
|
export default _.extend({}, Base, {
|
2014-12-03 01:10:56 +00:00
|
|
|
type: 'regexp',
|
|
|
|
|
2014-12-03 23:59:59 +00:00
|
|
|
render() {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
_.each(this.matches(), match => {
|
|
|
|
match.container = self.container.group();
|
|
|
|
match.render();
|
|
|
|
return match.container;
|
2014-11-30 22:54:12 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
position() {
|
2014-12-03 23:59:59 +00:00
|
|
|
var self = this,
|
|
|
|
center,
|
2014-12-01 02:20:03 +00:00
|
|
|
positions,
|
|
|
|
totalHeight,
|
2014-12-03 23:46:48 +00:00
|
|
|
verticalCenter,
|
2014-12-03 23:59:59 +00:00
|
|
|
matches = this.matches(),
|
2014-12-04 01:35:26 +00:00
|
|
|
includeLines = (matches.length > 1),
|
|
|
|
paths = [];
|
2014-11-30 22:54:12 +00:00
|
|
|
|
2014-12-03 23:59:59 +00:00
|
|
|
_.invoke(matches, 'position');
|
2014-11-30 22:54:12 +00:00
|
|
|
|
2014-12-03 23:59:59 +00:00
|
|
|
positions = _.chain(matches)
|
|
|
|
.map(match => {
|
|
|
|
return {
|
|
|
|
box: match.container.getBBox(),
|
|
|
|
content: match.container
|
|
|
|
};
|
2014-11-30 22:54:12 +00:00
|
|
|
});
|
|
|
|
center = positions.reduce((center, pos) => {
|
|
|
|
return Math.max(center, pos.box.cx);
|
|
|
|
}, 0).value();
|
|
|
|
|
2014-12-01 02:20:03 +00:00
|
|
|
totalHeight = positions.reduce((offset, pos) => {
|
2014-11-30 22:54:12 +00:00
|
|
|
pos.content.transform(Snap.matrix()
|
2014-12-03 23:46:48 +00:00
|
|
|
.translate(center - pos.box.cx + (includeLines ? 20 : 0), offset));
|
2014-11-30 22:54:12 +00:00
|
|
|
|
|
|
|
return offset + pos.box.height + 5;
|
2014-12-01 02:20:03 +00:00
|
|
|
}, 0).value() - 5;
|
|
|
|
|
|
|
|
verticalCenter = totalHeight / 2
|
|
|
|
|
2014-12-03 23:46:48 +00:00
|
|
|
if (includeLines) {
|
|
|
|
positions.each(pos => {
|
|
|
|
var box = pos.content.getBBox(),
|
|
|
|
direction = box.cy > verticalCenter ? 1 : -1,
|
2014-12-04 01:35:26 +00:00
|
|
|
pathStr;
|
2014-12-01 02:20:03 +00:00
|
|
|
|
2014-12-03 23:46:48 +00:00
|
|
|
pathStr = (verticalCenter === box.cy) ?
|
|
|
|
'M0,{center}H{side}' :
|
|
|
|
'M0,{center}q10,0 10,{d}V{target}q0,{d} 10,{d}H{side}';
|
2014-12-01 02:20:03 +00:00
|
|
|
|
2014-12-04 01:35:26 +00:00
|
|
|
paths.push(Snap.format(pathStr, {
|
2014-12-03 23:46:48 +00:00
|
|
|
center: verticalCenter,
|
|
|
|
target: box.cy - 10 * direction,
|
|
|
|
side: box.x,
|
|
|
|
d: 10 * direction
|
|
|
|
}));
|
2014-12-04 01:35:26 +00:00
|
|
|
});
|
2014-12-01 02:20:03 +00:00
|
|
|
|
2014-12-04 01:35:26 +00:00
|
|
|
this.container.path(paths.join(''))
|
|
|
|
.clone().transform(Snap.matrix()
|
2014-12-03 23:46:48 +00:00
|
|
|
.scale(-1, 1, center + 20, 0));
|
|
|
|
}
|
2014-11-30 22:54:12 +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
|
|
|
});
|