@modernman00/shared-js-lib 1.2.7 → 1.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Login.js CHANGED
@@ -1,8 +1,8 @@
1
- "use strict";
1
+ 'use strict';
2
2
  import { convertFormData } from './FormProcessing.js';
3
- import { postFormData } from "./Http.js";
3
+ import { postFormData } from './Http.js';
4
4
  import { showLoader, clearLoader } from './Loader.js';
5
- import { showError } from "./ShowResponse.js";
5
+ import { showError } from './ShowResponse.js';
6
6
 
7
7
  // block the setLoader div
8
8
 
@@ -15,29 +15,33 @@ import { showError } from "./ShowResponse.js";
15
15
  * @returns {void}
16
16
  * @throws {Error} - If there is an error with the submission
17
17
  */
18
- export const loginSubmission = async (formId, loginURL, redirect, css = null) => {
19
- const formData = convertFormData(formId);
20
- formData.clearError();
21
-
22
- try {
23
- formData.emailVal();
24
- formData.massValidate();
25
-
26
- if (!formData.error.length) {
27
-
28
- showLoader();
29
- localStorage.setItem('redirect', redirect);
30
-
31
- await postFormData(loginURL, formId, redirect, css);
32
- } else {
33
- alert('The form cannot be submitted. Please check the errors');
34
- }
35
- } catch (err) {
36
- showError(err);
37
- } finally {
38
- clearLoader();
18
+ export const loginSubmission = async (
19
+ formId,
20
+ loginURL,
21
+ redirect,
22
+ css = null, lengthLimit = null
23
+ ) => {
24
+ const formData = convertFormData(formId);
25
+ formData.clearError();
26
+ if (lengthLimit) {
27
+ formData.realTimeCheckLen(lengthLimit.maxLength.id, lengthLimit.maxLength.max);
28
+ }
29
+
30
+ try {
31
+ formData.emailVal();
32
+ formData.massValidate();
33
+
34
+ if (!formData.error.length) {
35
+ showLoader();
36
+ localStorage.setItem('redirect', redirect);
37
+
38
+ await postFormData(loginURL, formId, redirect, css);
39
+ } else {
40
+ alert('The form cannot be submitted. Please check the errors');
39
41
  }
40
-
41
- }
42
-
43
-
42
+ } catch (err) {
43
+ showError(err);
44
+ } finally {
45
+ clearLoader();
46
+ }
47
+ };
package/ShowResponse.js CHANGED
@@ -1,5 +1,5 @@
1
- import { id } from './UtilityHtml.js';
2
- import axios from 'axios';
1
+ import { id, qSel } from './UtilityHtml.js';
2
+
3
3
  export const showError = (e) =>
4
4
  console.error(
5
5
  `${e.name} at ${e.fileName}:${e.lineNumber} - ${e.message}\n${e.stack}`,
@@ -35,54 +35,7 @@ export const showNotification = (
35
35
  }, timer);
36
36
  };
37
37
 
38
- /************* ✨ Windsurf Command 🌟 *************/
39
- export const deleteNotification = async (elementId, yourId, famCode) => {
40
- export const deleteNotification = async (elementId) => {
41
- // Extract the user ID from the target ID
42
- const senderId = elementId.replace('deleteNotification', 'notificationBar');
43
-
44
- const elementData = id(elementId);
45
- const data = elementData.getAttribute('data-id');
46
- // change the background of the clicked element
47
- const notificationHTML = id(senderId);
48
- // Make sure required variables are defined before using them
49
- if (typeof yourId === 'undefined' || typeof famCode === 'undefined') {
50
- msgException('Required parameters (yourId or famCode) are not defined');
51
- }
52
- const url = `/removeNotification/${yourId}/${famCode}/${data}`;
53
- const response = await axios.put(url);
54
- if (response.data.message === 'Notification marked as read') {
55
- // remove a html element with notificationBar after 2 mins
56
- notificationHTML.remove();
57
- // reduce the notification count as you have deleted the notification
58
- const newValues = parseInt(
59
- sessionStorage.getItem('notificationCount') - 1,
60
- );
61
- id('notification_count').innerHTML = newValues;
62
- } else {
63
- msgException('Error removing notification' + ' ' + response.data.message);
64
- }
65
- };
66
- /******* 43b70b66-cdee-4764-a1e7-4f21be65d0d7 *******/
67
-
68
- const url = `/removeNotification/${yourId}/${famCode}/${data}`;
69
38
 
70
- try {
71
- const response = await axios.put(url);
72
-
73
- if (response.data.message === 'Notification marked as read') {
74
- notificationHTML.remove();
75
- const newValues = parseInt(
76
- sessionStorage.getItem('notificationCount') - 1,
77
- );
78
- id('notification_count').innerHTML = newValues;
79
- } else {
80
- console.error('Error removing notification', response.data.message);
81
- }
82
- } catch (error) {
83
- console.error('Error removing notification', error);
84
- }
85
- };
86
39
 
87
40
  export const showResponse = (theId, message, status) => {
88
41
  const responseEl = id(theId);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modernman00/shared-js-lib",
3
- "version": "1.2.7",
3
+ "version": "1.2.9",
4
4
  "description": "Reusable JS utilities for numerous js problems",
5
5
  "homepage": "https://github.com/modernman00/shared-js-lib#readme",
6
6
  "keywords": [
@@ -1,101 +1,93 @@
1
- export const autocomplete = (inp, arr) => {
2
- /*the autocomplete function takes two arguments,
3
- the text field element and an array of possible autocompleted values:*/
4
- let currentFocus;
5
- /*execute a function when someone writes in the text field:*/
6
- inp.addEventListener("input", function(e) {
7
- let a, b, i, val = this.value;
8
- /*close any already open lists of autocompleted values*/
9
- closeAllLists();
10
- if (!val) { return false; }
11
- currentFocus = -1;
12
- /*create a DIV element that will contain the items (values):*/
13
- a = document.createElement("DIV");
14
- a.setAttribute("id", this.id + "autocomplete-list");
15
- a.setAttribute("class", "autocomplete-items");
16
- /*append the DIV element as a child of the autocomplete container:*/
17
- this.parentNode.appendChild(a);
18
- /*for each item in the array...*/
19
- for (i = 0; i < arr.length; i++) {
20
- /*check if the item starts with the same letters as the text field value:*/
21
- if (arr[i] && arr[i].substr(0, val.length).toUpperCase() === val.toUpperCase()) {
22
- if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
23
- /*create a DIV element for each matching element:*/
24
- b = document.createElement("DIV");
25
- /*make the matching letters bold:*/
26
- b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
27
- b.innerHTML += arr[i].substr(val.length);
28
- /*insert a input field that will hold the current array item's value:*/
29
- b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
30
- /*execute a function when someone clicks on the item value (DIV element):*/
31
- b.addEventListener("click", function(e) {
32
- /*insert the value for the autocomplete text field:*/
33
- inp.value = this.getElementsByTagName("input")[0].value;
34
- /*close the list of autocompleted values,
35
- (or any other open lists of autocompleted values:*/
36
- closeAllLists();
37
- });
38
- a.appendChild(b);
39
- }
40
- }
41
- }
42
- });
43
- /*execute a function presses a key on the keyboard:*/
44
- inp.addEventListener("keydown", function(e) {
45
- var x = document.getElementById(this.id + "autocomplete-list");
46
- if (x) x = x.getElementsByTagName("div");
47
- if (e.keyCode == 40) {
48
- /*If the arrow DOWN key is pressed,
49
- increase the currentFocus variable:*/
50
- currentFocus++;
51
- /*and and make the current item more visible:*/
52
- addActive(x);
53
- } else if (e.keyCode == 38) { //up
54
- /*If the arrow UP key is pressed,
55
- decrease the currentFocus variable:*/
56
- currentFocus--;
57
- /*and and make the current item more visible:*/
58
- addActive(x);
59
- } else if (e.keyCode == 13) {
60
- /*If the ENTER key is pressed, prevent the form from being submitted,*/
61
- e.preventDefault();
62
- if (currentFocus > -1) {
63
- /*and simulate a click on the "active" item:*/
64
- if (x) x[currentFocus].click();
65
- }
1
+ import { id } from './UtilityHtml.js'
2
+
3
+ /**
4
+ *
5
+ * @param {string} inputId
6
+ * @param {Array} arr
7
+ */
8
+
9
+ /**
10
+ * Sets up an autocomplete functionality for an input element.
11
+ *
12
+ * @param {string} inputId - The ID of the input element to attach the autocomplete to.
13
+ * @param {Array<string>} arr - An array of strings containing the autocomplete suggestions.
14
+ *
15
+ * The function converts all strings in the array to lowercase for case-insensitive matching.
16
+ * It initializes the autocomplete with the given input element and array of suggestions,
17
+ * providing real-time suggestions based on user input. On selecting a suggestion,
18
+ * it populates the input element with the selected suggestion.
19
+ */
20
+ export const autocomplete = (inputId, arr) => {
21
+ const whatToBuyInput = id(inputId); // Get the text input
22
+ if (whatToBuyInput) {
23
+ // Create a <ul> for autocomplete suggestions
24
+ const suggestionList = document.createElement("ul");
25
+ suggestionList.classList.add("autocomplete-suggestions");
26
+ suggestionList.id = "suggestions"; // For accessibility
27
+ whatToBuyInput.parentElement.appendChild(suggestionList); // Append to input's parent
28
+
29
+ // Function to show matching suggestions based on user input
30
+ const showSuggestions = (inputValue) => {
31
+ suggestionList.innerHTML = ""; // Clear previous suggestions
32
+ if (!inputValue) return; // Exit if input is empty
33
+
34
+ // Filter items that match the input (case-insensitive), limit to 8
35
+ const matches = arr
36
+ .filter(item => item.toLowerCase().includes(inputValue.toLowerCase()))
37
+ .slice(0, 8);
38
+
39
+ // Create <li> for each match
40
+ matches.forEach((item, index) => {
41
+ const li = document.createElement("li");
42
+ li.textContent = item;
43
+ li.setAttribute("tabindex", "0"); // Make focusable for keyboard
44
+ li.setAttribute("data-index", index); // Store index for navigation
45
+ li.addEventListener("click", () => {
46
+ whatToBuyInput.value = item; // Set input value on click
47
+ suggestionList.innerHTML = ""; // Clear suggestions
48
+ });
49
+ li.addEventListener("keypress", (e) => {
50
+ if (e.key === "Enter") {
51
+ whatToBuyInput.value = item; // Set input value on Enter
52
+ suggestionList.innerHTML = ""; // Clear suggestions
66
53
  }
54
+ });
55
+ suggestionList.appendChild(li);
67
56
  });
57
+ };
68
58
 
69
- function addActive(x) {
70
- /*a function to classify an item as "active":*/
71
- if (!x) return false;
72
- /*start by removing the "active" class on all items:*/
73
- removeActive(x);
74
- if (currentFocus >= x.length) currentFocus = 0;
75
- if (currentFocus < 0) currentFocus = (x.length - 1);
76
- /*add class "autocomplete-active":*/
77
- x[currentFocus].classList.add("autocomplete-active");
78
- }
59
+ // Show suggestions as user types
60
+ whatToBuyInput.addEventListener("input", (e) => {
61
+ showSuggestions(e.target.value);
62
+ });
79
63
 
80
- function removeActive(x) {
81
- /*a function to remove the "active" class from all autocomplete items:*/
82
- for (var i = 0; i < x.length; i++) {
83
- x[i].classList.remove("autocomplete-active");
84
- }
85
- }
64
+ // Clear suggestions on blur (with delay for click to register)
65
+ whatToBuyInput.addEventListener("blur", () => {
66
+ setTimeout(() => suggestionList.innerHTML = "", 200);
67
+ });
86
68
 
87
- function closeAllLists(elmnt) {
88
- /*close all autocomplete lists in the document,
89
- except the one passed as an argument:*/
90
- var x = document.getElementsByClassName("autocomplete-items");
91
- for (var i = 0; i < x.length; i++) {
92
- if (elmnt != x[i] && elmnt != inp) {
93
- x[i].parentNode.removeChild(x[i]);
94
- }
95
- }
69
+ // Handle keyboard navigation for suggestions
70
+ whatToBuyInput.addEventListener("keydown", (e) => {
71
+ const suggestions = suggestionList.querySelectorAll("li");
72
+ if (!suggestions.length) return; // Exit if no suggestions
73
+
74
+ let focusedIndex = Array.from(suggestions).findIndex(li => li === document.activeElement);
75
+ if (e.key === "ArrowDown") {
76
+ e.preventDefault(); // Prevent cursor movement
77
+ focusedIndex = (focusedIndex + 1) % suggestions.length; // Loop to start
78
+ suggestions[focusedIndex].focus();
79
+ } else if (e.key === "ArrowUp") {
80
+ e.preventDefault();
81
+ focusedIndex = (focusedIndex - 1 + suggestions.length) % suggestions.length; // Loop to end
82
+ suggestions[focusedIndex].focus();
83
+ } else if (e.key === "Enter" && focusedIndex >= 0) {
84
+ e.preventDefault();
85
+ whatToBuyInput.value = suggestions[focusedIndex].textContent; // Select item
86
+ suggestionList.innerHTML = ""; // Clear suggestions
87
+ } else if (e.key === "Escape") {
88
+ suggestionList.innerHTML = ""; // Clear suggestions
96
89
  }
97
- /*execute a function when someone clicks in the document:*/
98
- document.addEventListener("click", function(e) {
99
- closeAllLists(e.target);
100
- });
101
- }
90
+ });
91
+ }
92
+ }
93
+