From 480f2b6bdbcc0024b20d638f1025d0c725845966 Mon Sep 17 00:00:00 2001 From: axunonb Date: Fri, 19 Jul 2024 18:42:32 +0200 Subject: [PATCH] Refactor codebase for modern practices - Updated `Gruntfile.js` to use `const` for declaring the `sass` variable. - Renamed and simplified methods in `Constants.cs` for clarity and efficiency. - Removed unnecessary `using` directives and renamed methods in `Location.Parser.cs` for better readability. - Corrected logical operators in `StringHelper.cs` and `ColorMath.cs` to ensure proper short-circuit evaluation. - Changed nullable boolean properties to non-nullable in `EditModel.cs`. - Converted `CalendarDisplayModel` property to a method in `NewsletterModel.cs`. - Improved variable checks and declarations in JavaScript files for robustness and modern standards. - Applied similar updates to `Site.TempusDominusFactory.min.js` for consistency. - Updated `Show.cshtml` to reflect changes in `NewsletterModel.cs`. --- Src/TournamentCalendar/Gruntfile.js | 2 +- .../Library/Authentication/Constants.cs | 10 +++++----- .../Axuno.Tools/GeoSpatial/Location.Parser.cs | 12 +++++------- .../Library/Axuno.Tools/StringHelper.cs | 2 +- Src/TournamentCalendar/Library/ColorMath.cs | 4 ++-- Src/TournamentCalendar/Models/Calendar/EditModel.cs | 4 ++-- .../Models/Contact/ContactModel.cs | 2 +- .../Models/Newsletter/NewsletterModel.cs | 5 +++-- Src/TournamentCalendar/Scripts/Site.Location.js | 12 ++++++------ .../Scripts/Site.TempusDominusFactory.js | 8 +++++--- .../Scripts/Site.TempusDominusFactory.min.js | 2 +- Src/TournamentCalendar/Views/Newsletter/Show.cshtml | 2 +- 12 files changed, 33 insertions(+), 32 deletions(-) diff --git a/Src/TournamentCalendar/Gruntfile.js b/Src/TournamentCalendar/Gruntfile.js index 549a52f..5463050 100644 --- a/Src/TournamentCalendar/Gruntfile.js +++ b/Src/TournamentCalendar/Gruntfile.js @@ -6,7 +6,7 @@ Click here to learn more. https://go.microsoft.com/fwlink/?LinkID=513275&clcid=0 console.log(`Node ${process.version}`); // node version module.exports = function (grunt) { 'use strict'; - var sass = require('node-sass'); // used in grunt-sass options, must be included in package.json + const sass = require('node-sass'); // used in grunt-sass options, must be included in package.json grunt.loadNpmTasks('grunt-sass'); // Bootstrap 5 uses https://sass-lang.com/dart-sass; they also maintain the node-sass package grunt.loadNpmTasks('@lodder/grunt-postcss'); // grunt-postcss is retired, use @lodder/grunt-postcss diff --git a/Src/TournamentCalendar/Library/Authentication/Constants.cs b/Src/TournamentCalendar/Library/Authentication/Constants.cs index db5f62c..8163c3a 100644 --- a/Src/TournamentCalendar/Library/Authentication/Constants.cs +++ b/Src/TournamentCalendar/Library/Authentication/Constants.cs @@ -16,18 +16,18 @@ public class RoleName /// Get the values of all constants in this class. /// /// Returns the values of all constants in this class. - public static IEnumerable GetAllValues() + public static IEnumerable GetAllRoleValues() { - return Constants.GetAllValues(typeof(RoleName)); + return GetAllValues(typeof(RoleName)); } /// /// Get the names of all constants in this class. /// /// Returns the names of all constants in this class. - public static IEnumerable GetAllNames() + public static IEnumerable GetAllRoleNames() { - return Constants.GetAllNames(typeof(RoleName)); + return GetAllNames(typeof(RoleName)); } } @@ -59,4 +59,4 @@ private static IEnumerable GetAllNames(IReflect type) if (val != null) yield return val; } } -} \ No newline at end of file +} diff --git a/Src/TournamentCalendar/Library/Axuno.Tools/GeoSpatial/Location.Parser.cs b/Src/TournamentCalendar/Library/Axuno.Tools/GeoSpatial/Location.Parser.cs index fc77216..990d746 100644 --- a/Src/TournamentCalendar/Library/Axuno.Tools/GeoSpatial/Location.Parser.cs +++ b/Src/TournamentCalendar/Library/Axuno.Tools/GeoSpatial/Location.Parser.cs @@ -1,6 +1,4 @@ -using System; -using System.Globalization; -using System.Text.RegularExpressions; +using System.Text.RegularExpressions; namespace Axuno.Tools.GeoSpatial; @@ -120,7 +118,7 @@ private static double MakeSameSign(double source, double value) return value; } - private static Location? Parse(string input, IFormatProvider? provider, Regex regex) + private static Location? ParseLocation(string input, IFormatProvider? provider, Regex regex) { var match = regex.Match(input.Replace(", ", " ")); if (!match.Success) return null; @@ -199,7 +197,7 @@ private static double MakeSameSign(double source, double value) internal static Location? ParseDegrees(string value, IFormatProvider? provider) { if (string.IsNullOrWhiteSpace(value)) return null; - return Parse(value, provider, DegreeRegex); + return ParseLocation(value, provider, DegreeRegex); } /// @@ -216,7 +214,7 @@ private static double MakeSameSign(double source, double value) internal static Location? ParseDegreesMinutes(string value, IFormatProvider? provider) { if (string.IsNullOrWhiteSpace(value)) return null; - return Parse(value, provider, DegreeMinuteRegex); + return ParseLocation(value, provider, DegreeMinuteRegex); } /// @@ -233,7 +231,7 @@ private static double MakeSameSign(double source, double value) internal static Location? ParseDegreesMinutesSeconds(string value, IFormatProvider? provider) { if (string.IsNullOrWhiteSpace(value)) return null; - return Parse(value, provider, DegreeMinuteSecondRegex); + return ParseLocation(value, provider, DegreeMinuteSecondRegex); } /// diff --git a/Src/TournamentCalendar/Library/Axuno.Tools/StringHelper.cs b/Src/TournamentCalendar/Library/Axuno.Tools/StringHelper.cs index 52eb82d..7ba3292 100644 --- a/Src/TournamentCalendar/Library/Axuno.Tools/StringHelper.cs +++ b/Src/TournamentCalendar/Library/Axuno.Tools/StringHelper.cs @@ -739,7 +739,7 @@ public static DataTable ConvertHtmlTableToDataTable(string html) foreach (Match rowMatch in rowMatches) { // Only loop through the row if it isn't a header row - if (!(currRow == 0 & headerExists)) + if (!(currRow == 0 && headerExists)) { // Create a new row and reset the current column counter var dr = dt.NewRow(); diff --git a/Src/TournamentCalendar/Library/ColorMath.cs b/Src/TournamentCalendar/Library/ColorMath.cs index ade8560..f0dbb0e 100644 --- a/Src/TournamentCalendar/Library/ColorMath.cs +++ b/Src/TournamentCalendar/Library/ColorMath.cs @@ -20,7 +20,7 @@ public static class ColorMath { public static Color? FromHtmlColor(string htmlColor) { - if (htmlColor.StartsWith("#") & (htmlColor.Length == 7 | htmlColor.Length == 4) & + if (htmlColor.StartsWith("#") && (htmlColor.Length == 7 || htmlColor.Length == 4) && htmlColor.Substring(1).All(c => "ABCDEF0123456789".IndexOf(char.ToUpper(c)) != -1)) { return (Color?) new ColorConverter().ConvertFromString(htmlColor); @@ -343,4 +343,4 @@ public HSLColor(double hue, double saturation, double luminosity) this.Luminosity = luminosity; } - } \ No newline at end of file + } diff --git a/Src/TournamentCalendar/Models/Calendar/EditModel.cs b/Src/TournamentCalendar/Models/Calendar/EditModel.cs index a42f96b..c75efef 100644 --- a/Src/TournamentCalendar/Models/Calendar/EditModel.cs +++ b/Src/TournamentCalendar/Models/Calendar/EditModel.cs @@ -241,7 +241,7 @@ public int? MinMaxMale /// means to toggle the approval status: /// if (ShowTournament) { "Click to hide the tournament" } else { "Click to show the tournament" } /// - public bool? ShowTournament + public bool ShowTournament { get { @@ -260,7 +260,7 @@ public bool? ShowTournament else { // Set deletion date, if tournament shall not be displayed - base.DeletedOn = value is true ? DateTime.Now : null; + base.DeletedOn = value ? DateTime.Now : null; } } } diff --git a/Src/TournamentCalendar/Models/Contact/ContactModel.cs b/Src/TournamentCalendar/Models/Contact/ContactModel.cs index bece5f6..209bbbf 100644 --- a/Src/TournamentCalendar/Models/Contact/ContactModel.cs +++ b/Src/TournamentCalendar/Models/Contact/ContactModel.cs @@ -14,7 +14,7 @@ public ContactModel() public string? Subject { get; set; } public string? Message { get; set; } - public bool? CarbonCopyToSender { get; set; } + public bool CarbonCopyToSender { get; set; } //NOSONAR public new void Normalize(ModelStateDictionary modelState) diff --git a/Src/TournamentCalendar/Models/Newsletter/NewsletterModel.cs b/Src/TournamentCalendar/Models/Newsletter/NewsletterModel.cs index 468a8c6..990b743 100644 --- a/Src/TournamentCalendar/Models/Newsletter/NewsletterModel.cs +++ b/Src/TournamentCalendar/Models/Newsletter/NewsletterModel.cs @@ -59,8 +59,9 @@ private async Task LoadCalendarEntitiesSinceLastSend(CancellationToken cancellat public ICollection Newsletters { get; private set; } = new HashSet(); - public ICollection CalendarDisplayModel => _tournaments - .Select(t => new CalendarEntityDisplayModel(t, _surfaces, _playingAbilities)).ToList(); + public ICollection GetCalendarDisplayModel() => + _tournaments + .Select(t => new CalendarEntityDisplayModel(t, _surfaces, _playingAbilities)).ToList(); public DateTime LastSendDate { get; private set; } diff --git a/Src/TournamentCalendar/Scripts/Site.Location.js b/Src/TournamentCalendar/Scripts/Site.Location.js index 759885b..56ad180 100644 --- a/Src/TournamentCalendar/Scripts/Site.Location.js +++ b/Src/TournamentCalendar/Scripts/Site.Location.js @@ -1,5 +1,5 @@ // All Site scripts go into the same namespace -if (Site === undefined) { +if (typeof Site === 'undefined') { var Site = {}; } @@ -94,7 +94,7 @@ Site.Location = class { // requires https://maps.googleapis.com/maps/api/js // Example for async/await: https://gabrieleromanato.name/javascript-how-to-use-the-google-maps-api-with-promises-and-async-await const address = this.getAddress(true); - var response = await this.geocoder.geocode({ 'address': address }) + const response = await this.geocoder.geocode({ 'address': address }); const results = response.results; if (Array.isArray(results)) { const latitude = results[0].geometry.location.lat(); @@ -116,10 +116,10 @@ Site.Location = class { mapTypeId: google.maps.MapTypeId.ROADMAP }; - //create the map, and place it in the HTML map div + // create the map, and place it in the HTML map div const map = new google.maps.Map(this.mapPlaceholderEle, mapOptions); - //place the initial marker + // place the initial marker const marker = new google.maps.Marker({ position: coords, map: map, @@ -130,7 +130,7 @@ Site.Location = class { /** * Initializes the Google Maps canvas element * @param {string} mapPlaceholderId - The Id of the map placeholder - * @param {any} coords - Optional: the LatLng coodinates to center the map initially. Default location is Kassel/Germany. + * @param {any} coords - Optional: the LatLng coordinates to center the map initially. Default location is Kassel/Germany. */ static initMap(mapPlaceholderId, coords) { const mapPlaceholderEle = document.getElementById(mapPlaceholderId); @@ -140,6 +140,6 @@ Site.Location = class { mapTypeControl: true, mapTypeId: google.maps.MapTypeId.ROADMAP }; - new google.maps.Map(mapPlaceholderEle, mapOptions); + const map = new google.maps.Map(mapPlaceholderEle, mapOptions); } } \ No newline at end of file diff --git a/Src/TournamentCalendar/Scripts/Site.TempusDominusFactory.js b/Src/TournamentCalendar/Scripts/Site.TempusDominusFactory.js index 2082848..e366328 100644 --- a/Src/TournamentCalendar/Scripts/Site.TempusDominusFactory.js +++ b/Src/TournamentCalendar/Scripts/Site.TempusDominusFactory.js @@ -1,8 +1,10 @@ // All Site scripts go into the same namespace -if (Site === undefined) { +if (typeof Site === 'undefined') { var Site = {}; } -if (tempusDominus === undefined) { tempusDominus = window.tempusDominus; } +if (typeof tempusDominus === 'undefined') { + var tempusDominus = window.tempusDominus; +} Site.TempusDominusFactory = class { 'use strict'; @@ -18,7 +20,7 @@ Site.TempusDominusFactory = class { this.tdLocale = tempusDominus.locales[locale] || tempusDominus.locales[fallbackLocale]; // .NET always uses ante meridiem designator for hours 0:00:00 (midnight) to 11:59:59.999, // and post meridiem designator for later hours (after noon). - this.tdHourCycle = hourCycle == 12 ? 'h11' : 'h23'; + this.tdHourCycle = hourCycle === 12 ? 'h11' : 'h23'; this.useBiIcons = useBiIcons; this._setTdDefaults(); diff --git a/Src/TournamentCalendar/Scripts/Site.TempusDominusFactory.min.js b/Src/TournamentCalendar/Scripts/Site.TempusDominusFactory.min.js index 6853edb..5c59435 100644 --- a/Src/TournamentCalendar/Scripts/Site.TempusDominusFactory.min.js +++ b/Src/TournamentCalendar/Scripts/Site.TempusDominusFactory.min.js @@ -1 +1 @@ -var Site;void 0===Site&&(Site={}),void 0===tempusDominus&&(tempusDominus=window.tempusDominus),Site.TempusDominusFactory=class{"use strict";constructor(e,t,i,s){this.tdLocale=tempusDominus.locales[e]||tempusDominus.locales[t],this.tdHourCycle=12==i?"h11":"h23",this.useBiIcons=s,this._setTdDefaults()}_setTdDefaults(){this.tdLocale&&(tempusDominus.DefaultOptions.localization=this.tdLocale.localization,tempusDominus.DefaultOptions.localization.hourCycle=this.tdHourCycle),tempusDominus.DefaultOptions.localization.dayViewHeaderFormat={month:"long",year:"numeric"},this.useBiIcons?tempusDominus.DefaultOptions.display.icons={time:"bi bi-clock",date:"bi bi-calendar",up:"bi bi-arrow-up",down:"bi bi-arrow-down",previous:"bi bi-chevron-left",next:"bi bi-chevron-right",today:"bi bi-calendar-check",clear:"bi bi-trash",close:"bi bi-x bi-lg"}:tempusDominus.DefaultOptions.display.icons={time:"fas fa-clock",date:"fas fa-calendar",up:"fas fa-arrow-up",down:"fas fa-arrow-down",previous:"fas fa-chevron-left",next:"fas fa-chevron-right",today:"fas fa-calendar-check",clear:"fas fa-trash",close:"fas fa-times"},tempusDominus.DefaultOptions.display.buttons={today:!1,clear:!0,close:!0}}CreateCalendarPicker(t,i){void 0===i&&(i="L");try{try{t._tempusDominus&&t._tempusDominus.dispose()}catch(e){}t._tempusDominus=this._tryCreateCalendarPicker(t,i)}catch(e){t.querySelector("input").value="",t._tempusDominus=this._tryCreateCalendarPicker(t,i)}}CreateTimePicker(t,i){void 0===i&&(i="LT");try{try{t._tempusDominus&&t._tempusDominus.dispose()}catch(e){}t._tempusDominus=this._tryCreateTimePicker(t,i)}catch(e){t.querySelector("input").value="",t._tempusDominus=this._tryCreateTimePicker(t,i)}}SetEscapeKeyClosesPicker(t){document.addEventListener("keydown",function(e){"Escape"===e.key&&[].forEach.call(t,function(e){e=e._tempusDominus;e&&e.hide()})})}_tryCreateCalendarPicker(e,t){e=new tempusDominus.TempusDominus(e,{container:e,allowInputToggle:!1,localization:{format:t},display:{viewMode:"calendar",components:{clock:!1}},restrictions:{}});return this._fixUncaughtExceptions(e),e}_tryCreateTimePicker(e,t){e=new tempusDominus.TempusDominus(e,{container:e,allowInputToggle:!1,stepping:15,localization:{format:t},display:{viewMode:"clock",components:{decades:!1,year:!1,month:!1,date:!1,hours:!0,minutes:!0,seconds:!1},buttons:{today:!1,clear:!0,close:!0}}});return this._fixUncaughtExceptions(e),e}_fixUncaughtExceptions(e){}}; \ No newline at end of file +var Site,tempusDominus;void 0===Site&&(Site={}),void 0===tempusDominus&&(tempusDominus=window.tempusDominus),Site.TempusDominusFactory=class{"use strict";constructor(e,t,i,s){this.tdLocale=tempusDominus.locales[e]||tempusDominus.locales[t],this.tdHourCycle=12===i?"h11":"h23",this.useBiIcons=s,this._setTdDefaults()}_setTdDefaults(){this.tdLocale&&(tempusDominus.DefaultOptions.localization=this.tdLocale.localization,tempusDominus.DefaultOptions.localization.hourCycle=this.tdHourCycle),tempusDominus.DefaultOptions.localization.dayViewHeaderFormat={month:"long",year:"numeric"},this.useBiIcons?tempusDominus.DefaultOptions.display.icons={time:"bi bi-clock",date:"bi bi-calendar",up:"bi bi-arrow-up",down:"bi bi-arrow-down",previous:"bi bi-chevron-left",next:"bi bi-chevron-right",today:"bi bi-calendar-check",clear:"bi bi-trash",close:"bi bi-x bi-lg"}:tempusDominus.DefaultOptions.display.icons={time:"fas fa-clock",date:"fas fa-calendar",up:"fas fa-arrow-up",down:"fas fa-arrow-down",previous:"fas fa-chevron-left",next:"fas fa-chevron-right",today:"fas fa-calendar-check",clear:"fas fa-trash",close:"fas fa-times"},tempusDominus.DefaultOptions.display.buttons={today:!1,clear:!0,close:!0}}CreateCalendarPicker(t,i){void 0===i&&(i="L");try{try{t._tempusDominus&&t._tempusDominus.dispose()}catch(e){}t._tempusDominus=this._tryCreateCalendarPicker(t,i)}catch(e){t.querySelector("input").value="",t._tempusDominus=this._tryCreateCalendarPicker(t,i)}}CreateTimePicker(t,i){void 0===i&&(i="LT");try{try{t._tempusDominus&&t._tempusDominus.dispose()}catch(e){}t._tempusDominus=this._tryCreateTimePicker(t,i)}catch(e){t.querySelector("input").value="",t._tempusDominus=this._tryCreateTimePicker(t,i)}}SetEscapeKeyClosesPicker(t){document.addEventListener("keydown",function(e){"Escape"===e.key&&[].forEach.call(t,function(e){e=e._tempusDominus;e&&e.hide()})})}_tryCreateCalendarPicker(e,t){e=new tempusDominus.TempusDominus(e,{container:e,allowInputToggle:!1,localization:{format:t},display:{viewMode:"calendar",components:{clock:!1}},restrictions:{}});return this._fixUncaughtExceptions(e),e}_tryCreateTimePicker(e,t){e=new tempusDominus.TempusDominus(e,{container:e,allowInputToggle:!1,stepping:15,localization:{format:t},display:{viewMode:"clock",components:{decades:!1,year:!1,month:!1,date:!1,hours:!0,minutes:!0,seconds:!1},buttons:{today:!1,clear:!0,close:!0}}});return this._fixUncaughtExceptions(e),e}_fixUncaughtExceptions(e){}}; \ No newline at end of file diff --git a/Src/TournamentCalendar/Views/Newsletter/Show.cshtml b/Src/TournamentCalendar/Views/Newsletter/Show.cshtml index 0fea0f1..f2fab04 100644 --- a/Src/TournamentCalendar/Views/Newsletter/Show.cshtml +++ b/Src/TournamentCalendar/Views/Newsletter/Show.cshtml @@ -8,7 +8,7 @@

Turniere seit letztem Newsletter

- @Model.CalendarDisplayModel.Count + @Model.GetCalendarDisplayModel().Count @((await Model.GetRecipients()).Count) aktive Abonnenten