From 76f906dec879075a6da34878babf293040c83bd2 Mon Sep 17 00:00:00 2001
From: Robert Wittek <r.m.wittek@gmail.com>
Date: Sun, 3 Oct 2021 15:44:23 +0200
Subject: [PATCH] #111 Improve personnel search by removing accents

---
 main/resources/ReleaseNotes/2.7.2.md                      | 2 ++
 main/view/src/main/webapp/static/js/edit/models/person.js | 1 +
 .../src/main/webapp/static/js/edit/viewmodels/units.js    | 8 ++++----
 main/view/src/main/webapp/static/js/utils/misc.js         | 5 +++++
 4 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/main/resources/ReleaseNotes/2.7.2.md b/main/resources/ReleaseNotes/2.7.2.md
index dffe58a2..faab153c 100644
--- a/main/resources/ReleaseNotes/2.7.2.md
+++ b/main/resources/ReleaseNotes/2.7.2.md
@@ -7,3 +7,5 @@
 * FMD-211 Fix display of units on hierarchy-edit page
   * Caused by breaking change of upgrade to knockout.js 3.4.2 to 3.5.1
   * Fixed by using named templates instead of html directly inside of 'knockout sortable'
+* \#111 ANSIfy personnel names
+  * Remove any accents in names and search terms on searching for crew members
diff --git a/main/view/src/main/webapp/static/js/edit/models/person.js b/main/view/src/main/webapp/static/js/edit/models/person.js
index 6b6f6ee1..0dda868a 100644
--- a/main/view/src/main/webapp/static/js/edit/models/person.js
+++ b/main/view/src/main/webapp/static/js/edit/models/person.js
@@ -28,6 +28,7 @@ define(function() {
     this.id = data.id;
     this.fullname = data.lastname + " " + data.firstname;
     this.personnelId = data.personnelId;
+    this.normalizedFullname = this.fullname.normalizeAccents();
   };
 
   return Person;
diff --git a/main/view/src/main/webapp/static/js/edit/viewmodels/units.js b/main/view/src/main/webapp/static/js/edit/viewmodels/units.js
index 88af8e52..5a0b9ea1 100644
--- a/main/view/src/main/webapp/static/js/edit/viewmodels/units.js
+++ b/main/view/src/main/webapp/static/js/edit/viewmodels/units.js
@@ -60,8 +60,8 @@ define(["jquery", "knockout", "../models/editableunit", "../models/person", "dat
         if (!filterVal) {
           return [];
         }
-        return $.map(filterVal.split(" "), function(item) {
-          return new RegExp(RegExp.escape(item), "i");
+        return $.map(filterVal.split(" "), function(singleSearchTerm) {
+          return new RegExp(RegExp.escape(singleSearchTerm.normalizeAccents()), "i");
         });
       }, this);
 
@@ -70,10 +70,10 @@ define(["jquery", "knockout", "../models/editableunit", "../models/person", "dat
       this.filtered = this.persons.extend({
         list: {
           filter: {
-            regex: function(item) {
+            regex: function(personToTest) {
               var regex = self.regex(), i;
               for (i = 0; i < regex.length; i++) {
-                if (!regex[i].test(item.pid) && !regex[i].test(item.fullname)) {
+                if (!regex[i].test(personToTest.personnelId) && !regex[i].test(personToTest.normalizedFullname)) {
                   return false;
                 }
               }
diff --git a/main/view/src/main/webapp/static/js/utils/misc.js b/main/view/src/main/webapp/static/js/utils/misc.js
index ef37a796..5aa6c0f7 100644
--- a/main/view/src/main/webapp/static/js/utils/misc.js
+++ b/main/view/src/main/webapp/static/js/utils/misc.js
@@ -82,4 +82,9 @@ define(["jquery", "knockout", "utils/conf"], function ($, ko, conf) {
       return '&' + String.escapeChars[m] + ';';
     });
   };
+
+  String.prototype.normalizeAccents = function () {
+    // From https://stackoverflow.com/questions/5700636/using-javascript-to-perform-text-matches-with-without-accented-characters
+    return this.normalize('NFD').replace(/[\u0300-\u036f]/g, "");
+  };
 });