@bash_candy/candy-js
Version:
Web Development for Dummies
170 lines (159 loc) • 4.86 kB
JavaScript
const fs = require('fs');
var existsNav;
var funct = [];
function add(txt1, txt2){
return "function add(){ "
+ getFields(txt1, txt2)
+ " alert('result = ' + (parseInt(txt1) + parseInt(txt2)));"
+" }";
}
function multiply(txt1, txt2){
return "function multiply(){ "
+ getFields(txt1, txt2)
+ " alert('result = ' + (parseInt(txt1) * parseInt(txt2)));"
+" }";
}
function substract(txt1, txt2){
return "function substract(){ "
+ getFields(txt1, txt2)
+ " alert('result = ' + (parseInt(txt1) - parseInt(txt2)));"
+" }";
}
function divide(txt1, txt2){
return "function divide(){ "
+ getFields(txt1, txt2)
+ " alert('result = ' + (parseInt(txt1) / parseInt(txt2)));"
+" }";
}
function getFields(text1, text2){
return " var txt1 = document.getElementById('" + text1 +"').value;"
+ " var txt2 = document.getElementById('" + text2 +"').value;"
}
function mapping(obj, id, arrayVal, objPadre) {
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
if (typeof obj[property] == "object") {
if (property == "children") {
if(objPadre[id]["tag"] != "nav") {
arrayVal[id] += ">";
var content = {};
mapping(obj[property], property, content, obj);
closeTag(content);
for (ele in content) {
arrayVal[id] += content[ele];
}
arrayVal[id] += "</div>";
} else{
var items = generateNavChildren(obj[property]);
arrayVal[id] += ">" /*+ "<div>"*/ + items + /*"</div>" +*/ "</nav><div class='container'>";
}
} else {
mapping(obj[property], property, arrayVal, obj);
}
} else {
switch (property) {
case "tag":
arrayVal[id] = "<" + obj[property] + " id='" + id + "'";
break;
case "text":
arrayVal[id] += ">" + obj[property];
break;
case "action":
funct.push(generateFuntion(obj , arrayVal, id));
//arrayVal[id] += " onClick='add()'/>"
break;
case "txt1":
break;
case "txt2":
break;
default:
arrayVal[id] += " " + property + "='" + obj[property] + "'";
}
}
}
}
}
function generateFuntion(obj, val, id){
var txt1 = obj["txt1"];
var txt2 = obj["txt2"];
switch (obj["action"]) {
case "add":
val[id] += " onClick='add()'/>"
return add(txt1, txt2);
break;
case "substract":
val[id] += " onClick='substract()'/>"
return substract(txt1, txt2);
break;
case "multiply":
val[id] += " onClick='multiply()'/>"
return multiply(txt1, txt2);
break;
case "divide":
val[id] += " onClick='divide()'/>"
return divide(txt1, txt2);
break;
default:
}
}
function generateNavChildren(ul){
var items = "";
var elem = ul[Object.keys(ul)[0]];
if(elem != null && elem["children"] != null){
items += "<ul>"
for(item in elem["children"]){
items += "<li><a href='" + elem["children"][item]["href"] + "'>"
items += elem["children"][item]["text"] + "</a>" + "</li>"
}
items += "</ul>"
}
return items;
}
function closeTag(elementos){
var tipo;
for (ele in elementos) {
tipo = (elementos[ele].split(" ")[0]).substring(1);
if(tipo != 'div'){
var agrega = "";
if(elementos[ele].substring(elementos[ele].length-1) != ">"){
if((elementos[ele])[1] != "p"){
agrega = ">"
}
elementos[ele] += agrega + "</" + tipo + ">"
}
}
}
}
function existsNav(json){
for(var x in json){
if(json[x].tag == 'nav'){
return true;
}
}
return false;
}
module.exports = {createHtml: function (json){
var theme = json.meta.theme;
delete json.meta;
existsNav = existsNav(json);
let elementos = {};
mapping(json, '', elementos, null);
closeTag(elementos);
var html = "<!DOCTYPE html><html lang='en'><head><meta charset='utf-8'><title>CandyJS</title>"
+ "<link rel='stylesheet' type='text/css' href='src/css/candy.css'>"
+ "<script>" ;
for(elem in funct){
html+= funct[elem];
//console.log(funct[elem]);
}
html += "</script></head><body class='" + theme + "'>";
if(!existsNav){
html += "<div class='container'>";
}
for (ele in elementos) {
html += elementos[ele];
}
html += "</div></body></html>";
fs.writeFileSync("index.html", html);
}
}