in src/Foundation/Theming/code/scripts/autoNumeric.js [847:1117]
init: function (options) {
return this.each(function () {
var $this = $(this),
settings = $this.data('autoNumeric'), /** attempt to grab 'autoNumeric' settings, if they don't exist returns "undefined". */
tagData = $this.data(); /** attempt to grab HTML5 data, if they don't exist we'll get "undefined".*/
if (typeof settings !== 'object') { /** If we couldn't grab settings, create them from defaults and passed options. */
var defaults = {
/** allowed numeric values
* please do not modify
*/
aNum: '0123456789',
/** allowed thousand separator characters
* comma = ','
* period "full stop" = '.'
* apostrophe is escaped = '\''
* space = ' '
* none = ''
* NOTE: do not use numeric characters
*/
aSep: ',',
/** digital grouping for the thousand separator used in Format
* dGroup: '2', results in 99,99,99,999 common in India for values less than 1 billion and greater than -1 billion
* dGroup: '3', results in 999,999,999 default
* dGroup: '4', results in 9999,9999,9999 used in some Asian countries
*/
dGroup: '3',
/** allowed decimal separator characters
* period "full stop" = '.'
* comma = ','
*/
aDec: '.',
/** allow to declare alternative decimal separator which is automatically replaced by aDec
* developed for countries the use a comma ',' as the decimal character
* and have keyboards\numeric pads that have a period 'full stop' as the decimal characters (Spain is an example)
*/
altDec: null,
/** allowed currency symbol
* Must be in quotes aSign: '$', a space is allowed aSign: '$ '
*/
aSign: '',
/** placement of currency sign
* for prefix pSign: 'p',
* for suffix pSign: 's',
*/
pSign: 'p',
/** maximum possible value
* value must be enclosed in quotes and use the period for the decimal point
* value must be larger than vMin
*/
vMax: '9999999999999.99',
/** minimum possible value
* value must be enclosed in quotes and use the period for the decimal point
* value must be smaller than vMax
*/
vMin: '0.00',
/** max number of decimal places = used to override decimal places set by the vMin & vMax values
* value must be enclosed in quotes example mDec: '3',
* This can also set the value via a call back function mDec: 'css:#
*/
mDec: null,
/** method used for rounding
* mRound: 'S', Round-Half-Up Symmetric (default)
* mRound: 'A', Round-Half-Up Asymmetric
* mRound: 's', Round-Half-Down Symmetric (lower case s)
* mRound: 'a', Round-Half-Down Asymmetric (lower case a)
* mRound: 'B', Round-Half-Even "Bankers Rounding"
* mRound: 'U', Round Up "Round-Away-From-Zero"
* mRound: 'D', Round Down "Round-Toward-Zero" - same as truncate
* mRound: 'C', Round to Ceiling "Toward Positive Infinity"
* mRound: 'F', Round to Floor "Toward Negative Infinity"
*/
mRound: 'S',
/** controls decimal padding
* aPad: true - always Pad decimals with zeros
* aPad: false - does not pad with zeros.
* aPad: `some number` - pad decimals with zero to number different from mDec
* thanks to Jonas Johansson for the suggestion
*/
aPad: true,
/** places brackets on negative value -$ 999.99 to (999.99)
* visible only when the field does NOT have focus the left and right symbols should be enclosed in quotes and seperated by a comma
* nBracket: null, nBracket: '(,)', nBracket: '[,]', nBracket: '<,>' or nBracket: '{,}'
*/
nBracket: null,
/** Displayed on empty string
* wEmpty: 'empty', - input can be blank
* wEmpty: 'zero', - displays zero
* wEmpty: 'sign', - displays the currency sign
*/
wEmpty: 'empty',
/** controls leading zero behavior
* lZero: 'allow', - allows leading zeros to be entered. Zeros will be truncated when entering additional digits. On focusout zeros will be deleted.
* lZero: 'deny', - allows only one leading zero on values less than one
* lZero: 'keep', - allows leading zeros to be entered. on fousout zeros will be retained.
*/
lZero: 'allow',
/** determine if the default value will be formatted on page ready.
* true = automatically formats the default value on page ready
* false = will not format the default value
*/
aForm: true,
/** future use */
onSomeEvent: function () {}
};
settings = $.extend({}, defaults, tagData, options); /** Merge defaults, tagData and options */
if (settings.aDec === settings.aSep) {
$.error("autoNumeric will not function properly when the decimal character aDec: '" + settings.aDec + "' and thousand separator aSep: '" + settings.aSep + "' are the same character");
return this;
}
$this.data('autoNumeric', settings); /** Save our new settings */
} else {
return this;
}
settings.runOnce = false;
var holder = getHolder($this, settings);
if ($.inArray($this.prop('tagName').toLowerCase(), settings.tagList) === -1 && $this.prop('tagName').toLowerCase() !== 'input') {
$.error("The <" + $this.prop('tagName').toLowerCase() + "> is not supported by autoNumeric()");
return this;
}
if (settings.runOnce === false && settings.aForm) {/** routine to format default value on page load */
if ($this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])')) {
var setValue = true;
if ($this[0].value === '' && settings.wEmpty === 'empty') {
$this[0].value = '';
setValue = false;
}
if ($this[0].value === '' && settings.wEmpty === 'sign') {
$this[0].value = settings.aSign;
setValue = false;
}
if (setValue) {
$this.autoNumeric('set', $this.val());
}
}
if ($.inArray($this.prop('tagName').toLowerCase(), settings.tagList) !== -1 && $this.text() !== '') {
$this.autoNumeric('set', $this.text());
}
}
settings.runOnce = true;
if ($this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])')) { /**added hidden type */
$this.on('keydown.autoNumeric', function (e) {
holder = getHolder($this);
if (holder.settings.aDec === holder.settings.aSep) {
$.error("autoNumeric will not function properly when the decimal character aDec: '" + holder.settings.aDec + "' and thousand separator aSep: '" + holder.settings.aSep + "' are the same character");
return this;
}
if (holder.that.readOnly) {
holder.processed = true;
return true;
}
/** The below streamed code / comment allows the "enter" keydown to throw a change() event */
/** if (e.keyCode === 13 && holder.inVal !== $this.val()){
$this.change();
holder.inVal = $this.val();
}*/
holder.init(e);
holder.settings.oEvent = 'keydown';
if (holder.skipAllways(e)) {
holder.processed = true;
return true;
}
if (holder.processAllways()) {
holder.processed = true;
holder.formatQuick();
e.preventDefault();
return false;
}
holder.formatted = false;
return true;
});
$this.on('keypress.autoNumeric', function (e) {
var holder = getHolder($this),
processed = holder.processed;
holder.init(e);
holder.settings.oEvent = 'keypress';
if (holder.skipAllways(e)) {
return true;
}
if (processed) {
e.preventDefault();
return false;
}
if (holder.processAllways() || holder.processKeypress()) {
holder.formatQuick();
e.preventDefault();
return false;
}
holder.formatted = false;
});
$this.on('keyup.autoNumeric', function (e) {
var holder = getHolder($this);
holder.init(e);
holder.settings.oEvent = 'keyup';
var skip = holder.skipAllways(e);
holder.kdCode = 0;
delete holder.valuePartsBeforePaste;
if ($this[0].value === holder.settings.aSign) { /** added to properly place the caret when only the currency is present */
if (holder.settings.pSign === 's') {
setElementSelection(this, 0, 0);
} else {
setElementSelection(this, holder.settings.aSign.length, holder.settings.aSign.length);
}
}
if (skip) {
return true;
}
if (this.value === '') {
return true;
}
if (!holder.formatted) {
holder.formatQuick();
}
});
$this.on('focusin.autoNumeric', function () {
var holder = getHolder($this);
holder.settingsClone.oEvent = 'focusin';
if (holder.settingsClone.nBracket !== null) {
var checkVal = $this.val();
$this.val(negativeBracket(checkVal, holder.settingsClone.nBracket, holder.settingsClone.oEvent));
}
holder.inVal = $this.val();
var onempty = checkEmpty(holder.inVal, holder.settingsClone, true);
if (onempty !== null) {
$this.val(onempty);
if (holder.settings.pSign === 's') {
setElementSelection(this, 0, 0);
} else {
setElementSelection(this, holder.settings.aSign.length, holder.settings.aSign.length);
}
}
});
$this.on('focusout.autoNumeric', function () {
var holder = getHolder($this),
settingsClone = holder.settingsClone,
value = $this.val(),
origValue = value;
holder.settingsClone.oEvent = 'focusout';
var strip_zero = ''; /** added to control leading zero */
if (settingsClone.lZero === 'allow') { /** added to control leading zero */
settingsClone.allowLeading = false;
strip_zero = 'leading';
}
if (value !== '') {
value = autoStrip(value, settingsClone, strip_zero);
if (checkEmpty(value, settingsClone) === null && autoCheck(value, settingsClone, $this[0])) {
value = fixNumber(value, settingsClone.aDec, settingsClone.aNeg);
value = autoRound(value, settingsClone);
value = presentNumber(value, settingsClone.aDec, settingsClone.aNeg);
} else {
value = '';
}
}
var groupedValue = checkEmpty(value, settingsClone, false);
if (groupedValue === null) {
groupedValue = autoGroup(value, settingsClone);
}
if (groupedValue !== origValue) {
$this.val(groupedValue);
}
if (groupedValue !== holder.inVal) {
$this.change();
delete holder.inVal;
}
if (settingsClone.nBracket !== null && $this.autoNumeric('get') < 0) {
holder.settingsClone.oEvent = 'focusout';
$this.val(negativeBracket($this.val(), settingsClone.nBracket, settingsClone.oEvent));
}
});
}
});
},