diff --git a/luciexpress/htdocs/js/app.js b/luciexpress/htdocs/js/app.js index 838f62c08..271f712ff 100644 --- a/luciexpress/htdocs/js/app.js +++ b/luciexpress/htdocs/js/app.js @@ -31,6 +31,8 @@ function supports_html5_storage() { } } +var PLUGIN_ROOT = ""; + require.config({ baseUrl: '/', urlArgs: 'v=1.0' @@ -54,7 +56,7 @@ angular.module("luci", [ $locationProvider.hashPrefix('!'); //$stateProvider.otherwise("login"); //$urlRouterProvider.otherwise("/otherwise"); - $stateProvider.state("/", { + $stateProvider.state("ready", { url: "", views: { "content": { @@ -63,35 +65,109 @@ angular.module("luci", [ }, luci_config: {} }); + $stateProvider.state("init", { + url: "/init", + views: { + "content": { + templateUrl: "pages/loading.html" + } + }, + onEnter: function($state, $config, $session, $rpc, $navigation, $rootScope, $http){ + console.log("INIT"); + async.series([ + function(next){ + console.log("Validating session.."); + $session.init().done(function(){ + next(); + }).fail(function(){ + console.log("Failed to verify session."); + $state.go("login"); + }); + }, + function(next){ + console.log("Getting config.."); + // TODO: use rpc + next(); + }, + function(next){ + console.log("Loading plugins.."); + async.eachSeries($config.plugins, function(id, next){ + console.log(".."+id); + var plugin_root = "plugins/"+id; + $http.get(plugin_root + "/plugin.json") + .success(function(data){ + if(data && data.scripts){ + var scripts = data.scripts.map(function(x){return plugin_root + "/" + x; }); + require(scripts, function(module){ + next(); /* + module.plugin_init({ + PLUGIN_ROOT: root + }, function(){ + next(); + }); */ + }); + } else { + next(); + } + }).error(function(data){ + next(); + }); + /*var plug = $config.plugins[id]; + var root = "plugins/"+id+"/"; + require(plug.scripts.map(function(s){return "plugins/"+s+"/"+s;}), function(module){ + module.plugin_init({ + PLUGIN_ROOT: root + }, function(){ + next(); + }); + }); */ + }, function(){ + next(); + }); + }, + function(next){ + console.log("Getting navigation.."); + + // get the menu navigation + $rpc.luci2.ui.menu().done(function(data){ + //console.log(JSON.stringify(data)); + Object.keys(data.menu).map(function(key){ + var menu = data.menu[key]; + var view = menu.view; + var path = key.replace("/", "."); + var obj = { + path: path, + modes: data.menu[key].modes || [ ], + text: data.menu[key].title, + index: data.menu[key].index || 0, + }; + if(menu.redirect){ + obj.redirect = menu.redirect; + } + if(view){ + obj.page = "/pages/"+view.replace("/", ".")+".html"; + } + $navigation.register(obj); + }); + //$rootScope.$apply(); + next(); + }); + } + ], function(err){ + if(err) $state.go("error"); + console.log("READY"); + $state.go("ready"); + }); + }, + luci_config: {} + }); }) .run(function($rootScope, $state, $session, gettextCatalog, $rpc, $navigation){ // set current language //gettextCatalog.currentLanguage = "se"; //gettextCatalog.debug = true; + $state.go("init"); - // get the menu navigation - $rpc.luci2.ui.menu().done(function(data){ - //console.log(JSON.stringify(data)); - Object.keys(data.menu).map(function(key){ - var menu = data.menu[key]; - var view = menu.view; - var path = key.replace("/", "."); - var obj = { - path: path, - modes: data.menu[key].modes || [ ], - text: data.menu[key].title, - index: data.menu[key].index || 0, - }; - if(menu.redirect){ - obj.redirect = menu.redirect; - } - if(view){ - obj.page = "/pages/"+view.replace("/", ".")+".html"; - } - $navigation.register(obj); - }); - $rootScope.$apply(); - }); }) angular.module("luci").controller("BodyCtrl", function ($scope, $state, $session, $location, $window, $rootScope, $config) { @@ -124,14 +200,7 @@ angular.module("luci").controller("BodyCtrl", function ($scope, $state, $session } localStorage.setItem("mode", selected); }); - $session.init().done(function(){ - // make browser refresh work - $state.transitionTo($location.path().replace("/", "").replace(".", "_")||"/"); - //$rootScope.$apply(); - }).fail(function(){ - $location.path("/login"); - $state.transitionTo($location.path().replace("/", "").replace(".", "_")); - }); + }) $(document).ready(function(){ diff --git a/luciexpress/htdocs/js/config.js b/luciexpress/htdocs/js/config.js index 58e37ffc4..ca8e6b043 100644 --- a/luciexpress/htdocs/js/config.js +++ b/luciexpress/htdocs/js/config.js @@ -22,8 +22,11 @@ angular.module("luci") "red" : "/themes/inteno-red/", "vodaphone" : "/themes/vodaphone/" }, + plugins: [ + "hello_world" + ], rpc: { - host: "http://192.168.1.1", + host: "", exposed_calls: [ "session.login", "session.access", diff --git a/luciexpress/server.js b/luciexpress/server.js index 5bfe50f24..30ab537d0 100644 --- a/luciexpress/server.js +++ b/luciexpress/server.js @@ -21,6 +21,34 @@ app.use(bodyParser.urlencoded({ // to support URL-encoded bodies app.use(express.static(__dirname + '/htdocs')); +var rpc_calls = { + "luci2.ui.menu": function(params, next){ + var menu = {}; + [ + "share/menu.d/overview.json", + "share/menu.d/settings.json", + "share/menu.d/phone.json", + "share/menu.d/internet.json", + "share/menu.d/status.json", + "share/menu.d/status.vodaphone.json", + "share/menu.d/wifi.json" + ].map(function(file){ + var obj = JSON.parse(fs.readFileSync(file)); + Object.keys(obj).map(function(k){ + menu[k] = obj[k]; + }); + }); + next({ + menu: menu + }); + }, + "session.access": function(params, next){ + next({ + "access-group": [ "a", "b" ] + }); + } +}; + // RPC end point app.post('/ubus', function(req, res) { res.header('Content-Type', 'application/json'); @@ -38,30 +66,20 @@ app.post('/ubus', function(req, res) { } //console.log("Call: "+data.method+" "+JSON.stringify(data.params)); - - if(data.params[1] == "luci2.ui" && data.params[2] == "menu"){ - console.log("luci2.ui.menu"); - res.write(JSON.stringify({ - jsonrpc: "2.0", - result: [0, { - menu: JSON.parse(fs.readFileSync("share/menu.d/overview.json")) - }] - })); - } if(data.params[1] == "session" && data.params[2] == "access"){ - console.log("luci2.ui.menu"); - res.write(JSON.stringify({ - jsonrpc: "2.0", - result: [0, { - "access-group": [ "a", "b" ] - }] - })); + var name = data.params[1]+"."+data.params[2]; + if(name in rpc_calls){ + rpc_calls[name](null, function(resp){ + res.write(JSON.stringify({ + jsonrpc: "2.0", + result: [0, resp] + })); + + res.end(); + }); } else { - res.write(JSON.stringify({ - jsonrpc: "2.0", - result: {} - })); + console.log("Unknown RPC call "+name); + res.end(); } - res.end(); }); var server = app.listen(3000, function () {