2021-08-10 20:18:57 -07:00
import { IgniteTemplate } from "../ignite-html/ignite-template.js" ;
/**
* Forces an input to only allow digits for input.
2024-11-13 06:14:55 -08:00
* @param {Number} maxLength Max number of digits allowed to be inputed. Default is -1 which disables this feature.
2021-08-11 10:45:24 -07:00
* @returns {IgniteTemplate} This ignite template.
2021-08-10 20:18:57 -07:00
*/
2024-11-13 06:14:55 -08:00
IgniteTemplate . prototype . inputDigits = function ( maxLength = - 1 ) {
2021-08-10 20:18:57 -07:00
this . on ( "keydown" , e => {
//If the input key isn't a digit, and it's not a backspace or escape or tab, ignore it.
if (( e . key < '0' || e . key > '9' ) && e . key != 'Backspace' && e . key != 'Escape' && e . key != 'Tab' && e . key != 'Control' && e . ctrlKey == false ) {
e . preventDefault ();
return false ;
} else {
//If we reached the max and the key isn't a special one then block this.
2024-11-13 06:14:55 -08:00
if ( maxLength != - 1 && e . target . value . length >= maxLength && e . key != 'Backspace' && e . key != 'Escape' && e . key != 'Tab' && e . key != 'Control' && e . ctrlKey == false ) {
2021-08-10 20:18:57 -07:00
e . preventDefault ();
return false ;
}
//If the control key is down, and it's not ctrl-a or ctrl-c or ctrl-v or ctrl-r then prevent
else if ( e . ctrlKey && e . key != 'a' && e . key != 'c' && e . key != 'v' && e . key != 'R' && e . key != 'r' )
{
e . preventDefault ();
return false ;
}
}
});
return this ;
}
2021-09-29 05:31:00 -07:00
/**
* Forces an input to only allow digits and periods for an input.
2024-11-13 06:14:55 -08:00
* @param {Number} maxLength Max number of digits allowed to be inputed. Default is -1 which disables this feature.
2021-09-29 05:31:00 -07:00
* @returns {IgniteTemplate} This ignite template.
*/
2024-11-13 06:14:55 -08:00
IgniteTemplate . prototype . inputNumber = function ( maxLength = - 1 ) {
2021-09-29 05:31:00 -07:00
this . on ( "keydown" , e => {
//If the input key isn't a digit, and it's not a backspace or escape or tab, ignore it.
if (( e . key < '0' || e . key > '9' ) && e . key != 'Backspace' && e . key != 'Escape' && e . key != 'Tab' && e . key != 'Control' && e . ctrlKey == false && e . key != '.' ) {
e . preventDefault ();
return false ;
2025-10-03 08:25:59 -07:00
}
//Only allow one period
else if ( e . key == '.' && e . target . value . includes ( '.' )) {
e . preventDefault ();
return false ;
}
//Otherwise check for special keys
else {
2021-09-29 05:31:00 -07:00
//If we reached the max and the key isn't a special one then block this.
2024-11-13 06:14:55 -08:00
if ( maxLength != - 1 && e . target . value . length >= maxLength && e . key != 'Backspace' && e . key != 'Escape' && e . key != 'Tab' && e . key != 'Control' && e . ctrlKey == false ) {
2021-09-29 05:31:00 -07:00
e . preventDefault ();
return false ;
}
//If the control key is down, and it's not ctrl-a or ctrl-c or ctrl-v or ctrl-r then prevent
else if ( e . ctrlKey && e . key != 'a' && e . key != 'c' && e . key != 'v' && e . key != 'R' && e . key != 'r' )
{
e . preventDefault ();
return false ;
}
}
});
return this ;
}
2021-08-10 20:18:57 -07:00
/**
* Forces an input to only allow letters for input.
2024-11-13 06:14:55 -08:00
* @param {Number} maxLength Max number of letters allowed to be inputed. Default is -1 which disables this feature.
2022-06-27 07:48:43 -07:00
* @param {Boolean} allowSpaces Whether or not to allow spaces to be entered. Default is true.
* @param {Number} maxSpaces Max number of spaces allowed tp be inputed. Default is -1 which disables this feature. This only works if allowSpaces is true.
2021-08-11 10:45:24 -07:00
* @returns {IgniteTemplate} This ignite template.
2021-08-10 20:18:57 -07:00
*/
2024-11-13 06:14:55 -08:00
IgniteTemplate . prototype . inputLetters = function ( maxLength = - 1 , allowSpaces = true , maxSpaces = - 1 ) {
2021-08-10 20:18:57 -07:00
this . on ( "keydown" , e => {
//If the input key isn't a letter, and it's not a space, backspace or escape or tab, ignore it.
if (( e . key < 'a' || e . key > 'z' ) && ( e . key < 'A' || e . key > 'Z' ) && e . key != ' ' && e . key != 'Backspace' && e . key != 'Escape' && e . key != 'Tab' && e . key != 'Control' && e . ctrlKey == false ) {
e . preventDefault ();
return false ;
} else {
//If we reached the max and the key isn't a special one then block this.
2024-11-13 06:14:55 -08:00
if ( maxLength != - 1 && e . target . value . length >= maxLength && e . key != 'Backspace' && e . key != 'Escape' && e . key != 'Tab' && e . key != 'Control' && e . ctrlKey == false ) {
2021-08-10 20:18:57 -07:00
e . preventDefault ();
return false ;
}
//If the control key is down, and it's not ctrl-a or ctrl-c or ctrl-v or ctrl-r then prevent
else if ( e . ctrlKey && e . key != 'a' && e . key != 'c' && e . key != 'v' && e . key != 'R' && e . key != 'r' )
{
e . preventDefault ();
return false ;
}
2022-06-27 07:48:43 -07:00
//If the key is space and space is not allowed prevent it, or if it's allowed and we have too many spaces prevent it.
2024-11-13 06:14:55 -08:00
else if (( ! allowSpaces && e . key == ' ' ) || ( allowSpaces && e . key == ' ' && maxSpaces != - 1 && countSpaces ( e . target . value ) >= maxSpaces )) {
2021-08-10 20:18:57 -07:00
e . preventDefault ();
return false ;
}
}
});
return this ;
}
2024-11-13 06:14:55 -08:00
/**
* Limits an input to a max length or max number of spaces if wanted.
* @param {Number} maxLength Max number of letters allowed to be inputed. Default is -1 which disables this feature.
* @param {Boolean} allowSpaces Whether or not to allow spaces to be entered. Default is true.
* @param {Number} maxSpaces Max number of spaces allowed tp be inputed. Default is -1 which disables this feature. This only works if allowSpaces is true.
* @returns {IgniteTemplate} This ignite template.
*/
IgniteTemplate . prototype . inputText = function ( maxLength = - 1 , allowSpaces = true , maxSpaces = - 1 ) {
this . on ( "keydown" , e => {
//If we reached the max and the key isn't a special one then block this.
2024-11-20 07:51:34 -08:00
if ( maxLength != - 1 && ( e . target . value ? . length >= maxLength || ( e . target . isContentEditable && e . target . innerText . length >= maxLength )) && e . key != 'Backspace' && e . key != 'Escape' && e . key != 'Tab' && e . key != 'Control' && e . ctrlKey == false ) {
2024-11-13 06:14:55 -08:00
e . preventDefault ();
return false ;
}
//If the key is space and space is not allowed prevent it, or if it's allowed and we have too many spaces prevent it.
2024-11-20 07:51:34 -08:00
else if (( ! allowSpaces && e . key == ' ' ) || ( allowSpaces && e . key == ' ' && maxSpaces != - 1 && countSpaces (( e . target . isContentEditable ? e . target . innerText : e . target . value )) >= maxSpaces )) {
2024-11-13 06:14:55 -08:00
e . preventDefault ();
return false ;
}
//If the backspace key is pressed and this is content editable, clear the element
//otherwise a hidden <br/> may be added.
2024-11-20 07:51:34 -08:00
else if ( e . key == "Backspace" && e . target . isContentEditable && e . target . innerText . length <= 1 ) {
2024-11-13 06:14:55 -08:00
e . target . innerHTML = null ;
}
});
return this ;
}
2021-08-10 20:18:57 -07:00
/**
* Forces an input to only allow email based characters for input.
2024-11-13 06:14:55 -08:00
* @param {Number} maxLength Max number of letters allowed to be inputed. Default is -1 which disables this feature.
2021-08-11 10:45:24 -07:00
* @returns {IgniteTemplate} This ignite template.
2021-08-10 20:18:57 -07:00
*/
2024-11-13 06:14:55 -08:00
IgniteTemplate . prototype . inputEmail = function ( maxLength = - 1 ) {
2021-08-10 20:18:57 -07:00
this . on ( "keydown" , e => {
//If the input key isn't a letter, and it's not a space, backspace or escape or tab, ignore it.
if (( e . key < 'a' || e . key > 'z' ) && ( e . key < 'A' || e . key > 'Z' ) && ( e . key < '0' || e . key > '9' ) && e . key != '@' && e . key != '.' && e . key != '-' && e . key != '_' && e . key != '+' && e . key != '~' && e . key != 'Backspace' && e . key != 'Escape' && e . key != 'Tab' && e . key != 'Control' && e . ctrlKey == false ) {
e . preventDefault ();
return false ;
} else {
//If we reached the max and the key isn't a special one then block this.
2024-11-13 06:14:55 -08:00
if ( maxLength != - 1 && e . target . value . length >= maxLength && e . key != 'Backspace' && e . key != 'Escape' && e . key != 'Tab' && e . key != 'Control' && e . ctrlKey == false ) {
2021-08-10 20:18:57 -07:00
e . preventDefault ();
return false ;
}
//If the control key is down, and it's not ctrl-a or ctrl-c or ctrl-v or ctrl-r then prevent
else if ( e . ctrlKey && e . key != 'a' && e . key != 'c' && e . key != 'v' && e . key != 'R' && e . key != 'r' )
{
e . preventDefault ();
return false ;
}
}
});
return this ;
}
2024-11-13 06:14:55 -08:00
/**
* Counts the number of spaces in a string and returns the result.
* @param {String} str
* @returns {Number} The number of spaces found.
*/
function countSpaces ( str ) {
if ( ! str ) {
return 0 ;
}
var count = 0 ;
for ( var i = 0 ; i < str . length ; i ++ ) {
if ( str [ i ] == " " ) {
count ++ ;
}
}
return count ;
}