Add account switching
parent
a148a34597
commit
fb333ce6bd
@ -0,0 +1,131 @@
|
|||||||
|
/*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
userinfo = null;
|
||||||
|
theme = {"title": "Business", "color": "#ffffff", "bgcolor": ""};
|
||||||
|
accountid = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the accounts configuration seems valid.
|
||||||
|
* @returns {Boolean} true if valid, otherwise false
|
||||||
|
*/
|
||||||
|
function isconfigvalid() {
|
||||||
|
if (localStorage.getItem("accounts") !== null) {
|
||||||
|
if (JSON.parse(localStorage.getItem("accounts")).length > 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getaccounts() {
|
||||||
|
if (isconfigvalid()) {
|
||||||
|
return JSON.parse(localStorage.getItem("accounts"));
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveaccounts(accounts) {
|
||||||
|
localStorage.setItem("accounts", JSON.stringify(accounts));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Switch to a different account.
|
||||||
|
* @param {int} account The selected account index from localStorage.getItem('accounts')
|
||||||
|
* @returns {undefined}
|
||||||
|
*/
|
||||||
|
function switchaccount(account) {
|
||||||
|
// If there isn't an accounts list yet, this shouldi take us back to the setup
|
||||||
|
if (!isconfigvalid()) {
|
||||||
|
restartApplication();
|
||||||
|
}
|
||||||
|
var accounts = getaccounts();
|
||||||
|
// Selected account doesn't exist, choose the first one instead
|
||||||
|
if (typeof accounts[account] === 'undefined') {
|
||||||
|
account = 0;
|
||||||
|
}
|
||||||
|
var accountinfo = accounts[account];
|
||||||
|
localStorage.setItem("username", accountinfo['username']);
|
||||||
|
localStorage.setItem("password", accountinfo['password']);
|
||||||
|
localStorage.setItem("syncurl", accountinfo['syncurl']);
|
||||||
|
localStorage.setItem("key", accountinfo['key']);
|
||||||
|
accountid = account;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a Business Apps account.
|
||||||
|
* @param {String} username
|
||||||
|
* @param {String} password
|
||||||
|
* @param {String} url
|
||||||
|
* @param {String} key
|
||||||
|
* @returns {Number} The index of the new account
|
||||||
|
*/
|
||||||
|
function addaccount(username, password, url, key) {
|
||||||
|
var accounts = getaccounts();
|
||||||
|
accounts.push({"username": username, "password": password, "syncurl": url, "key": key});
|
||||||
|
saveaccounts(accounts);
|
||||||
|
return accounts.length - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a Business Apps account.
|
||||||
|
* @param {Number} Account index
|
||||||
|
*/
|
||||||
|
function rmaccount(id) {
|
||||||
|
var accounts = getaccounts();
|
||||||
|
accounts.splice(id, 1);
|
||||||
|
saveaccounts(accounts);
|
||||||
|
if (id == accountid) {
|
||||||
|
switchaccount(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch user info (name, email, etc) from the server and save to the global
|
||||||
|
* variable `userinfo`.
|
||||||
|
* @param function callback An optional function to run if/when the request
|
||||||
|
* succeeds.
|
||||||
|
*/
|
||||||
|
function getuserinfo(callback) {
|
||||||
|
$(".loading-text").text("Loading account...");
|
||||||
|
if (localStorage.getItem("username") === null
|
||||||
|
|| localStorage.getItem("password") === null
|
||||||
|
|| localStorage.getItem("syncurl") === null
|
||||||
|
|| localStorage.getItem("key") === null) {
|
||||||
|
switchaccount(0);
|
||||||
|
}
|
||||||
|
$.post(localStorage.getItem("syncurl"), {
|
||||||
|
username: localStorage.getItem("username"),
|
||||||
|
key: localStorage.getItem("key"),
|
||||||
|
password: localStorage.getItem("password"),
|
||||||
|
action: "user_info"
|
||||||
|
}, function (data) {
|
||||||
|
if (data.status === 'OK') {
|
||||||
|
$(".loading-text").text("Loading...");
|
||||||
|
userinfo = data.info;
|
||||||
|
if (typeof callback == 'function') {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
navigator.notification.alert(data.msg, null, "Error", 'Dismiss');
|
||||||
|
openscreen("homeloaderror");
|
||||||
|
}
|
||||||
|
}, "json").fail(function () {
|
||||||
|
navigator.notification.alert("Could not connect to the server. Try again later.", null, "Error", 'Dismiss');
|
||||||
|
openscreen("homeloaderror");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a new password for the current account
|
||||||
|
* @param {String} newpass
|
||||||
|
* @returns {undefined}
|
||||||
|
*/
|
||||||
|
function passwd(newpass) {
|
||||||
|
var accounts = getaccounts();
|
||||||
|
accounts[accountid]["password"] = newpass;
|
||||||
|
saveaccounts(accounts);
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
|
||||||
|
<div class="circle-btn btn btn-light-blue" onclick="openscreen('setup1', 'FADE');">
|
||||||
|
<img src="icons/ic_add.svg" />
|
||||||
|
</div>
|
||||||
|
<div id="noaccounts">
|
||||||
|
<div class="app-dock-container">
|
||||||
|
<div class="app-dock" id="app-dock">
|
||||||
|
<div style="margin-top: 50px; text-align: center; font-size: 120%;">
|
||||||
|
<img src="img/noaccounts.svg" alt="" style="max-width: 80%; max-height: 25%;" />
|
||||||
|
<br /><br />
|
||||||
|
<p style="max-width: 80%; margin: 0 auto;">You haven't added any Business Apps accounts yet. Press <i class="fa fa-plus"></i> to add one.</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="list-group" id="accountlist">
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
setnavbar("app", "Accounts", "settings");
|
||||||
|
|
||||||
|
var accounts = getaccounts();
|
||||||
|
if (accounts.length > 0) {
|
||||||
|
$("#noaccounts").css("display", "none");
|
||||||
|
}
|
||||||
|
for (var i = 0; i < accounts.length; i++) {
|
||||||
|
// Escape HTML characters
|
||||||
|
var username = $('<div/>').html(accounts[i]["username"]).html();
|
||||||
|
var syncurl = $('<div/>').html(accounts[i]["syncurl"].replace("/mobile/index.php", "")).html();
|
||||||
|
var synckey = accounts[i]["key"];
|
||||||
|
var stars = "";
|
||||||
|
for (var j = 0; j < synckey.length - 6; j++) {
|
||||||
|
stars += "*";
|
||||||
|
}
|
||||||
|
synckey = $('<div/>').html(key.slice(0, 3) + stars + key.slice(-3)).html();
|
||||||
|
$("#accountlist").append("<div class=\"list-group-item\" id=\"accountitem_" + i + "\">"
|
||||||
|
+ "<span class=\"pull-right\" style=\"color: red;\" onclick=\"deleteAccount(" + i + ")\"><i class=\"fa fa-trash-o\"></i></span>"
|
||||||
|
+ "<div onclick=\"switchAccount(" + i + ")\" class=\"h3\" style=\"font-weight: bold;\"><i class=\"fa fa-user\"></i> " + username + "</div>"
|
||||||
|
+ "<p onclick=\"switchAccount(" + i + ")\">"
|
||||||
|
+ "<i class=\"fa fa-key\"></i> Pairing Code: " + synckey + "<br />"
|
||||||
|
+ "<i class=\"fa fa-server\"></i> Server: " + syncurl + "</p>"
|
||||||
|
+ "</div>");
|
||||||
|
}
|
||||||
|
|
||||||
|
function deleteAccount(id) {
|
||||||
|
navigator.notification.confirm("Really delete account? You'll need to re-add the account to use it on this device again.", function (result) {
|
||||||
|
if (result != 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
rmaccount(id);
|
||||||
|
openscreen("accounts");
|
||||||
|
}, "Delete " + accounts[id]['username'] + "?");
|
||||||
|
}
|
||||||
|
|
||||||
|
function switchAccount(id) {
|
||||||
|
switchaccount(id);
|
||||||
|
openscreen("home");
|
||||||
|
navigator.notification.alert("Successfully switched accounts.", null, "Switched", 'OK');
|
||||||
|
}
|
||||||
|
</script>
|
Loading…
Reference in New Issue