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-11 00:01:57 +00:00
|
|
|
_render() {
|
2014-12-10 21:52:57 +00:00
|
|
|
var matches = this.matches();
|
|
|
|
|
|
|
|
if (matches.length === 1) {
|
2014-12-11 00:01:57 +00:00
|
|
|
this.proxy(matches[0]);
|
2014-12-10 21:52:57 +00:00
|
|
|
} else {
|
|
|
|
this.matchContainer = this.container.group()
|
|
|
|
.addClass('regexp-matches');
|
|
|
|
|
|
|
|
_.each(matches, (match => {
|
2014-12-11 00:31:07 +00:00
|
|
|
match.render(this.matchContainer.group());
|
2014-12-10 21:52:57 +00:00
|
|
|
}).bind(this));
|
|
|
|
}
|
2014-11-30 22:54:12 +00:00
|
|
|
},
|
|
|
|
|
2014-12-11 00:01:57 +00:00
|
|
|
_position() {
|
2014-12-10 01:08:34 +00:00
|
|
|
var matches = this.matches(),
|
|
|
|
containerBox,
|
|
|
|
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-11 00:01:57 +00:00
|
|
|
this.spaceVertically(matches, {
|
|
|
|
padding: 5
|
|
|
|
});
|
2014-12-10 21:52:57 +00:00
|
|
|
|
2014-12-11 00:01:57 +00:00
|
|
|
this.matchContainer.transform(Snap.matrix()
|
|
|
|
.translate(20, 0));
|
2014-12-10 01:08:34 +00:00
|
|
|
|
2014-12-11 00:01:57 +00:00
|
|
|
containerBox = this.getBBox();
|
|
|
|
paths = _.map(matches, match => {
|
|
|
|
var box = match.getBBox(),
|
|
|
|
direction = box.cy > containerBox.cy ? 1 : -1,
|
|
|
|
distance = Math.abs(box.cy - containerBox.cy),
|
|
|
|
pathStr;
|
2014-12-01 02:20:03 +00:00
|
|
|
|
2014-12-11 00:01:57 +00:00
|
|
|
if (distance >= 15) {
|
|
|
|
pathStr = [
|
|
|
|
'M10,{box.cy}m0,{shift}q0,{curve} 10,{curve}',
|
|
|
|
'M{containerBox.width},{box.cy}m30,{shift}q0,{curve} -10,{curve}'
|
|
|
|
].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('');
|
|
|
|
}
|
2014-12-01 02:20:03 +00:00
|
|
|
|
2014-12-11 00:01:57 +00:00
|
|
|
return Snap.format(pathStr, {
|
|
|
|
containerBox,
|
|
|
|
box,
|
|
|
|
shift: -10 * direction,
|
|
|
|
curve: 10 * direction,
|
|
|
|
anchor: {
|
|
|
|
x: box.x + 20,
|
|
|
|
y: box.cy - containerBox.cy
|
|
|
|
}
|
2014-12-04 01:35:26 +00:00
|
|
|
});
|
2014-12-11 00:01:57 +00:00
|
|
|
});
|
2014-12-01 02:20:03 +00:00
|
|
|
|
2014-12-11 00:01:57 +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,
|
|
|
|
top: _.first(matches).getBBox().cy + 10,
|
|
|
|
bottom: _.last(matches).getBBox().cy - 10
|
|
|
|
}));
|
2014-12-10 01:08:34 +00:00
|
|
|
|
2014-12-11 00:09:31 +00:00
|
|
|
this.container.prepend(
|
|
|
|
this.container.path(paths.join('')));
|
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
|
|
|
});
|