2014-11-30 22:54:12 +00:00
|
|
|
import _ from 'lodash';
|
|
|
|
import Base from './base.js';
|
|
|
|
|
|
|
|
export default _.extend({}, Base, {
|
2014-12-03 01:10:56 +00:00
|
|
|
type: 'match',
|
2014-12-03 02:02:48 +00:00
|
|
|
|
2014-12-03 23:59:59 +00:00
|
|
|
render() {
|
|
|
|
var self = this;
|
|
|
|
|
2014-12-03 02:02:48 +00:00
|
|
|
this.contents = {};
|
|
|
|
|
|
|
|
if (this.anchor_start()) {
|
2014-12-04 01:02:56 +00:00
|
|
|
this.contents.anchor_start = this.render_label(this.container, 'Start of line')
|
2014-12-04 01:20:08 +00:00
|
|
|
.addClass('anchor');
|
2014-12-03 02:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.contents.parts = _.map(this.parts(), function(part) {
|
2014-12-03 23:59:59 +00:00
|
|
|
part.container = self.container.group();
|
|
|
|
part.render();
|
|
|
|
return part;
|
2014-12-03 02:02:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (this.anchor_end()) {
|
2014-12-04 01:02:56 +00:00
|
|
|
this.contents.anchor_end = this.render_label(this.container, 'End of line')
|
2014-12-04 01:20:08 +00:00
|
|
|
.addClass('anchor');
|
2014-12-03 02:02:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
position() {
|
2014-12-04 01:26:09 +00:00
|
|
|
var offset = 0,
|
|
|
|
path = [];
|
2014-12-03 02:02:48 +00:00
|
|
|
|
|
|
|
if (this.anchor_start()) {
|
|
|
|
this.position_label(this.contents.anchor_start);
|
2014-12-04 01:26:09 +00:00
|
|
|
offset += this.contents.anchor_start.getBBox().width + 10;
|
|
|
|
path.push(Snap.format('M{x2},{cy}h10', this.contents.anchor_start.getBBox()));
|
2014-12-03 02:02:48 +00:00
|
|
|
}
|
|
|
|
|
2014-12-03 23:59:59 +00:00
|
|
|
_.each(this.contents.parts, function(part) {
|
|
|
|
part.position();
|
|
|
|
part.container.transform(Snap.matrix()
|
2014-12-03 02:02:48 +00:00
|
|
|
.translate(offset, 0));
|
2014-12-04 01:26:09 +00:00
|
|
|
offset += part.container.getBBox().width + 10;
|
|
|
|
path.push(Snap.format('M{x2},{cy}h10', part.container.getBBox()));
|
2014-12-03 02:02:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (this.anchor_end()) {
|
|
|
|
this.position_label(this.contents.anchor_end);
|
|
|
|
this.contents.anchor_end.transform(Snap.matrix()
|
|
|
|
.translate(offset, 0));
|
2014-12-05 02:23:18 +00:00
|
|
|
} else {
|
|
|
|
path.pop();
|
2014-12-03 02:02:48 +00:00
|
|
|
}
|
2014-12-04 01:26:09 +00:00
|
|
|
|
|
|
|
this.container.prepend(
|
|
|
|
this.container.path(path.join(''))
|
|
|
|
);
|
2014-12-03 02:02:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
anchor_start() {
|
|
|
|
return this._anchor_start.textValue !== '';
|
|
|
|
},
|
|
|
|
|
|
|
|
anchor_end() {
|
|
|
|
return this._anchor_end.textValue !== '';
|
|
|
|
},
|
|
|
|
|
|
|
|
parts() {
|
|
|
|
return _.reduce(this._parts.elements, function(result, node) {
|
|
|
|
var last = result.pop();
|
|
|
|
|
|
|
|
if (last) {
|
|
|
|
if (node.elements[0].type === 'terminal' && last.elements[0].type === 'terminal' && last.elements[1].textValue === '') {
|
2014-12-03 02:09:20 +00:00
|
|
|
last = _.clone(last, true);
|
2014-12-03 02:02:48 +00:00
|
|
|
last.textValue += node.textValue;
|
|
|
|
last.elements[0].textValue += node.elements[0].textValue;
|
|
|
|
last.elements[1] = node.elements[1];
|
|
|
|
node = last;
|
|
|
|
} else {
|
|
|
|
result.push(last);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-03 02:09:20 +00:00
|
|
|
result.push(node);
|
2014-12-03 02:02:48 +00:00
|
|
|
return result;
|
|
|
|
}, []);
|
|
|
|
}
|
2014-11-30 22:54:12 +00:00
|
|
|
});
|