react-templates/playground/main.js

110 lines
4.8 KiB
JavaScript
Raw Normal View History

2014-11-18 15:06:25 +01:00
'use strict';
/*eslint-env browser*/
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
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');
2014-11-18 15:06:25 +01:00
var html = '<div>hello</div>';
2014-11-16 16:20:04 +01:00
var res = reactTemplates.convertTemplateToReact(html.trim());
console.log(res);
function emptyFunc() {
return null;
}
2014-11-18 14:00:28 +01:00
function generateTemplateSource(html) {
var code = null;
try {
2014-11-18 15:06:25 +01:00
code = reactTemplates.convertTemplateToReact(html.trim().replace(/\r/g, ''));
2014-11-18 14:00:28 +01:00
} catch (e) {
}
return code;
}
function generateTemplateFunction(code) {
2014-11-16 16:20:04 +01:00
try {
2014-11-18 15:06:25 +01:00
var defineMap = {react: React, lodash: _};
var define = function (requirementsNames, content) {
2014-11-16 16:20:04 +01:00
var requirements = _.map(requirementsNames,function (reqName) {
return defineMap[reqName];
});
return content.apply(this,requirements);
};
2014-11-18 15:06:25 +01:00
/*eslint no-eval:0*/
2014-11-16 16:20:04 +01:00
var res = eval(code);
return res;
} catch (e) {
2014-11-18 15:06:25 +01:00
return emptyFunc;
2014-11-16 16:20:04 +01:00
}
2014-11-16 17:20:43 +01:00
}
function generateRenderFunc(renderFunc) {
return function() {
var res = null;
try {
2014-11-18 15:06:25 +01:00
res = renderFunc.apply(this);
2014-11-16 17:20:43 +01:00
} catch (e) {
2014-11-18 15:06:25 +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-18 15:06:25 +01:00
{key: 'result'},
2014-11-16 17:20:43 +01:00
res
]));
2014-11-18 15:06:25 +01:00
};
2014-11-16 16:20:04 +01:00
}
2014-11-18 15:06:25 +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) {
2014-11-18 14:00:28 +01:00
this.templateSource = generateTemplateSource(state.templateHTML);
this.sampleFunc = generateTemplateFunction(this.templateSource);
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);
2014-11-18 15:06:25 +01:00
classBase = eval('(' + state.templateProps + ')');
2014-11-17 12:27:57 +01:00
if (!_.isObject(classBase)) {
2014-11-18 15:06:25 +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-18 15:06:25 +01:00
templateHTML: templateHTML,
2014-11-16 18:51:59 +01:00
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;
},
2014-11-18 15:06:25 +01:00
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);
}
});
2014-11-18 15:06:25 +01:00
React.render(Playground(), document.getElementById('playground'));