diff --git a/index.js b/index.js index 242d60b2d..cb9352331 100644 --- a/index.js +++ b/index.js @@ -149,19 +149,10 @@ function refreshLibrary() { } panelbody.innerHTML = visibleApps.map((app,idx) => { - var icon = "icon-upload"; - var versionInfo = app.version || ""; - if (app.custom) - icon = "icon-menu"; - if (appsInstalled.find(a=>a.id==app.id)) { - icon = "icon-delete"; - versionInfo+=" installed"; - } - var buttons = ""; + var appInstalled = appsInstalled.find(a=>a.id==app.id); + var version = getVersionInfo(app, appInstalled); + var versionInfo = version.text; if (versionInfo) versionInfo = " ("+versionInfo+")"; - if (app.allow_emulator) - buttons += ``; - buttons += ``; return `
${escapeHtml(app.name)}
@@ -171,7 +162,11 @@ function refreshLibrary() {

${escapeHtml(app.description)}

- ${buttons} + + + + +
`;}).join(""); @@ -181,10 +176,12 @@ function refreshLibrary() { tab.setAttribute("data-badge", appJSON.length); htmlToArray(panelbody.getElementsByTagName("button")).forEach(button => { button.addEventListener("click",event => { - var icon = event.target; - var appid = icon.getAttribute("appid"); - var app = appJSON.find(app=>app.id==appid); - if (!app) return; + var button = event.currentTarget; + var icon = button.firstChild; + var appid = button.getAttribute("appid"); + var app = appNameToApp(appid); + if (!app) throw new Error("App "+appid+" not found"); + // check icon to figure out what we should do if (icon.classList.contains("icon-share")) { // emulator var file = app.storage.find(f=>f.name[0]=='-'); @@ -196,6 +193,7 @@ function refreshLibrary() { var url = baseurl+"apps/"+app.id+"/"+file.url; window.open(`https://espruino.com/ide/emulator.html?codeurl=${url}&upload`); } else if (icon.classList.contains("icon-upload")) { + // upload icon.classList.remove("icon-upload"); icon.classList.add("loading"); Comms.uploadApp(app).then((appJSON) => { @@ -204,12 +202,14 @@ function refreshLibrary() { icon.classList.remove("loading"); icon.classList.add("icon-delete"); refreshMyApps(); + refreshLibrary(); }).catch(err => { showToast("Upload failed, "+err, "error"); icon.classList.remove("loading"); icon.classList.add("icon-upload"); }); } else if (icon.classList.contains("icon-menu")) { + // custom HTML update if (app.custom) { icon.classList.remove("icon-menu"); icon.classList.add("loading"); @@ -219,16 +219,23 @@ function refreshLibrary() { icon.classList.remove("loading"); icon.classList.add("icon-delete"); refreshMyApps(); + refreshLibrary(); }).catch(err => { showToast("Customise failed, "+err, "error"); icon.classList.remove("loading"); icon.classList.add("icon-menu"); }); } - } else { + } else if (icon.classList.contains("icon-delete")) { + // Remove app icon.classList.remove("icon-delete"); icon.classList.add("loading"); removeApp(app); + } else if (icon.classList.contains("icon-refresh")) { + // Update app + icon.classList.remove("icon-refresh"); + icon.classList.add("loading"); + updateApp(app); } }); }); @@ -239,14 +246,29 @@ refreshLibrary(); function removeApp(app) { return showPrompt("Delete","Really remove '"+app.name+"'?").then(() => { - Comms.removeApp(app).then(()=>{ - appsInstalled = appsInstalled.filter(a=>a.id!=app.id); - showToast(app.name+" removed successfully","success"); - refreshMyApps(); - refreshLibrary(); - }, err=>{ - showToast(app.name+" removal failed, "+err,"error"); - }); + return Comms.removeApp(app); + }).then(()=>{ + appsInstalled = appsInstalled.filter(a=>a.id!=app.id); + showToast(app.name+" removed successfully","success"); + refreshMyApps(); + refreshLibrary(); + }, err=>{ + showToast(app.name+" removal failed, "+err,"error"); + }); +} + +function updateApp(app) { + Comms.removeApp(app).then(()=>{ + showToast(app.name+" removed successfully. Updating...",); + appsInstalled = appsInstalled.filter(a=>a.id!=app.id); + return Comms.uploadApp(app); + }).then((appJSON) => { + if (appJSON) appsInstalled.push(appJSON); + showToast(app.name+" Updated!", "success"); + refreshMyApps(); + refreshLibrary(); + }, err=>{ + showToast(app.name+" update failed, "+err,"error"); }); } @@ -281,34 +303,31 @@ function refreshMyApps() { tab.setAttribute("data-badge", appsInstalled.length); panelbody.innerHTML = appsInstalled.map(appJSON => { var app = appNameToApp(appJSON.id); -var version = ""; -if (!appJSON.version) { - version = "Unknown version"; - if (app.version) version += ", latest "+app.version; -} else { - version = appJSON.version; - if (app.version == appJSON.version) version += ", up to date"; - else if (app.version) version += ", latest "+app.version; -} +var version = getVersionInfo(app, appJSON); return `
${escapeHtml(app.name)}
-

${escapeHtml(app.name)} (${version})

+

${escapeHtml(app.name)} (${version.text})

${escapeHtml(app.description)}

- + +
`}).join(""); htmlToArray(panelbody.getElementsByTagName("button")).forEach(button => { button.addEventListener("click",event => { - var icon = event.target; - var appid = icon.getAttribute("appid"); + var button = event.currentTarget; + var icon = button.firstChild; + var appid = button.getAttribute("appid"); var app = appNameToApp(appid); - removeApp(app); + if (!app) throw new Error("App "+appid+" not found"); + // check icon to figure out what we should do + if (icon.classList.contains("icon-delete")) removeApp(app); + if (icon.classList.contains("icon-refresh")) updateApp(app); }); }); } diff --git a/utils.js b/utils.js index 86e8db3e2..699b247a9 100644 --- a/utils.js +++ b/utils.js @@ -42,3 +42,24 @@ function appSorter(a,b) { if (sa>sb) return 1; return (a.name==b.name) ? 0 : ((a.name