From 5a505dff24bd6fac864d0654d601454166f8eb17 Mon Sep 17 00:00:00 2001 From: "Sean R. Abraham" Date: Tue, 17 Dec 2024 09:29:08 -0500 Subject: [PATCH] lint all the JavaScript files this applies some standard JS style/fixes for errorprone usage, and it tidies up a few confusing code bits too. This should be pretty much a no-op --- src/ims/element/static/admin_events.js | 71 +++-- src/ims/element/static/admin_streets.js | 36 ++- src/ims/element/static/admin_types.js | 31 +- src/ims/element/static/ims.js | 205 ++++++------- src/ims/element/static/incident.js | 323 +++++++++------------ src/ims/element/static/incident_report.js | 49 ++-- src/ims/element/static/incident_reports.js | 43 +-- src/ims/element/static/incidents.js | 68 ++--- 8 files changed, 380 insertions(+), 446 deletions(-) diff --git a/src/ims/element/static/admin_events.js b/src/ims/element/static/admin_events.js index 349f5d85d..77d6e681f 100644 --- a/src/ims/element/static/admin_events.js +++ b/src/ims/element/static/admin_events.js @@ -28,19 +28,19 @@ function initPage() { } -var accessControlList = null +let accessControlList = null function loadAccessControlList(success) { function ok(data, status, xhr) { accessControlList = data; - if (success != undefined) { + if (success) { success(); } } function fail(error, status, xhr) { - var message = "Failed to load access control list:\n" + error + const message = "Failed to load access control list:\n" + error console.error(message); window.alert(message); } @@ -49,13 +49,13 @@ function loadAccessControlList(success) { } -var _accessTemplate = null; -var _entryTemplate = null; +let _accessTemplate = null; +let _entryTemplate = null; -var accessModes = ["readers", "writers", "reporters"]; +let accessModes = ["readers", "writers", "reporters"]; function drawAccess() { - var container = $("#event_access_container"); + const container = $("#event_access_container"); if (_accessTemplate == null) { _accessTemplate = container.children(".event_access:first"); @@ -68,13 +68,9 @@ function drawAccess() { container.empty(); - for (var i in events) { - var event = events[i]; - - for (var i in accessModes) { - var mode = accessModes[i]; - - var eventAccess = $(_accessTemplate).clone(); + for (const event of events) { + for (const mode of accessModes) { + const eventAccess = $(_accessTemplate).clone(); // Add an id to the element for future reference eventAccess.attr("id", "event_access_" + event + "_" + mode); @@ -89,25 +85,24 @@ function drawAccess() { function updateEventAccess(event, mode) { - var eventACL = accessControlList[event]; + const eventACL = accessControlList[event]; - if (eventACL == undefined) { + if (eventACL == null) { return; } - var eventAccess = $("#event_access_" + event + "_" + mode); + const eventAccess = $("#event_access_" + event + "_" + mode); // Set displayed event name and mode eventAccess.find(".event_name").text(event); eventAccess.find(".access_mode").text(mode); - var entryContainer = eventAccess.find(".list-group:first"); + const entryContainer = eventAccess.find(".list-group:first"); entryContainer.empty(); - for (var i in eventACL[mode]) { - var expression = eventACL[mode][i]; - var entryItem = _entryTemplate.clone(); + for (const expression of eventACL[mode]) { + const entryItem = _entryTemplate.clone(); entryItem.append(expression); entryItem.attr("value", expression); @@ -118,7 +113,7 @@ function updateEventAccess(event, mode) { function addEvent(sender) { - var event = sender.value.trim(); + const event = sender.value.trim(); function refresh() { loadAccessControlList(drawAccess); @@ -130,7 +125,7 @@ function addEvent(sender) { } function fail(requestError, status, xhr) { - var message = "Failed to add event:\n" + requestError + const message = "Failed to add event:\n" + requestError console.log(message); refresh(); controlHasError($(sender)); @@ -142,22 +137,22 @@ function addEvent(sender) { function addAccess(sender) { - var container = $(sender).parents(".event_access:first"); - var event = container.find(".event_name:first").text(); - var mode = container.find(".access_mode:first").text(); - var newExpression = sender.value.trim(); + const container = $(sender).parents(".event_access:first"); + const event = container.find(".event_name:first").text(); + const mode = container.find(".access_mode:first").text(); + const newExpression = sender.value.trim(); - var acl = accessControlList[event][mode].slice(); + const acl = accessControlList[event][mode].slice(); acl.push(newExpression); - edits = {}; + const edits = {}; edits[event] = {}; edits[event][mode] = acl; function refresh() { - for (var i in accessModes) { - updateEventAccess(event, accessModes[i]); + for (const mode of accessModes) { + updateEventAccess(event, mode); } } @@ -176,12 +171,12 @@ function addAccess(sender) { function removeAccess(sender) { - var container = $(sender).parents(".event_access:first"); - var event = container.find(".event_name:first").text(); - var mode = container.find(".access_mode:first").text(); - var expression = $(sender).parent().attr("value").trim(); + const container = $(sender).parents(".event_access:first"); + const event = container.find(".event_name:first").text(); + const mode = container.find(".access_mode:first").text(); + const expression = $(sender).parent().attr("value").trim(); - var acl = accessControlList[event][mode].slice(); + const acl = accessControlList[event][mode].slice(); if (acl.indexOf(expression) < 0) { console.error("no such ACL: " + expression); @@ -190,7 +185,7 @@ function removeAccess(sender) { acl.splice(acl.indexOf(expression), 1); - edits = {}; + const edits = {}; edits[event] = {}; edits[event][mode] = acl; @@ -218,7 +213,7 @@ function sendACL(edits, success, error) { } function fail(requestError, status, xhr) { - var message = "Failed to edit ACL:\n" + requestError + const message = "Failed to edit ACL:\n" + requestError; console.log(message); error(); window.alert(message); diff --git a/src/ims/element/static/admin_streets.js b/src/ims/element/static/admin_streets.js index e761a1e3b..94f4b0d57 100644 --- a/src/ims/element/static/admin_streets.js +++ b/src/ims/element/static/admin_streets.js @@ -28,21 +28,21 @@ function initPage() { } -var streets = null; +let streets = null; function loadStreets(success) { - var url = url_streets; + const url = url_streets; function ok(data, status, xhr) { streets = data; - if (success != undefined) { + if (success) { success(); } } function fail(error, status, xhr) { - var message = "Failed to load streets:\n" + error + const message = "Failed to load streets:\n" + error; console.error(message); window.alert(message); } @@ -51,11 +51,11 @@ function loadStreets(success) { } -var _streetsTemplate = null; -var _entryTemplate = null; +let _streetsTemplate = null; +let _entryTemplate = null; function drawStreets() { - var container = $("#event_streets_container"); + const container = $("#event_streets_container"); if (_streetsTemplate == null) { _streetsTemplate = container.children(".event_streets:first"); @@ -68,10 +68,8 @@ function drawStreets() { container.empty(); - for (var i in events) { - var event = events[i]; - - var eventStreets = $(_streetsTemplate).clone(); + for (const event of events) { + const eventStreets = $(_streetsTemplate).clone(); // Add an id to the element for future reference eventStreets.attr("id", "event_streets_" + event); @@ -85,24 +83,24 @@ function drawStreets() { function updateEventStreets(event) { - var eventStreets = streets[event]; + const eventStreets = streets[event]; - if (eventStreets == undefined) { + if (eventStreets == null) { return; } - var eventStreetsElement = $("#event_streets_" + event); + const eventStreetsElement = $("#event_streets_" + event); // Set displayed event name eventStreetsElement.find(".event_name").text(event); - var entryContainer = eventStreetsElement.find(".list-group:first"); + const entryContainer = eventStreetsElement.find(".list-group:first"); entryContainer.empty(); - for (var streetID in eventStreets) { - var streetName = eventStreets[streetID]; - var entryItem = _entryTemplate.clone(); + for (const streetID in eventStreets) { + const streetName = eventStreets[streetID]; + const entryItem = _entryTemplate.clone(); entryItem.append(streetID + ": " + streetName); entryItem.attr("value", streetID); @@ -128,7 +126,7 @@ function sendStreets(edits, success, error) { } function fail(requestError, status, xhr) { - var message = "Failed to edit streets:\n" + requestError + const message = "Failed to edit streets:\n" + requestError; console.log(message); error(); window.alert(message); diff --git a/src/ims/element/static/admin_types.js b/src/ims/element/static/admin_types.js index 98dfb8996..9bdde77cc 100644 --- a/src/ims/element/static/admin_types.js +++ b/src/ims/element/static/admin_types.js @@ -33,16 +33,16 @@ function loadAndDrawIncidentTypes() { } -var incidentTypes = null; -var incidentTypesVisible = null; +let incidentTypes = null; +let incidentTypesVisible = null; function loadIncidentTypes(success) { - var gotAll = false; - var gotVisible = false; + let gotAll = false; + let gotVisible = false; function ok() { if (gotAll && gotVisible) { - if (success != undefined) { + if (success) { success(); } } @@ -62,7 +62,7 @@ function loadIncidentTypes(success) { } function fail(error, status, xhr) { - var message = "Failed to load incident types:\n" + error + const message = "Failed to load incident types:\n" + error; console.error(message); window.alert(message); } @@ -72,11 +72,11 @@ function loadIncidentTypes(success) { } -var _incidentTypesTemplate = null; -var _entryTemplate = null; +let _incidentTypesTemplate = null; +let _entryTemplate = null; function drawIncidentTypes() { - var container = $("#incident_types_container"); + const container = $("#incident_types_container"); if (_incidentTypesTemplate == null) { _incidentTypesTemplate = container.children(".incident_types:first"); @@ -92,17 +92,16 @@ function drawIncidentTypes() { function updateIncidentTypes() { - var incidentTypesElement = $("#incident_types"); + const incidentTypesElement = $("#incident_types"); - var entryContainer = incidentTypesElement.find(".list-group:first"); + const entryContainer = incidentTypesElement.find(".list-group:first"); entryContainer.empty(); - for (var i in incidentTypes) { - var incidentType = incidentTypes[i]; - var entryItem = _entryTemplate.clone(); + for (const incidentType of incidentTypes) { + const entryItem = _entryTemplate.clone(); - if (incidentTypesVisible.indexOf(incidentType) == -1) { + if (incidentTypesVisible.indexOf(incidentType) === -1) { entryItem.addClass("item-hidden") } else { entryItem.addClass("item-visible") @@ -157,7 +156,7 @@ function sendIncidentTypes(edits, success, error) { } function fail(requestError, status, xhr) { - var message = "Failed to edit incident types:\n" + requestError + const message = "Failed to edit incident types:\n" + requestError; console.log(message); error(); window.alert(message); diff --git a/src/ims/element/static/ims.js b/src/ims/element/static/ims.js index 12bf98ddb..a82225c05 100644 --- a/src/ims/element/static/ims.js +++ b/src/ims/element/static/ims.js @@ -99,7 +99,7 @@ setTheme(getPreferredTheme()); // browser will implement this correctly, and any solution using .replace() // will be buggy. And this will be fast. But still, this is weak. -var _domTextAreaForHaxxors = document.createElement("textarea") +let _domTextAreaForHaxxors = document.createElement("textarea") // Convert text to HTML. function textAsHTML(text) { @@ -144,9 +144,9 @@ ValueError.prototype.constructor = ValueError; // Build an array from a range. function range(start, end, step) { - if (step == undefined) { + if (step == null) { step = 1; - } else if (step == 0) { + } else if (step === 0) { throw new ValueError("step = 0"); } @@ -178,7 +178,7 @@ function compareReportEntries(a, b) { function jsonRequest(url, jsonOut, success, error) { function ok(data, status, xhr) { - if (success != undefined) { + if (success) { success(data, status, xhr); } } @@ -186,29 +186,29 @@ function jsonRequest(url, jsonOut, success, error) { function fail(xhr, status, requestError) { // Intentionally empty response is not an error. if ( - status == "parsererror" && - xhr.status == 201 && - xhr.responseText == "" + status === "parsererror" && + xhr.status === 201 && + xhr.responseText === "" ) { ok("", "", xhr); return; } - if (error != undefined) { + if (error) { error(requestError, status, xhr); } } - var args = { + const args = { "url": url, "method": "GET", "dataType": "json", "success": ok, "error": fail, - } + }; if (jsonOut) { - var jsonText = JSON.stringify(jsonOut); + const jsonText = JSON.stringify(jsonOut); args["method"] = "POST"; args["contentType"] = "application/json"; @@ -226,13 +226,13 @@ function jsonRequest(url, jsonOut, success, error) { // Pad a string representing an integer to two digits. function padTwo(value) { - if (value == undefined) { + if (value == null) { return "?"; } value = value.toString(); - if (value.length == 1) { + if (value.length === 1) { return "0" + value; } @@ -264,7 +264,7 @@ function parseInt(stringInt) { // Create a