Skip to content

Commit 9918d2d

Browse files
committed
lint fixes
1 parent bf8d62e commit 9918d2d

File tree

1 file changed

+97
-94
lines changed

1 file changed

+97
-94
lines changed

js/index.js

Lines changed: 97 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function appJSONLoadedHandler() {
4848
if (s.url) s.url = "apps/"+app.id+"/"+s.url;
4949
});
5050
});
51-
var promise = Promise.resolve();
51+
let promise = Promise.resolve();
5252
if ("undefined" != typeof onAppJSONLoaded)
5353
promise = promise.then(onAppJSONLoaded);
5454
// finally update what we're showing
@@ -58,7 +58,7 @@ function appJSONLoadedHandler() {
5858
if (window.location.search) {
5959
let searchParams = new URLSearchParams(window.location.search);
6060
if (searchParams.has("id") && searchParams.has("readme")) {
61-
var id = searchParams.get("id").toLowerCase();
61+
let id = searchParams.get("id").toLowerCase();
6262
showReadme(null, id);
6363
}
6464
}
@@ -87,14 +87,14 @@ httpGet(Const.APPS_JSON_FILE).then(apps=>{
8787
let appsURL = baseurl+"apps/";
8888
httpGet(appsURL).then(htmlText=>{
8989
showToast(Const.APPS_JSON_FILE+" can't be read, scanning 'apps' folder for apps","warning");
90-
var parser = new DOMParser();
91-
var xmlDoc = parser.parseFromString(htmlText,"text/html");
90+
let parser = new DOMParser();
91+
let xmlDoc = parser.parseFromString(htmlText,"text/html");
9292
appJSON = [];
93-
var promises = [];
93+
let promises = [];
9494
htmlToArray(xmlDoc.querySelectorAll("a")).forEach(a=>{
95-
var href = a.getAttribute("href");
95+
let href = a.getAttribute("href");
9696
if (!href || href.startsWith("/") || href.startsWith("_") || !href.endsWith("/")) return;
97-
var metadataURL = appsURL+"/"+href+"metadata.json";
97+
let metadataURL = appsURL+"/"+href+"metadata.json";
9898
console.log(" - Loading "+metadataURL);
9999
promises.push(httpGet(metadataURL).then(metadataText=>{
100100
try {
@@ -139,7 +139,7 @@ if (Const.APP_DATES_CSV) httpGet(Const.APP_DATES_CSV).then(csv=>{
139139
});
140140

141141
if (Const.APP_USAGE_JSON) httpGet(Const.APP_USAGE_JSON).then(jsonTxt=>{
142-
var json;
142+
let json;
143143
try {
144144
json = JSON.parse(jsonTxt);
145145
} catch (e) {
@@ -192,7 +192,7 @@ function showChangeLog(appid, installedVersion) {
192192
}
193193
showPrompt(app.name+" ChangeLog",contents,{ok:true}, shouldEscapeHtml).catch(()=>{});
194194
if (installedVersion) {
195-
var elem = document.getElementById(installedVersion);
195+
let elem = document.getElementById(installedVersion);
196196
if (elem) elem.scrollIntoView();
197197
}
198198
}
@@ -219,8 +219,8 @@ function getAppDescription(app) {
219219

220220
/** Setup IFRAME callbacks for handleCustomApp and handleInterface */
221221
function iframeSetup(options) {
222-
var iframe = options.iframe;
223-
var modal = options.modal;
222+
let iframe = options.iframe;
223+
let modal = options.modal;
224224
document.body.append(modal);
225225
htmlToArray(modal.getElementsByTagName("a")).forEach(button => {
226226
button.addEventListener("click",event => {
@@ -315,8 +315,8 @@ function iframeSetup(options) {
315315
data: device
316316
},"*");
317317
// Push any data received back through to IFRAME
318-
if (Comms.isConnected())
319-
console.log("Adding Comms.on('data') handler for iframe");
318+
if (Comms.isConnected()) {
319+
console.log("Adding Comms.on('data') handler for iframe");
320320
Comms.on("data", data => {
321321
if (!iframe.contentWindow) {
322322
// if no frame, disable
@@ -328,8 +328,8 @@ function iframeSetup(options) {
328328
type : "recvdata",
329329
data : data
330330
});
331-
});
332-
331+
});
332+
}
333333
}, false);
334334
}
335335

@@ -357,38 +357,40 @@ function handleCustomApp(appTemplate) {
357357
</div>
358358
</div>`);
359359
let iframe = modal.getElementsByTagName("iframe")[0];
360-
iframeSetup({ iframe : iframe,
361-
modal : modal,
362-
jsFile : "customize.js",
363-
onClose: reject,
364-
messageHandler : function(event) {
365-
let msg = event.data;
366-
if (msg.type=="app") {
367-
let appFiles = msg.data;
368-
let app = JSON.parse(JSON.stringify(appTemplate)); // clone template
369-
// copy extra keys from appFiles
370-
Object.keys(appFiles).forEach(k => {
371-
if (k!="storage") app[k] = appFiles[k]
372-
});
373-
appFiles.storage.forEach(f => {
374-
app.storage = app.storage.filter(s=>s.name!=f.name); // remove existing item
375-
app.storage.push(f); // add new
376-
});
377-
console.log("Received custom app", app);
378-
modal.remove();
379-
380-
getInstalledApps()
381-
.then(()=>checkDependencies(app))
382-
.then(()=>Comms.uploadApp(app,{device:device, language:LANGUAGE, noFinish: msg.options && msg.options.noFinish}))
383-
.then(()=>{
384-
Progress.hide({sticky:true});
385-
resolve();
386-
}).catch(err => {
387-
Progress.hide({sticky:true});
388-
reject('Upload failed, ' + err, 'error');
360+
iframeSetup({
361+
iframe : iframe,
362+
modal : modal,
363+
jsFile : "customize.js",
364+
onClose: reject,
365+
messageHandler : function(event) {
366+
let msg = event.data;
367+
if (msg.type=="app") {
368+
let appFiles = msg.data;
369+
let app = JSON.parse(JSON.stringify(appTemplate)); // clone template
370+
// copy extra keys from appFiles
371+
Object.keys(appFiles).forEach(k => {
372+
if (k!="storage") app[k] = appFiles[k]
373+
});
374+
appFiles.storage.forEach(f => {
375+
app.storage = app.storage.filter(s=>s.name!=f.name); // remove existing item
376+
app.storage.push(f); // add new
389377
});
378+
console.log("Received custom app", app);
379+
modal.remove();
380+
381+
getInstalledApps()
382+
.then(()=>checkDependencies(app))
383+
.then(()=>Comms.uploadApp(app,{device:device, language:LANGUAGE, noFinish: msg.options && msg.options.noFinish}))
384+
.then(()=>{
385+
Progress.hide({sticky:true});
386+
resolve();
387+
}).catch(err => {
388+
Progress.hide({sticky:true});
389+
reject('Upload failed, ' + err, 'error');
390+
});
391+
}
390392
}
391-
}});
393+
});
392394
});
393395
}
394396

@@ -412,13 +414,15 @@ function handleAppInterface(app) {
412414
</div>
413415
</div>`);
414416
let iframe = modal.getElementsByTagName("iframe")[0];
415-
iframeSetup({ iframe : iframe,
416-
modal : modal,
417-
jsFile : "interface.js",
418-
// onClose: reject, // we don't need to reject when the window is closed
419-
messageHandler : function(event) {
420-
// nothing custom needed in here
421-
}});
417+
iframeSetup({
418+
iframe : iframe,
419+
modal : modal,
420+
jsFile : "interface.js",
421+
// onClose: reject, // we don't need to reject when the window is closed
422+
messageHandler : function(event) {
423+
// nothing custom needed in here
424+
}
425+
});
422426
iframe.src = `apps/${app.id}/${app.interface}`;
423427
});
424428
}
@@ -469,8 +473,8 @@ function getAppHTML(app, appInstalled, forInterface) {
469473
let versionTitle = '';
470474
let appFavourites;
471475
if (app.id in appSortInfo) {
472-
var infoTxt = [];
473-
var info = appSortInfo[app.id];
476+
let infoTxt = [];
477+
let info = appSortInfo[app.id];
474478
if ("object"==typeof info.modified)
475479
infoTxt.push(`Last update: ${(info.modified.toLocaleDateString())}`);
476480
if (info.installs)
@@ -490,7 +494,7 @@ function getAppHTML(app, appInstalled, forInterface) {
490494
`<a href="${Const.APP_SOURCECODE_URL}/${app.id}" target="_blank" class="link-github"><img src="core/img/github-icon-sml.png" alt="See the code on GitHub"/></a>` : "";
491495
let getAppFavouritesHTML = cnt => {
492496
if (!cnt) return "";
493-
var txt = (cnt > 999) ? Math.round(cnt/1000)+"k" : cnt;
497+
let txt = (cnt > 999) ? Math.round(cnt/1000)+"k" : cnt;
494498
return `<span>${txt}</span>`;
495499
};
496500

@@ -520,7 +524,7 @@ function getAppHTML(app, appInstalled, forInterface) {
520524
<button class="btn btn-link btn-action btn-lg" appid="${app.id}" title="Remove App"><i class="icon icon-delete"></i></button>`;
521525
html += "</div>";
522526
if (forInterface=="library") {
523-
var screenshots = (app.screenshots || []).filter(s=>s.url);
527+
let screenshots = (app.screenshots || []).filter(s=>s.url);
524528
if (screenshots.length)
525529
html += `<img class="tile-screenshot" appid="${app.id}" src="${screenshots[0].url}" alt="Screenshot"/>`;
526530
}
@@ -630,10 +634,10 @@ function refreshLibrary(options) {
630634
searchResult = visibleApps.map(app => ({
631635
app : app,
632636
relevance :
633-
searchRelevance(app.id, searchValue) +
634-
searchRelevance(app.name, searchValue) +
637+
Utils.searchRelevance(app.id, searchValue) +
638+
Utils.searchRelevance(app.name, searchValue) +
635639
(app.tags && app.tags.includes(searchValue))
636-
}));
640+
}));
637641
} else if (searchType === "id") {
638642
searchResult = visibleApps.map(app => ({
639643
app:app,
@@ -644,12 +648,12 @@ function refreshLibrary(options) {
644648
searchResult = visibleApps.map(app => ({
645649
app:app,
646650
relevance:
647-
searchRelevance(app.id, searchValue) +
648-
searchRelevance(app.name, searchValue)*(app.shortName?1:2) +
649-
(app.shortName?searchRelevance(app.shortName, searchValue):0) + // if we have shortname, match on that as well
650-
searchRelevance(app.description, searchValue)/5 + // match on description, but pay less attention
651+
Utils.searchRelevance(app.id, searchValue) +
652+
Utils.searchRelevance(app.name, searchValue)*(app.shortName?1:2) +
653+
(app.shortName?Utils.searchRelevance(app.shortName, searchValue):0) + // if we have shortname, match on that as well
654+
Utils.searchRelevance(app.description, searchValue)/5 + // match on description, but pay less attention
651655
((app.tags && app.tags.includes(searchValue))?10:0)
652-
}));
656+
}));
653657
}
654658
// Now finally, filter, sort based on relevance and set the search result
655659
visibleApps = searchResult.filter(a => a.relevance>0).sort((a,b) => (b.relevance-(0|b.sortorder)) - (a.relevance-(0|a.sortorder))).map(a => a.app);
@@ -661,12 +665,12 @@ function refreshLibrary(options) {
661665
if (activeSort) {
662666
if (["created","modified","installs","favourites"].includes(activeSort)) {
663667
visibleApps = visibleApps.sort((a,b) =>
664-
((appSortInfo[b.id]||{})[activeSort]||0) -
665-
((appSortInfo[a.id]||{})[activeSort]||0));
668+
((appSortInfo[b.id]||{})[activeSort]||0) -
669+
((appSortInfo[a.id]||{})[activeSort]||0));
666670
} else throw new Error("Unknown sort type "+activeSort);
667671
}
668672

669-
var viewMoreText = "";
673+
let viewMoreText = "";
670674
if (!options.showAll && visibleApps.length > Const.MAX_APPS_SHOWN) {
671675
viewMoreText = `<div class="tile column col-6 col-sm-12 col-xs-12 app-tile" onclick="javascript:refreshLibrary({dontChangeSearchBox:true,showAll:true})" style="cursor: pointer;">
672676
<div class="tile-icon">
@@ -763,7 +767,7 @@ function refreshLibrary(options) {
763767
function showScreenshots(appId) {
764768
let app = appJSON.find(app=>app.id==appId);
765769
if (!app || !app.screenshots) return;
766-
var screenshots = app.screenshots.filter(s=>s.url);
770+
let screenshots = app.screenshots.filter(s=>s.url);
767771
showPrompt(app.name+" Screenshots",`<div class="columns">
768772
${screenshots.map(s=>`
769773
<div class="column col-4">
@@ -782,14 +786,14 @@ function uploadApp(app, options) {
782786
options = options||{};
783787
if (app.type == "defaultconfig" && !options.force) {
784788
return showPrompt("Default Configuration Install","<b>This will remove all apps and data from your Bangle</b> and will install a new set of apps. Please ensure you have backed up your Bangle first. Continue?",{yes:1,no:1},false)
785-
.then(() => showPrompt("Device Erasure","<b>Everything will be deleted from your Bangle.</b> Are you really sure?",{yes:1,no:1},false))
786-
.then(() => Comms.removeAllApps())
787-
.then(() => uploadApp(app, {force:true}))
788-
.catch(err => {
789-
showToast("Configuration install failed, "+err,"error");
790-
refreshMyApps();
791-
refreshLibrary();
792-
});
789+
.then(() => showPrompt("Device Erasure","<b>Everything will be deleted from your Bangle.</b> Are you really sure?",{yes:1,no:1},false))
790+
.then(() => Comms.removeAllApps())
791+
.then(() => uploadApp(app, {force:true}))
792+
.catch(err => {
793+
showToast("Configuration install failed, "+err,"error");
794+
refreshMyApps();
795+
refreshLibrary();
796+
});
793797
}
794798

795799
return getInstalledApps().then(()=>{
@@ -974,7 +978,7 @@ function getAppsToUpdate(options) {
974978
let appsToUpdate = [];
975979
device.appsInstalled.forEach(appInstalled => {
976980
let app = appNameToApp(appInstalled.id);
977-
appInstalled.canUpdate = isAppUpdateable(appInstalled, app) && (!options.excludeCustomApps || app.custom === undefined);
981+
appInstalled.canUpdate = Utils.isAppUpdateable(appInstalled, app) && (!options.excludeCustomApps || app.custom === undefined);
978982
if (appInstalled.canUpdate) {
979983
appsToUpdate.push(app);
980984
}
@@ -1005,8 +1009,8 @@ function refreshMyApps() {
10051009
if (icon.classList.contains("icon-refresh")) updateApp(app);
10061010
if (icon.classList.contains("icon-interface")) handleAppInterface(app);
10071011
if (icon.classList.contains("icon-favourite")) {
1008-
let favourite = SETTINGS.favourites.find(e => e == app.id);
1009-
changeAppFavourite(!favourite, app);
1012+
let favourite = SETTINGS.favourites.find(e => e == app.id);
1013+
changeAppFavourite(!favourite, app);
10101014
}
10111015
});
10121016
});
@@ -1214,8 +1218,7 @@ Comms.watchConnectionChange(handleConnectionChange);
12141218
let filtersContainer = document.querySelector("#librarycontainer .filter-nav");
12151219
filtersContainer.addEventListener('click', ({ target }) => {
12161220
if (target.classList.contains('active')) return;
1217-
1218-
var filterName = target.getAttribute('filterid') || '';
1221+
let filterName = target.getAttribute('filterid') || '';
12191222
// Update window URL
12201223
window.history.replaceState(null, null, "?c=" + filterName);
12211224
refreshLibrary();
@@ -1317,14 +1320,14 @@ if (btn) btn.addEventListener("click",event=>{
13171320

13181321
// Install all favourite apps in one go
13191322
btn = document.getElementById("installfavourite");
1320-
if (btn) btn.addEventListener("click",event=>{
1321-
let nonCustomFavourites = SETTINGS.favourites.filter(appId => appJSON.find(app => app.id === appId && !app.custom));
1322-
const mustHave = [ "boot","setting" ]; // apps that we absolutely need installed
1323-
mustHave.forEach(id => {
1324-
if (!nonCustomFavourites.includes(id))
1325-
nonCustomFavourites.unshift(id);
1326-
});
1327-
installMultipleApps(nonCustomFavourites, "favourite").catch(err=>{
1323+
if (btn) btn.addEventListener("click",event => {
1324+
let nonCustomFavourites = SETTINGS.favourites.filter(appId => appJSON.find(app => app.id === appId && !app.custom));
1325+
const mustHave = [ "boot","setting" ]; // apps that we absolutely need installed
1326+
mustHave.forEach(id => {
1327+
if (!nonCustomFavourites.includes(id))
1328+
nonCustomFavourites.unshift(id);
1329+
});
1330+
installMultipleApps(nonCustomFavourites, "favourite").catch(err=>{
13281331
Progress.hide({sticky:true});
13291332
showToast("App Install failed, "+err,"error");
13301333
});
@@ -1398,8 +1401,8 @@ if (Espruino.Core.Terminal)
13981401
Espruino.Core.Terminal.OVERRIDE_CONTENTS = "Click here and type to communicate with Bangle.js";
13991402
btn = document.getElementById("terminalEnable");
14001403
if (btn) btn.addEventListener("click",event=>{
1401-
document.getElementById("terminalEnable").remove();
1402-
document.querySelector(".editor__canvas").style.display = "inherit";
1403-
Comms.on("data",x=>Espruino.Core.Terminal.outputDataHandler(x))
1404-
Espruino.Core.Terminal.setInputDataHandler(function(d) { Comms.write(d); })
1405-
});
1404+
document.getElementById("terminalEnable").remove();
1405+
document.querySelector(".editor__canvas").style.display = "inherit";
1406+
Comms.on("data",x=>Espruino.Core.Terminal.outputDataHandler(x))
1407+
Espruino.Core.Terminal.setInputDataHandler(function(d) { Comms.write(d); })
1408+
});

0 commit comments

Comments
 (0)