2016-02-22 20:02:45 -06:00
|
|
|
import rt from 'react-templates';
|
2016-02-23 00:52:24 -06:00
|
|
|
import React from 'react';
|
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
2016-03-01 13:16:31 -06:00
|
|
|
import {COMPONENT_MAP} from './component_map';
|
2016-02-22 20:02:45 -06:00
|
|
|
|
|
|
|
|
var TEMPLATES = {};
|
|
|
|
|
|
|
|
|
|
class Templates {
|
|
|
|
|
|
|
|
|
|
static sync(){
|
|
|
|
|
var all = [];
|
2016-03-01 13:16:31 -06:00
|
|
|
for (var component_name in COMPONENT_MAP){
|
2016-02-22 20:02:45 -06:00
|
|
|
var done = new Promise((fnResolve, fnReject)=>{
|
2016-03-01 13:16:31 -06:00
|
|
|
Templates.evalTemplate(component_name, fnResolve);
|
2016-02-22 20:02:45 -06:00
|
|
|
});
|
|
|
|
|
all.push(done);
|
|
|
|
|
}
|
|
|
|
|
return Promise.all(all);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-01 13:16:31 -06:00
|
|
|
static forComponent(name){
|
|
|
|
|
return TEMPLATES[name];
|
2016-02-22 20:02:45 -06:00
|
|
|
}
|
|
|
|
|
|
2016-03-01 13:16:31 -06:00
|
|
|
static evalTemplate(component_name, fnResolve){
|
2016-02-23 00:52:24 -06:00
|
|
|
jQuery.ajax({
|
2016-03-01 13:16:31 -06:00
|
|
|
url: COMPONENT_MAP[component_name] + '.rt'
|
2016-02-23 00:52:24 -06:00
|
|
|
}).done((template)=>{
|
2016-03-01 13:16:31 -06:00
|
|
|
var code = rt.convertTemplateToReact(template, {modules: 'none', name: component_name}),
|
2016-02-29 18:20:00 -06:00
|
|
|
eval_context = {};
|
2016-03-01 13:16:31 -06:00
|
|
|
code = code.replace('var ' + component_name + ' = ', 'eval_context.' + component_name + ' = ');
|
2016-02-23 00:52:24 -06:00
|
|
|
new Function('with(this){ ' + code + ' } ').call({
|
2016-02-29 18:20:00 -06:00
|
|
|
eval_context: eval_context,
|
2016-02-23 00:52:24 -06:00
|
|
|
'_': _,
|
|
|
|
|
'React': React
|
|
|
|
|
});
|
2016-03-01 13:16:31 -06:00
|
|
|
TEMPLATES[component_name] = eval_context[component_name];
|
2016-02-23 00:52:24 -06:00
|
|
|
fnResolve();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-22 20:02:45 -06:00
|
|
|
}
|
2016-02-23 00:52:24 -06:00
|
|
|
|
|
|
|
|
export default Templates;
|