react-templates/playground/main.js

104 lines
4.8 KiB
JavaScript
Raw Normal View History

2014-11-16 16:20:04 +01:00
var reactTemplates = require('../src/reactTemplates');
var playgroundTemplate = require('./playground.rt.js');
2014-11-17 12:27:57 +01:00
var htmlMode = require('codemirror/mode/htmlmixed/htmlmixed');
var javascriptMode = require('codemirror/mode/javascript/javascript');
var xmlMode = require('codemirror/mode/xml/xml');
var cssMode = require('codemirror/mode/css/css');
var vbScriptMode = require('codemirror/mode/vbscript/vbscript');
2014-11-16 16:20:04 +01:00
var React = require('react/addons');
2014-11-17 12:27:57 +01:00
2014-11-16 16:20:04 +01:00
var _ = require('lodash');
var html = "<div>hello</div>";
var res = reactTemplates.convertTemplateToReact(html.trim());
console.log(res);
function emptyFunc() {
return null;
}
function generateTemplateFunction(html) {
try {
var code = reactTemplates.convertTemplateToReact(html.trim().replace(/\r/g,""));
var defineMap = {"react":React,"lodash":_};
var define = function (requirementsNames,content) {
var requirements = _.map(requirementsNames,function (reqName) {
return defineMap[reqName];
});
return content.apply(this,requirements);
};
var res = eval(code);
return res;
} catch (e) {
return emptyFunc
}
2014-11-16 17:20:43 +01:00
}
function generateRenderFunc(renderFunc) {
return function() {
var res = null;
try {
res = renderFunc.apply(this)
} catch (e) {
2014-11-17 12:27:57 +01:00
res = React.DOM.div.apply(this,[{style:{color:"red"}},"Exception:"+e.message]);
2014-11-16 17:20:43 +01:00
}
return React.DOM.div.apply(this, _.flatten([
2014-11-17 12:27:57 +01:00
{key:"result"},
2014-11-16 17:20:43 +01:00
res
]));
}
2014-11-16 16:20:04 +01:00
}
2014-11-16 17:20:43 +01:00
var z = {getInitialState: function() {return {name:"reactTemplates"}}};
var templateHTML = "<div>\n Have {_.filter(this.state.todos, {done:true}).length} todos done,\n and {_.filter(this.state.todos, {done:false}).length} not done\n <br/>\n <div rt-repeat=\"todo in this.state.todos\" key=\"{todo.key}\">\n <button onClick=\"(e)=>e.preventDefault(); this.remove(todo)\">x</button>\n <input type=\"checkbox\" checked=\"{todo.done}\" onChange=\"()=>this.toggleChecked(todoIndex)\"/>\n <span style=\"text-decoration: {todo.done ? 'line-through': 'none'}\">{todo.value}</span>\n </div>\n <input key=\"myinput\" type=\"text\" onKeyDown=\"(e) => if (e.keyCode == 13) { this.add(); }\" valueLink=\"{this.linkState('edited')}\"/>\n <button onClick=\"(e)=>e.preventDefault(); this.add()\" >Add</button><br/>\n <button onClick=\"(e)=>e.preventDefault(); this.clearDone()\">Clear done</button>\n</div>";
2014-11-17 08:31:57 +01:00
var templateProps = "{\n mixins: [React.addons.LinkedStateMixin],\n getInitialState: function () {\n return {edited: '', todos: [], counter: 0};\n },\n add: function () {\n if (this.state.edited.trim().length === 0) {\n return;\n }\n var newTodo = {value: this.state.edited, done: false, key: this.state.counter};\n this.setState({todos: this.state.todos.concat(newTodo), edited: '', counter: this.state.counter + 1});\n },\n remove: function (todo) {\n this.setState({todos: _.reject(this.state.todos, todo)});\n },\n toggleChecked: function (index) {\n var todos = _.cloneDeep(this.state.todos);\n todos[index].done = !todos[index].done;\n this.setState({todos: todos});\n },\n clearDone: function () {\n this.setState({todos: _.filter(this.state.todos, {done: false})});\n }\n}";
2014-11-16 16:20:04 +01:00
var Playground = React.createClass({
displayName: 'Playground',
mixins: [React.addons.LinkedStateMixin],
2014-11-16 17:20:43 +01:00
updateSample: function (state) {
this.sampleFunc = generateTemplateFunction(state.templateHTML);
2014-11-17 12:27:57 +01:00
this.validHTML = this.sampleFunc !== emptyFunc;
2014-11-16 17:20:43 +01:00
this.sampleRender = generateRenderFunc(this.sampleFunc);
var classBase = {};
try {
2014-11-17 12:27:57 +01:00
this.validProps = true;
2014-11-16 17:20:43 +01:00
console.log(state.templateProps);
classBase = eval("("+state.templateProps+")");
2014-11-17 12:27:57 +01:00
if (!_.isObject(classBase)) {
2014-11-16 17:20:43 +01:00
throw "failed to eval";
2014-11-17 12:27:57 +01:00
}
2014-11-16 17:20:43 +01:00
} catch (e) {
classBase = {};
2014-11-17 12:27:57 +01:00
this.validProps = false;
2014-11-16 17:20:43 +01:00
}
classBase.render = this.sampleRender;
console.log(classBase);
2014-11-17 12:27:57 +01:00
this.sample = React.createFactory(React.createClass(classBase));
2014-11-16 17:20:43 +01:00
},
2014-11-16 16:20:04 +01:00
getInitialState: function () {
var currentState = {
2014-11-16 18:51:59 +01:00
templateHTML:templateHTML,
templateProps: templateProps
2014-11-16 16:20:04 +01:00
};
2014-11-16 17:20:43 +01:00
this.updateSample(currentState);
2014-11-16 16:20:04 +01:00
return currentState;
},
componentWillUpdate: function (nextProps,nextState) {
2014-11-16 17:20:43 +01:00
if (nextState.templateHTML !== this.state.templateHTML || nextState.templateProps !== this.state.templateProps) {
this.updateSample(nextState);
2014-11-16 16:20:04 +01:00
}
},
render: function () {
return playgroundTemplate.apply(this);
}
});
React.render(Playground(),document.getElementById('playground'));