webui: using m.route()

This commit is contained in:
zeners 2016-01-06 22:20:32 +01:00
parent 408f379db8
commit a5aa997947
7 changed files with 145 additions and 96 deletions

View File

@ -4,30 +4,8 @@ var m = require("mithril");
var rs = require("retroshare");
var curraccount = null;
var currentpasswd = null;
var accountMap = new Map();
function login(){
rs.request("control/login", {id: curraccount.id}, function(){
console.log("login sent");
waitForPassword(currentpasswd);
});
}
function waitForPassword(password){
rs.request("control/password",null,function(data){
if (data.want_password) {
console.log("sending pwd for " + data.key_name + "...");
rs.request("control/password", {password: password}, function(){
m.redraw();
});
} else {
console.log("waiting for pwd ...");
setTimeout(50, waitForPassword(password));
}
}, { method: "GET"})
}
function cancel(){
curraccount=null;
m.redraw();
@ -36,10 +14,9 @@ function cancel(){
function selAccount(account){
curraccount=accountMap.get(account);
m.redraw();
}
function setPasswd(password) {
currentpasswd = password
rs.request("control/login", {id: curraccount.id}, function(){
console.log("login sent");
});
}
module.exports = {view: function(){
@ -60,15 +37,7 @@ module.exports = {view: function(){
]
})]);
} else {
return m("div", [
m("h2","login:"),
m("hr"),
m("h2","account:" + curraccount.location + " (" + curraccount.name + ")"),
m("input",{type:"password", onchange:m.withAttr("value", setPasswd)}),
m("br"),
m("button",{onclick: function(){login();}},"login"),
m("button",{onclick: function(){cancel();}},"cancel")
]);
return m("div", "logging in ..." );
}
}
};

View File

@ -4,18 +4,17 @@
<meta charset="utf-8">
<title>rswebui5</title>
<link rel="stylesheet" href="app.css">
</head>
<body>
<div id="main">if app does not load, enable JavaScript!</div>
<script src="app.js"></script>
</head>
<body onload="load_ui();">
<div id="main">if app does not load, enable JavaScript!</div>
<script type="text/javascript">
function load_ui(){
var m = require("mithril");
var ui = require("main");
var main = document.getElementById("main");
m.mount(main, ui);
ui.init(main);
}
load_ui();
</script>
</body>
</html>

View File

@ -0,0 +1,6 @@
var m = require("mithril");
module.exports = {view: function(){
return m("div","RetroShare - WebClient - Welcome");
}
};

View File

@ -3,37 +3,84 @@
var m = require("mithril");
var rs = require("retroshare");
var menu =require("menu");
var currentpasswd = null;
module.exports = {view: function(){
var runstate = rs("control/runstate");
console.log("runstate: " + (runstate === undefined ? runstate : runstate.runstate));
if(runstate === undefined){
return m("div", "waiting_server ... ");
} else if (runstate.runstate == null){
return m("div", "server down");
} else {
if (rs.content === undefined) {
rs.content = null;
}
if (runstate.runstate == "waiting_init") {
return m("div","server starting ...")
} else if(runstate.runstate =="waiting_account_select") {
return m("div", [
m("div", menu.view()),
m("hr"),
m("div", require("accountselect").view())
]);
} else if (runstate.runstate == "waiting_startup") {
return m("div","RetroShare starting ... please wait ...");
} else if(runstate.runstate =="running_ok" || runstate.runstate=="running_ok_no_full_control") {
return m("div", [
m("div", menu.view()),
m("hr"),
m("div", rs.content == null ? null : rs.content.view())
function setPasswd(password) {
currentpasswd = password
}
function sendPassword(data) {
console.log("sending pwd for " + data.key_name + "...");
rs.request("control/password", {password: currentpasswd}, function(){
m.redraw();
});
}
function Page(content, runst){
this.view = function(){
var runstate = rs("control/runstate");
var needpasswd = rs("control/password");
console.log("runstate: " + (runstate === undefined ? runstate : runstate.runstate));
if(runstate === undefined){
return m("div", "waiting_server ... ");
} else if (runstate.runstate == null){
return m("div", "server down");
} else if (needpasswd != undefined && needpasswd.want_password === true){
return m("div",[
m("div","password required"),
m("div",needpasswd.key_name),
m("input",{type:"password", onchange:m.withAttr("value", setPasswd)}),
m("br"),
m("button",{onclick: function(){sendPassword(needpasswd);}},"send password"),
]);
} else {
return m("div", "unknown runstate: " + runstate.runstate);
if ("waiting_init|waiting_startup".match(runstate.runstate)) {
return m("div","server starting ...")
} else if("waiting_account_select|running_ok*") {
if (runst === undefined || runst.match(runstate.runstate)) {
return m("div", [
m("div", menu.view()),
m("hr"),
m("div", content)
]);
} else {
// funktion currently not available
m.route("/");
return m("div", [
m("div", menu.view()),
m("hr"),
m("div", require("home").view())
]);
};
} else {
return m("div", "unknown runstate: " + runstate.runstate);
}
}
}
}
};
var me ={};
me.init = function(main){
console.log("start init ...");
var menudef = require("menudef");
var maps = {};
var m = require("mithril");
menudef.nodes.map(function(menu){
if (menu.action === undefined) {
var path = menu.path != undefined ? menu.path : ("/" + menu.name);
var module = menu.module != undefined ? menu.module : menu.name
console.log("adding route " + menu.name + " for " + path + " with module " + module);
maps[path] = new Page(require(module), menu.runstate);
}
});
m.route.mode = "hash";
m.route(main,"/chat",maps);
console.log("init done.");
};
module.exports = me;

View File

@ -2,45 +2,38 @@
var m = require("mithril");
var rs = require("retroshare");
function shutdown(){
rs.request("control/shutdown",null,function(){
rs("control/runstate").runstate=null;
m.redraw();
});
}
var mnodes = require("menudef");
function goback(){
rs.content=null;
m.redraw();
}
function peers(){
rs.content=require("peers");
}
function chat(){
rs.content=require("chat");
}
module.exports = {view: function(){
if (rs.content!=undefined && rs.content != null) {
return m("div", {onclick:goback}, "back");
if (m.route() != "/") {
return m("div", {onclick:function(){m.route("/");}}, "home");
}
var nodes = [];
var runstate = rs("control/runstate");
if (runstate.runstate == "running_ok_no_full_control"||runstate.runstate =="running_ok") {
nodes.push(m("div",{onclick:function(){peers();}},"peers"));
nodes.push(m("div",{onclick:function(){chat();}},"chat"));
}
if (runstate.runstate == "waiting_account_select"||runstate.runstate == "running_ok"){
nodes.push(m("div",{onclick:shutdown},"shutdown"));
}
if (runstate === undefined || runstate.runstate === undefined || runstate.runstate == null)
return m("h2","menu: waiting for server ...");
return m("div", [
m("h2","menu:"),
m("hr"),
nodes
])
mnodes.nodes.map(function(menu){
if (menu.runstate === undefined || runstate.runstate.match(menu.runstate)){
if (menu.action === undefined) {
return m("div", {
onclick: function(){
m.route(
menu.path != undefined ? menu.path : "/" + menu.name
)
}
}, menu.name);
} else {
return m("div", {onclick: function(){menu.action(rs,m)}}, menu.name);
}
}
})
]);
}
};

View File

@ -0,0 +1,31 @@
module.exports = { nodes: [
{
name: "home",
path: "/"
},
{
name: "login",
module: "accountselect",
runstate: "waiting_account_select",
},
{
name: "peers",
runstate: "running_ok.*",
},
{
name: "chat",
runstate: "running_ok.*",
},
{
name: "shutdown",
runstate: "running_ok|waiting_account_select",
action: function(rs, m){
rs.request("control/shutdown",null,function(){
rs("control/runstate").runstate=null;
rs.forceUpdate("control/runstate");
m.redraw();
});
}
},
]
}

View File

@ -205,5 +205,9 @@ rs.request=function(path, args, callback, options){
requestFail(path, null, value);
});
return req;
};
rs.forceUpdate = function(path){
cache[path].requested=false;
}