/**
* Class gui-serve.js
*
* Copyright: Ascora GmbH, www.ascora.de
*
* @author Gerrit Klasen, Bastian von Halem
*/
var express = require('express');
var app = express();
var path = require('path');
var bodyParser = require('body-parser');
var fs = require("fs-extra");
var mobile_builder = require("./scripts/build_mobile");
var preview_builder = require("./scripts/build_preview");
var translation_helper = require("./scripts/translation_helper");
var options_helper = require("./scripts/options_helper");
var template_helper = require("./scripts/template_helper");
var activeBuild = false;
app.use(express.static(path.join(__dirname, '/')));
app.use(bodyParser.text({type: '*/*'}));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
var settings = JSON.parse(fs.readFileSync('./preview/settings.json', 'utf-8'));
var port = settings.port;
var server = settings.server;
app.listen(port, function () {
console.log('Example app listening on port '+server+":"+port+'!');
});
app.get('/',function(req,res){
res.sendFile(path.join(__dirname+'/preview/__temp.htm'));
});
app.get('/designer',function(req,res){
res.sendFile(path.join(__dirname+'/client/client.html'));
});
app.post('/getoptions', function (req, res) {
res.send(options_helper.getOptions(req));
});
app.post('/gettemplates', function (req, res) {
res.send(template_helper.getTemplates(req.body));
});
app.post('/gettemplatesbyfolder', function (req, res) {
res.send(template_helper.getTemplatesByFolder(req.body));
});
app.post('/setpreview', function(req, res){
res.send(preview_builder.buildPreview(req.body));
});
app.post('/setmarkup', function(req, res){
res.send(preview_builder.setMarkup(req.body));
});
app.post('/setstrings', function (req, res) {
res.send(translation_helper.saveTranslation(JSON.parse(req.body), res));
});
app.post('/deletestrings', function (req, res) {
res.send(translation_helper.deleteTranslation(JSON.parse(req.body), res));
});
app.get('/gettranslations', function (req, res) {
res.send(translation_helper.getAllTranslations());
});
app.get('/getmarkup', function (req, res) {
res.send(preview_builder.getMarkup());
});
/*
app.post('/setmarkup', function (req, res) {
console.log(req.body)
res.send(preview_builder.setMarkup(req.body));
/* if(activeBuild) {
return;
}
activeBuild = true;
var settings = JSON.parse(fs.readFileSync('./preview/settings.json', 'utf-8'));
if(settings.platform === "android" || settings.platform === "ios") {
mobile_builder.build(settings, "basic_apps/mobile");
} else {
//oldPath = "basic_apps/desktop";
}*/
//});
app.post('/setcurrentpage', function (req, res) {
res.send(preview_builder.setCurrentPage(req.body));
});
app.get('/getcurrentpage', function (req, res) {
res.send(preview_builder.getCurrentPage());
});
app.post('/setcurrentuser', function (req, res) {
res.send(preview_builder.setCurrentUser(req.body));
});
app.get('/getcurrentuser', function (req, res) {
res.send(preview_builder.getCurrentUser());
});