autonumeric-rails 1.9.46.2 → 2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/.idea/.name +1 -0
  4. data/.idea/.rakeTasks +7 -0
  5. data/.idea/autonumeric-rails.iml +171 -0
  6. data/.idea/encodings.xml +6 -0
  7. data/.idea/misc.xml +4 -0
  8. data/.idea/modules.xml +8 -0
  9. data/.idea/runConfigurations/Run_spec__autonumeric_rails_spec___autonumeric_rails.xml +35 -0
  10. data/.idea/vcs.xml +6 -0
  11. data/.idea/workspace.xml +1156 -0
  12. data/CHANGELOG.md +4 -0
  13. data/README.md +11 -3
  14. data/lib/autonumeric/rails/version.rb +1 -1
  15. data/spec/autonumeric-rails_spec.rb +8 -15
  16. data/spec/dummy/app/assets/javascripts/application.js +0 -13
  17. data/spec/dummy/app/assets/javascripts/test_version_1.js +1 -0
  18. data/spec/dummy/app/assets/javascripts/test_version_2.js +1 -0
  19. data/spec/dummy/app/controllers/forms_controller.rb +13 -0
  20. data/spec/dummy/app/views/forms/ajax_fields.html.erb +1 -1
  21. data/spec/dummy/app/views/layouts/test_version_1.html.erb +14 -0
  22. data/spec/dummy/app/views/layouts/test_version_2.html.erb +14 -0
  23. data/spec/dummy/config/application.rb +1 -11
  24. data/spec/spec_helper.rb +12 -1
  25. data/spec/support/firefox_path.rb +20 -0
  26. data/spec/{shared_examples.rb → support/shared_examples.rb} +29 -3
  27. data/spec/{wait_for_jquery.rb → support/wait_for_jquery.rb} +0 -0
  28. data/vendor/assets/javascripts/autoNumeric-2.0.js +3 -0
  29. data/vendor/assets/javascripts/autonumeric.js +1 -1
  30. data/vendor/assets/javascripts/autonumeric1.js +2 -0
  31. data/vendor/assets/javascripts/autonumeric_ujs.js +2 -2
  32. data/vendor/assets/javascripts/autonumeric_ujs1.js +94 -0
  33. metadata +29 -13
  34. data/spec/dummy/app/assets/stylesheets/application.css +0 -13
  35. data/spec/dummy/app/views/layouts/application.html.erb +0 -14
  36. data/vendor/assets/javascripts/autoNumeric-2.0-BETA.js +0 -2041
  37. data/vendor/assets/javascripts/autonumeric2.0-beta.js +0 -2
@@ -1,13 +0,0 @@
1
- /*
2
- * This is a manifest file that'll be compiled into application.css, which will include all the files
3
- * listed below.
4
- *
5
- * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
- * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
- *
8
- * You're free to add application-wide styles to this file and they'll appear at the top of the
9
- * compiled file, but it's generally better to create a new file per style scope.
10
- *
11
- *= require_self
12
- *= require_tree .
13
- */
@@ -1,14 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <title>Dummy</title>
5
- <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
- <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
- <%= csrf_meta_tags %>
8
- </head>
9
- <body>
10
-
11
- <%= yield %>
12
-
13
- </body>
14
- </html>
@@ -1,2041 +0,0 @@
1
- /**
2
- * autoNumeric.js
3
- * @author: Bob Knothe
4
- * @contributor: Sokolov Yura
5
- * @version: 2.0-beta - 2016-09-11 GMT 10:00 PM / 22:00
6
- *
7
- * Created by Robert J. Knothe on 2009-08-09. Please report any bugs to https://github.com/BobKnothe/autoNumeric
8
- *
9
- * Copyright (c) 2009 Robert J. Knothe http://www.decorplanit.com/plugin/
10
- *
11
- * The MIT License (http://www.opensource.org/licenses/mit-license.php)
12
- *
13
- * Permission is hereby granted, free of charge, to any person
14
- * obtaining a copy of this software and associated documentation
15
- * files (the "Software"), to deal in the Software without
16
- * restriction, including without limitation the rights to use,
17
- * copy, modify, merge, publish, distribute, sub license, and/or sell
18
- * copies of the Software, and to permit persons to whom the
19
- * Software is furnished to do so, subject to the following
20
- * conditions:
21
- *
22
- * The above copyright notice and this permission notice shall be
23
- * included in all copies or substantial portions of the Software.
24
- *
25
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
27
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
29
- * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
30
- * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32
- * OTHER DEALINGS IN THE SOFTWARE.
33
- */
34
- (function (factory) {
35
- if (typeof define === "function" && define.amd) {
36
- /** AMD. Register as an anonymous module. */
37
- define(["jquery"], factory);
38
- } else if (typeof module === "object" && module.exports) {
39
- /** Node/CommonJS */
40
- module.exports = factory(require("jquery"));
41
- } else {
42
- /** Browser globals */
43
- factory(window.jQuery);
44
- }
45
- }(function ($) {
46
- "use strict";
47
- /*jslint browser: true, bitwise: true*/
48
- /*global jQuery: false, sessionStorage: false*/
49
-
50
- /**
51
- * Cross browser routine for getting selected range/cursor position
52
- */
53
-
54
- function getElementSelection(that) {
55
- var position = {};
56
- if (that.selectionStart === undefined) {
57
- that.focus();
58
- var select = document.selection.createRange();
59
- position.length = select.text.length;
60
- select.moveStart('character', -that.value.length);
61
- position.end = select.text.length;
62
- position.start = position.end - position.length;
63
- } else {
64
- position.start = that.selectionStart;
65
- position.end = that.selectionEnd;
66
- position.length = position.end - position.start;
67
- }
68
- return position;
69
- }
70
-
71
- /**
72
- * Cross browser routine for setting selected range/cursor position
73
- */
74
-
75
- function setElementSelection(that, start, end) {
76
- if (that.selectionStart === undefined) {
77
- that.focus();
78
- var r = that.createTextRange();
79
- r.collapse(true);
80
- r.moveEnd('character', end);
81
- r.moveStart('character', start);
82
- r.select();
83
- } else {
84
- that.selectionStart = start;
85
- that.selectionEnd = end;
86
- }
87
- }
88
-
89
- /**
90
- * Function to handle errors messages
91
- */
92
-
93
- function throwErr(message, debug) {
94
- if (debug) {
95
- var err = new Error(message);
96
- throw err;
97
- }
98
- }
99
-
100
- /**
101
- * run callbacks in parameters if any
102
- * any parameter could be a callback:
103
- * - a function, which invoked with jQuery element, parameters and this parameter name and returns parameter value
104
- * - a name of function, attached to $(selector).autoNumeric.functionName(){} - which was called previously
105
- */
106
-
107
- function runCallbacks($this, settings) {
108
- /**
109
- * loops through the settings object (option array) to find the following
110
- * k = option name example k=aNum
111
- * val = option value example val=0123456789
112
- */
113
- $.each(settings, function (k, val) {
114
- if (typeof val === 'function') {
115
- settings[k] = val($this, settings, k);
116
- } else if (typeof $this.autoNumeric[val] === 'function') {
117
- /**
118
- * calls the attached function from the html5 data example: data-a-sign="functionName"
119
- */
120
- settings[k] = $this.autoNumeric[val]($this, settings, k);
121
- }
122
- });
123
- }
124
-
125
- /**
126
- * Preparing user defined options for further usage
127
- * merge them with defaults appropriately
128
- */
129
-
130
- function autoCode($this, settings) {
131
- runCallbacks($this, settings);
132
- var vmax = settings.vMax.toString().split('.'),
133
- vmin = (!settings.vMin && settings.vMin !== 0) ? [] : settings.vMin.toString().split('.');
134
- settings.aNeg = settings.vMin < 0 ? '-' : '';
135
- vmax[0] = vmax[0].replace('-', '');
136
- vmin[0] = vmin[0].replace('-', '');
137
- settings.mIntPos = Math.max(vmax[0].length, 1);
138
- settings.mIntNeg = Math.max(vmin[0].length, 1);
139
- if (settings.mDec === null) {
140
- var vmaxLength = 0,
141
- vminLength = 0;
142
- if (vmax[1]) {
143
- vmaxLength = vmax[1].length;
144
- }
145
- if (vmin[1]) {
146
- vminLength = vmin[1].length;
147
- }
148
- settings.mDec = Math.max(vmaxLength, vminLength);
149
- settings.oDec = settings.mDec;
150
- } else {
151
- settings.mDec = Number(settings.mDec);
152
- }
153
- /** set alternative decimal separator key */
154
- if (settings.altDec === null && settings.mDec > 0) {
155
- if (settings.aDec === '.' && settings.aSep !== ',') {
156
- settings.altDec = ',';
157
- } else if (settings.aDec === ',' && settings.aSep !== '.') {
158
- settings.altDec = '.';
159
- }
160
- }
161
- /** cache regexps for autoStrip */
162
- var aNegReg = settings.aNeg ? '([-\\' + settings.aNeg + ']?)' : '(-?)';
163
- settings.aNegRegAutoStrip = aNegReg;
164
- settings.skipFirstAutoStrip = new RegExp(aNegReg + '[^-' + (settings.aNeg ? '\\' + settings.aNeg : '') + '\\' + settings.aDec + '\\d]' + '.*?(\\d|\\' + settings.aDec + '\\d)');
165
- settings.skipLastAutoStrip = new RegExp('(\\d\\' + settings.aDec + '?)[^\\' + settings.aDec + '\\d]\\D*$');
166
- var allowed = '-' + settings.aNum + '\\' + settings.aDec;
167
- settings.allowedAutoStrip = new RegExp('[^' + allowed + ']', 'gi');
168
- settings.numRegAutoStrip = new RegExp(aNegReg + '(?:\\' + settings.aDec + '?(\\d+\\' + settings.aDec + '\\d+)|(\\d*(?:\\' + settings.aDec + '\\d*)?))');
169
- return settings;
170
- }
171
-
172
- /**
173
- * strip all unwanted characters and leave only a number alert
174
- */
175
-
176
- function autoStrip(s, settings) {
177
- if (settings.aSign !== '') { /** remove currency sign */
178
- s = s.replace(settings.aSign, '');
179
- }
180
- if (settings.aSuffix) { /** remove suffix */
181
- while (s.indexOf(settings.aSuffix) > -1) {
182
- s = s.replace(settings.aSuffix, '');
183
- }
184
- }
185
- s = s.replace(settings.skipFirstAutoStrip, '$1$2'); /** first replace anything before digits */
186
- if ((settings.pNeg === 's' || (settings.pSign === 's' && settings.pNeg !== 'p')) && s.indexOf('-') > -1 && s !== '') {
187
- settings.trailingNegative = true;
188
- }
189
- s = s.replace(settings.skipLastAutoStrip, '$1'); /** then replace anything after digits */
190
- s = s.replace(settings.allowedAutoStrip, ''); /** then remove any uninterested characters */
191
- if (settings.altDec) {
192
- s = s.replace(settings.altDec, settings.aDec);
193
- } /** get only number string */
194
- var m = s.match(settings.numRegAutoStrip);
195
- s = m ? [m[1], m[2], m[3]].join('') : '';
196
- if (settings.lZero === 'allow' || settings.lZero === 'keep') {
197
- var parts = [],
198
- nSign = '';
199
- parts = s.split(settings.aDec);
200
- if (parts[0].indexOf(settings.aNeg) !== -1) {
201
- nSign = settings.aNeg;
202
- parts[0] = parts[0].replace(settings.aNeg, '');
203
- }
204
- if (nSign === "" && parts[0].length > settings.mIntPos && parts[0].charAt(0) === '0') { /** strip leading zero if need */
205
- parts[0] = parts[0].slice(1);
206
- }
207
- if (nSign !== '' && parts[0].length > settings.mIntNeg && parts[0].charAt(0) === '0') { /** strip leading zero if need */
208
- parts[0] = parts[0].slice(1);
209
- }
210
- s = nSign + parts.join(settings.aDec);
211
- }
212
- if ((settings.onOff && settings.lZero === 'deny') || (settings.lZero === 'allow' && settings.onOff === false)) {
213
- var strip_reg = '^' + settings.aNegRegAutoStrip + '0*(\\d|$)';
214
- strip_reg = new RegExp(strip_reg);
215
- s = s.replace(strip_reg, '$1$2');
216
- }
217
- return s;
218
- }
219
-
220
- /**
221
- * places or removes brackets on negative values
222
- */
223
-
224
- function negativeBracket(s, settings) {
225
- if ((settings.pSign === 'p' && settings.pNeg === 'l') || (settings.pSign === 's' && settings.pNeg === 'p')) {
226
- var brackets = settings.nBracket.split(',');
227
- if (!settings.onOff) {
228
- s = s.replace(settings.aNeg, '');
229
- s = brackets[0] + s + brackets[1];
230
- } else if (settings.onOff && s.charAt(0) === brackets[0]) {
231
- s = s.replace(brackets[0], settings.aNeg);
232
- s = s.replace(brackets[1], '');
233
- }
234
- }
235
- return s;
236
- }
237
-
238
- /**
239
- * convert locale format to javaSript numeric string
240
- * allows locale decimal separator to be a period or comma - no thousand separator allowed of currency signs allowed
241
- * '1234.56' OK
242
- * '-1234.56' OK
243
- * '1234.56-' OK
244
- * '1234,56' OK
245
- * '-1234,56' OK
246
- * '1234,56-' OK
247
- */
248
-
249
- function convertLocale(s) {
250
- s = s.replace(',', '.');
251
- if (s.lastIndexOf('-') !== -1 && s.lastIndexOf('-') === s.length - 1) {
252
- s = s.replace('-', '');
253
- s = '-' + s;
254
- }
255
- return s;
256
- }
257
-
258
- /**
259
- * prepare number string to be converted to real number
260
- */
261
-
262
- function fixNumber(s, aDec, aNeg) {
263
- if (aDec && aDec !== '.') {
264
- s = s.replace(aDec, '.');
265
- }
266
- if (aNeg && aNeg !== '-') {
267
- s = s.replace(aNeg, '-');
268
- }
269
- if (!s.match(/\d/)) {
270
- s += '0';
271
- }
272
- return s;
273
- }
274
-
275
- /**
276
- * prepare real number to be converted to our format
277
- */
278
-
279
- function presentNumber(s, aDec, aNeg) {
280
- if (aNeg && aNeg !== '-') {
281
- s = s.replace('-', aNeg);
282
- }
283
- if (aDec && aDec !== '.') {
284
- s = s.replace('.', aDec);
285
- }
286
- return s;
287
- }
288
-
289
- /**
290
- * private function to check for empty value
291
- */
292
-
293
- function checkEmpty(iv, settings, signOnEmpty) {
294
- if (iv === '' || iv === settings.aNeg) {
295
- if (settings.wEmpty === 'always' || signOnEmpty) {
296
- return (settings.pNeg === 'l') ? iv + settings.aSign + settings.aSuffix : settings.aSign + iv + settings.aSuffix;
297
- }
298
- return iv;
299
- }
300
- return null;
301
- }
302
-
303
- /**
304
- * private function that formats our number
305
- */
306
- function autoGroup(iv, settings) {
307
- iv = autoStrip(iv, settings);
308
- if (settings.trailingNegative && iv.indexOf('-') === -1) {
309
- iv = '-' + iv;
310
- }
311
- var empty = checkEmpty(iv, settings, true),
312
- isNeg = iv.indexOf('-') > -1;
313
- if (isNeg) {
314
- iv = iv.replace('-', '');
315
- }
316
- if (empty !== null) {
317
- return empty;
318
- }
319
- var digitalGroup = '';
320
- settings.dGroup = settings.dGroup.toString();
321
- if (settings.dGroup === '2') {
322
- digitalGroup = /(\d)((\d)(\d{2}?)+)$/;
323
- } else if (settings.dGroup === '2s') {
324
- digitalGroup = /(\d)((?:\d{2}){0,2}\d{3}(?:(?:\d{2}){2}\d{3})*?)$/;
325
- } else if (settings.dGroup === '4') {
326
- digitalGroup = /(\d)((\d{4}?)+)$/;
327
- } else {
328
- digitalGroup = /(\d)((\d{3}?)+)$/;
329
- } /** splits the string at the decimal string */
330
- var ivSplit = iv.split(settings.aDec);
331
- if (settings.altDec && ivSplit.length === 1) {
332
- ivSplit = iv.split(settings.altDec);
333
- } /** assigns the whole number to the a variable (s) */
334
- var s = ivSplit[0];
335
- if (settings.aSep !== '') {
336
- while (digitalGroup.test(s)) { /** re-inserts the thousand separator via a regular expression */
337
- s = s.replace(digitalGroup, '$1' + settings.aSep + '$2');
338
- }
339
- }
340
- if (settings.mDec !== 0 && ivSplit.length > 1) {
341
- if (ivSplit[1].length > settings.mDec) {
342
- ivSplit[1] = ivSplit[1].substring(0, settings.mDec);
343
- } /** joins the whole number with the decimal value */
344
- iv = s + settings.aDec + ivSplit[1];
345
- } else { /** if whole numbers only */
346
- iv = s;
347
- }
348
- if (settings.pSign === 'p') {
349
- if (isNeg && settings.pNeg === 'l') {
350
- iv = settings.aNeg + settings.aSign + iv;
351
- }
352
- if (isNeg && settings.pNeg === 'r') {
353
- iv = settings.aSign + settings.aNeg + iv;
354
- }
355
- if (isNeg && settings.pNeg === 's') {
356
- iv = settings.aSign + iv + settings.aNeg;
357
- }
358
- if (!isNeg) {
359
- iv = settings.aSign + iv;
360
- }
361
- }
362
- if (settings.pSign === 's') {
363
- if (isNeg && settings.pNeg === 'r') {
364
- iv = iv + settings.aSign + settings.aNeg;
365
- }
366
- if (isNeg && settings.pNeg === 'l') {
367
- iv = iv + settings.aNeg + settings.aSign;
368
- }
369
- if (isNeg && settings.pNeg === 'p') {
370
- iv = settings.aNeg + iv + settings.aSign;
371
- }
372
- if (!isNeg) {
373
- iv = iv + settings.aSign;
374
- }
375
- }
376
- if (settings.rawValue < 0 && settings.nBracket !== null) { /** removes the negative sign and places brackets */
377
- iv = negativeBracket(iv, settings);
378
- }
379
- settings.trailingNegative = false;
380
- return iv + settings.aSuffix;
381
- }
382
-
383
- /**
384
- * round number after setting by pasting or $().autoNumericSet()
385
- * private function for round the number
386
- * please note this handled as text - JavaScript math function can return inaccurate values
387
- * also this offers multiple rounding methods that are not easily accomplished in JavaScript
388
- */
389
-
390
- function autoRound(iv, settings) { /** value to string */
391
- iv = (iv === '') ? '0' : iv.toString();
392
- if (settings.mRound === 'N05' || settings.mRound === 'U05' || settings.mRound === 'D05') {
393
- iv = (settings.mRound === 'N05') ? (Math.round(iv * 20) / 20).toString() : (settings.mRound === 'U05') ? (Math.ceil(iv * 20) / 20).toString() : (Math.floor(iv * 20) / 20).toString();
394
- return (iv.indexOf('.') === -1) ? iv + '.00' : (iv.length - iv.indexOf('.') < 3) ? iv + '0' : iv;
395
- }
396
- var ivRounded = '',
397
- i = 0,
398
- nSign = '',
399
- rDec = (typeof(settings.aPad) === 'boolean' || settings.aPad === null) ? (settings.aPad ? settings.mDec : 0) : Number(settings.aPad);
400
- var truncateZeros = function (ivRounded) { /** truncate not needed zeros */
401
- var regex = rDec === 0 ? (/(\.(?:\d*[1-9])?)0*$/) : rDec === 1 ? (/(\.\d(?:\d*[1-9])?)0*$/) : new RegExp('(\\.\\d{' + rDec + '}(?:\\d*[1-9])?)0*');
402
- ivRounded = ivRounded.replace(regex, '$1'); /** If there are no decimal places, we don't need a decimal point at the end */
403
- if (rDec === 0) {
404
- ivRounded = ivRounded.replace(/\.$/, '');
405
- }
406
- return ivRounded;
407
- };
408
- if (iv.charAt(0) === '-') { /** Checks if the iv (input Value)is a negative value */
409
- nSign = '-';
410
- iv = iv.replace('-', ''); /** removes the negative sign will be added back later if required */
411
- }
412
- if (!iv.match(/^\d/)) { /** append a zero if first character is not a digit (then it is likely to be a dot)*/
413
- iv = '0' + iv;
414
- }
415
- if (nSign === '-' && Number(iv) === 0) { /** determines if the value is zero - if zero no negative sign */
416
- nSign = '';
417
- }
418
- if ((Number(iv) > 0 && settings.lZero !== 'keep') || (iv.length > 0 && settings.lZero === 'allow')) { /** trims leading zero's if needed */
419
- iv = iv.replace(/^0*(\d)/, '$1');
420
- }
421
- var dPos = iv.lastIndexOf('.'),
422
- /** virtual decimal position */
423
- vdPos = (dPos === -1) ? iv.length - 1 : dPos,
424
- /** checks decimal places to determine if rounding is required */
425
- cDec = (iv.length - 1) - vdPos; /** check if no rounding is required */
426
- if (cDec <= settings.mDec) {
427
- ivRounded = iv; /** check if we need to pad with zeros */
428
- if (cDec < rDec) {
429
- if (dPos === -1) {
430
- //ivRounded += '.';
431
- ivRounded += settings.aDec;
432
- }
433
- var zeros = '000000';
434
- while (cDec < rDec) {
435
- zeros = zeros.substring(0, rDec - cDec);
436
- ivRounded += zeros;
437
- cDec += zeros.length;
438
- }
439
- } else if (cDec > rDec) {
440
- ivRounded = truncateZeros(ivRounded);
441
- } else if (cDec === 0 && rDec === 0) {
442
- ivRounded = ivRounded.replace(/\.$/, '');
443
- }
444
- return (Number(ivRounded) === 0) ? ivRounded : nSign + ivRounded;
445
- } /** rounded length of the string after rounding */
446
- var rLength = dPos + settings.mDec,
447
- tRound = Number(iv.charAt(rLength + 1)),
448
- ivArray = iv.substring(0, rLength + 1).split(''),
449
- odd = (iv.charAt(rLength) === '.') ? (iv.charAt(rLength - 1) % 2) : (iv.charAt(rLength) % 2);
450
- /*jslint white: true*/
451
- if ((tRound > 4 && settings.mRound === 'S') || /** Round half up symmetric */
452
- (tRound > 4 && settings.mRound === 'A' && nSign === '') || /** Round half up asymmetric positive values */
453
- (tRound > 5 && settings.mRound === 'A' && nSign === '-') || /** Round half up asymmetric negative values */
454
- (tRound > 5 && settings.mRound === 's') || /** Round half down symmetric */
455
- (tRound > 5 && settings.mRound === 'a' && nSign === '') || /** Round half down asymmetric positive values */
456
- (tRound > 4 && settings.mRound === 'a' && nSign === '-') || /** Round half down asymmetric negative values */
457
- (tRound > 5 && settings.mRound === 'B') || /** Round half even "Banker's Rounding" */
458
- (tRound === 5 && settings.mRound === 'B' && odd === 1) || /** Round half even "Banker's Rounding" */
459
- (tRound > 0 && settings.mRound === 'C' && nSign === '') || /** Round to ceiling toward positive infinite */
460
- (tRound > 0 && settings.mRound === 'F' && nSign === '-') || /** Round to floor toward negative infinite */
461
- (tRound > 0 && settings.mRound === 'U')) { /** Round up away from zero */
462
- /*jslint white: false*/
463
- 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 */
464
- if (ivArray[i] !== '.') {
465
- ivArray[i] = +ivArray[i] + 1;
466
- if (ivArray[i] < 10) {
467
- break;
468
- }
469
- if (i > 0) {
470
- ivArray[i] = '0';
471
- }
472
- }
473
- }
474
- }
475
- ivArray = ivArray.slice(0, rLength + 1); /** Reconstruct the string, converting any 10's to 0's */
476
- ivRounded = truncateZeros(ivArray.join('')); /** return rounded value */
477
- return (Number(ivRounded) === 0) ? ivRounded : nSign + ivRounded;
478
- }
479
-
480
- /**
481
- * truncate decimal part of a number
482
- */
483
-
484
- function truncateDecimal(s, settings, paste) {
485
- var aDec = settings.aDec,
486
- mDec = settings.mDec;
487
- s = (paste === 'paste') ? autoRound(s, settings) : s;
488
- if (aDec && mDec) {
489
- var parts = s.split(aDec);
490
- /** truncate decimal part to satisfying length
491
- * cause we would round it anyway */
492
- if (parts[1] && parts[1].length > mDec) {
493
- if (mDec > 0) {
494
- parts[1] = parts[1].substring(0, mDec);
495
- s = parts.join(aDec);
496
- } else {
497
- s = parts[0];
498
- }
499
- }
500
- }
501
- return s;
502
- }
503
-
504
- /**
505
- * Function to parse vMin, vMax & the input value to prepare for testing to determine if the value falls within the min / max range
506
- * Return an object example: vMin: "999999999999999.99" returns the following "{s: -1, e: 12, c: Array[15]}"
507
- * This function is adapted from Big.js https://github.com/MikeMcl/big.js/
508
- * Many thanks to Mike
509
- */
510
-
511
- function parseStr(n) {
512
- var x = {},
513
- e,
514
- i,
515
- nL,
516
- j;
517
- /** Minus zero? */
518
- if (n === 0 && 1 / n < 0) {
519
- n = '-0';
520
- }
521
- /** Determine sign. 1 positive, -1 negative */
522
- if (n.charAt(0) === '-') {
523
- n = n.slice(1);
524
- x.s = -1;
525
- } else {
526
- x.s = 1;
527
- }
528
- /** Decimal point? */
529
- e = n.indexOf('.');
530
- if (e > -1) {
531
- n = n.replace('.', '');
532
- }
533
- /** length of string if no decimal character */
534
- if (e < 0) {
535
- /** Integer. */
536
- e = n.length;
537
- }
538
- /** Determine leading zeros. */
539
- i = (n.search(/[1-9]/i) === -1) ? n.length : n.search(/[1-9]/i);
540
- nL = n.length;
541
- if (i === nL) {
542
- /** Zero. */
543
- x.e = 0;
544
- x.c = [0];
545
- } else {
546
- /** Determine trailing zeros. */
547
- for (j = nL - 1; n.charAt(j) === '0'; j -= 1) {
548
- nL -= 1;
549
- }
550
- nL -= 1;
551
- /** Decimal location. */
552
- x.e = e - i - 1;
553
- x.c = [];
554
- /** Convert string to array of digits without leading/trailing zeros. */
555
- for (e = 0; i <= nL; i += 1) {
556
- x.c[e] = +n.charAt(i);
557
- e += 1;
558
- }
559
- }
560
- return x;
561
- }
562
-
563
- /**
564
- * Function to test if the input value falls with the Min / Max settings
565
- * This uses the parsed strings for the above parseStr function
566
- * This function is adapted from Big.js https://github.com/MikeMcl/big.js/
567
- * Many thanks to Mike
568
- */
569
-
570
- function testMinMax(y, x) {
571
- var xNeg,
572
- xc = x.c,
573
- yc = y.c,
574
- i = x.s,
575
- j = y.s,
576
- k = x.e,
577
- l = y.e;
578
- /** Either zero? */
579
- if (!xc[0] || !yc[0]) {
580
- return !xc[0] ? !yc[0] ? 0 : -j : i;
581
- }
582
- /** Signs differ? */
583
- if (i !== j) {
584
- return i;
585
- }
586
- xNeg = i < 0;
587
- /** Compare exponents. */
588
- if (k !== l) {
589
- return k > l ^ xNeg ? 1 : -1;
590
- }
591
- i = -1;
592
- k = xc.length;
593
- l = yc.length;
594
- j = (k < l) ? k : l;
595
- /** Compare digit by digit. */
596
- for (i += 1; i < j; i += 1) {
597
- if (xc[i] !== yc[i]) {
598
- return xc[i] > yc[i] ^ xNeg ? 1 : -1;
599
- }
600
- }
601
- /** Compare lengths */
602
- return k === l ? 0 : k > l ^ xNeg ? 1 : -1;
603
- }
604
-
605
- /**
606
- * checking that number satisfy format conditions
607
- * and lays between settings.vMin and settings.vMax
608
- * and the string length does not exceed the digits in settings.vMin and settings.vMax
609
- */
610
-
611
- function autoCheck(s, settings) {
612
- s = s.toString();
613
- s = s.replace(',', '.');
614
- var minParse = parseStr(settings.vMin),
615
- maxParse = parseStr(settings.vMax),
616
- valParse = parseStr(s);
617
- return testMinMax(minParse, valParse) > -1 && testMinMax(maxParse, valParse) < 1;
618
- }
619
-
620
- /**
621
- * Holder object for field properties
622
- */
623
-
624
- function AutoNumericHolder(that, settings) {
625
- this.settings = settings;
626
- this.that = that;
627
- this.$that = $(that);
628
- this.formatted = false;
629
- this.settingsClone = autoCode(this.$that, this.settings);
630
- this.value = that.value;
631
- }
632
- AutoNumericHolder.prototype = {
633
- init: function (e) {
634
- this.value = this.that.value;
635
- this.settingsClone = autoCode(this.$that, this.settings);
636
- this.ctrlKey = e.ctrlKey;
637
- this.cmdKey = e.metaKey;
638
- this.shiftKey = e.shiftKey;
639
- this.selection = getElementSelection(this.that); /** keypress event overwrites meaningful value of e.keyCode */
640
- if (e.type === 'keydown' || e.type === 'keyup') {
641
- this.kdCode = e.keyCode;
642
- }
643
- this.which = e.which;
644
- this.processed = false;
645
- this.formatted = false;
646
- },
647
- setSelection: function (start, end, setReal) {
648
- start = Math.max(start, 0);
649
- end = Math.min(end, this.that.value.length);
650
- this.selection = {
651
- start: start,
652
- end: end,
653
- length: end - start
654
- };
655
- if (setReal === undefined || setReal) {
656
- setElementSelection(this.that, start, end);
657
- }
658
- },
659
- setPosition: function (pos, setReal) {
660
- this.setSelection(pos, pos, setReal);
661
- },
662
- getBeforeAfter: function () {
663
- var value = this.value,
664
- left = value.substring(0, this.selection.start),
665
- right = value.substring(this.selection.end, value.length);
666
- return [left, right];
667
- },
668
- getBeforeAfterStriped: function () {
669
- var settingsClone = this.settingsClone,
670
- parts = this.getBeforeAfter();
671
- parts[0] = autoStrip(parts[0], this.settingsClone);
672
- parts[1] = autoStrip(parts[1], this.settingsClone);
673
- if (settingsClone.trailingNegative && parts[0].indexOf('-') === -1) {
674
- parts[0] = '-' + parts[0];
675
- parts[1] = (parts[1] === '-') ? '' : parts[1];
676
- }
677
- settingsClone.trailingNegative = false;
678
- return parts;
679
- },
680
-
681
- /**
682
- * strip parts from excess characters and leading zeroes
683
- */
684
- normalizeParts: function (left, right) {
685
- var settingsClone = this.settingsClone;
686
- left = autoStrip(left, settingsClone); /** prevents multiple leading zeros from being entered */
687
- right = autoStrip(right, settingsClone); /** if right is not empty and first character is not aDec, */
688
- if (settingsClone.trailingNegative && left.indexOf('-') === -1) {
689
- left = '-' + left;
690
- settingsClone.trailingNegative = false;
691
- }
692
- if ((left === '' || left === settingsClone.aNeg) && settingsClone.lZero === 'deny') {
693
- if (right > '') {
694
- right = right.replace(/^0*(\d)/, '$1');
695
- }
696
- }
697
- var new_value = left + right; /** insert zero if has leading dot */
698
- if (settingsClone.aDec) {
699
- var m = new_value.match(new RegExp('^' + settingsClone.aNegRegAutoStrip + '\\' + settingsClone.aDec));
700
- if (m) {
701
- left = left.replace(m[1], m[1] + '0');
702
- new_value = left + right;
703
- }
704
- }
705
- return [left, right];
706
- },
707
-
708
- /**
709
- * set part of number to value keeping position of cursor
710
- */
711
- setValueParts: function (left, right, paste) {
712
- var settingsClone = this.settingsClone,
713
- parts = this.normalizeParts(left, right),
714
- new_value = parts.join(''),
715
- position = parts[0].length;
716
- if (autoCheck(new_value, settingsClone)) {
717
- new_value = truncateDecimal(new_value, settingsClone, paste);
718
- var test_value = (new_value.indexOf(',') !== -1) ? new_value.replace(',', '.') : new_value,
719
- text_value = test_value;
720
- if (test_value === '' || test_value === settingsClone.aNeg) {
721
- settingsClone.rawValue = '';
722
- } else {
723
- settingsClone.rawValue = text_value;
724
- }
725
- if (position > new_value.length) {
726
- position = new_value.length;
727
- }
728
- this.value = new_value;
729
- this.setPosition(position, false);
730
- return true;
731
- }
732
- return false;
733
- },
734
-
735
- /**
736
- * helper function for expandSelectionOnSign
737
- * returns sign position of a formatted value
738
- */
739
- signPosition: function () {
740
- var settingsClone = this.settingsClone,
741
- aSign = settingsClone.aSign,
742
- that = this.that;
743
- if (aSign) {
744
- var aSignLen = aSign.length;
745
- if (settingsClone.pSign === 'p') {
746
- var hasNeg = settingsClone.aNeg && that.value && that.value.charAt(0) === settingsClone.aNeg;
747
- return hasNeg ? [1, aSignLen + 1] : [0, aSignLen];
748
- }
749
- var valueLen = that.value.length;
750
- return [valueLen - aSignLen, valueLen];
751
- }
752
- return [1000, -1];
753
- },
754
-
755
- /**
756
- * expands selection to cover whole sign
757
- * prevents partial deletion/copying/overwriting of a sign
758
- */
759
- expandSelectionOnSign: function (setReal) {
760
- var sign_position = this.signPosition(),
761
- selection = this.selection;
762
- if (selection.start < sign_position[1] && selection.end > sign_position[0]) { /** if selection catches something except sign and catches only space from sign */
763
- 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 */
764
- if (selection.start < sign_position[0]) {
765
- this.setSelection(selection.start, sign_position[0], setReal);
766
- } else {
767
- this.setSelection(sign_position[1], selection.end, setReal);
768
- }
769
- } else { /** else select with whole sign */
770
- this.setSelection(Math.min(selection.start, sign_position[0]), Math.max(selection.end, sign_position[1]), setReal);
771
- }
772
- }
773
- },
774
-
775
- /**
776
- * try to strip pasted value to digits
777
- */
778
- checkPaste: function () {
779
- if (this.valuePartsBeforePaste !== undefined) {
780
- var parts = this.getBeforeAfter(),
781
- oldParts = this.valuePartsBeforePaste;
782
- delete this.valuePartsBeforePaste; /** try to strip pasted value first */
783
- parts[0] = parts[0].substr(0, oldParts[0].length) + autoStrip(parts[0].substr(oldParts[0].length), this.settingsClone);
784
- if (!this.setValueParts(parts[0], parts[1], 'paste')) {
785
- this.value = oldParts.join('');
786
- this.setPosition(oldParts[0].length, false);
787
- }
788
- }
789
- },
790
-
791
- /**
792
- * process pasting, cursor moving and skipping of not interesting keys
793
- * if returns true, further processing is not performed
794
- */
795
- skipAllways: function (e) {
796
- var kdCode = this.kdCode,
797
- which = this.which,
798
- ctrlKey = this.ctrlKey,
799
- cmdKey = this.cmdKey,
800
- shiftKey = this.shiftKey; /** catch the ctrl up on ctrl-v */
801
- if (((ctrlKey || cmdKey) && e.type === 'keyup' && this.valuePartsBeforePaste !== undefined) || (shiftKey && kdCode === 45)) {
802
- this.checkPaste();
803
- return false;
804
- }
805
- /** codes are taken from http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx
806
- * skip Fx keys, windows keys, other special keys
807
- */
808
- 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 || kdCode === 224) {
809
- return true;
810
- }
811
- if ((ctrlKey || cmdKey) && kdCode === 65) { /** if select all (a=65)*/
812
- if (this.settings.sNumber) {
813
- e.preventDefault();
814
- var valueLen = this.that.value.length,
815
- aSignLen = this.settings.aSign.length,
816
- negLen = (this.that.value.indexOf('-') === -1) ? 0 : 1,
817
- aSuffixLen = this.settings.aSuffix.length,
818
- pSign = this.settings.pSign,
819
- pNeg = this.settings.pNeg,
820
- start = (pSign === 's') ? 0 : (pNeg === 'l' && negLen === 1 && aSignLen > 0) ? aSignLen + 1 : aSignLen,
821
- end = (pSign === 'p') ? valueLen - aSuffixLen : (pNeg === 'l') ? valueLen - (aSuffixLen + aSignLen) : (pNeg === 'r') ? (aSignLen > 0) ? valueLen - (aSignLen + negLen + aSuffixLen) : valueLen - (aSignLen + aSuffixLen) : valueLen - (aSignLen + aSuffixLen);
822
- setElementSelection(this.that, start, end);
823
- }
824
- return true;
825
- }
826
- if ((ctrlKey || cmdKey) && (kdCode === 67 || kdCode === 86 || kdCode === 88)) { /** if copy (c=67) paste (v=86) or cut (x=88) */
827
- if (e.type === 'keydown') {
828
- this.expandSelectionOnSign();
829
- }
830
- if (kdCode === 86 || kdCode === 45) { /** try to prevent wrong paste */
831
- if (e.type === 'keydown' || e.type === 'keypress') {
832
- if (this.valuePartsBeforePaste === undefined) {
833
- this.valuePartsBeforePaste = this.getBeforeAfter();
834
- }
835
- } else {
836
- this.checkPaste();
837
- }
838
- }
839
- return e.type === 'keydown' || e.type === 'keypress' || kdCode === 67;
840
- }
841
- if (ctrlKey || cmdKey) {
842
- return true;
843
- }
844
- if (kdCode === 37 || kdCode === 39) { /** jump over thousand separator */
845
- var aSep = this.settingsClone.aSep,
846
- aDec = this.settingsClone.aDec,
847
- startJump = this.selection.start,
848
- value = this.that.value;
849
- if (e.type === 'keydown' && !this.shiftKey) {
850
- if (kdCode === 37 && (value.charAt(startJump - 2) === aSep || value.charAt(startJump - 2) === aDec)) {
851
- this.setPosition(startJump - 1);
852
- } else if (kdCode === 39 && (value.charAt(startJump + 1) === aSep || value.charAt(startJump + 1) === aDec)) {
853
- this.setPosition(startJump + 1);
854
- }
855
- }
856
- return true;
857
- }
858
- if (kdCode >= 34 && kdCode <= 40) {
859
- return true;
860
- }
861
- return false;
862
- },
863
-
864
- /**
865
- * process deletion of characters when the minus sign is to the right of the numeric characters
866
- */
867
- processTrailing: function (parts) {
868
- var settingsClone = this.settingsClone;
869
- if (settingsClone.pSign === 'p' && settingsClone.pNeg === 's') {
870
- if (this.kdCode === 8) {
871
- settingsClone.caretFix = (this.selection.start >= this.value.indexOf(settingsClone.aSuffix) && settingsClone.aSuffix !== '') ? true : false;
872
- if (this.value.charAt(this.selection.start - 1) === '-') {
873
- parts[0] = parts[0].substring(1);
874
- } else if (this.selection.start <= this.value.length - settingsClone.aSuffix.length) {
875
- parts[0] = parts[0].substring(0, parts[0].length - 1);
876
- }
877
- } else {
878
- settingsClone.caretFix = (this.selection.start >= this.value.indexOf(settingsClone.aSuffix) && settingsClone.aSuffix !== '') ? true : false;
879
- if (this.selection.start >= this.value.indexOf(settingsClone.aSign) + settingsClone.aSign.length) {
880
- parts[1] = parts[1].substring(1, parts[1].length);
881
- }
882
- if (parts[0].indexOf('-') > -1 && this.value.charAt(this.selection.start) === '-') {
883
- parts[0] = parts[0].substring(1);
884
- }
885
- }
886
- }
887
- if (settingsClone.pSign === 's' && settingsClone.pNeg === 'l') {
888
- settingsClone.caretFix = (this.selection.start >= this.value.indexOf(settingsClone.aNeg) + settingsClone.aNeg.length) ? true : false;
889
- if (this.kdCode === 8) {
890
- if (this.selection.start === (this.value.indexOf(settingsClone.aNeg) + settingsClone.aNeg.length) && this.value.indexOf(settingsClone.aNeg) !== -1) {
891
- parts[0] = parts[0].substring(1);
892
- } else if (parts[0] !== '-' && ((this.selection.start <= this.value.indexOf(settingsClone.aNeg)) || this.value.indexOf(settingsClone.aNeg) === -1)) {
893
- parts[0] = parts[0].substring(0, parts[0].length - 1);
894
- }
895
- } else {
896
- if (parts[0][0] === '-') {
897
- parts[1] = parts[1].substring(1);
898
- }
899
- if (this.selection.start === this.value.indexOf(settingsClone.aNeg) && this.value.indexOf(settingsClone.aNeg) !== -1) {
900
- parts[0] = parts[0].substring(1);
901
- }
902
- }
903
- }
904
- if (settingsClone.pSign === 's' && settingsClone.pNeg === 'r') {
905
- settingsClone.caretFix = (this.selection.start >= this.value.indexOf(settingsClone.aNeg) + settingsClone.aNeg.length) ? true : false;
906
- if (this.kdCode === 8) {
907
- if (this.selection.start === (this.value.indexOf(settingsClone.aNeg) + settingsClone.aNeg.length)) {
908
- parts[0] = parts[0].substring(1);
909
- } else if (parts[0] !== '-' && this.selection.start <= (this.value.indexOf(settingsClone.aNeg) - settingsClone.aSign.length)) {
910
- parts[0] = parts[0].substring(0, parts[0].length - 1);
911
- } else if (parts[0] !== '' && this.value.indexOf(settingsClone.aNeg) === -1) {
912
- parts[0] = parts[0].substring(0, parts[0].length - 1);
913
- }
914
- } else {
915
- settingsClone.caretFix = (this.selection.start >= this.value.indexOf(settingsClone.aSign) && settingsClone.aSign !== '') ? true : false;
916
- if (this.selection.start === this.value.indexOf(settingsClone.aNeg)) {
917
- parts[0] = parts[0].substring(1);
918
- }
919
- parts[1] = parts[1].substring(1);
920
- }
921
- }
922
- return parts;
923
- },
924
-
925
- /**
926
- * process deletion of characters
927
- * returns true if processing performed
928
- */
929
- processAllways: function () {
930
- var settingsClone = this.settingsClone,
931
- parts = [];
932
- if (this.kdCode === 8 || this.kdCode === 46) {
933
- if (!this.selection.length) {
934
- parts = this.getBeforeAfterStriped();
935
- if (parts[0] === '' && parts[1] === '') {
936
- settingsClone.throwInput = false;
937
- }
938
- if (((settingsClone.pSign === 'p' && settingsClone.pNeg === 's') || (settingsClone.pSign === 's' && (settingsClone.pNeg === 'l' || settingsClone.pNeg === 'r'))) && this.value.indexOf('-') !== -1) {
939
- parts = this.processTrailing(parts);
940
- } else {
941
- if (this.kdCode === 8) {
942
- parts[0] = parts[0].substring(0, parts[0].length - 1);
943
- } else {
944
- parts[1] = parts[1].substring(1, parts[1].length);
945
- }
946
- }
947
- this.setValueParts(parts[0], parts[1]);
948
- } else {
949
- this.expandSelectionOnSign(false);
950
- parts = this.getBeforeAfterStriped();
951
- this.setValueParts(parts[0], parts[1]);
952
- }
953
- return true;
954
- }
955
- return false;
956
- },
957
-
958
- /**
959
- * process insertion of characters
960
- * returns true if processing performed
961
- */
962
- processKeypress: function () {
963
- var settingsClone = this.settingsClone,
964
- cCode = String.fromCharCode(this.which),
965
- parts = this.getBeforeAfterStriped(),
966
- left = parts[0],
967
- right = parts[1];
968
- settingsClone.throwInput = true;
969
- /** start rules when the decimal character key is pressed always use numeric pad dot to insert decimal separator */
970
- if (cCode === settingsClone.aDec || (settingsClone.altDec && cCode === settingsClone.altDec) || ((cCode === '.' || cCode === ',') && this.kdCode === 110)) { /** do not allow decimal character if no decimal part allowed */
971
- if (!settingsClone.mDec || !settingsClone.aDec) {
972
- return true;
973
- } /** do not allow decimal character before aNeg character */
974
- if (settingsClone.aNeg && right.indexOf(settingsClone.aNeg) > -1) {
975
- return true;
976
- } /** do not allow decimal character if other decimal character present */
977
- if (left.indexOf(settingsClone.aDec) > -1) {
978
- return true;
979
- }
980
- if (right.indexOf(settingsClone.aDec) > 0) {
981
- return true;
982
- }
983
- if (right.indexOf(settingsClone.aDec) === 0) {
984
- right = right.substr(1);
985
- }
986
- this.setValueParts(left + settingsClone.aDec, right, null);
987
- return true;
988
- }
989
- if ((cCode === '-' || cCode === '+') && settingsClone.aNeg === '-') { /** prevent minus if not allowed ************************************************************************************************************************************/
990
- if (!settingsClone) {
991
- return true;
992
- } /** caret is always after minus */
993
- if ((settingsClone.pSign === 'p' && settingsClone.pNeg === 's') || (settingsClone.pSign === 's' && settingsClone.pNeg !== 'p')) {
994
- if (left === '' && right.indexOf(settingsClone.aNeg) > -1) {
995
- left = settingsClone.aNeg;
996
- right = right.substring(1, right.length);
997
- } /** change sign of number, remove part if should */
998
- if (left.charAt(0) === '-' || left.indexOf(settingsClone.aNeg) !== -1) {
999
- left = left.substring(1, left.length);
1000
- } else {
1001
- left = (cCode === '-') ? settingsClone.aNeg + left : left;
1002
- }
1003
- } else {
1004
- if (left === '' && right.indexOf(settingsClone.aNeg) > -1) {
1005
- left = settingsClone.aNeg;
1006
- right = right.substring(1, right.length);
1007
- } /** change sign of number, remove part if should */
1008
- if (left.charAt(0) === settingsClone.aNeg) {
1009
- left = left.substring(1, left.length);
1010
- } else {
1011
- left = (cCode === '-') ? settingsClone.aNeg + left : left;
1012
- }
1013
- }
1014
- this.setValueParts(left, right, null);
1015
- return true;
1016
- }
1017
- if (cCode >= '0' && cCode <= '9') { /** if try to insert digit before minus */
1018
- if (settingsClone.aNeg && left === '' && right.indexOf(settingsClone.aNeg) > -1) {
1019
- left = settingsClone.aNeg;
1020
- right = right.substring(1, right.length);
1021
- }
1022
- if (settingsClone.vMax <= 0 && settingsClone.vMin < settingsClone.vMax && this.value.indexOf(settingsClone.aNeg) === -1 && cCode !== '0') {
1023
- left = settingsClone.aNeg + left;
1024
- }
1025
- this.setValueParts(left + cCode, right, null);
1026
- return true;
1027
- } /** prevent any other character */
1028
- settingsClone.throwInput = false;
1029
- return true;
1030
- },
1031
-
1032
- /**
1033
- * formatting of just processed value with keeping of cursor position
1034
- */
1035
- formatQuick: function (e) {
1036
- var settingsClone = this.settingsClone,
1037
- parts = this.getBeforeAfterStriped(),
1038
- leftLength = this.value,
1039
- kuCode = e.keyCode;
1040
- /** no grouping separator and no currency sign */
1041
- if ((settingsClone.aSep === '' || (settingsClone.aSep !== '' && leftLength.indexOf(settingsClone.aSep) === -1)) && (settingsClone.aSign === '' || (settingsClone.aSign !== '' && leftLength.indexOf(settingsClone.aSign) === -1))) {
1042
- var subParts = [],
1043
- nSign = '';
1044
- subParts = leftLength.split(settingsClone.aDec);
1045
- if (subParts[0].indexOf('-') > -1) {
1046
- nSign = '-';
1047
- subParts[0] = subParts[0].replace('-', '');
1048
- parts[0] = parts[0].replace('-', '');
1049
- }
1050
- if (nSign === '' && subParts[0].length > settingsClone.mIntPos && parts[0].charAt(0) === '0') { /** strip leading zero on positive value if need */
1051
- parts[0] = parts[0].slice(1);
1052
- }
1053
- if (nSign === '-' && subParts[0].length > settingsClone.mIntNeg && parts[0].charAt(0) === '0') { /** strip leading zero on negative value if need */
1054
- parts[0] = parts[0].slice(1);
1055
- }
1056
- parts[0] = nSign + parts[0];
1057
- }
1058
- var value = autoGroup(this.value, this.settingsClone);
1059
- var position = value.length;
1060
- if (value) {
1061
- /** prepare regexp which searches for cursor position from unformatted left part */
1062
- var left_ar = parts[0].split(''),
1063
- i = 0;
1064
- /** fixes caret position with trailing minus sign */
1065
- if ((settingsClone.pNeg === 's' || (settingsClone.pSign === 's' && settingsClone.pNeg !== 'p')) && left_ar[0] === '-' && settingsClone.aNeg !== '') {
1066
- left_ar.shift();
1067
- if (settingsClone.pSign === 's' && settingsClone.pNeg === 'l' && (kuCode === 8 || this.kdCode === 8 || kuCode === 46 || this.kdCode === 46) && settingsClone.caretFix) {
1068
- left_ar.push('-');
1069
- settingsClone.caretFix = (e.type === 'keydown') ? true : false;
1070
- }
1071
- if (settingsClone.pSign === 'p' && settingsClone.pNeg === 's' && (kuCode === 8 || this.kdCode === 8 || kuCode === 46 || this.kdCode === 46) && settingsClone.caretFix) {
1072
- left_ar.push('-');
1073
- settingsClone.caretFix = (e.type === 'keydown') ? true : false;
1074
- }
1075
- if (settingsClone.pSign === 's' && settingsClone.pNeg === 'r' && (kuCode === 8 || this.kdCode === 8 || kuCode === 46 || this.kdCode === 46) && settingsClone.caretFix) {
1076
- var signParts = settingsClone.aSign.split(''),
1077
- escapeChr = ['\\', '^', '$', '.', '|', '?', '*', '+', '(', ')', '['],
1078
- escapedParts = [],
1079
- escapedSign = '';
1080
- $.each(signParts, function (i, miniParts) {
1081
- miniParts = signParts[i];
1082
- if ($.inArray(miniParts, escapeChr) !== -1) {
1083
- escapedParts.push('\\' + miniParts);
1084
- } else {
1085
- escapedParts.push(miniParts);
1086
- }
1087
- });
1088
- if (kuCode === 8 || this.kdCode === 8) {
1089
- escapedParts.push('-');
1090
- }
1091
- escapedSign = escapedParts.join('');
1092
- left_ar.push(escapedSign);
1093
- settingsClone.caretFix = (e.type === 'keydown') ? true : false;
1094
- }
1095
- }
1096
- for (i; i < left_ar.length; i += 1) { /** thanks Peter Kovari */
1097
- if (!left_ar[i].match('\\d')) {
1098
- left_ar[i] = '\\' + left_ar[i];
1099
- }
1100
- }
1101
- var leftReg = new RegExp('^.*?' + left_ar.join('.*?')),
1102
- newLeft = value.match(leftReg); /** search cursor position in formatted value */
1103
- if (newLeft) {
1104
- position = newLeft[0].length;
1105
- /** if we are just before sign which is in prefix position */
1106
- if (((position === 0 && value.charAt(0) !== settingsClone.aNeg) || (position === 1 && value.charAt(0) === settingsClone.aNeg)) && settingsClone.aSign && settingsClone.pSign === 'p') {
1107
- /** place caret after prefix sign */
1108
- position = this.settingsClone.aSign.length + (value.charAt(0) === '-' ? 1 : 0);
1109
- }
1110
- } else {
1111
- if (settingsClone.aSign && settingsClone.pSign === 's') {
1112
- /** if we could not find a place for cursor and have a sign as a suffix */
1113
- /** place caret before suffix currency sign */
1114
- position -= settingsClone.aSign.length;
1115
- }
1116
- if (settingsClone.aSuffix) {
1117
- /** if we could not find a place for cursor and have a suffix */
1118
- /** place caret before suffix */
1119
- position -= settingsClone.aSuffix.length;
1120
- }
1121
- }
1122
- }
1123
- if (this.that.value !== value) {
1124
- this.that.value = value;
1125
- this.setPosition(position);
1126
- }
1127
- this.formatted = true;
1128
- }
1129
- };
1130
-
1131
- /**
1132
- * thanks to Anthony & Evan C
1133
- */
1134
-
1135
- function autoGet(obj) {
1136
- if (typeof obj === 'string') {
1137
- obj = obj.replace(/\[/g, "\\[").replace(/\]/g, "\\]");
1138
- obj = '#' + obj.replace(/(:|\.)/g, '\\$1');
1139
- /** obj = '#' + obj.replace(/([;&,\.\+\*\~':"\!\^#$%@\[\]\(\)=>\|])/g, '\\$1'); */
1140
- /** possible modification to replace the above 2 lines */
1141
- }
1142
- return $(obj);
1143
- }
1144
-
1145
- /**
1146
- * function to attach data to the element
1147
- * and imitate the holder
1148
- */
1149
-
1150
- function getHolder($that, settings, update) {
1151
- var data = $that.data('autoNumeric');
1152
- if (!data) {
1153
- data = {};
1154
- $that.data('autoNumeric', data);
1155
- }
1156
- var holder = data.holder;
1157
- if ((holder === undefined && settings) || update) {
1158
- holder = new AutoNumericHolder($that.get(0), settings);
1159
- data.holder = holder;
1160
- }
1161
- return holder;
1162
- }
1163
-
1164
- /**
1165
- * original settings saved for use when eDec & nSep options are being used
1166
- */
1167
-
1168
- function originalSettings(settings) {
1169
- settings.oDec = settings.mDec;
1170
- settings.oPad = settings.aPad;
1171
- settings.oBracket = settings.nBracket;
1172
- settings.oSep = settings.aSep;
1173
- settings.oSign = settings.aSign;
1174
- return settings;
1175
- }
1176
-
1177
- /**
1178
- * original settings saved for use when eDec & nSep options are being used
1179
- * taken from Quirksmode
1180
- */
1181
-
1182
- function readCookie(name) {
1183
- var nameEQ = name + "=",
1184
- ca = document.cookie.split(';'),
1185
- i = 0,
1186
- c = '';
1187
- for (i; i < ca.length; i += 1) {
1188
- c = ca[i];
1189
- while (c.charAt(0) === ' ') {
1190
- c = c.substring(1, c.length);
1191
- }
1192
- if (c.indexOf(nameEQ) === 0) {
1193
- return c.substring(nameEQ.length, c.length);
1194
- }
1195
- }
1196
- return null;
1197
- }
1198
-
1199
- /**
1200
- * Test if sessionStorage is supported - taken from moderizr
1201
- */
1202
-
1203
- function storageTest() {
1204
- var mod = 'modernizr';
1205
- try {
1206
- sessionStorage.setItem(mod, mod);
1207
- sessionStorage.removeItem(mod);
1208
- return true;
1209
- } catch (e) {
1210
- return false;
1211
- }
1212
- }
1213
-
1214
- /**
1215
- * creates or removes sessionStorage or cookie depending on browser support
1216
- */
1217
-
1218
- function autoSave($this, settings, toDo) {
1219
- if (settings.aStor) {
1220
- var storedName = ($this[0].name !== '' && $this[0].name !== undefined) ? 'AUTO_' + decodeURIComponent($this[0].name) : 'AUTO_' + $this[0].id;
1221
- if (storageTest() === false) { /** sets cookie for browser that do not support sessionStorage IE 6 & ie7 */
1222
- if (toDo === 'get') {
1223
- return readCookie(storedName);
1224
- }
1225
- if (toDo === 'set') {
1226
- document.cookie = storedName + '=' + settings.rawValue + '; expires= ; path=/';
1227
- }
1228
- if (toDo === 'wipe') {
1229
- var date = new Date();
1230
- date.setTime(date.getTime() + (-1 * 24 * 60 * 60 * 1000));
1231
- var expires = "; expires=" + date.toGMTString();
1232
- document.cookie = storedName + '="" ;' + expires + '; path=/';
1233
- }
1234
- } else {
1235
- if (toDo === 'get') {
1236
- return sessionStorage.getItem(storedName);
1237
- }
1238
- if (toDo === 'set') {
1239
- sessionStorage.setItem(storedName, settings.rawValue);
1240
- }
1241
- if (toDo === 'wipe') {
1242
- sessionStorage.removeItem(storedName);
1243
- }
1244
- }
1245
- }
1246
- return;
1247
- }
1248
-
1249
- /**
1250
- * Methods supported by autoNumeric
1251
- */
1252
- var methods = {
1253
-
1254
- /**
1255
- * Method to initiate autoNumeric and attached the settings (default and options passed as a parameter
1256
- * $(someSelector).autoNumeric('init'); // initiate autoNumeric with defaults
1257
- * $(someSelector).autoNumeric('init', {option}); // initiate autoNumeric with options
1258
- * $(someSelector).autoNumeric(); // initiate autoNumeric with defaults
1259
- * $(someSelector).autoNumeric({option}); // initiate autoNumeric with options
1260
- * options passes as a parameter example '{aSep: '.', aDec: ',', aSign: '€ '}
1261
- */
1262
- init: function (options) {
1263
- return this.each(function () {
1264
- var $this = $(this),
1265
- /** attempt to grab 'autoNumeric' settings, if they don't exist returns "undefined". */
1266
- settings = $this.data('autoNumeric'),
1267
- /** attempt to grab HTML5 data, if they don't exist we'll get "undefined".*/
1268
- tagData = $this.data(),
1269
- /** supported input types*/
1270
- $input = $this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])');
1271
- $.each(tagData, function (key, value) {
1272
- if (typeof value === 'number') {
1273
- tagData[key] = value.toString();
1274
- }
1275
- });
1276
- if (typeof settings !== 'object') { /** If we couldn't grab settings, create them from defaults and passed options. */
1277
- settings = $.extend({}, $.fn.autoNumeric.defaults, tagData, options, {
1278
- aNum: '0123456789',
1279
- onOff: false,
1280
- runOnce: false,
1281
- rawValue: '',
1282
- trailingNegative: false,
1283
- caretFix: false,
1284
- throwInput: true,
1285
- tagList: ['b', 'caption', 'cite', 'code', 'dd', 'del', 'div', 'dfn', 'dt', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ins', 'kdb', 'label', 'li', 'option', 'output', 'p', 'q', 's', 'sample', 'span', 'strong', 'td', 'th', 'u', 'var']
1286
- }); /** Merge defaults, tagData and options */
1287
- if (settings.aDec === settings.aSep) {
1288
- throwErr('autoNumeric will not function properly when the decimal character aDec: "' + settings.aDec + '" and thousand separator aSep: "' + settings.aSep + '" are the same character', settings.debug);
1289
- }
1290
- $.each(settings, function (key, value) {
1291
- if (value === 'true' || value === 'false') {
1292
- settings[key] = (value === 'true') ? true : false;
1293
- }
1294
- });
1295
- $this.data('autoNumeric', settings); /** Save our new settings */
1296
- } else {
1297
- return this;
1298
- }
1299
- settings = originalSettings(settings); /** original settings saved for use when eDec & nSep options are being used */
1300
- var holder = getHolder($this, settings);
1301
- if (!$input && $this.prop('tagName').toLowerCase() === 'input') { /** checks for non-supported input types */
1302
- throwErr('The input type "' + $this.prop('type') + '" is not supported by autoNumeric()', settings.debug);
1303
- }
1304
- if ($.inArray($this.prop('tagName').toLowerCase(), settings.tagList) === -1 && $this.prop('tagName').toLowerCase() !== 'input') { /** checks for non-supported tags */
1305
- throwErr('The "<' + $this.prop('tagName').toLowerCase() + '>" is not supported by autoNumeric()', settings.debug);
1306
- }
1307
- if (settings.aDec === settings.aSep) { /** checks if the decimal and thousand are characters are the same */
1308
- throwErr('autoNumeric will not function properly when the decimal character aDec: "' + settings.aDec + '" and thousand separator aSep: "' + settings.aSep + '" are the same character', settings.debug);
1309
- }
1310
- if (settings.eDec < settings.mDec && settings.eDec !== null) { /** checks the extended decimal places "eDec" is greater than the normal decimal places "mDec" */
1311
- throwErr('autoNumeric will not function properly when the extended decimal places "eDec: ' + settings.eDec + '" is greater than mDec: "' + settings.mDec + '" value', settings.debug);
1312
- }
1313
- /** routine to format default value on page load */
1314
- if (settings.runOnce === false && settings.aForm) {
1315
- var setValue = true;
1316
- if ($input) {
1317
- /** checks for page reload from back button
1318
- * also checks for ASP.net form post back
1319
- * the following HTML data attribute is REQUIRED (data-an-default="same value as the value attribute")
1320
- * example: <asp:TextBox runat="server" id="someID" text="1234.56" data-an-default="1234.56">
1321
- */
1322
- if ((settings.anDefault !== null && settings.anDefault.toString() !== $this.val()) || (settings.anDefault === null && $this.val() !== '' && $this.val() !== $this.attr('value')) || ($this.val() !== '' && $this.attr('type') === 'hidden' && !$.isNumeric($this.val().replace(',', '.')))) {
1323
- if (settings.eDec !== null && settings.aStor) {
1324
- settings.rawValue = autoSave($this, settings, 'get');
1325
- }
1326
- if (!settings.aStor) {
1327
- var toStrip = '';
1328
- if (settings.nBracket !== null && settings.aNeg !== '') {
1329
- settings.onOff = true;
1330
- toStrip = negativeBracket($this.val(), settings);
1331
- } else {
1332
- toStrip = $this.val();
1333
- }
1334
- settings.rawValue = ((settings.pNeg === 's' || (settings.pSign === 's' && settings.pNeg !== 'p')) && settings.aNeg !== '' && $this.val().indexOf('-') > -1) ? '-' + autoStrip(toStrip, settings) : autoStrip(toStrip, settings);
1335
- }
1336
- setValue = false;
1337
- }
1338
- if ($this.val() === '' && settings.wEmpty === 'focus') {
1339
- setValue = false;
1340
- }
1341
- if ($this.val() === '' && settings.wEmpty === 'always') {
1342
- $this.val(settings.aSign);
1343
- setValue = false;
1344
- }
1345
- if (setValue && $this.val() !== '' && $this.val() === $this.attr('value')) {
1346
- $this.autoNumeric('set', $this.val());
1347
- }
1348
- }
1349
- if ($.inArray($this.prop('tagName').toLowerCase(), settings.tagList) !== -1 && $this.text() !== '') {
1350
- if (settings.anDefault !== null) {
1351
- if (settings.anDefault === $this.text()) {
1352
- $this.autoNumeric('set', $this.text());
1353
- }
1354
- } else {
1355
- $this.autoNumeric('set', $this.text());
1356
- }
1357
- }
1358
- }
1359
- settings.runOnce = true;
1360
- if ($input) { /**input types supported "text", "hidden", "tel" and no type*/
1361
- $this.on('focusin.autoNumeric', function () {
1362
- holder = getHolder($this);
1363
- var $settings = holder.settingsClone;
1364
- $settings.onOff = true;
1365
- if ($settings.nBracket !== null && $settings.aNeg !== '') {
1366
- $this.val(negativeBracket($this.val(), $settings));
1367
- }
1368
- if ($settings.nSep === true) {
1369
- $settings.aSep = '';
1370
- $settings.aSign = '';
1371
- }
1372
- if ($settings.eDec !== null) {
1373
- $settings.mDec = $settings.eDec;
1374
- $this.autoNumeric('set', $settings.rawValue);
1375
- } else {
1376
- $this.autoNumeric('set', $settings.rawValue);
1377
- }
1378
- holder.inVal = $this.val();
1379
- holder.lastVal = $this.val();
1380
- var onEmpty = checkEmpty(holder.inVal, $settings, true);
1381
- if ((onEmpty !== null && onEmpty !== '') && $settings.wEmpty === 'focus') {
1382
- $this.val(onEmpty);
1383
- }
1384
- });
1385
- $this.on('keydown.autoNumeric', function (e) {
1386
- holder = getHolder($this);
1387
- if (holder.that.readOnly) {
1388
- holder.processed = true;
1389
- return true;
1390
- }
1391
- /** The below streamed code / comment allows the "enter" keydown to throw a change() event */
1392
- /** if (e.keyCode === 13 && holder.inVal !== $this.val()){
1393
- $this.change();
1394
- holder.inVal = $this.val();
1395
- }*/
1396
- holder.init(e);
1397
- if (holder.skipAllways(e)) {
1398
- holder.processed = true;
1399
- return true;
1400
- }
1401
- if (holder.processAllways()) {
1402
- holder.processed = true;
1403
- holder.formatQuick(e);
1404
- if (($this.val() !== holder.lastVal) && holder.settingsClone.throwInput) {
1405
- $this.trigger('input'); /** throws input event in deletion character */
1406
- }
1407
- holder.lastVal = $this.val();
1408
- holder.settingsClone.throwInput = true;
1409
- e.preventDefault();
1410
- return false;
1411
- }
1412
- holder.formatted = false;
1413
- return true;
1414
- });
1415
- $this.on('keypress.autoNumeric', function (e) {
1416
- if (e.shiftKey && e.keyCode === 45) { /** FF fix for Shift && insert paste event */
1417
- return;
1418
- }
1419
- holder = getHolder($this);
1420
- var processed = holder.processed;
1421
- holder.init(e);
1422
- if (holder.skipAllways(e)) {
1423
- return true;
1424
- }
1425
- if (processed) {
1426
- e.preventDefault();
1427
- return false;
1428
- }
1429
- if (holder.processAllways() || holder.processKeypress()) {
1430
- holder.formatQuick(e);
1431
- if (($this.val() !== holder.lastVal) && holder.settingsClone.throwInput) {
1432
- $this.trigger('input'); /** throws input event on adding character */
1433
- }
1434
- holder.lastVal = $this.val();
1435
- holder.settingsClone.throwInput = true;
1436
- e.preventDefault();
1437
- return;
1438
- }
1439
- holder.formatted = false;
1440
- });
1441
- $this.on('keyup.autoNumeric', function (e) {
1442
- holder = getHolder($this);
1443
- holder.init(e);
1444
- var skip = holder.skipAllways(e),
1445
- tab = holder.kdCode;;
1446
- holder.kdCode = 0;
1447
- delete holder.valuePartsBeforePaste;
1448
- if ($this[0].value === holder.settingsClone.aSign) { /** added to properly place the caret when only the currency sign is present */
1449
- if (holder.settingsClone.pSign === 's') {
1450
- setElementSelection(this, 0, 0);
1451
- } else {
1452
- setElementSelection(this, holder.settingsClone.aSign.length, holder.settingsClone.aSign.length);
1453
- }
1454
- } else if (tab === 9) {
1455
- setElementSelection(this, 0, $this.val().length);
1456
- }
1457
- if ($this[0].value === holder.settingsClone.aSuffix) {
1458
- setElementSelection(this, 0, 0);
1459
- }
1460
- if (holder.settingsClone.rawValue === '' && holder.settingsClone.aSign !== '' && holder.settingsClone.aSuffix !== '') {
1461
- setElementSelection(this, 0, 0);
1462
- }
1463
- if (holder.settingsClone.eDec !== null && holder.settingsClone.aStor) { /** saves the extended decimal to preserve the data when navigating away from the page */
1464
- autoSave($this, settings, 'set');
1465
- }
1466
- if (skip) {
1467
- return true;
1468
- }
1469
- if (this.value === '') {
1470
- return true;
1471
- }
1472
- if (!holder.formatted) {
1473
- holder.formatQuick(e);
1474
- }
1475
- });
1476
- $this.on('focusout.autoNumeric', function () {
1477
- holder = getHolder($this);
1478
- var value = $this.val(),
1479
- origValue = value,
1480
- $settings = holder.settingsClone;
1481
- $settings.onOff = false;
1482
- if ($settings.aStor) {
1483
- autoSave($this, $settings, 'set');
1484
- }
1485
- if ($settings.nSep === true) {
1486
- $settings.aSep = $settings.oSep;
1487
- $settings.aSign = $settings.oSign;
1488
- }
1489
- if ($settings.eDec !== null) {
1490
- $settings.mDec = $settings.oDec;
1491
- $settings.aPad = $settings.oPad;
1492
- $settings.nBracket = $settings.oBracket;
1493
- }
1494
- if (value !== '') {
1495
- value = autoStrip(value, $settings);
1496
- if ($settings.trailingNegative) {
1497
- value = '-' + value;
1498
- $settings.trailingNegative = false;
1499
- }
1500
- if (checkEmpty(value, $settings) === null && autoCheck(value, $settings, $this[0])) {
1501
- value = fixNumber(value, $settings.aDec, $settings.aNeg);
1502
- $settings.rawValue = value;
1503
- value = autoRound(value, $settings);
1504
- value = presentNumber(value, $settings.aDec, $settings.aNeg);
1505
- } else {
1506
- value = $settings.rawValue;
1507
- }
1508
- } else {
1509
- $settings.rawValue = '';
1510
- }
1511
- var groupedValue = checkEmpty(value, $settings, false);
1512
- if (groupedValue === null) {
1513
- groupedValue = autoGroup(value, $settings);
1514
- }
1515
- if (groupedValue !== origValue) {
1516
- $this.val(groupedValue);
1517
- }
1518
- if (groupedValue !== holder.inVal) {
1519
- $this.change();
1520
- delete holder.inVal;
1521
- }
1522
- });
1523
- $this.closest('form').on('submit.autoNumeric', function () {
1524
- holder = getHolder($this);
1525
- if (holder) {
1526
- var $settings = holder.settingsClone;
1527
- if ($settings.unSetOnSubmit) {
1528
- $this.val($settings.rawValue);
1529
- }
1530
- }
1531
- });
1532
- }
1533
- });
1534
- },
1535
-
1536
- /**
1537
- * method to remove settings and stop autoNumeric() - does not remove the formatting
1538
- * $(someSelector).autoNumeric('destroy'); // destroys autoNumeric
1539
- * no parameters accepted
1540
- */
1541
- destroy: function () {
1542
- return $(this).each(function () {
1543
- var $this = autoGet($(this)),
1544
- settings = $this.data('autoNumeric');
1545
- if (typeof settings === 'object') {
1546
- $this.val('');
1547
- autoSave($this, settings, 'wipe');
1548
- $this.removeData('autoNumeric');
1549
- $this.off('autoNumeric');
1550
- }
1551
- });
1552
- },
1553
-
1554
- /**
1555
- * method to clear the value and sessionStorage or cookie depending on browser supports
1556
- * $(someSelector).autoNumeric('wipe'); // removes session storage and cookies from memory
1557
- * no parameters accepted
1558
- */
1559
- wipe: function () {
1560
- return $(this).each(function () {
1561
- var $this = autoGet($(this)),
1562
- settings = $this.data('autoNumeric');
1563
- if (typeof settings === 'object') {
1564
- $this.val('');
1565
- settings.rawValue = '';
1566
- autoSave($this, settings, 'wipe');
1567
- }
1568
- });
1569
- },
1570
-
1571
- /**
1572
- * method to update settings - can be call as many times
1573
- * $(someSelector).autoNumeric('update', {options}); // updates the settings
1574
- * options passed as a parameter example '{aSep: '.', aDec: ',', aSign: '€ '}
1575
- */
1576
- update: function (options) {
1577
- return $(this).each(function () {
1578
- var $this = autoGet($(this)),
1579
- settings = $this.data('autoNumeric');
1580
- if (typeof settings !== 'object') {
1581
- throwErr('Initializing autoNumeric is required prior to calling the "update" method', true);
1582
- }
1583
- var strip = $this.autoNumeric('get');
1584
- settings = $.extend(settings, options);
1585
- settings = originalSettings(settings);
1586
- getHolder($this, settings, true);
1587
- if (settings.aDec === settings.aSep) {
1588
- throwErr('autoNumeric will not function properly when the decimal character aDec: "' + settings.aDec + '" and thousand separator aSep: "' + settings.aSep + '" are the same character', settings.debug);
1589
- }
1590
- $this.data('autoNumeric', settings);
1591
- if ($this.val() !== '' || $this.text() !== '') {
1592
- return $this.autoNumeric('set', strip);
1593
- }
1594
- return;
1595
- });
1596
- },
1597
-
1598
- /**
1599
- * method to format value sent as a parameter ""
1600
- * $(someSelector).autoNumeric('set', 'value'}); // formats the value being passed
1601
- * value passed as a string - can be a integer '1234' or double '1234.56789'
1602
- * must contain only numbers and one decimal (period) character
1603
- */
1604
- set: function (valueIn) {
1605
- return $(this).each(function () {
1606
- if (valueIn === null || isNaN(valueIn)) {
1607
- return;
1608
- }
1609
- var $this = autoGet($(this)),
1610
- settings = $this.data('autoNumeric'),
1611
- value = valueIn.toString(),
1612
- $input = $this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])');
1613
- if (typeof settings !== 'object') {
1614
- throwErr('Initializing autoNumeric is required prior to calling the "set" method', true);
1615
- }
1616
- /** allows locale decimal separator to be a comma - no thousand separator allowed */
1617
- value = convertLocale(value);
1618
- /** Throws an error if the value being set is not numeric */
1619
- if (!$.isNumeric(Number(value))) {
1620
- throwErr('The value "' + value + '" being "set" is not numeric and has caused a error to be thrown', settings.debug);
1621
- }
1622
- if (value !== '') {
1623
- if (autoCheck(value, settings)) {
1624
- if ($input && settings.eDec !== null) {
1625
- settings.rawValue = value;
1626
- }
1627
- if ($input || $.inArray($this.prop('tagName').toLowerCase(), settings.tagList) !== -1) { /** checks if the value falls within the min max range */
1628
- value = autoRound(value, settings);
1629
- if (settings.eDec === null) {
1630
- settings.rawValue = value;
1631
- }
1632
- value = presentNumber(value, settings.aDec, settings.aNeg);
1633
- value = autoGroup(value, settings);
1634
- }
1635
- if (settings.aStor && settings.eDec !== null) {
1636
- autoSave($this, settings, 'set');
1637
- }
1638
- } else {
1639
- settings.rawValue = '';
1640
- autoSave($this, settings, 'wipe');
1641
- value = '';
1642
- throwErr('The value being set falls outside the min: "' + settings.vMin + ' max: "' + settings.vMax + '" ) settings for this element', settings.debug);
1643
- return '';
1644
- }
1645
- } else {
1646
- return '';
1647
- }
1648
- if ($input) {
1649
- return $this.val(value);
1650
- }
1651
- if ($.inArray($this.prop('tagName').toLowerCase(), settings.tagList) !== -1) {
1652
- return $this.text(value);
1653
- }
1654
- return false;
1655
- });
1656
- },
1657
-
1658
- /**
1659
- * method to un-format inputs - handy to use right before form submission
1660
- * $(someSelector).autoNumeric('unSet'); // parameter optional
1661
- * $(someSelector).autoNumeric('unSet', 'komma'); // returns string '1234,56' with a komma as the decimal character
1662
- * $(someSelector).autoNumeric('unSet', 'checkOptions'); // returns string '1234.56' or '1234.56' depending of the format setting for the input
1663
- */
1664
- unSet: function (outPut) {
1665
- return $(this).each(function () {
1666
- var $this = autoGet($(this)),
1667
- settings = $this.data('autoNumeric');
1668
- if (typeof settings === 'object') {
1669
- settings.onOff = true;
1670
- $this.val($this.autoNumeric('get', outPut));
1671
- }
1672
- });
1673
- },
1674
-
1675
- /**
1676
- * method to re-format inputs - handy to use right after form submission
1677
- * $(someSelector).autoNumeric('reSet'); // no parameters accepted
1678
- * this is called after the 'unSet' method to reformat the input
1679
- */
1680
- reSet: function () {
1681
- return $(this).each(function () {
1682
- var $this = autoGet($(this)),
1683
- settings = $this.data('autoNumeric');
1684
- if (typeof settings === 'object') {
1685
- $this.autoNumeric('set', $this.val());
1686
- }
1687
- });
1688
- },
1689
-
1690
- /**
1691
- * method to get the unformatted that accepts up to one parameter
1692
- * $(someSelector).autoNumeric('get'); no parameter used - values returned as ISO numeric string "1234.56" where the decimal character is a period
1693
- * $(someSelector).autoNumeric('get', 'asKomma'); values returned as strings "nnnn,nn" where the comma / komma is the decimal character
1694
- * $(someSelector).autoNumeric('get', 'checkOptions'); values returned as strings - either as "nnnn.nn" or "nnnn,mm" depending of the format setting for the input
1695
- * only the first element in the selector is returned
1696
- */
1697
- get: function (outPut) {
1698
- var $this = autoGet($(this)),
1699
- settings = $this.data('autoNumeric'),
1700
- $input = $this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])');
1701
- if (typeof settings !== 'object') {
1702
- throwErr('Initializing autoNumeric is required prior to calling the "get" method', true);
1703
- }
1704
- var getValue = '';
1705
- /** determine the element type then use .eq(0) selector to grab the value of the first element in selector */
1706
- if ($input) {
1707
- getValue = $this.eq(0).val();
1708
- } else if ($.inArray($this.prop('tagName').toLowerCase(), settings.tagList) !== -1) {
1709
- getValue = $this.eq(0).text();
1710
- } else {
1711
- throwErr('The "<' + $this.prop('tagName').toLowerCase() + '>" is not supported by autoNumeric()', settings.debug);
1712
- }
1713
- if (settings.rawValue !== '') {
1714
- getValue = settings.rawValue;
1715
- }
1716
- if (!((/\d/).test(getValue) || Number(getValue) === 0) && settings.wEmpty === 'focus') {
1717
- return '';
1718
- }
1719
- if (getValue !== '' && settings.nBracket !== null) {
1720
- settings.onOff = true;
1721
- getValue = negativeBracket(getValue, settings);
1722
- }
1723
- getValue = fixNumber(getValue, settings.aDec, settings.aNeg);
1724
- if (Number(getValue) === 0 && settings.lZero !== 'keep') {
1725
- getValue = '0';
1726
- }
1727
- if (outPut === ',') {
1728
- getValue = getValue.replace('.', ',');
1729
- }
1730
- if (outPut === '.-' && getValue.indexOf('-') > -1) {
1731
- getValue = getValue.replace('-', '') + '-';
1732
- }
1733
- if (outPut === ',-' && getValue.indexOf('-') > -1) {
1734
- getValue = getValue.replace('.', ',').replace('-', '') + '-';
1735
- }
1736
- return getValue; /** returned Numeric String */
1737
- },
1738
-
1739
- /**
1740
- * The 'getString' method used jQuerys .serialize() method that creates a text string in standard URL-encoded notation
1741
- * it then loops through the string and un-formats the inputs with autoNumeric
1742
- * $(someSelector).autoNumeric('getString'); no parameter used - values returned as ISO numeric string "1234.56" where the decimal character is a period
1743
- * $(someSelector).autoNumeric('getString', 'asKomma'); values returned as strings "nnnn,nn" where the comma / komma is the decimal character
1744
- * $(someSelector).autoNumeric('getString', 'checkOptions'); values returned as strings - either as "nnnn.nn" or "nnnn,mm" depending of the format setting for the input
1745
- */
1746
- getString: function (outPut) {
1747
- var $this = autoGet($(this)),
1748
- formFields = $this.serialize(),
1749
- formParts = formFields.split('&'),
1750
- formIndex = $('form').index($this),
1751
- allFormElements = $('form:eq(' + formIndex + ')'),
1752
- aiIndex = [],
1753
- /** all input index */
1754
- scIndex = [],
1755
- /** successful control index */
1756
- rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i,
1757
- /* from jQuery serialize method */
1758
- rsubmittable = /^(?:input|select|textarea|keygen)/i,
1759
- /* from jQuery serialize method */
1760
- rcheckableType = /^(?:checkbox|radio)$/i,
1761
- rnonAutoNumericTypes = /^(?:button|checkbox|color|date|datetime|datetime-local|email|file|image|month|number|password|radio|range|reset|search|submit|time|url|week)/i,
1762
- count = 0;
1763
- /*jslint unparam: true*/
1764
- /** index of successful elements */
1765
- $.each(allFormElements[0], function (i, field) {
1766
- if (field.name !== '' && rsubmittable.test(field.localName) && !rsubmitterTypes.test(field.type) && !field.disabled && (field.checked || !rcheckableType.test(field.type))) {
1767
- scIndex.push(count);
1768
- count = count + 1;
1769
- } else {
1770
- scIndex.push(-1);
1771
- }
1772
- });
1773
- /** index of all inputs tags except checkbox */
1774
- count = 0;
1775
- $.each(allFormElements[0], function (i, field) {
1776
- if (field.localName === 'input' && (field.type === '' || field.type === 'text' || field.type === 'hidden' || field.type === 'tel')) {
1777
- aiIndex.push(count);
1778
- count = count + 1;
1779
- } else {
1780
- aiIndex.push(-1);
1781
- if (field.localName === 'input' && rnonAutoNumericTypes.test(field.type)) {
1782
- count = count + 1;
1783
- }
1784
- }
1785
- });
1786
- $.each(formParts, function (i, miniParts) {
1787
- miniParts = formParts[i].split('=');
1788
- var scElement = $.inArray(i, scIndex);
1789
- if (scElement > -1 && aiIndex[scElement] > -1) {
1790
- var testInput = $('form:eq(' + formIndex + ') input:eq(' + aiIndex[scElement] + ')'),
1791
- settings = testInput.data('autoNumeric');
1792
- if (typeof settings === 'object') {
1793
- if (miniParts[1] !== null) {
1794
- miniParts[1] = $('form:eq(' + formIndex + ') input:eq(' + aiIndex[scElement] + ')').autoNumeric('get', outPut).toString();
1795
- formParts[i] = miniParts.join('=');
1796
- }
1797
- }
1798
- }
1799
- });
1800
- /*jslint unparam: false*/
1801
- return formParts.join('&');
1802
- },
1803
-
1804
- /**
1805
- * The 'getString' method used jQuerys .serializeArray() method that creates array or objects that can be encoded as a JSON string
1806
- * it then loops through the string and un-formats the inputs with autoNumeric
1807
- * $(someSelector).autoNumeric('getArray'); no parameter used - values returned as ISO numeric string "1234.56" where the decimal character is a period
1808
- * $(someSelector).autoNumeric('getArray', 'asKomma'); values returned as strings "nnnn,nn" where the comma / komma is the decimal character
1809
- * $(someSelector).autoNumeric('getArray', 'checkOptions'); values returned as strings - either as "nnnn.nn" or "nnnn,mm" depending of the format setting for the input
1810
- */
1811
- getArray: function (outPut) {
1812
- var $this = autoGet($(this)),
1813
- formFields = $this.serializeArray(),
1814
- formIndex = $('form').index($this),
1815
- allFormElements = $('form:eq(' + formIndex + ')'),
1816
- aiIndex = [],
1817
- /* all input index */
1818
- scIndex = [],
1819
- /* successful control index */
1820
- rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i,
1821
- /* from jQuery serialize method */
1822
- rsubmittable = /^(?:input|select|textarea|keygen)/i,
1823
- /* from jQuery serialize method */
1824
- rcheckableType = /^(?:checkbox|radio)$/i,
1825
- rnonAutoNumericTypes = /^(?:button|checkbox|color|date|datetime|datetime-local|email|file|image|month|number|password|radio|range|reset|search|submit|time|url|week)/i,
1826
- count = 0;
1827
- /*jslint unparam: true*/
1828
- /* index of successful elements */
1829
- $.each(allFormElements[0], function (i, field) {
1830
- if (field.name !== '' && rsubmittable.test(field.localName) && !rsubmitterTypes.test(field.type) && !field.disabled && (field.checked || !rcheckableType.test(field.type))) {
1831
- scIndex.push(count);
1832
- count = count + 1;
1833
- } else {
1834
- scIndex.push(-1);
1835
- }
1836
- });
1837
- /* index of all inputs tags */
1838
- count = 0;
1839
- $.each(allFormElements[0], function (i, field) {
1840
- if (field.localName === 'input' && (field.type === '' || field.type === 'text' || field.type === 'hidden' || field.type === 'tel')) {
1841
- aiIndex.push(count);
1842
- count = count + 1;
1843
- } else {
1844
- aiIndex.push(-1);
1845
- if (field.localName === 'input' && rnonAutoNumericTypes.test(field.type)) {
1846
- count = count + 1;
1847
- }
1848
- }
1849
- });
1850
- $.each(formFields, function (i, field) {
1851
- var scElement = $.inArray(i, scIndex);
1852
- if (scElement > -1 && aiIndex[scElement] > -1) {
1853
- var testInput = $('form:eq(' + formIndex + ') input:eq(' + aiIndex[scElement] + ')'),
1854
- settings = testInput.data('autoNumeric');
1855
- if (typeof settings === 'object') {
1856
- field.value = $('form:eq(' + formIndex + ') input:eq(' + aiIndex[scElement] + ')').autoNumeric('get', outPut).toString();
1857
- }
1858
- }
1859
- });
1860
- /*jslint unparam: false*/
1861
- return formFields;
1862
- },
1863
-
1864
- /**
1865
- * The 'getSteetings returns the object with autoNumeric settings for those who need to look under the hood
1866
- * $(someSelector).autoNumeric('getSettings'); // no parameters accepted
1867
- * $(someSelector).autoNumeric('getSettings').aDec; // return the aDec setting as a string - ant valid setting can be used
1868
- */
1869
- getSettings: function () {
1870
- var $this = autoGet($(this));
1871
- return $this.eq(0).data('autoNumeric');
1872
- }
1873
- };
1874
-
1875
- /**
1876
- * autoNumeric function
1877
- */
1878
- $.fn.autoNumeric = function (method) {
1879
- if (methods[method]) {
1880
- return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
1881
- }
1882
- if (typeof method === 'object' || !method) {
1883
- return methods.init.apply(this, arguments);
1884
- }
1885
- throwErr('Method "' + method + '" is not supported by autoNumeric()', true);
1886
- };
1887
-
1888
- /**
1889
- * Defaults are public - these can be overridden by the following:
1890
- * HTML5 data attributes
1891
- * Options passed by the 'init' or 'update' methods
1892
- * Use jQuery's $.extend method for global changes - also a great way to pass ASP.NET current culture settings
1893
- */
1894
- $.fn.autoNumeric.defaults = {
1895
- /** allowed thousand separator characters
1896
- * comma = ','
1897
- * period "full stop" = '.'
1898
- * apostrophe is escaped = '\''
1899
- * space = ' '
1900
- * none = ''
1901
- * NOTE: do not use numeric characters
1902
- */
1903
- aSep: ',',
1904
- /** when true => when the input has focus only the decimal character is visible
1905
- */
1906
- nSep: false,
1907
- /** digital grouping for the thousand separator used in Format
1908
- * dGroup: '2', results in 99,99,99,999 India's lakhs
1909
- * dGroup: '2s', results in 99,999,99,99,999 India's lakhs scaled
1910
- * dGroup: '3', results in 999,999,999 default
1911
- * dGroup: '4', results in 9999,9999,9999 used in some Asian countries
1912
- */
1913
- dGroup: '3',
1914
- /** allowed decimal separator characters
1915
- * period "full stop" = '.'
1916
- * comma = ','
1917
- */
1918
- aDec: '.',
1919
- /** allow to declare alternative decimal separator which is automatically replaced by aDec
1920
- * developed for countries the use a comma ',' as the decimal character
1921
- * and have keyboards\numeric pads that have a period 'full stop' as the decimal characters (Spain is an example)
1922
- */
1923
- altDec: null,
1924
- /** aSign = allowed currency symbol
1925
- * Must be in quotes aSign: '$'
1926
- * space to the right of the currency symbol aSign: '$ '
1927
- * space to the left of the currency symbol aSign: ' $'
1928
- */
1929
- aSign: '',
1930
- /** pSign = placement of currency sign as a p=prefix or s=suffix
1931
- * for prefix pSign: 'p' (default)
1932
- * for suffix pSign: 's'
1933
- */
1934
- pSign: 'p',
1935
- /** placement of negative sign relative to the aSign option l=left, r=right, p=prefix & s=suffix
1936
- * -1,234.56 => defaults no options required
1937
- * 1,234.56- => {pNeg: 's'}
1938
- * -$1,234.56 => {aSign: '$'}
1939
- * $-1,234.56 => {aSign: '$', pNeg: 'r'}
1940
- * $1,234.56- => {aSign: '$', pNeg: 's'}
1941
- * 1,234.56-$ => {aSign: '$', pSign: 's'}
1942
- * 1,234.56$- => {aSign: '$', pSign: 's', pNeg: 'r'}
1943
- * -1,234.56$ => {aSign: '$', pSign: 's', pNeg: 'p'}
1944
- */
1945
- pNeg: 'l',
1946
- /** Additional suffix
1947
- * Must be in quotes aSuffix: 'gross', a space is allowed aSuffix: ' dollars'
1948
- * Numeric characters and negative sign not allowed'
1949
- */
1950
- aSuffix: '',
1951
- /** maximum possible value
1952
- * value must be enclosed in quotes and use the period for the decimal point
1953
- * value must be larger than vMin
1954
- */
1955
- vMax: '9999999999999.99',
1956
- /** minimum possible value
1957
- * value must be enclosed in quotes and use the period for the decimal point
1958
- * value must be smaller than vMax
1959
- */
1960
- vMin: '-9999999999999.99',
1961
- /** Maximum number of decimal places = used to override decimal places set by the vMin & vMax values
1962
- * value must be enclosed in quotes example mDec: '3',
1963
- */
1964
- mDec: null,
1965
- /** Expanded decimal places visible when input has focus
1966
- * value must be enclosed in quotes example mDec: '3',
1967
- */
1968
- eDec: null,
1969
- /** Set to true to allow the eDec value to be saved with sessionStorage
1970
- * if ie 6 or 7 the value will be saved as a session cookie
1971
- */
1972
- aStor: false,
1973
- /** method used for rounding
1974
- * mRound: 'S', Round-Half-Up Symmetric (default)
1975
- * mRound: 'A', Round-Half-Up Asymmetric
1976
- * mRound: 's', Round-Half-Down Symmetric (lower case s)
1977
- * mRound: 'a', Round-Half-Down Asymmetric (lower case a)
1978
- * mRound: 'B', Round-Half-Even "Bankers Rounding"
1979
- * mRound: 'U', Round Up "Round-Away-From-Zero"
1980
- * mRound: 'D', Round Down "Round-Toward-Zero" - same as truncate
1981
- * mRound: 'C', Round to Ceiling "Toward Positive Infinity"
1982
- * mRound: 'F', Round to Floor "Toward Negative Infinity"
1983
- * mRound: 'N05' Rounds to the nearest .05
1984
- * mRound: 'U05' Rounds up to next .05
1985
- * mRound: 'D05' Rounds down to next .05
1986
- */
1987
- mRound: 'S',
1988
- /** controls decimal padding
1989
- * aPad: true - always Pad decimals with zeros
1990
- * aPad: false - does not pad with zeros.
1991
- * aPad: `some number` - pad decimals with zero to number different from mDec
1992
- * thanks to Jonas Johansson for the suggestion
1993
- */
1994
- aPad: true,
1995
- /** places brackets on negative value -$ 999.99 to (999.99)
1996
- * visible only when the field does NOT have focus the left and right symbols should be enclosed in quotes and separated by a comma
1997
- * nBracket: null - (default)
1998
- * nBracket: '(,)', nBracket: '[,]', nBracket: '<,>' or nBracket: '{,}'
1999
- */
2000
- nBracket: null,
2001
- /** Displayed on empty string ""
2002
- * wEmpty: 'focus' - (default) currency sign displayed and the input receives focus
2003
- * wEmpty: 'press' - currency sign displays on any key being pressed
2004
- * wEmpty: 'always' - always displays the currency sign
2005
- */
2006
- wEmpty: 'focus',
2007
- /** controls leading zero behavior
2008
- * lZero: 'allow', - allows leading zeros to be entered. Zeros will be truncated when entering additional digits. On focusout zeros will be deleted.
2009
- * lZero: 'deny', - allows only one leading zero on values less than one
2010
- * lZero: 'keep', - allows leading zeros to be entered. on focusout zeros will be retained.
2011
- */
2012
- lZero: 'allow',
2013
- /** determine if the default value will be formatted on page ready.
2014
- * true = automatically formats the default value on page ready
2015
- * false = will not format the default value
2016
- */
2017
- aForm: true,
2018
- /** determine if the select all keyboard command will select
2019
- * the complete input text or only the input numeric value
2020
- * if the currency symbol is between the numeric value and the negative sign only the numeric value will selected
2021
- */
2022
- sNumber: false,
2023
- /** helper option for ASP.NET postback
2024
- * should be the value of the unformatted default value
2025
- * examples:
2026
- * no default value='' {anDefault: ''}
2027
- * value=1234.56 {anDefault: '1234.56'}
2028
- */
2029
- anDefault: null,
2030
- /** removes formatting on submit event
2031
- * this output format: positive nnnn.nn, negative -nnnn.nn
2032
- * review the 'unSet' method for other formats
2033
- */
2034
- unSetOnSubmit: false,
2035
- /** error handling function
2036
- * true => all errors are thrown - helpful in site development
2037
- * false => throws errors when calling methods prior to the supported element has been initialized be autoNumeric
2038
- */
2039
- debug: true
2040
- };
2041
- }));