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 rs = require("retroshare");
var curraccount = null; var curraccount = null;
var currentpasswd = null;
var accountMap = new Map(); 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(){ function cancel(){
curraccount=null; curraccount=null;
m.redraw(); m.redraw();
@ -36,10 +14,9 @@ function cancel(){
function selAccount(account){ function selAccount(account){
curraccount=accountMap.get(account); curraccount=accountMap.get(account);
m.redraw(); m.redraw();
} rs.request("control/login", {id: curraccount.id}, function(){
console.log("login sent");
function setPasswd(password) { });
currentpasswd = password
} }
module.exports = {view: function(){ module.exports = {view: function(){
@ -60,15 +37,7 @@ module.exports = {view: function(){
] ]
})]); })]);
} else { } else {
return m("div", [ return m("div", "logging in ..." );
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")
]);
} }
} }
}; };

View File

@ -4,18 +4,17 @@
<meta charset="utf-8"> <meta charset="utf-8">
<title>rswebui5</title> <title>rswebui5</title>
<link rel="stylesheet" href="app.css"> <link rel="stylesheet" href="app.css">
</head>
<body>
<div id="main">if app does not load, enable JavaScript!</div>
<script src="app.js"></script> <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"> <script type="text/javascript">
function load_ui(){ function load_ui(){
var m = require("mithril"); var m = require("mithril");
var ui = require("main"); var ui = require("main");
var main = document.getElementById("main"); var main = document.getElementById("main");
m.mount(main, ui); ui.init(main);
} }
load_ui();
</script> </script>
</body> </body>
</html> </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 m = require("mithril");
var rs = require("retroshare"); var rs = require("retroshare");
var menu =require("menu"); var menu =require("menu");
var currentpasswd = null;
module.exports = {view: function(){
var runstate = rs("control/runstate"); function setPasswd(password) {
console.log("runstate: " + (runstate === undefined ? runstate : runstate.runstate)); currentpasswd = password
if(runstate === undefined){ }
return m("div", "waiting_server ... ");
} else if (runstate.runstate == null){ function sendPassword(data) {
return m("div", "server down"); console.log("sending pwd for " + data.key_name + "...");
} else { rs.request("control/password", {password: currentpasswd}, function(){
if (rs.content === undefined) { m.redraw();
rs.content = null; });
} }
if (runstate.runstate == "waiting_init") {
return m("div","server starting ...") function Page(content, runst){
} else if(runstate.runstate =="waiting_account_select") { this.view = function(){
return m("div", [ var runstate = rs("control/runstate");
m("div", menu.view()), var needpasswd = rs("control/password");
m("hr"), console.log("runstate: " + (runstate === undefined ? runstate : runstate.runstate));
m("div", require("accountselect").view()) if(runstate === undefined){
]); return m("div", "waiting_server ... ");
} else if (runstate.runstate == "waiting_startup") { } else if (runstate.runstate == null){
return m("div","RetroShare starting ... please wait ..."); return m("div", "server down");
} else if(runstate.runstate =="running_ok" || runstate.runstate=="running_ok_no_full_control") { } else if (needpasswd != undefined && needpasswd.want_password === true){
return m("div", [ return m("div",[
m("div", menu.view()), m("div","password required"),
m("hr"), m("div",needpasswd.key_name),
m("div", rs.content == null ? null : rs.content.view()) m("input",{type:"password", onchange:m.withAttr("value", setPasswd)}),
m("br"),
m("button",{onclick: function(){sendPassword(needpasswd);}},"send password"),
]); ]);
} else { } 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 m = require("mithril");
var rs = require("retroshare"); var rs = require("retroshare");
var mnodes = require("menudef");
function shutdown(){
rs.request("control/shutdown",null,function(){
rs("control/runstate").runstate=null;
m.redraw();
});
}
function goback(){ function goback(){
rs.content=null; rs.content=null;
m.redraw(); m.redraw();
} }
function peers(){
rs.content=require("peers");
}
function chat(){
rs.content=require("chat");
}
module.exports = {view: function(){ module.exports = {view: function(){
if (rs.content!=undefined && rs.content != null) { if (m.route() != "/") {
return m("div", {onclick:goback}, "back"); return m("div", {onclick:function(){m.route("/");}}, "home");
} }
var nodes = [];
var runstate = rs("control/runstate"); var runstate = rs("control/runstate");
if (runstate.runstate == "running_ok_no_full_control"||runstate.runstate =="running_ok") { if (runstate === undefined || runstate.runstate === undefined || runstate.runstate == null)
nodes.push(m("div",{onclick:function(){peers();}},"peers")); return m("h2","menu: waiting for server ...");
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"));
}
return m("div", [ return m("div", [
m("h2","menu:"), m("h2","menu:"),
m("hr"), 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); requestFail(path, null, value);
}); });
return req; return req;
};
rs.forceUpdate = function(path){
cache[path].requested=false;
} }