2014-11-30 22:54:12 +00:00
|
|
|
import _ from 'lodash';
|
2014-12-13 14:09:58 +00:00
|
|
|
import Q from 'q';
|
2014-11-30 22:54:12 +00:00
|
|
|
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-11 00:01:57 +00:00
|
|
|
_render() {
|
2014-12-11 00:58:36 +00:00
|
|
|
this.contents = { parts: this.parts() };
|
2014-12-03 02:02:48 +00:00
|
|
|
|
2014-12-11 00:58:36 +00:00
|
|
|
if (this.anchorStart()) {
|
|
|
|
this.contents.anchor_start = this.renderLabel('Start of line')
|
|
|
|
.addClass('anchor');
|
|
|
|
}
|
2014-12-10 21:52:57 +00:00
|
|
|
|
2014-12-11 00:58:36 +00:00
|
|
|
if (this.anchorEnd()) {
|
|
|
|
this.contents.anchor_end = this.renderLabel('End of line')
|
|
|
|
.addClass('anchor');
|
|
|
|
}
|
2014-12-03 02:02:48 +00:00
|
|
|
|
2014-12-11 00:58:36 +00:00
|
|
|
if (this.contents.anchor_start || this.contents.anchor_end || this.contents.parts.length !== 1) {
|
2014-12-13 14:09:58 +00:00
|
|
|
return Q.all(_.map(this.contents.parts, (function(part) {
|
|
|
|
return part.render(this.container.group());
|
|
|
|
}).bind(this)));
|
2014-12-10 21:52:57 +00:00
|
|
|
} else {
|
2014-12-13 14:09:58 +00:00
|
|
|
return this.proxy(this.contents.parts[0]);
|
2014-12-03 02:02:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-12-11 00:01:57 +00:00
|
|
|
_position() {
|
2014-12-13 14:09:58 +00:00
|
|
|
var items = _(this.contents).at('anchor_start', 'parts', 'anchor_end').flatten().compact().value();
|
2014-12-03 02:02:48 +00:00
|
|
|
|
2014-12-11 00:01:57 +00:00
|
|
|
this.spaceHorizontally(items, {
|
|
|
|
padding: 10
|
|
|
|
});
|
2014-12-03 02:02:48 +00:00
|
|
|
},
|
|
|
|
|
2014-12-09 23:08:40 +00:00
|
|
|
anchorStart() {
|
2014-12-03 02:02:48 +00:00
|
|
|
return this._anchor_start.textValue !== '';
|
|
|
|
},
|
|
|
|
|
2014-12-09 23:08:40 +00:00
|
|
|
anchorEnd() {
|
2014-12-03 02:02:48 +00:00
|
|
|
return this._anchor_end.textValue !== '';
|
|
|
|
},
|
|
|
|
|
|
|
|
parts() {
|
|
|
|
return _.reduce(this._parts.elements, function(result, node) {
|
2014-12-11 01:11:51 +00:00
|
|
|
var last = _.last(result);
|
|
|
|
|
|
|
|
if (last && node.elements[0].type === 'literal' && node.elements[1].textValue === '' && last.elements[0].type === 'literal' && last.elements[1].textValue === '') {
|
|
|
|
last.textValue += node.textValue;
|
|
|
|
last.elements[0].textValue += node.elements[0].textValue;
|
|
|
|
last.elements[0].literal.textValue += node.elements[0].literal.textValue;
|
|
|
|
} else {
|
|
|
|
result.push(node);
|
2014-12-03 02:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}, []);
|
|
|
|
}
|
2014-11-30 22:54:12 +00:00
|
|
|
});
|