2014-12-18 16:13:15 +00:00
|
|
|
import Q from 'q';
|
2014-12-21 20:22:01 +00:00
|
|
|
import Snap from 'snapsvg';
|
2014-12-30 02:31:36 +00:00
|
|
|
import _ from 'lodash';
|
2014-12-18 16:13:15 +00:00
|
|
|
|
2014-12-20 01:41:02 +00:00
|
|
|
import javascript from './javascript/parser.js';
|
2014-11-26 23:24:40 +00:00
|
|
|
|
2014-12-18 16:13:15 +00:00
|
|
|
export default class Parser {
|
2014-12-30 02:31:36 +00:00
|
|
|
constructor(container, options) {
|
2014-12-18 16:13:15 +00:00
|
|
|
this.state = {
|
|
|
|
groupCounter: 1,
|
|
|
|
renderCounter: 0,
|
|
|
|
maxCounter: 0,
|
2014-12-22 21:57:30 +00:00
|
|
|
cancelRender: false,
|
|
|
|
warnings: []
|
2014-12-18 16:13:15 +00:00
|
|
|
};
|
2014-12-30 02:31:36 +00:00
|
|
|
|
|
|
|
this.options = options || {};
|
|
|
|
_.defaults(this.options, {
|
|
|
|
keepContent: false
|
|
|
|
});
|
|
|
|
|
|
|
|
this.container = container;
|
|
|
|
}
|
|
|
|
|
|
|
|
set container(cont) {
|
|
|
|
this._container = cont;
|
|
|
|
this._container.innerHTML = [
|
|
|
|
document.querySelector('#svg-container-base').innerHTML,
|
|
|
|
this.options.keepContent ? this.container.innerHTML : ''
|
|
|
|
].join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
get container() {
|
|
|
|
return this._container;
|
|
|
|
}
|
|
|
|
|
|
|
|
_addClass(className) {
|
|
|
|
this.container.className = _.compact([this.container.className, className]).join(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
_removeClass(className) {
|
|
|
|
this.container.className = _.without(this.container.className.split(' '), className).join(' ');
|
2014-12-18 16:13:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parse(expression) {
|
|
|
|
var deferred = Q.defer();
|
|
|
|
|
2014-12-30 02:31:36 +00:00
|
|
|
this._addClass('loading');
|
|
|
|
|
2014-12-18 16:13:15 +00:00
|
|
|
setTimeout(() => {
|
2014-12-20 15:47:41 +00:00
|
|
|
try {
|
|
|
|
javascript.Parser.SyntaxNode.state = this.state;
|
|
|
|
|
|
|
|
this.parsed = javascript.parse(expression.replace(/\n/g, '\\n'));
|
|
|
|
deferred.resolve(this);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
deferred.reject(e);
|
|
|
|
}
|
2014-12-18 16:13:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
2014-12-30 02:31:36 +00:00
|
|
|
render() {
|
|
|
|
var svg = Snap(this.container.querySelector('svg')),
|
|
|
|
progress = this.container.querySelector('.progress div');
|
2014-12-25 04:01:32 +00:00
|
|
|
|
2014-12-18 16:13:15 +00:00
|
|
|
return this.parsed.render(svg.group())
|
2014-12-30 02:31:36 +00:00
|
|
|
.then(
|
|
|
|
result => {
|
|
|
|
var box = result.getBBox();
|
|
|
|
|
|
|
|
result.transform(Snap.matrix()
|
|
|
|
.translate(10 - box.x, 10 - box.y));
|
|
|
|
svg.attr({
|
|
|
|
width: box.width + 20,
|
|
|
|
height: box.height + 20
|
|
|
|
});
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
percent => {
|
|
|
|
progress.style.width = percent * 100 + '%';
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.finally(() => {
|
|
|
|
this._removeClass('loading');
|
|
|
|
this.container.removeChild(this.container.querySelector('.progress'));
|
2014-12-18 16:13:15 +00:00
|
|
|
});
|
|
|
|
}
|
2014-12-18 00:24:33 +00:00
|
|
|
|
2014-12-18 16:13:15 +00:00
|
|
|
cancel() {
|
|
|
|
this.state.cancelRender = true;
|
|
|
|
}
|
2014-12-22 21:57:30 +00:00
|
|
|
|
|
|
|
get warnings() {
|
|
|
|
return this.state.warnings;
|
|
|
|
}
|
2014-12-18 16:13:15 +00:00
|
|
|
}
|