regexper-static/src/js/parser/javascript/node.js

159 lines
3.3 KiB
JavaScript
Raw Normal View History

import util from '../../util.js';
2014-12-10 00:02:31 +00:00
import _ from 'lodash';
export default class Node {
constructor(textValue, offset, elements, properties) {
2014-12-17 17:07:25 +00:00
this.textValue = textValue;
this.offset = offset;
this.elements = elements || [];
this.properties = properties;
this.state = Node.state;
}
set module(mod) {
_.extend(this, mod);
2014-12-17 21:04:55 +00:00
if (this.setup) {
this.setup();
}
2014-12-17 21:04:55 +00:00
_.forOwn(this.definedProperties || {}, (methods, name) => {
Object.defineProperty(this, name, methods);
});
}
2014-12-17 20:14:33 +00:00
set container(container) {
this._container = container;
this._container.addClass(this.type);
}
get container() {
return this._container;
}
2014-12-17 20:24:27 +00:00
get anchor() {
var box;
2014-12-17 20:12:04 +00:00
if (this.proxy) {
2014-12-17 20:24:27 +00:00
return this.proxy.anchor;
2014-12-13 16:35:01 +00:00
} else {
2014-12-17 20:24:27 +00:00
box = this.container.getBBox();
return _.extend({
ax: box.x,
ax2: box.x2,
ay: box.cy
}, this._anchor || {});
2014-12-13 16:35:01 +00:00
}
}
2014-12-13 16:35:01 +00:00
2014-12-10 00:02:31 +00:00
getBBox() {
2014-12-17 20:24:27 +00:00
return _.extend(this.container.getBBox(), this.anchor);
}
2014-12-10 00:02:31 +00:00
2014-12-11 01:28:02 +00:00
transform(matrix) {
return this.container.transform(matrix);
}
2014-12-11 01:28:02 +00:00
deferredStep(value) {
return util.tick().then(() => {
if (this.state.cancelRender) {
throw 'Render cancelled';
}
return value;
2015-03-14 21:11:14 +00:00
});
}
renderLabel(text) {
var group = this.container.group()
.addClass('label'),
rect = group.rect(),
text = group.text(0, 0, _.flatten([text]));
return this.deferredStep(group)
.then(group => {
var box = text.getBBox(),
margin = 5;
text.transform(Snap.matrix()
.translate(margin, box.height / 2 + 2 * margin));
rect.attr({
width: box.width + 2 * margin,
height: box.height + 2 * margin
});
return group;
});
}
2014-12-15 19:20:04 +00:00
startRender() {
this.state.renderCounter++;
}
2014-12-15 19:20:04 +00:00
doneRender() {
this.state.renderCounter--;
}
2014-12-15 19:20:04 +00:00
render(container) {
if (container) {
2014-12-17 20:14:33 +00:00
this.container = container;
}
2014-12-17 20:12:04 +00:00
if (this.proxy) {
2014-12-17 22:18:46 +00:00
return this.proxy.render(this.container);
2014-12-17 20:12:04 +00:00
} else {
this.startRender();
return this._render()
.then(
() => {
this.doneRender();
return this;
},
2015-04-16 21:13:00 +00:00
null
);
2014-12-17 20:12:04 +00:00
}
}
renderLabeledBox(label, content, options) {
2014-12-19 16:48:09 +00:00
var label = this.container.text(0, 0, label)
.addClass([this.type, 'label'].join('-')),
box = this.container.rect()
.addClass([this.type, 'box'].join('-'))
.attr({
rx: 3,
ry: 3
});
2014-12-19 16:48:09 +00:00
options = _.defaults(options || {}, {
padding: 0
});
this.container.prepend(label);
this.container.prepend(box);
return this.deferredStep()
.then(() => {
var labelBox = label.getBBox(),
contentBox = content.getBBox();
label.transform(Snap.matrix()
.translate(0, labelBox.height));
box
.transform(Snap.matrix()
.translate(0, labelBox.height))
.attr({
width: Math.max(contentBox.width + options.padding * 2, labelBox.width),
height: contentBox.height + options.padding * 2
});
content.transform(Snap.matrix()
.translate(box.getBBox().cx - contentBox.cx, labelBox.height + options.padding));
});
}
};