2014-12-10 00:02:31 +00:00
|
|
|
import _ from 'lodash';
|
2014-12-13 14:09:58 +00:00
|
|
|
import Q from 'q';
|
2014-12-10 00:02:31 +00:00
|
|
|
|
2014-11-30 19:10:27 +00:00
|
|
|
export default {
|
2014-12-10 21:57:42 +00:00
|
|
|
setContainer(container) {
|
|
|
|
this.container = container;
|
|
|
|
this.container.addClass(this.type);
|
|
|
|
},
|
|
|
|
|
2014-12-10 00:02:31 +00:00
|
|
|
getBBox() {
|
|
|
|
return this.container.getBBox();
|
|
|
|
},
|
|
|
|
|
2014-12-11 01:28:02 +00:00
|
|
|
transform(matrix) {
|
|
|
|
return this.container.transform(matrix);
|
|
|
|
},
|
|
|
|
|
2014-12-11 00:12:17 +00:00
|
|
|
renderLabel(text) {
|
2014-12-13 17:28:16 +00:00
|
|
|
var deferred = Q.defer(),
|
|
|
|
group = this.container.group()
|
|
|
|
.addClass('label'),
|
|
|
|
rect = group.rect(),
|
|
|
|
text = group.text().attr({
|
|
|
|
text: text
|
|
|
|
});
|
|
|
|
|
|
|
|
setTimeout(deferred.resolve.bind(deferred, group));
|
|
|
|
deferred.promise.then(() => {
|
|
|
|
var box = text.getBBox(),
|
|
|
|
margin = 5;
|
|
|
|
|
|
|
|
text.transform(Snap.matrix()
|
|
|
|
.translate(margin, box.height + margin));
|
|
|
|
|
|
|
|
rect.attr({
|
|
|
|
width: box.width + 2 * margin,
|
|
|
|
height: box.height + 2 * margin
|
|
|
|
});
|
2014-11-30 19:10:27 +00:00
|
|
|
});
|
|
|
|
|
2014-12-13 17:28:16 +00:00
|
|
|
return deferred.promise;
|
2014-11-30 19:10:27 +00:00
|
|
|
},
|
|
|
|
|
2014-12-11 00:31:07 +00:00
|
|
|
render(container) {
|
|
|
|
if (container) {
|
|
|
|
this.setContainer(container);
|
|
|
|
}
|
|
|
|
|
2014-12-13 18:00:46 +00:00
|
|
|
return this._render().then(_.constant(this));
|
2014-12-11 00:01:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
proxy(node) {
|
2014-12-13 18:00:46 +00:00
|
|
|
return node.render(this.container);
|
2014-12-13 14:09:58 +00:00
|
|
|
},
|
|
|
|
|
2014-12-11 00:01:57 +00:00
|
|
|
_render() {
|
2014-12-13 14:09:58 +00:00
|
|
|
console.log(this.type, this);
|
2014-12-06 21:03:58 +00:00
|
|
|
|
2014-12-04 01:20:08 +00:00
|
|
|
this.container.addClass('placeholder');
|
2014-11-30 19:10:27 +00:00
|
|
|
|
2014-12-13 17:28:16 +00:00
|
|
|
return this.renderLabel(this.type + ': ' + this.textValue).then(label => {
|
|
|
|
label.select('rect').attr({
|
2014-12-11 00:23:14 +00:00
|
|
|
rx: 10,
|
|
|
|
ry: 10
|
|
|
|
});
|
2014-12-13 17:28:16 +00:00
|
|
|
});
|
2014-12-10 00:02:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
spaceHorizontally(items, options) {
|
|
|
|
var verticalCenter = 0;
|
|
|
|
|
|
|
|
_.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 = item.getBBox();
|
|
|
|
|
|
|
|
verticalCenter = Math.max(verticalCenter, box.cy);
|
|
|
|
|
|
|
|
return offset + options.padding + box.width;
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
_.each(items, item => {
|
2014-12-11 01:28:02 +00:00
|
|
|
item.transform(Snap.matrix()
|
|
|
|
.add(item.transform().localMatrix)
|
2014-12-10 00:02:31 +00:00
|
|
|
.translate(0, verticalCenter - item.getBBox().cy));
|
|
|
|
});
|
2014-12-10 01:08:34 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
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-10 01:08:34 +00:00
|
|
|
|
2014-12-11 01:28:02 +00:00
|
|
|
item.transform(Snap.matrix()
|
2014-12-10 01:08:34 +00:00
|
|
|
.translate(0, offset));
|
|
|
|
|
|
|
|
box = item.getBBox();
|
|
|
|
|
|
|
|
horizontalCenter = Math.max(horizontalCenter, box.cx);
|
|
|
|
|
|
|
|
return offset + options.padding + box.height;
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
_.each(items, item => {
|
2014-12-11 01:28:02 +00:00
|
|
|
item.transform(Snap.matrix()
|
|
|
|
.add(item.transform().localMatrix)
|
2014-12-10 01:08:34 +00:00
|
|
|
.translate(horizontalCenter - item.getBBox().cx, 0));
|
|
|
|
});
|
2014-12-10 23:27:07 +00:00
|
|
|
},
|
|
|
|
|
2014-12-13 18:26:43 +00:00
|
|
|
renderLabeledBox(label, content, options) {
|
|
|
|
var deferred = Q.defer(),
|
|
|
|
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
|
|
|
|
});
|
2014-12-10 23:27:07 +00:00
|
|
|
|
|
|
|
_.defaults(options, {
|
|
|
|
padding: 0
|
|
|
|
});
|
|
|
|
|
2014-12-13 18:26:43 +00:00
|
|
|
this.container.prepend(label);
|
|
|
|
this.container.prepend(box);
|
2014-12-10 23:27:07 +00:00
|
|
|
|
2014-12-13 18:26:43 +00:00
|
|
|
setTimeout(deferred.resolve);
|
|
|
|
deferred.promise.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
|
|
|
|
});
|
2014-12-10 23:27:07 +00:00
|
|
|
|
2014-12-13 18:26:43 +00:00
|
|
|
content.transform(Snap.matrix()
|
|
|
|
.translate(box.getBBox().cx - contentBox.cx, labelBox.height + options.padding));
|
|
|
|
});
|
2014-12-10 23:27:07 +00:00
|
|
|
|
2014-12-13 18:26:43 +00:00
|
|
|
return deferred.promise;
|
2014-11-30 19:10:27 +00:00
|
|
|
}
|
|
|
|
};
|