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

95 lines
2.1 KiB
JavaScript
Raw Normal View History

import Q from 'q';
import Snap from 'snapsvg';
import _ from 'lodash';
import javascript from './javascript/parser.js';
import ParserState from './javascript/parser_state.js';
export default class Parser {
constructor(container, options) {
this.options = options || {};
_.defaults(this.options, {
keepContent: false
});
this.container = container;
this.state = new ParserState(this.container.querySelector('.progress div'));
}
set container(cont) {
this._container = cont;
this._container.innerHTML = [
document.querySelector('#svg-container-base').innerHTML,
this.options.keepContent ? this.container.innerHTML : ''
].join('');
2014-12-30 15:17:55 +00:00
this._addClass('svg-container');
}
get container() {
return this._container;
}
_addClass(className) {
2014-12-30 15:17:55 +00:00
this.container.className = _(this.container.className.split(' '))
.union([className])
.value()
.join(' ');
}
_removeClass(className) {
2014-12-30 15:17:55 +00:00
this.container.className = _(this.container.className.split(' '))
.without(className)
.value()
.join(' ');
}
parse(expression) {
var deferred = Q.defer();
this._addClass('loading');
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);
}
});
return deferred.promise;
}
render() {
var svg = Snap(this.container.querySelector('svg'));
return this.parsed.render(svg.group())
.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
});
})
.finally(() => {
this._removeClass('loading');
this.container.removeChild(this.container.querySelector('.progress'));
});
}
cancel() {
this.state.cancelRender = true;
}
get warnings() {
return this.state.warnings;
}
}