autonumeric-rails 1.9.24.0 → 1.9.25.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MWI4OTNmOTc4NWM1ZTM0MzQ2ZmZlYjBkN2I5MWFiY2JlZjlmMzA2OA==
5
+ data.tar.gz: !binary |-
6
+ NmY5Zjc5Zjc5ODFiOWJhYmVlOGM1MTg4NmQyYWViYjViNTI5ZDEwZg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YzZhYjUwOTgzZTMzNGM1NWU0NzE1ODVkYzAwOGZmNzhlMmRiNWU4MGU3YmNj
10
+ OTBhYTY4ZmJjMTUzNGQzYTMwMjJiYTcxMGE0NjFjZjRmMjExNDgwMDJmNGQ1
11
+ ZjFjOWUwMmMwNzZmNGEwOTAwMzNjNzhkYjA4ZGVmZGVkNGNmOTY=
12
+ data.tar.gz: !binary |-
13
+ N2U5YmEwOWUzMWQ2MTljMmZhZTZlZjQ0ZTM4Yjg2OWNjZmZmM2E1NWUzNGZm
14
+ MmFlY2Y1MmIyOWFkYTM2YzhhODZkNDJiYmNmMDA5YTAwYmVjMjIwNzY1OTk3
15
+ YjIxYzMxYTQ0ZWVlMjJmZjJmZTIyOTIzMDBhZmY1ZGM3NTM3YzY=
@@ -1,3 +1,7 @@
1
+ # 1.9.25.0
2
+
3
+ - Update autoNumeric v 1.9.25
4
+
1
5
  # 1.9.24.0
2
6
 
3
7
  - Update autoNumeric v 1.9.24
@@ -1,5 +1,5 @@
1
1
  module Autonumeric
2
2
  module Rails
3
- VERSION = '1.9.24.0'
3
+ VERSION = '1.9.25.0'
4
4
  end
5
5
  end
@@ -0,0 +1,1293 @@
1
+ /**
2
+ * autoNumeric.js
3
+ * @author: Bob Knothe
4
+ * @author: Sokolov Yura
5
+ * @version: 1.9.25 - 2014-08-02 GMT 11:00 AM
6
+ *
7
+ * Created by Robert J. Knothe on 2010-10-25. Please report any bugs to https://github.com/BobKnothe/autoNumeric
8
+ * Created by Sokolov Yura on 2010-11-07
9
+ *
10
+ * Copyright (c) 2011 Robert J. Knothe http://www.decorplanit.com/plugin/
11
+ *
12
+ * The MIT License (http://www.opensource.org/licenses/mit-license.php)
13
+ *
14
+ * Permission is hereby granted, free of charge, to any person
15
+ * obtaining a copy of this software and associated documentation
16
+ * files (the "Software"), to deal in the Software without
17
+ * restriction, including without limitation the rights to use,
18
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
19
+ * copies of the Software, and to permit persons to whom the
20
+ * Software is furnished to do so, subject to the following
21
+ * conditions:
22
+ *
23
+ * The above copyright notice and this permission notice shall be
24
+ * included in all copies or substantial portions of the Software.
25
+ *
26
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
28
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
30
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33
+ * OTHER DEALINGS IN THE SOFTWARE.
34
+ */
35
+ (function ($) {
36
+ "use strict";
37
+ /*jslint browser: true*/
38
+ /*global jQuery: false*/
39
+ /* Cross browser routine for getting selected range/cursor position
40
+ */
41
+ function getElementSelection(that) {
42
+ var position = {};
43
+ if (that.selectionStart === undefined) {
44
+ that.focus();
45
+ var select = document.selection.createRange();
46
+ position.length = select.text.length;
47
+ select.moveStart('character', -that.value.length);
48
+ position.end = select.text.length;
49
+ position.start = position.end - position.length;
50
+ } else {
51
+ position.start = that.selectionStart;
52
+ position.end = that.selectionEnd;
53
+ position.length = position.end - position.start;
54
+ }
55
+ return position;
56
+ }
57
+ /**
58
+ * Cross browser routine for setting selected range/cursor position
59
+ */
60
+ function setElementSelection(that, start, end) {
61
+ if (that.selectionStart === undefined) {
62
+ that.focus();
63
+ var r = that.createTextRange();
64
+ r.collapse(true);
65
+ r.moveEnd('character', end);
66
+ r.moveStart('character', start);
67
+ r.select();
68
+ } else {
69
+ that.selectionStart = start;
70
+ that.selectionEnd = end;
71
+ }
72
+ }
73
+ /**
74
+ * run callbacks in parameters if any
75
+ * any parameter could be a callback:
76
+ * - a function, which invoked with jQuery element, parameters and this parameter name and returns parameter value
77
+ * - a name of function, attached to $(selector).autoNumeric.functionName(){} - which was called previously
78
+ */
79
+ function runCallbacks($this, settings) {
80
+ /**
81
+ * loops through the settings object (option array) to find the following
82
+ * k = option name example k=aNum
83
+ * val = option value example val=0123456789
84
+ */
85
+ $.each(settings, function (k, val) {
86
+ if (typeof val === 'function') {
87
+ settings[k] = val($this, settings, k);
88
+ } else if (typeof $this.autoNumeric[val] === 'function') {
89
+ /**
90
+ * calls the attached function from the html5 data example: data-a-sign="functionName"
91
+ */
92
+ settings[k] = $this.autoNumeric[val]($this, settings, k);
93
+ }
94
+ });
95
+ }
96
+ function convertKeyToNumber(settings, key) {
97
+ if (typeof (settings[key]) === 'string') {
98
+ settings[key] *= 1;
99
+ }
100
+ }
101
+ /**
102
+ * Preparing user defined options for further usage
103
+ * merge them with defaults appropriately
104
+ */
105
+ function autoCode($this, settings) {
106
+ runCallbacks($this, settings);
107
+ settings.oEvent = null;
108
+ settings.tagList = ['b', 'caption', 'cite', 'code', 'dd', 'del', 'div', 'dfn', 'dt', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ins', 'kdb', 'label', 'li', 'output', 'p', 'q', 's', 'sample', 'span', 'strong', 'td', 'th', 'u', 'var'];
109
+ var vmax = settings.vMax.toString().split('.'),
110
+ vmin = (!settings.vMin && settings.vMin !== 0) ? [] : settings.vMin.toString().split('.');
111
+ convertKeyToNumber(settings, 'vMax');
112
+ convertKeyToNumber(settings, 'vMin');
113
+ convertKeyToNumber(settings, 'mDec'); /** set mDec if not defined by user */
114
+ settings.mDec = (settings.mRound === 'CHF') ? '2' : settings.mDec;
115
+ settings.allowLeading = true;
116
+ settings.aNeg = settings.vMin < 0 ? '-' : '';
117
+ vmax[0] = vmax[0].replace('-', '');
118
+ vmin[0] = vmin[0].replace('-', '');
119
+ settings.mInt = Math.max(vmax[0].length, vmin[0].length, 1);
120
+ if (settings.mDec === null) {
121
+ var vmaxLength = 0,
122
+ vminLength = 0;
123
+ if (vmax[1]) {
124
+ vmaxLength = vmax[1].length;
125
+ }
126
+ if (vmin[1]) {
127
+ vminLength = vmin[1].length;
128
+ }
129
+ settings.mDec = Math.max(vmaxLength, vminLength);
130
+ } /** set alternative decimal separator key */
131
+ if (settings.altDec === null && settings.mDec > 0) {
132
+ if (settings.aDec === '.' && settings.aSep !== ',') {
133
+ settings.altDec = ',';
134
+ } else if (settings.aDec === ',' && settings.aSep !== '.') {
135
+ settings.altDec = '.';
136
+ }
137
+ }
138
+ /** cache regexps for autoStrip */
139
+ var aNegReg = settings.aNeg ? '([-\\' + settings.aNeg + ']?)' : '(-?)';
140
+ settings.aNegRegAutoStrip = aNegReg;
141
+ settings.skipFirstAutoStrip = new RegExp(aNegReg + '[^-' + (settings.aNeg ? '\\' + settings.aNeg : '') + '\\' + settings.aDec + '\\d]' + '.*?(\\d|\\' + settings.aDec + '\\d)');
142
+ settings.skipLastAutoStrip = new RegExp('(\\d\\' + settings.aDec + '?)[^\\' + settings.aDec + '\\d]\\D*$');
143
+ var allowed = '-' + settings.aNum + '\\' + settings.aDec;
144
+ settings.allowedAutoStrip = new RegExp('[^' + allowed + ']', 'gi');
145
+ settings.numRegAutoStrip = new RegExp(aNegReg + '(?:\\' + settings.aDec + '?(\\d+\\' + settings.aDec + '\\d+)|(\\d*(?:\\' + settings.aDec + '\\d*)?))');
146
+ return settings;
147
+ }
148
+ /**
149
+ * strip all unwanted characters and leave only a number alert
150
+ */
151
+ function autoStrip(s, settings, strip_zero) {
152
+ if (settings.aSign) { /** remove currency sign */
153
+ while (s.indexOf(settings.aSign) > -1) {
154
+ s = s.replace(settings.aSign, '');
155
+ }
156
+ }
157
+ s = s.replace(settings.skipFirstAutoStrip, '$1$2'); /** first replace anything before digits */
158
+ s = s.replace(settings.skipLastAutoStrip, '$1'); /** then replace anything after digits */
159
+ s = s.replace(settings.allowedAutoStrip, ''); /** then remove any uninterested characters */
160
+ if (settings.altDec) {
161
+ s = s.replace(settings.altDec, settings.aDec);
162
+ } /** get only number string */
163
+ var m = s.match(settings.numRegAutoStrip);
164
+ s = m ? [m[1], m[2], m[3]].join('') : '';
165
+ if ((settings.lZero === 'allow' || settings.lZero === 'keep') && strip_zero !== 'strip') {
166
+ var parts = [],
167
+ nSign = '';
168
+ parts = s.split(settings.aDec);
169
+ if (parts[0].indexOf('-') !== -1) {
170
+ nSign = '-';
171
+ parts[0] = parts[0].replace('-', '');
172
+ }
173
+ if (parts[0].length > settings.mInt && parts[0].charAt(0) === '0') { /** strip leading zero if need */
174
+ parts[0] = parts[0].slice(1);
175
+ }
176
+ s = nSign + parts.join(settings.aDec);
177
+ }
178
+ if ((strip_zero && settings.lZero === 'deny') || (strip_zero && settings.lZero === 'allow' && settings.allowLeading === false)) {
179
+ var strip_reg = '^' + settings.aNegRegAutoStrip + '0*(\\d' + (strip_zero === 'leading' ? ')' : '|$)');
180
+ strip_reg = new RegExp(strip_reg);
181
+ s = s.replace(strip_reg, '$1$2');
182
+ }
183
+ return s;
184
+ }
185
+ /**
186
+ * places or removes brackets on negative values
187
+ */
188
+ function negativeBracket(s, nBracket, oEvent) { /** oEvent = settings.oEvent */
189
+ nBracket = nBracket.split(',');
190
+ if (oEvent === 'set' || oEvent === 'focusout') {
191
+ s = s.replace('-', '');
192
+ s = nBracket[0] + s + nBracket[1];
193
+ } else if ((oEvent === 'get' || oEvent === 'focusin' || oEvent === 'pageLoad') && s.charAt(0) === nBracket[0]) {
194
+ s = s.replace(nBracket[0], '-');
195
+ s = s.replace(nBracket[1], '');
196
+ }
197
+ return s;
198
+ }
199
+ /**
200
+ * truncate decimal part of a number
201
+ */
202
+ function truncateDecimal(s, aDec, mDec) {
203
+ if (aDec && mDec) {
204
+ var parts = s.split(aDec);
205
+ /** truncate decimal part to satisfying length
206
+ * cause we would round it anyway */
207
+ if (parts[1] && parts[1].length > mDec) {
208
+ if (mDec > 0) {
209
+ parts[1] = parts[1].substring(0, mDec);
210
+ s = parts.join(aDec);
211
+ } else {
212
+ s = parts[0];
213
+ }
214
+ }
215
+ }
216
+ return s;
217
+ }
218
+ /**
219
+ * prepare number string to be converted to real number
220
+ */
221
+ function fixNumber(s, aDec, aNeg) {
222
+ if (aDec && aDec !== '.') {
223
+ s = s.replace(aDec, '.');
224
+ }
225
+ if (aNeg && aNeg !== '-') {
226
+ s = s.replace(aNeg, '-');
227
+ }
228
+ if (!s.match(/\d/)) {
229
+ s += '0';
230
+ }
231
+ return s;
232
+ }
233
+ /**
234
+ * function to handle numbers less than 0 that are stored in Exponential notation ex: .0000001 stored as 1e-7
235
+ */
236
+ function checkValue(value, settings) {
237
+ if (value) {
238
+ var checkSmall = +value;
239
+ if (checkSmall < 0.000001 && checkSmall > -1) {
240
+ value = +value;
241
+ if (value < 0.000001 && value > 0) {
242
+ value = (value + 10).toString();
243
+ value = value.substring(1);
244
+ }
245
+ if (value < 0 && value > -1) {
246
+ value = (value - 10).toString();
247
+ value = '-' + value.substring(2);
248
+ }
249
+ value = value.toString();
250
+ } else {
251
+ var parts = value.split('.');
252
+ if (parts[1] !== undefined) {
253
+ if (+parts[1] === 0) {
254
+ value = parts[0];
255
+ } else {
256
+ parts[1] = parts[1].replace(/0*$/, '');
257
+ value = parts.join('.');
258
+ }
259
+ }
260
+ }
261
+ }
262
+ return (settings.lZero === 'keep') ? value : value.replace(/^0*(\d)/, '$1');
263
+ }
264
+ /**
265
+ * prepare real number to be converted to our format
266
+ */
267
+ function presentNumber(s, aDec, aNeg) {
268
+ if (aNeg && aNeg !== '-') {
269
+ s = s.replace('-', aNeg);
270
+ }
271
+ if (aDec && aDec !== '.') {
272
+ s = s.replace('.', aDec);
273
+ }
274
+ return s;
275
+ }
276
+ /**
277
+ * checking that number satisfy format conditions
278
+ * and lays between settings.vMin and settings.vMax
279
+ * and the string length does not exceed the digits in settings.vMin and settings.vMax
280
+ */
281
+ function autoCheck(s, settings) {
282
+ s = autoStrip(s, settings);
283
+ s = truncateDecimal(s, settings.aDec, settings.mDec);
284
+ s = fixNumber(s, settings.aDec, settings.aNeg);
285
+ var value = +s;
286
+ if (settings.oEvent === 'set' && (value < settings.vMin || value > settings.vMax)) {
287
+ $.error("The value (" + value + ") from the 'set' method falls outside of the vMin / vMax range");
288
+ }
289
+ return value >= settings.vMin && value <= settings.vMax;
290
+ }
291
+ /**
292
+ * private function to check for empty value
293
+ */
294
+ function checkEmpty(iv, settings, signOnEmpty) {
295
+ if (iv === '' || iv === settings.aNeg) {
296
+ if (settings.wEmpty === 'zero') {
297
+ return iv + '0';
298
+ }
299
+ if (settings.wEmpty === 'sign' || signOnEmpty) {
300
+ return iv + settings.aSign;
301
+ }
302
+ return iv;
303
+ }
304
+ return null;
305
+ }
306
+ /**
307
+ * private function that formats our number
308
+ */
309
+ function autoGroup(iv, settings) {
310
+ iv = autoStrip(iv, settings);
311
+ var testNeg = iv.replace(',', '.'),
312
+ empty = checkEmpty(iv, settings, true);
313
+ if (empty !== null) {
314
+ return empty;
315
+ }
316
+ var digitalGroup = '';
317
+ if (settings.dGroup === 2) {
318
+ digitalGroup = /(\d)((\d)(\d{2}?)+)$/;
319
+ } else if (settings.dGroup === 4) {
320
+ digitalGroup = /(\d)((\d{4}?)+)$/;
321
+ } else {
322
+ digitalGroup = /(\d)((\d{3}?)+)$/;
323
+ } /** splits the string at the decimal string */
324
+ var ivSplit = iv.split(settings.aDec);
325
+ if (settings.altDec && ivSplit.length === 1) {
326
+ ivSplit = iv.split(settings.altDec);
327
+ } /** assigns the whole number to the a varibale (s) */
328
+ var s = ivSplit[0];
329
+ if (settings.aSep) {
330
+ while (digitalGroup.test(s)) { /** re-inserts the thousand sepparator via a regualer expression */
331
+ s = s.replace(digitalGroup, '$1' + settings.aSep + '$2');
332
+ }
333
+ }
334
+ if (settings.mDec !== 0 && ivSplit.length > 1) {
335
+ if (ivSplit[1].length > settings.mDec) {
336
+ ivSplit[1] = ivSplit[1].substring(0, settings.mDec);
337
+ } /** joins the whole number with the deciaml value */
338
+ iv = s + settings.aDec + ivSplit[1];
339
+ } else { /** if whole numbers only */
340
+ iv = s;
341
+ }
342
+ if (settings.aSign) {
343
+ var has_aNeg = iv.indexOf(settings.aNeg) !== -1;
344
+ iv = iv.replace(settings.aNeg, '');
345
+ iv = settings.pSign === 'p' ? settings.aSign + iv : iv + settings.aSign;
346
+ if (has_aNeg) {
347
+ iv = settings.aNeg + iv;
348
+ }
349
+ }
350
+ if (settings.oEvent === 'set' && testNeg < 0 && settings.nBracket !== null) { /** removes the negative sign and places brackets */
351
+ iv = negativeBracket(iv, settings.nBracket, settings.oEvent);
352
+ }
353
+ return iv;
354
+ }
355
+ /**
356
+ * round number after setting by pasting or $().autoNumericSet()
357
+ * private function for round the number
358
+ * please note this handled as text - JavaScript math function can return inaccurate values
359
+ * also this offers multiple rounding methods that are not easily accomplished in JavaScript
360
+ */
361
+ function autoRound(iv, settings) { /** value to string */
362
+ iv = (iv === '') ? '0' : iv.toString();
363
+ convertKeyToNumber(settings, 'mDec'); /** set mDec to number needed when mDec set by 'update method */
364
+ if (settings.mRound === 'CHF') {
365
+ iv = (Math.round(iv * 20) / 20).toString();
366
+ }
367
+ var ivRounded = '',
368
+ i = 0,
369
+ nSign = '',
370
+ rDec = (typeof (settings.aPad) === 'boolean' || settings.aPad === null) ? (settings.aPad ? settings.mDec : 0) : +settings.aPad;
371
+ var truncateZeros = function (ivRounded) { /** truncate not needed zeros */
372
+ var regex = (rDec === 0) ? (/(\.(?:\d*[1-9])?)0*$/) : rDec === 1 ? (/(\.\d(?:\d*[1-9])?)0*$/) : new RegExp('(\\.\\d{' + rDec + '}(?:\\d*[1-9])?)0*$');
373
+ ivRounded = ivRounded.replace(regex, '$1'); /** If there are no decimal places, we don't need a decimal point at the end */
374
+ if (rDec === 0) {
375
+ ivRounded = ivRounded.replace(/\.$/, '');
376
+ }
377
+ return ivRounded;
378
+ };
379
+ if (iv.charAt(0) === '-') { /** Checks if the iv (input Value)is a negative value */
380
+ nSign = '-';
381
+ iv = iv.replace('-', ''); /** removes the negative sign will be added back later if required */
382
+ }
383
+ if (!iv.match(/^\d/)) { /** append a zero if first character is not a digit (then it is likely to be a dot)*/
384
+ iv = '0' + iv;
385
+ }
386
+ if (nSign === '-' && +iv === 0) { /** determines if the value is zero - if zero no negative sign */
387
+ nSign = '';
388
+ }
389
+ if ((+iv > 0 && settings.lZero !== 'keep') || (iv.length > 0 && settings.lZero === 'allow')) { /** trims leading zero's if needed */
390
+ iv = iv.replace(/^0*(\d)/, '$1');
391
+ }
392
+ var dPos = iv.lastIndexOf('.'), /** virtual decimal position */
393
+ vdPos = (dPos === -1) ? iv.length - 1 : dPos, /** checks decimal places to determine if rounding is required */
394
+ cDec = (iv.length - 1) - vdPos; /** check if no rounding is required */
395
+ if (cDec <= settings.mDec) {
396
+ ivRounded = iv; /** check if we need to pad with zeros */
397
+ if (cDec < rDec) {
398
+ if (dPos === -1) {
399
+ ivRounded += '.';
400
+ }
401
+ var zeros = '000000';
402
+ while (cDec < rDec) {
403
+ zeros = zeros.substring(0, rDec - cDec);
404
+ ivRounded += zeros;
405
+ cDec += zeros.length;
406
+ }
407
+ } else if (cDec > rDec) {
408
+ ivRounded = truncateZeros(ivRounded);
409
+ } else if (cDec === 0 && rDec === 0) {
410
+ ivRounded = ivRounded.replace(/\.$/, '');
411
+ }
412
+ if (settings.mRound !== 'CHF') {
413
+ return (+ivRounded === 0) ? ivRounded : nSign + ivRounded;
414
+ }
415
+ if (settings.mRound === 'CHF') {
416
+ dPos = ivRounded.lastIndexOf('.');
417
+ iv = ivRounded;
418
+ }
419
+
420
+ } /** rounded length of the string after rounding */
421
+ var rLength = dPos + settings.mDec,
422
+ tRound = +iv.charAt(rLength + 1),
423
+ ivArray = iv.substring(0, rLength + 1).split(''),
424
+ odd = (iv.charAt(rLength) === '.') ? (iv.charAt(rLength - 1) % 2) : (iv.charAt(rLength) % 2),
425
+ onePass = true;
426
+ if (odd !== 1) {
427
+ odd = (odd === 0 && (iv.substring(rLength + 2, iv.length) > 0)) ? 1 : 0;
428
+ }
429
+ if ((tRound > 4 && settings.mRound === 'S') || /** Round half up symmetric */
430
+ (tRound > 4 && settings.mRound === 'A' && nSign === '') || /** Round half up asymmetric positive values */
431
+ (tRound > 5 && settings.mRound === 'A' && nSign === '-') || /** Round half up asymmetric negative values */
432
+ (tRound > 5 && settings.mRound === 's') || /** Round half down symmetric */
433
+ (tRound > 5 && settings.mRound === 'a' && nSign === '') || /** Round half down asymmetric positive values */
434
+ (tRound > 4 && settings.mRound === 'a' && nSign === '-') || /** Round half down asymmetric negative values */
435
+ (tRound > 5 && settings.mRound === 'B') || /** Round half even "Banker's Rounding" */
436
+ (tRound === 5 && settings.mRound === 'B' && odd === 1) || /** Round half even "Banker's Rounding" */
437
+ (tRound > 0 && settings.mRound === 'C' && nSign === '') || /** Round to ceiling toward positive infinite */
438
+ (tRound > 0 && settings.mRound === 'F' && nSign === '-') || /** Round to floor toward negative infinite */
439
+ (tRound > 0 && settings.mRound === 'U') ||
440
+ (settings.mRound === 'CHF')) { /** round up away from zero */
441
+ for (i = (ivArray.length - 1); i >= 0; i -= 1) { /** Round up the last digit if required, and continue until no more 9's are found */
442
+ if (ivArray[i] !== '.') {
443
+ if (settings.mRound === 'CHF' && ivArray[i] <= 2 && onePass) {
444
+ ivArray[i] = 0;
445
+ onePass = false;
446
+ break;
447
+ }
448
+ if (settings.mRound === 'CHF' && ivArray[i] <= 7 && onePass) {
449
+ ivArray[i] = 5;
450
+ onePass = false;
451
+ break;
452
+ }
453
+ if (settings.mRound === 'CHF' && onePass) {
454
+ ivArray[i] = 10;
455
+ onePass = false;
456
+ } else {
457
+ ivArray[i] = +ivArray[i] + 1;
458
+ }
459
+ if (ivArray[i] < 10) {
460
+ break;
461
+ }
462
+ if (i > 0) {
463
+ ivArray[i] = '0';
464
+ }
465
+ }
466
+ }
467
+ }
468
+ ivArray = ivArray.slice(0, rLength + 1); /** Reconstruct the string, converting any 10's to 0's */
469
+ ivRounded = truncateZeros(ivArray.join('')); /** return rounded value */
470
+ return (+ivRounded === 0) ? ivRounded : nSign + ivRounded;
471
+ }
472
+ /**
473
+ * Holder object for field properties
474
+ */
475
+ function AutoNumericHolder(that, settings) {
476
+ this.settings = settings;
477
+ this.that = that;
478
+ this.$that = $(that);
479
+ this.formatted = false;
480
+ this.settingsClone = autoCode(this.$that, this.settings);
481
+ this.value = that.value;
482
+ }
483
+ AutoNumericHolder.prototype = {
484
+ init: function (e) {
485
+ this.value = this.that.value;
486
+ this.settingsClone = autoCode(this.$that, this.settings);
487
+ this.ctrlKey = e.ctrlKey;
488
+ this.cmdKey = e.metaKey;
489
+ this.shiftKey = e.shiftKey;
490
+ this.selection = getElementSelection(this.that); /** keypress event overwrites meaningful value of e.keyCode */
491
+ if (e.type === 'keydown' || e.type === 'keyup') {
492
+ this.kdCode = e.keyCode;
493
+ }
494
+ this.which = e.which;
495
+ this.processed = false;
496
+ this.formatted = false;
497
+ },
498
+ setSelection: function (start, end, setReal) {
499
+ start = Math.max(start, 0);
500
+ end = Math.min(end, this.that.value.length);
501
+ this.selection = {
502
+ start: start,
503
+ end: end,
504
+ length: end - start
505
+ };
506
+ if (setReal === undefined || setReal) {
507
+ setElementSelection(this.that, start, end);
508
+ }
509
+ },
510
+ setPosition: function (pos, setReal) {
511
+ this.setSelection(pos, pos, setReal);
512
+ },
513
+ getBeforeAfter: function () {
514
+ var value = this.value,
515
+ left = value.substring(0, this.selection.start),
516
+ right = value.substring(this.selection.end, value.length);
517
+ return [left, right];
518
+ },
519
+ getBeforeAfterStriped: function () {
520
+ var parts = this.getBeforeAfter();
521
+ parts[0] = autoStrip(parts[0], this.settingsClone);
522
+ parts[1] = autoStrip(parts[1], this.settingsClone);
523
+ return parts;
524
+ },
525
+ /**
526
+ * strip parts from excess characters and leading zeroes
527
+ */
528
+ normalizeParts: function (left, right) {
529
+ var settingsClone = this.settingsClone;
530
+ right = autoStrip(right, settingsClone); /** if right is not empty and first character is not aDec, */
531
+ /** we could strip all zeros, otherwise only leading */
532
+ var strip = right.match(/^\d/) ? true : 'leading';
533
+ left = autoStrip(left, settingsClone, strip); /** prevents multiple leading zeros from being entered */
534
+ if ((left === '' || left === settingsClone.aNeg) && settingsClone.lZero === 'deny') {
535
+ if (right > '') {
536
+ right = right.replace(/^0*(\d)/, '$1');
537
+ }
538
+ }
539
+ var new_value = left + right; /** insert zero if has leading dot */
540
+ if (settingsClone.aDec) {
541
+ var m = new_value.match(new RegExp('^' + settingsClone.aNegRegAutoStrip + '\\' + settingsClone.aDec));
542
+ if (m) {
543
+ left = left.replace(m[1], m[1] + '0');
544
+ new_value = left + right;
545
+ }
546
+ } /** insert zero if number is empty and io.wEmpty == 'zero' */
547
+ if (settingsClone.wEmpty === 'zero' && (new_value === settingsClone.aNeg || new_value === '')) {
548
+ left += '0';
549
+ }
550
+ return [left, right];
551
+ },
552
+ /**
553
+ * set part of number to value keeping position of cursor
554
+ */
555
+ setValueParts: function (left, right) {
556
+ var settingsClone = this.settingsClone,
557
+ parts = this.normalizeParts(left, right),
558
+ new_value = parts.join(''),
559
+ position = parts[0].length;
560
+ if (autoCheck(new_value, settingsClone)) {
561
+ new_value = truncateDecimal(new_value, settingsClone.aDec, settingsClone.mDec);
562
+ if (position > new_value.length) {
563
+ position = new_value.length;
564
+ }
565
+ this.value = new_value;
566
+ this.setPosition(position, false);
567
+ return true;
568
+ }
569
+ return false;
570
+ },
571
+ /**
572
+ * helper function for expandSelectionOnSign
573
+ * returns sign position of a formatted value
574
+ */
575
+ signPosition: function () {
576
+ var settingsClone = this.settingsClone,
577
+ aSign = settingsClone.aSign,
578
+ that = this.that;
579
+ if (aSign) {
580
+ var aSignLen = aSign.length;
581
+ if (settingsClone.pSign === 'p') {
582
+ var hasNeg = settingsClone.aNeg && that.value && that.value.charAt(0) === settingsClone.aNeg;
583
+ return hasNeg ? [1, aSignLen + 1] : [0, aSignLen];
584
+ }
585
+ var valueLen = that.value.length;
586
+ return [valueLen - aSignLen, valueLen];
587
+ }
588
+ return [1000, -1];
589
+ },
590
+ /**
591
+ * expands selection to cover whole sign
592
+ * prevents partial deletion/copying/overwriting of a sign
593
+ */
594
+ expandSelectionOnSign: function (setReal) {
595
+ var sign_position = this.signPosition(),
596
+ selection = this.selection;
597
+ if (selection.start < sign_position[1] && selection.end > sign_position[0]) { /** if selection catches something except sign and catches only space from sign */
598
+ if ((selection.start < sign_position[0] || selection.end > sign_position[1]) && this.value.substring(Math.max(selection.start, sign_position[0]), Math.min(selection.end, sign_position[1])).match(/^\s*$/)) { /** then select without empty space */
599
+ if (selection.start < sign_position[0]) {
600
+ this.setSelection(selection.start, sign_position[0], setReal);
601
+ } else {
602
+ this.setSelection(sign_position[1], selection.end, setReal);
603
+ }
604
+ } else { /** else select with whole sign */
605
+ this.setSelection(Math.min(selection.start, sign_position[0]), Math.max(selection.end, sign_position[1]), setReal);
606
+ }
607
+ }
608
+ },
609
+ /**
610
+ * try to strip pasted value to digits
611
+ */
612
+ checkPaste: function () {
613
+ if (this.valuePartsBeforePaste !== undefined) {
614
+ var parts = this.getBeforeAfter(),
615
+ oldParts = this.valuePartsBeforePaste;
616
+ delete this.valuePartsBeforePaste; /** try to strip pasted value first */
617
+ parts[0] = parts[0].substr(0, oldParts[0].length) + autoStrip(parts[0].substr(oldParts[0].length), this.settingsClone);
618
+ if (!this.setValueParts(parts[0], parts[1])) {
619
+ this.value = oldParts.join('');
620
+ this.setPosition(oldParts[0].length, false);
621
+ }
622
+ }
623
+ },
624
+ /**
625
+ * process pasting, cursor moving and skipping of not interesting keys
626
+ * if returns true, further processing is not performed
627
+ */
628
+ skipAllways: function (e) {
629
+ var kdCode = this.kdCode,
630
+ which = this.which,
631
+ ctrlKey = this.ctrlKey,
632
+ cmdKey = this.cmdKey,
633
+ shiftKey = this.shiftKey; /** catch the ctrl up on ctrl-v */
634
+ if (((ctrlKey || cmdKey) && e.type === 'keyup' && this.valuePartsBeforePaste !== undefined) || (shiftKey && kdCode === 45)) {
635
+ this.checkPaste();
636
+ return false;
637
+ }
638
+ /** codes are taken from http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx
639
+ * skip Fx keys, windows keys, other special keys
640
+ */
641
+ if ((kdCode >= 112 && kdCode <= 123) || (kdCode >= 91 && kdCode <= 93) || (kdCode >= 9 && kdCode <= 31) || (kdCode < 8 && (which === 0 || which === kdCode)) || kdCode === 144 || kdCode === 145 || kdCode === 45) {
642
+ return true;
643
+ }
644
+ if ((ctrlKey || cmdKey) && kdCode === 65) { /** if select all (a=65)*/
645
+ return true;
646
+ }
647
+ if ((ctrlKey || cmdKey) && (kdCode === 67 || kdCode === 86 || kdCode === 88)) { /** if copy (c=67) paste (v=86) or cut (x=88) */
648
+ if (e.type === 'keydown') {
649
+ this.expandSelectionOnSign();
650
+ }
651
+ if (kdCode === 86 || kdCode === 45) { /** try to prevent wrong paste */
652
+ if (e.type === 'keydown' || e.type === 'keypress') {
653
+ if (this.valuePartsBeforePaste === undefined) {
654
+ this.valuePartsBeforePaste = this.getBeforeAfter();
655
+ }
656
+ } else {
657
+ this.checkPaste();
658
+ }
659
+ }
660
+ return e.type === 'keydown' || e.type === 'keypress' || kdCode === 67;
661
+ }
662
+ if (ctrlKey || cmdKey) {
663
+ return true;
664
+ }
665
+ if (kdCode === 37 || kdCode === 39) { /** jump over thousand separator */
666
+ var aSep = this.settingsClone.aSep,
667
+ start = this.selection.start,
668
+ value = this.that.value;
669
+ if (e.type === 'keydown' && aSep && !this.shiftKey) {
670
+ if (kdCode === 37 && value.charAt(start - 2) === aSep) {
671
+ this.setPosition(start - 1);
672
+ } else if (kdCode === 39 && value.charAt(start + 1) === aSep) {
673
+ this.setPosition(start + 1);
674
+ }
675
+ }
676
+ return true;
677
+ }
678
+ if (kdCode >= 34 && kdCode <= 40) {
679
+ return true;
680
+ }
681
+ return false;
682
+ },
683
+ /**
684
+ * process deletion of characters
685
+ * returns true if processing performed
686
+ */
687
+ processAllways: function () {
688
+ var parts; /** process backspace or delete */
689
+ if (this.kdCode === 8 || this.kdCode === 46) {
690
+ if (!this.selection.length) {
691
+ parts = this.getBeforeAfterStriped();
692
+ if (this.kdCode === 8) {
693
+ parts[0] = parts[0].substring(0, parts[0].length - 1);
694
+ } else {
695
+ parts[1] = parts[1].substring(1, parts[1].length);
696
+ }
697
+ this.setValueParts(parts[0], parts[1]);
698
+ } else {
699
+ this.expandSelectionOnSign(false);
700
+ parts = this.getBeforeAfterStriped();
701
+ this.setValueParts(parts[0], parts[1]);
702
+ }
703
+ return true;
704
+ }
705
+ return false;
706
+ },
707
+ /**
708
+ * process insertion of characters
709
+ * returns true if processing performed
710
+ */
711
+ processKeypress: function () {
712
+ var settingsClone = this.settingsClone,
713
+ cCode = String.fromCharCode(this.which),
714
+ parts = this.getBeforeAfterStriped(),
715
+ left = parts[0],
716
+ right = parts[1]; /** start rules when the decimal character key is pressed */
717
+ /** always use numeric pad dot to insert decimal separator */
718
+ if (cCode === settingsClone.aDec || (settingsClone.altDec && cCode === settingsClone.altDec) || ((cCode === '.' || cCode === ',') && this.kdCode === 110)) { /** do not allow decimal character if no decimal part allowed */
719
+ if (!settingsClone.mDec || !settingsClone.aDec) {
720
+ return true;
721
+ } /** do not allow decimal character before aNeg character */
722
+ if (settingsClone.aNeg && right.indexOf(settingsClone.aNeg) > -1) {
723
+ return true;
724
+ } /** do not allow decimal character if other decimal character present */
725
+ if (left.indexOf(settingsClone.aDec) > -1) {
726
+ return true;
727
+ }
728
+ if (right.indexOf(settingsClone.aDec) > 0) {
729
+ return true;
730
+ }
731
+ if (right.indexOf(settingsClone.aDec) === 0) {
732
+ right = right.substr(1);
733
+ }
734
+ this.setValueParts(left + settingsClone.aDec, right);
735
+ return true;
736
+ }
737
+ /**
738
+ * start rule on negative sign & prevent minus if not allowed
739
+ */
740
+ if (cCode === '-' || cCode === '+') {
741
+ if (!settingsClone.aNeg) {
742
+ return true;
743
+ } /** caret is always after minus */
744
+ if (left === '' && right.indexOf(settingsClone.aNeg) > -1) {
745
+ left = settingsClone.aNeg;
746
+ right = right.substring(1, right.length);
747
+ } /** change sign of number, remove part if should */
748
+ if (left.charAt(0) === settingsClone.aNeg) {
749
+ left = left.substring(1, left.length);
750
+ } else {
751
+ left = (cCode === '-') ? settingsClone.aNeg + left : left;
752
+ }
753
+ this.setValueParts(left, right);
754
+ return true;
755
+ } /** digits */
756
+ if (cCode >= '0' && cCode <= '9') { /** if try to insert digit before minus */
757
+ if (settingsClone.aNeg && left === '' && right.indexOf(settingsClone.aNeg) > -1) {
758
+ left = settingsClone.aNeg;
759
+ right = right.substring(1, right.length);
760
+ }
761
+ if (settingsClone.vMax <= 0 && settingsClone.vMin < settingsClone.vMax && this.value.indexOf(settingsClone.aNeg) === -1 && cCode !== '0') {
762
+ left = settingsClone.aNeg + left;
763
+ }
764
+ this.setValueParts(left + cCode, right);
765
+ return true;
766
+ } /** prevent any other character */
767
+ return true;
768
+ },
769
+ /**
770
+ * formatting of just processed value with keeping of cursor position
771
+ */
772
+ formatQuick: function () {
773
+ var settingsClone = this.settingsClone,
774
+ parts = this.getBeforeAfterStriped(),
775
+ leftLength = this.value;
776
+ if ((settingsClone.aSep === '' || (settingsClone.aSep !== '' && leftLength.indexOf(settingsClone.aSep) === -1)) && (settingsClone.aSign === '' || (settingsClone.aSign !== '' && leftLength.indexOf(settingsClone.aSign) === -1))) {
777
+ var subParts = [],
778
+ nSign = '';
779
+ subParts = leftLength.split(settingsClone.aDec);
780
+ if (subParts[0].indexOf('-') > -1) {
781
+ nSign = '-';
782
+ subParts[0] = subParts[0].replace('-', '');
783
+ parts[0] = parts[0].replace('-', '');
784
+ }
785
+ if (subParts[0].length > settingsClone.mInt && parts[0].charAt(0) === '0') { /** strip leading zero if need */
786
+ parts[0] = parts[0].slice(1);
787
+ }
788
+ parts[0] = nSign + parts[0];
789
+ }
790
+ var value = autoGroup(this.value, this.settingsClone),
791
+ position = value.length;
792
+ if (value) {
793
+ /** prepare regexp which searches for cursor position from unformatted left part */
794
+ var left_ar = parts[0].split(''),
795
+ i = 0;
796
+ for (i; i < left_ar.length; i += 1) { /** thanks Peter Kovari */
797
+ if (!left_ar[i].match('\\d')) {
798
+ left_ar[i] = '\\' + left_ar[i];
799
+ }
800
+ }
801
+ var leftReg = new RegExp('^.*?' + left_ar.join('.*?'));
802
+ /** search cursor position in formatted value */
803
+ var newLeft = value.match(leftReg);
804
+ if (newLeft) {
805
+ position = newLeft[0].length;
806
+ /** if we are just before sign which is in prefix position */
807
+ if (((position === 0 && value.charAt(0) !== settingsClone.aNeg) || (position === 1 && value.charAt(0) === settingsClone.aNeg)) && settingsClone.aSign && settingsClone.pSign === 'p') {
808
+ /** place caret after prefix sign */
809
+ position = this.settingsClone.aSign.length + (value.charAt(0) === '-' ? 1 : 0);
810
+ }
811
+ } else if (settingsClone.aSign && settingsClone.pSign === 's') {
812
+ /** if we could not find a place for cursor and have a sign as a suffix */
813
+ /** place carret before suffix currency sign */
814
+ position -= settingsClone.aSign.length;
815
+ }
816
+ }
817
+ this.that.value = value;
818
+ this.setPosition(position);
819
+ this.formatted = true;
820
+ }
821
+ };
822
+ /** thanks to Anthony & Evan C */
823
+ function autoGet(obj) {
824
+ if (typeof obj === 'string') {
825
+ obj = obj.replace(/\[/g, "\\[").replace(/\]/g, "\\]");
826
+ obj = '#' + obj.replace(/(:|\.)/g, '\\$1');
827
+ /** obj = '#' + obj.replace(/([;&,\.\+\*\~':"\!\^#$%@\[\]\(\)=>\|])/g, '\\$1'); */
828
+ /** possible modification to replace the above 2 lines */
829
+ }
830
+ return $(obj);
831
+ }
832
+
833
+ function getHolder($that, settings, update) {
834
+ var data = $that.data('autoNumeric');
835
+ if (!data) {
836
+ data = {};
837
+ $that.data('autoNumeric', data);
838
+ }
839
+ var holder = data.holder;
840
+ if ((holder === undefined && settings) || update) {
841
+ holder = new AutoNumericHolder($that.get(0), settings);
842
+ data.holder = holder;
843
+ }
844
+ return holder;
845
+ }
846
+ var methods = {
847
+ init: function (options) {
848
+ return this.each(function () {
849
+ var $this = $(this),
850
+ settings = $this.data('autoNumeric'), /** attempt to grab 'autoNumeric' settings, if they don't exist returns "undefined". */
851
+ tagData = $this.data(); /** attempt to grab HTML5 data, if they don't exist we'll get "undefined".*/
852
+ if (typeof settings !== 'object') { /** If we couldn't grab settings, create them from defaults and passed options. */
853
+ var defaults = {
854
+ /** allowed numeric values
855
+ * please do not modify
856
+ */
857
+ aNum: '0123456789',
858
+ /** allowed thousand separator characters
859
+ * comma = ','
860
+ * period "full stop" = '.'
861
+ * apostrophe is escaped = '\''
862
+ * space = ' '
863
+ * none = ''
864
+ * NOTE: do not use numeric characters
865
+ */
866
+ aSep: ',',
867
+ /** digital grouping for the thousand separator used in Format
868
+ * dGroup: '2', results in 99,99,99,999 common in India for values less than 1 billion and greater than -1 billion
869
+ * dGroup: '3', results in 999,999,999 default
870
+ * dGroup: '4', results in 9999,9999,9999 used in some Asian countries
871
+ */
872
+ dGroup: '3',
873
+ /** allowed decimal separator characters
874
+ * period "full stop" = '.'
875
+ * comma = ','
876
+ */
877
+ aDec: '.',
878
+ /** allow to declare alternative decimal separator which is automatically replaced by aDec
879
+ * developed for countries the use a comma ',' as the decimal character
880
+ * and have keyboards\numeric pads that have a period 'full stop' as the decimal characters (Spain is an example)
881
+ */
882
+ altDec: null,
883
+ /** allowed currency symbol
884
+ * Must be in quotes aSign: '$', a space is allowed aSign: '$ '
885
+ */
886
+ aSign: '',
887
+ /** placement of currency sign
888
+ * for prefix pSign: 'p',
889
+ * for suffix pSign: 's',
890
+ */
891
+ pSign: 'p',
892
+ /** maximum possible value
893
+ * value must be enclosed in quotes and use the period for the decimal point
894
+ * value must be larger than vMin
895
+ */
896
+ vMax: '9999999999999.99',
897
+ /** minimum possible value
898
+ * value must be enclosed in quotes and use the period for the decimal point
899
+ * value must be smaller than vMax
900
+ */
901
+ vMin: '0.00',
902
+ /** max number of decimal places = used to override decimal places set by the vMin & vMax values
903
+ * value must be enclosed in quotes example mDec: '3',
904
+ * This can also set the value via a call back function mDec: 'css:#
905
+ */
906
+ mDec: null,
907
+ /** method used for rounding
908
+ * mRound: 'S', Round-Half-Up Symmetric (default)
909
+ * mRound: 'A', Round-Half-Up Asymmetric
910
+ * mRound: 's', Round-Half-Down Symmetric (lower case s)
911
+ * mRound: 'a', Round-Half-Down Asymmetric (lower case a)
912
+ * mRound: 'B', Round-Half-Even "Bankers Rounding"
913
+ * mRound: 'U', Round Up "Round-Away-From-Zero"
914
+ * mRound: 'D', Round Down "Round-Toward-Zero" - same as truncate
915
+ * mRound: 'C', Round to Ceiling "Toward Positive Infinity"
916
+ * mRound: 'F', Round to Floor "Toward Negative Infinity"
917
+ */
918
+ mRound: 'S',
919
+ /** controls decimal padding
920
+ * aPad: true - always Pad decimals with zeros
921
+ * aPad: false - does not pad with zeros.
922
+ * aPad: `some number` - pad decimals with zero to number different from mDec
923
+ * thanks to Jonas Johansson for the suggestion
924
+ */
925
+ aPad: true,
926
+ /** places brackets on negative value -$ 999.99 to (999.99)
927
+ * visible only when the field does NOT have focus the left and right symbols should be enclosed in quotes and seperated by a comma
928
+ * nBracket: null, nBracket: '(,)', nBracket: '[,]', nBracket: '<,>' or nBracket: '{,}'
929
+ */
930
+ nBracket: null,
931
+ /** Displayed on empty string
932
+ * wEmpty: 'empty', - input can be blank
933
+ * wEmpty: 'zero', - displays zero
934
+ * wEmpty: 'sign', - displays the currency sign
935
+ */
936
+ wEmpty: 'empty',
937
+ /** controls leading zero behavior
938
+ * lZero: 'allow', - allows leading zeros to be entered. Zeros will be truncated when entering additional digits. On focusout zeros will be deleted.
939
+ * lZero: 'deny', - allows only one leading zero on values less than one
940
+ * lZero: 'keep', - allows leading zeros to be entered. on fousout zeros will be retained.
941
+ */
942
+ lZero: 'allow',
943
+ /** determine if the default value will be formatted on page ready.
944
+ * true = automatically formats the default value on page ready
945
+ * false = will not format the default value
946
+ */
947
+ aForm: true,
948
+ /** future use */
949
+ onSomeEvent: function () {}
950
+ };
951
+ settings = $.extend({}, defaults, tagData, options); /** Merge defaults, tagData and options */
952
+ if (settings.aDec === settings.aSep) {
953
+ $.error("autoNumeric will not function properly when the decimal character aDec: '" + settings.aDec + "' and thousand separator aSep: '" + settings.aSep + "' are the same character");
954
+ return this;
955
+ }
956
+ $this.data('autoNumeric', settings); /** Save our new settings */
957
+ } else {
958
+ return this;
959
+ }
960
+ settings.runOnce = false;
961
+ var holder = getHolder($this, settings);
962
+ if ($.inArray($this.prop('tagName').toLowerCase(), settings.tagList) === -1 && $this.prop('tagName').toLowerCase() !== 'input') {
963
+ $.error("The <" + $this.prop('tagName').toLowerCase() + "> is not supported by autoNumeric()");
964
+ return this;
965
+ }
966
+ if (settings.runOnce === false && settings.aForm) {/** routine to format default value on page load */
967
+ if ($this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])')) {
968
+ var setValue = true;
969
+ if ($this[0].value === '' && settings.wEmpty === 'empty') {
970
+ $this[0].value = '';
971
+ setValue = false;
972
+ }
973
+ if ($this[0].value === '' && settings.wEmpty === 'sign') {
974
+ $this[0].value = settings.aSign;
975
+ setValue = false;
976
+ }
977
+ if (setValue) {
978
+ $this.autoNumeric('set', $this.val());
979
+ }
980
+ }
981
+ if ($.inArray($this.prop('tagName').toLowerCase(), settings.tagList) !== -1 && $this.text() !== '') {
982
+ $this.autoNumeric('set', $this.text());
983
+ }
984
+ }
985
+ settings.runOnce = true;
986
+ if ($this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])')) { /**added hidden type */
987
+ $this.on('keydown.autoNumeric', function (e) {
988
+ holder = getHolder($this);
989
+ if (holder.settings.aDec === holder.settings.aSep) {
990
+ $.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");
991
+ return this;
992
+ }
993
+ if (holder.that.readOnly) {
994
+ holder.processed = true;
995
+ return true;
996
+ }
997
+ /** The below streamed code / comment allows the "enter" keydown to throw a change() event */
998
+ /** if (e.keyCode === 13 && holder.inVal !== $this.val()){
999
+ $this.change();
1000
+ holder.inVal = $this.val();
1001
+ }*/
1002
+ holder.init(e);
1003
+ holder.settings.oEvent = 'keydown';
1004
+ if (holder.skipAllways(e)) {
1005
+ holder.processed = true;
1006
+ return true;
1007
+ }
1008
+ if (holder.processAllways()) {
1009
+ holder.processed = true;
1010
+ holder.formatQuick();
1011
+ e.preventDefault();
1012
+ return false;
1013
+ }
1014
+ holder.formatted = false;
1015
+ return true;
1016
+ });
1017
+ $this.on('keypress.autoNumeric', function (e) {
1018
+ var holder = getHolder($this),
1019
+ processed = holder.processed;
1020
+ holder.init(e);
1021
+ holder.settings.oEvent = 'keypress';
1022
+ if (holder.skipAllways(e)) {
1023
+ return true;
1024
+ }
1025
+ if (processed) {
1026
+ e.preventDefault();
1027
+ return false;
1028
+ }
1029
+ if (holder.processAllways() || holder.processKeypress()) {
1030
+ holder.formatQuick();
1031
+ e.preventDefault();
1032
+ return false;
1033
+ }
1034
+ holder.formatted = false;
1035
+ });
1036
+ $this.on('keyup.autoNumeric', function (e) {
1037
+ var holder = getHolder($this);
1038
+ holder.init(e);
1039
+ holder.settings.oEvent = 'keyup';
1040
+ var skip = holder.skipAllways(e);
1041
+ holder.kdCode = 0;
1042
+ delete holder.valuePartsBeforePaste;
1043
+ if ($this[0].value === holder.settings.aSign) { /** added to properly place the caret when only the currency is present */
1044
+ if (holder.settings.pSign === 's') {
1045
+ setElementSelection(this, 0, 0);
1046
+ } else {
1047
+ setElementSelection(this, holder.settings.aSign.length, holder.settings.aSign.length);
1048
+ }
1049
+ }
1050
+ if (skip) {
1051
+ return true;
1052
+ }
1053
+ if (this.value === '') {
1054
+ return true;
1055
+ }
1056
+ if (!holder.formatted) {
1057
+ holder.formatQuick();
1058
+ }
1059
+ });
1060
+ $this.on('focusin.autoNumeric', function () {
1061
+ var holder = getHolder($this);
1062
+ holder.settingsClone.oEvent = 'focusin';
1063
+ if (holder.settingsClone.nBracket !== null) {
1064
+ var checkVal = $this.val();
1065
+ $this.val(negativeBracket(checkVal, holder.settingsClone.nBracket, holder.settingsClone.oEvent));
1066
+ }
1067
+ holder.inVal = $this.val();
1068
+ var onempty = checkEmpty(holder.inVal, holder.settingsClone, true);
1069
+ if (onempty !== null) {
1070
+ $this.val(onempty);
1071
+ if (holder.settings.pSign === 's') {
1072
+ setElementSelection(this, 0, 0);
1073
+ } else {
1074
+ setElementSelection(this, holder.settings.aSign.length, holder.settings.aSign.length);
1075
+ }
1076
+ }
1077
+ });
1078
+ $this.on('focusout.autoNumeric', function () {
1079
+ var holder = getHolder($this),
1080
+ settingsClone = holder.settingsClone,
1081
+ value = $this.val(),
1082
+ origValue = value;
1083
+ holder.settingsClone.oEvent = 'focusout';
1084
+ var strip_zero = ''; /** added to control leading zero */
1085
+ if (settingsClone.lZero === 'allow') { /** added to control leading zero */
1086
+ settingsClone.allowLeading = false;
1087
+ strip_zero = 'leading';
1088
+ }
1089
+ if (value !== '') {
1090
+ value = autoStrip(value, settingsClone, strip_zero);
1091
+ if (checkEmpty(value, settingsClone) === null && autoCheck(value, settingsClone, $this[0])) {
1092
+ value = fixNumber(value, settingsClone.aDec, settingsClone.aNeg);
1093
+ value = autoRound(value, settingsClone);
1094
+ value = presentNumber(value, settingsClone.aDec, settingsClone.aNeg);
1095
+ } else {
1096
+ value = '';
1097
+ }
1098
+ }
1099
+ var groupedValue = checkEmpty(value, settingsClone, false);
1100
+ if (groupedValue === null) {
1101
+ groupedValue = autoGroup(value, settingsClone);
1102
+ }
1103
+ if (groupedValue !== origValue) {
1104
+ $this.val(groupedValue);
1105
+ }
1106
+ if (groupedValue !== holder.inVal) {
1107
+ $this.change();
1108
+ delete holder.inVal;
1109
+ }
1110
+ if (settingsClone.nBracket !== null && $this.autoNumeric('get') < 0) {
1111
+ holder.settingsClone.oEvent = 'focusout';
1112
+ $this.val(negativeBracket($this.val(), settingsClone.nBracket, settingsClone.oEvent));
1113
+ }
1114
+ });
1115
+ }
1116
+ });
1117
+ },
1118
+ /** method to remove settings and stop autoNumeric() */
1119
+ destroy: function () {
1120
+ return $(this).each(function () {
1121
+ var $this = $(this);
1122
+ $this.off('.autoNumeric');
1123
+ $this.removeData('autoNumeric');
1124
+ });
1125
+ },
1126
+ /** method to update settings - can call as many times */
1127
+ update: function (options) {
1128
+ return $(this).each(function () {
1129
+ var $this = autoGet($(this)),
1130
+ settings = $this.data('autoNumeric');
1131
+ if (typeof settings !== 'object') {
1132
+ $.error("You must initialize autoNumeric('init', {options}) prior to calling the 'update' method");
1133
+ return this;
1134
+ }
1135
+ var strip = $this.autoNumeric('get');
1136
+ settings = $.extend(settings, options);
1137
+ getHolder($this, settings, true);
1138
+ if (settings.aDec === settings.aSep) {
1139
+ $.error("autoNumeric will not function properly when the decimal character aDec: '" + settings.aDec + "' and thousand separator aSep: '" + settings.aSep + "' are the same character");
1140
+ return this;
1141
+ }
1142
+ $this.data('autoNumeric', settings);
1143
+ if ($this.val() !== '' || $this.text() !== '') {
1144
+ return $this.autoNumeric('set', strip);
1145
+ }
1146
+ return;
1147
+ });
1148
+ },
1149
+ /** returns a formatted strings for "input:text" fields Uses jQuery's .val() method*/
1150
+ set: function (valueIn) {
1151
+ if (valueIn === null) {
1152
+ return;
1153
+ }
1154
+ return $(this).each(function () {
1155
+ var $this = autoGet($(this)),
1156
+ settings = $this.data('autoNumeric'),
1157
+ value = valueIn.toString(),
1158
+ testValue = valueIn.toString();
1159
+ if (typeof settings !== 'object') {
1160
+ $.error("You must initialize autoNumeric('init', {options}) prior to calling the 'set' method");
1161
+ return this;
1162
+ }
1163
+ /** routine to handle page re-load from back button */
1164
+ if (testValue !== $this.attr('value') && $this.prop('tagName').toLowerCase() === 'input' && settings.runOnce === false) {
1165
+ value = (settings.nBracket !== null) ? negativeBracket($this.val(), settings.nBracket, 'pageLoad') : value;
1166
+ value = autoStrip(value, settings);
1167
+ }
1168
+ /** allows locale decimal separator to be a comma */
1169
+ if ((testValue === $this.attr('value') || testValue === $this.text()) && settings.runOnce === false) {
1170
+ value = value.replace(',', '.');
1171
+ }
1172
+ /** returns a empty string if the value being 'set' contains non-numeric characters and or more than decimal point (full stop) and will not be formatted */
1173
+ if (!$.isNumeric(+value)) {
1174
+ return '';
1175
+ }
1176
+ value = checkValue(value, settings);
1177
+ settings.oEvent = 'set';
1178
+ value.toString();
1179
+ if (value !== '') {
1180
+ value = autoRound(value, settings);
1181
+ }
1182
+ value = presentNumber(value, settings.aDec, settings.aNeg);
1183
+ if (!autoCheck(value, settings)) {
1184
+ value = autoRound('', settings);
1185
+ }
1186
+ value = autoGroup(value, settings);
1187
+ if ($this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])')) { /**added hidden type */
1188
+ return $this.val(value);
1189
+ }
1190
+ if ($.inArray($this.prop('tagName').toLowerCase(), settings.tagList) !== -1) {
1191
+ return $this.text(value);
1192
+ }
1193
+ $.error("The <" + $this.prop('tagName').toLowerCase() + "> is not supported by autoNumeric()");
1194
+ return false;
1195
+ });
1196
+ },
1197
+ /** method to get the unformatted value from a specific input field, returns a numeric value */
1198
+ get: function () {
1199
+ var $this = autoGet($(this)),
1200
+ settings = $this.data('autoNumeric');
1201
+ if (typeof settings !== 'object') {
1202
+ $.error("You must initialize autoNumeric('init', {options}) prior to calling the 'get' method");
1203
+ return this;
1204
+ }
1205
+ settings.oEvent = 'get';
1206
+ var getValue = '';
1207
+ /** determine the element type then use .eq(0) selector to grab the value of the first element in selector */
1208
+ if ($this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])')) { /**added hidden type */
1209
+ getValue = $this.eq(0).val();
1210
+ } else if ($.inArray($this.prop('tagName').toLowerCase(), settings.tagList) !== -1) {
1211
+ getValue = $this.eq(0).text();
1212
+ } else {
1213
+ $.error("The <" + $this.prop('tagName').toLowerCase() + "> is not supported by autoNumeric()");
1214
+ return false;
1215
+ }
1216
+ if ((getValue === '' && settings.wEmpty === 'empty') || (getValue === settings.aSign && (settings.wEmpty === 'sign' || settings.wEmpty === 'empty'))) {
1217
+ return '';
1218
+ }
1219
+ if (settings.nBracket !== null && getValue !== '') {
1220
+ getValue = negativeBracket(getValue, settings.nBracket, settings.oEvent);
1221
+ }
1222
+ if (settings.runOnce || settings.aForm === false) {
1223
+ getValue = autoStrip(getValue, settings);
1224
+ }
1225
+ getValue = fixNumber(getValue, settings.aDec, settings.aNeg);
1226
+ if (+getValue === 0 && settings.lZero !== 'keep') {
1227
+ getValue = '0';
1228
+ }
1229
+ if (settings.lZero === 'keep') {
1230
+ return getValue;
1231
+ }
1232
+ getValue = checkValue(getValue, settings);
1233
+ return getValue; /** returned Numeric String */
1234
+ },
1235
+ /** method to get the unformatted value from multiple fields */
1236
+ getString: function () {
1237
+ var isAutoNumeric = false,
1238
+ $this = autoGet($(this)),
1239
+ str = $this.serialize(),
1240
+ parts = str.split('&'),
1241
+ i = 0;
1242
+ for (i; i < parts.length; i += 1) {
1243
+ var miniParts = parts[i].split('=');
1244
+ var settings = $('*[name="' + decodeURIComponent(miniParts[0]) + '"]').data('autoNumeric');
1245
+ if (typeof settings === 'object') {
1246
+ if (miniParts[1] !== null && $('*[name="' + decodeURIComponent(miniParts[0]) + '"]').data('autoNumeric') !== undefined) {
1247
+ miniParts[1] = $('input[name="' + decodeURIComponent(miniParts[0]) + '"]').autoNumeric('get');
1248
+ parts[i] = miniParts.join('=');
1249
+ isAutoNumeric = true;
1250
+ }
1251
+ }
1252
+ }
1253
+ if (isAutoNumeric === true) {
1254
+ return parts.join('&');
1255
+ }
1256
+ return str;
1257
+ },
1258
+ /** method to get the unformatted value from multiple fields */
1259
+ getArray: function () {
1260
+ var isAutoNumeric = false,
1261
+ $this = autoGet($(this)),
1262
+ formFields = $this.serializeArray();
1263
+ $.each(formFields, function (i, field) {
1264
+ var settings = $('*[name="' + decodeURIComponent(field.name) + '"]').data('autoNumeric');
1265
+ if (typeof settings === 'object') {
1266
+ if (field.value !== '' && $('*[name="' + decodeURIComponent(field.name) + '"]').data('autoNumeric') !== undefined) {
1267
+ field.value = $('input[name="' + decodeURIComponent(field.name) + '"]').autoNumeric('get').toString();
1268
+ }
1269
+ isAutoNumeric = true;
1270
+ }
1271
+ });
1272
+ if (isAutoNumeric === true) {
1273
+ return formFields;
1274
+ }
1275
+ $.error("You must initialize autoNumeric('init', {options}) prior to calling the 'getArray' method");
1276
+ return this;
1277
+ },
1278
+ /** returns the settings object for those who need to look under the hood */
1279
+ getSettings: function () {
1280
+ var $this = autoGet($(this));
1281
+ return $this.eq(0).data('autoNumeric');
1282
+ }
1283
+ };
1284
+ $.fn.autoNumeric = function (method) {
1285
+ if (methods[method]) {
1286
+ return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
1287
+ }
1288
+ if (typeof method === 'object' || !method) {
1289
+ return methods.init.apply(this, arguments);
1290
+ }
1291
+ $.error('Method "' + method + '" is not supported by autoNumeric()');
1292
+ };
1293
+ }(jQuery));