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

245 lines
5.2 KiB
JavaScript
Raw Normal View History

2014-12-10 00:02:31 +00:00
import _ from 'lodash';
import Q from 'q';
2014-12-10 00:02:31 +00:00
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
normalizeBBox(box) {
return _.extend({
ax: box.x,
ax2: box.x2,
ay: box.cy
}, box);
}
2014-12-11 01:28:02 +00:00
transform(matrix) {
return this.container.transform(matrix);
}
2014-12-11 01:28:02 +00:00
deferredStep() {
var deferred = Q.defer(),
result = arguments;
setTimeout(() => {
if (this.state.cancelRender) {
deferred.reject('Render cancelled');
} else {
deferred.resolve.apply(this, result);
}
}, 1);
return deferred.promise;
}
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() {
var evt;
2014-12-15 23:06:16 +00:00
if (this.state.maxCounter === 0) {
this.state.maxCounter = this.state.renderCounter;
2014-12-15 19:20:04 +00:00
}
this.state.renderCounter--;
2014-12-15 23:06:16 +00:00
evt = document.createEvent('Event');
evt.initEvent('updateStatus', true, true);
evt.detail = {
percentage: (this.state.maxCounter - this.state.renderCounter) / this.state.maxCounter
2014-12-15 23:06:16 +00:00
};
document.body.dispatchEvent(evt);
2014-12-15 19:20:04 +00:00
if (this.state.renderCounter === 0) {
this.state.maxCounter = 0;
2014-12-15 19:20:04 +00:00
}
2014-12-15 23:06:16 +00:00
return this.deferredStep();
}
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.bind(this))
.then(_.constant(this));
}
}
2014-12-10 00:02:31 +00:00
spaceHorizontally(items, options) {
var verticalCenter = 0,
normalize = this.normalizeBBox;
2014-12-10 00:02:31 +00:00
_.defaults(options, {
padding: 0
});
_.reduce(items, (offset, item) => {
2014-12-11 01:28:02 +00:00
var box;
2014-12-10 00:02:31 +00:00
2014-12-11 01:28:02 +00:00
item.transform(Snap.matrix()
2014-12-10 00:02:31 +00:00
.translate(offset, 0));
box = normalize(item.getBBox());
verticalCenter = Math.max(verticalCenter, box.ay);
2014-12-10 00:02:31 +00:00
return offset + options.padding + box.width;
}, 0);
for (var item of items) {
let box = normalize(item.getBBox());
2014-12-11 01:28:02 +00:00
item.transform(Snap.matrix()
.add(item.transform().localMatrix)
.translate(0, verticalCenter - box.ay));
}
}
spaceVertically(items, options) {
var horizontalCenter = 0;
_.defaults(options, {
padding: 0
});
_.reduce(items, (offset, item) => {
2014-12-11 01:28:02 +00:00
var box;
2014-12-11 01:28:02 +00:00
item.transform(Snap.matrix()
.translate(0, offset));
box = item.getBBox();
horizontalCenter = Math.max(horizontalCenter, box.cx);
return offset + options.padding + box.height;
}, 0);
for (var item of items) {
2014-12-11 01:28:02 +00:00
item.transform(Snap.matrix()
.add(item.transform().localMatrix)
.translate(horizontalCenter - item.getBBox().cx, 0));
}
}
renderLabeledBox(label, content, options) {
var label = this.container.text()
.addClass([this.type, 'label'].join('-'))
.attr({
text: label
}),
box = this.container.rect()
.addClass([this.type, 'box'].join('-'))
.attr({
rx: 3,
ry: 3
});
_.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));
});
}
};