forked from customd/jquery-number
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.number.js
598 lines (525 loc) · 18.9 KB
/
jquery.number.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/**
* jQuery number plug-in 2.0.4
* Copyright 2012, Digital Fusion
* Licensed under the MIT license.
* http://opensource.teamdf.com/license/
*
* A jQuery plugin which implements a permutation of phpjs.org's number_format to provide
* simple number formatting, insertion, and as-you-type masking of a number.
*
* @author Sam Sehnert
* @docs http://www.teamdf.com/web/jquery-number-format-redux/196/
*/
(function($){
/**
* Method for selecting a range of characters in an input/textarea.
*
* @param int rangeStart : Where we want the selection to start.
* @param int rangeEnd : Where we want the selection to end.
*
* @return void;
*/
function setSelectionRange( rangeStart, rangeEnd )
{
// Check which way we need to define the text range.
if( this.createTextRange )
{
var range = this.createTextRange();
range.collapse( true );
range.moveStart( 'character', rangeStart );
range.moveEnd( 'character', rangeEnd-rangeStart );
range.select();
}
// Alternate setSelectionRange method for supporting browsers.
else if( this.setSelectionRange )
{
this.focus();
this.setSelectionRange( rangeStart, rangeEnd );
}
}
/**
* Get the selection position for the given part.
*
* @param string part : Options, 'Start' or 'End'. The selection position to get.
*
* @return int : The index position of the selection part.
*/
function getSelection( part )
{
var pos = this.value.length;
// Work out the selection part.
part = ( part.toLowerCase() == 'start' ? 'Start' : 'End' );
if( document.selection ){
// The current selection
var range = document.selection.createRange(), stored_range, selectionStart, selectionEnd;
// We'll use this as a 'dummy'
stored_range = range.duplicate();
// Select all text
//stored_range.moveToElementText( this );
stored_range.expand('textedit');
// Now move 'dummy' end point to end point of original range
stored_range.setEndPoint( 'EndToEnd', range );
// Now we can calculate start and end points
selectionStart = stored_range.text.length - range.text.length;
selectionEnd = selectionStart + range.text.length;
return part == 'Start' ? selectionStart : selectionEnd;
}
else if(typeof(this['selection'+part])!="undefined")
{
pos = this['selection'+part];
}
return pos;
}
/**
* Substitutions for keydown keycodes.
* Allows conversion from e.which to ascii characters.
*/
var _keydown = {
codes : {
188 : 44,
109 : 45,
190 : 46,
191 : 47,
192 : 96,
220 : 92,
222 : 39,
221 : 93,
219 : 91,
173 : 45,
187 : 61, //IE Key codes
186 : 59, //IE Key codes
189 : 45, //IE Key codes
110 : 46 //IE Key codes
},
shifts : {
96 : "~",
49 : "!",
50 : "@",
51 : "#",
52 : "$",
53 : "%",
54 : "^",
55 : "&",
56 : "*",
57 : "(",
48 : ")",
45 : "_",
61 : "+",
91 : "{",
93 : "}",
92 : "|",
59 : ":",
39 : "\"",
44 : "<",
46 : ">",
47 : "?"
}
}
/**
* jQuery number formatter plugin. This will allow you to format numbers on an element.
*
* @params proxied for format_number method.
*
* @return : The jQuery collection the method was called with.
*/
$.fn.number = function( number, decimals, dec_point, thousands_sep ){
// Enter the default thousands separator, and the decimal placeholder.
thousands_sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
dec_point = (typeof dec_point === 'undefined') ? '.' : dec_point;
// Work out the unicode character for the decimal placeholder.
var u_dec = ('\\u'+('0000'+(dec_point.charCodeAt(0).toString(16))).slice(-4)),
regex_dec_num = new RegExp('[^'+u_dec+'0-9]','g'),
regex_dec = new RegExp(u_dec,'g');
// If we've specified to take the number from the target element,
// we loop over the collection, and get the number.
if( number === true )
{
// If this element is a number, then we add a keyup
if( this.is('input:text') )
{
// Return the jquery collection.
return this.on({
/**
* Handles keyup events, re-formatting numbers.
*
* @param object e : the keyup event object.s
*
* @return void;
*/
'keydown.format' : function(e){
// Define variables used in the code below.
var $this = $(this),
data = $this.data('numFormat'),
code = (e.keyCode ? e.keyCode : e.which),
chara = '', //unescape(e.originalEvent.keyIdentifier.replace('U+','%u')),
start = getSelection.apply(this,['start']),
end = getSelection.apply(this,['end']),
val = '';
setPos = false;
if( typeof e.originalEvent.keyIdentifier !== 'undefined' )
{
chara = unescape(e.originalEvent.keyIdentifier.replace('U+','%u'));
}
else
{
//chara = String.fromCharCode(code);
if (_keydown.codes.hasOwnProperty(code)) {
code = _keydown.codes[code];
}
if (!e.shiftKey && (code >= 65 && code <= 90)){
code += 32;
} else if (!e.shiftKey && (code >= 69 && code <= 105)){
code -= 48;
} else if (e.shiftKey && _keydown.shifts.hasOwnProperty(code)){
//get shifted keyCode value
chara = _keydown.shifts[code];
}
if( chara == '' ) chara = String.fromCharCode(code);
}
// Stop executing if the user didn't type a number key, a decimal, or a comma.
if( code !== 8 && chara != dec_point && (code < 48 || code > 57) && (code < 96 || code > 105 ) ) return;
// The whole lot has been selected, or if the field is empty, and the character
if( ( start == 0 && end == this.value.length || $this.val() == 0 ) && !e.metaKey && !e.ctrlKey && !e.altKey && chara.length === 1 && chara != 0 )
{
// Blank out the field, but only if the data object has already been instanciated.
start = end = 1;
this.value = '';
// Reset the cursor position.
data.init = -1;
data.c = -(decimals+1);
setSelectionRange.apply(this, [0,0]);
}
// Otherwise, we need to reset the caret position
// based on the users selection.
else
{
data.c = end-this.value.length;
}
// If the start position is before the decimal point,
// and the user has typed a decimal point, we need to move the caret
// past the decimal place.
if( chara == dec_point && start == this.value.length-decimals-1 )
{
data.c++;
data.init = Math.max(0,data.init);
e.preventDefault();
// Set the selection position.
setPos = this.value.length+data.c;
}
// If the user is just typing the decimal place,
// we simply ignore it.
else if( chara == dec_point )
{
data.init = Math.max(0,data.init);
e.preventDefault();
}
// If hitting the delete key, and the cursor is behind a decimal place,
// we simply move the cursor to the other side of the decimal place.
else if( code == 8 && start == this.value.length-decimals )
{
e.preventDefault();
data.c--;
// Set the selection position.
setPos = this.value.length+data.c;
}
// If hitting the delete key, and the cursor is to the right of the decimal
// (but not directly to the right) we replace the character preceeding the
// caret with a 0.
else if( code == 8 && start > this.value.length-decimals )
{
if( this.value === '' ) return;
// If the character preceeding is not already a 0,
// replace it with one.
if( this.value.slice(start-1, start) != '0' )
{
val = this.value.slice(0, start-1) + '0' + this.value.slice(start);
$this.val(val.replace(regex_dec_num,'').replace(regex_dec,dec_point));
}
e.preventDefault();
data.c--;
// Set the selection position.
setPos = this.value.length+data.c;
}
// If the delete key was pressed, and the character immediately
// before the caret is a thousands_separator character, simply
// step over it.
else if( code == 8 && this.value.slice(start-1, start) == thousands_sep )
{
e.preventDefault();
data.c--;
// Set the selection position.
setPos = this.value.length+data.c;
}
// If the caret is to the right of the decimal place, and the user is entering a
// number, remove the following character before putting in the new one.
else if(
start == end &&
this.value.length > (decimals+1) &&
start > this.value.length-decimals-1 && isFinite(+chara) &&
!e.metaKey && !e.ctrlKey && !e.altKey && chara.length === 1
)
{
// If the character preceeding is not already a 0,
// replace it with one.
if( end === this.value.length )
{
val = this.value.slice(0, start-1);
}
else
{
val = this.value.slice(0, start)+this.value.slice(start+1);
}
// Reset the position.
this.value = val;
setPos = start;
}
// If we need to re-position the characters.
if( setPos !== false )
{
setSelectionRange.apply(this, [setPos, setPos]);
}
// Store the data on the element.
$this.data('numFormat', data);
},
/**
* Handles keyup events, re-formatting numbers.
*
* @param object e : the keyup event object.s
*
* @return void;
*/
'keyup.format' : function(e){
// Store these variables for use below.
var $this = $(this),
data = $this.data('numFormat'),
code = (e.keyCode ? e.keyCode : e.which),
start = getSelection.apply(this,['start']),
setPos;
// Stop executing if the user didn't type a number key, a decimal, or a comma.
if( this.value === '' || (code < 48 || code > 57) && (code < 96 || code > 105 ) && code !== 8 ) return;
// Re-format the textarea.
$this.val($this.val());
// If we haven't marked this item as 'initialised'
// then do so now. It means we should place the caret just
// before the decimal. This will never be un-initialised before
// the decimal character itself is entered.
if( data.init < 1 )
{
start = this.value.length-decimals-( data.init < 0 ? 1 : 0 );
data.c = start-this.value.length;
data.init = 1;
$this.data('numFormat', data);
}
// Increase the cursor position if the caret is to the right
// of the decimal place, and the character pressed isn't the delete key.
else if( start > this.value.length-decimals && code != 8 )
{
data.c++;
// Store the data, now that it's changed.
$this.data('numFormat', data);
}
// Set the selection position.
setPos = this.value.length+data.c;
setSelectionRange.apply(this, [setPos, setPos]);
},
/**
* Reformat when pasting into the field.
*
* @param object e : jQuery event object.
*
* @return false : prevent default action.
*/
'paste.format' : function(e){
// Defint $this. It's used twice!.
var $this = $(this),
original = e.originalEvent,
val = null
// Get the text content stream.
if (window.clipboardData && window.clipboardData.getData) { // IE
val = window.clipboardData.getData('Text');
} else if (original.clipboardData && original.clipboardData.getData) {
val = original.clipboardData.getData('text/plain');
}
// Do the reformat operation.
$this.val(val);
// Stop the actual content from being pasted.
e.preventDefault();
return false;
}
})
// Loop each element (which isn't blank) and do the format.
.each(function(){
var $this = $(this).data('numFormat',{
c : -(decimals+1),
decimals : decimals,
thousands_sep : thousands_sep,
dec_point : dec_point,
regex_dec_num : regex_dec_num,
regex_dec : regex_dec,
init : false
});
// Return if the element is empty.
if( this.value === '' ) return;
// Otherwise... format!!
$this.val($this.val());
});
}
else
{
// return the collection.
return this.each(function(){
var $this = $(this), num = +$this.text().replace(regex_dec_num,'').replace(regex_dec,'.');
$this.number( !isFinite(num) ? 0 : +num, decimals, dec_point, thousands_sep );
});
}
}
// Add this number to the element as text.
return this.text( $.number.apply(window,arguments) );
};
//
// Create .val() hooks to get and set formatted numbers in inputs.
//
// We check if any hooks already exist, and cache
// them in case we need to re-use them later on.
var origHookGet = null, origHookSet = null;
// Check if a text valHook already exists.
if( $.valHooks.text )
{
// Preserve the original valhook function
// we'll call this for values we're not
// explicitly handling.
origHookGet = $.valHooks.text.get;
origHookSet = $.valHooks.text.set;
}
else
{
// Define an object for the new valhook.
$.valHooks.text = {};
}
/**
* Define the valHook to return normalised field data against an input
* which has been tagged by the number formatter.
*
* @param object el : The raw DOM element that we're getting the value from.
*
* @return mixed : Returns the value that was written to the element as a
* javascript number, or undefined to let jQuery handle it normally.
*/
$.valHooks.text.get = function( el ){
// Get the element, and its data.
var $this = $(el), num,
data = $this.data('numFormat');
// Does this element have our data field?
if( !data )
{
// Check if the valhook function already existed
if( $.isFunction( origHookGet ) )
{
// There was, so go ahead and call it
return origHookGet(el);
}
else
{
// No previous function, return undefined to have jQuery
// take care of retrieving the value
return undefined;
}
}
else
{
// Remove formatting, and return as number.
if( el.value === '' ) return '';
// Convert to a number.
num = +(el.value
.replace( data.regex_dec_num, '' )
.replace( data.regex_dec, '.' ));
// If we've got a finite number, return it.
// Otherwise, simply return 0.
// Return as a string... thats what we're
// used to with .val()
return ''+( isFinite( num ) ? num : 0 );
}
}
/**
* A valhook which formats a number when run against an input
* which has been tagged by the number formatter.
*
* @param object el : The raw DOM element (input element).
* @param float : The number to set into the value field.
*
* @return mixed : Returns the value that was written to the element,
* or undefined to let jQuery handle it normally.
*/
$.valHooks.text.set = function( el, val )
{
// Get the element, and its data.
var $this = $(el),
data = $this.data('numFormat');
// Does this element have our data field?
if( !data )
{
// Check if the valhook function already existed
if( $.isFunction( origHookSet ) )
{
// There was, so go ahead and call it
return origHookSet(el,val);
}
else
{
// No previous function, return undefined to have jQuery
// take care of retrieving the value
return undefined;
}
}
else
{
return el.value = $.number( val, data.decimals, data.dec_point, data.thousands_sep )
}
}
/**
* The (modified) excellent number formatting method from PHPJS.org.
* http://phpjs.org/functions/number_format/
*
* @modified by Sam Sehnert (teamdf.com)
* - don't redefine dec_point, thousands_sep... just overwrite with defaults.
* - don't redefine decimals, just overwrite as numeric.
* - Generate regex for normalizing pre-formatted numbers.
*
* @param float number : The number you wish to format, or TRUE to use the text contents
* of the element as the number. Please note that this won't work for
* elements which have child nodes with text content.
* @param int decimals : The number of decimal places that should be displayed. Defaults to 0.
* @param string dec_point : The character to use as a decimal point. Defaults to '.'.
* @param string thousands_sep : The character to use as a thousands separator. Defaults to ','.
*
* @return string : The formatted number as a string.
*/
$.number = function( number, decimals, dec_point, thousands_sep ){
// Set the default values here, instead so we can use them in the replace below.
thousands_sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
dec_point = (typeof dec_point === 'undefined') ? '.' : dec_point;
decimals = !isFinite(+decimals) ? 0 : Math.abs(decimals);
// Work out the unicode representation for the decimal place.
var u_dec = ('\\u'+('0000'+(dec_point.charCodeAt(0).toString(16))).slice(-4));
// Fix the number, so that it's an actual number.
number = (number + '')
.replace(new RegExp(u_dec,'g'),'.')
.replace(new RegExp('[^0-9+\-Ee.]','g'),'');
var n = !isFinite(+number) ? 0 : +number,
s = '',
toFixedFix = function (n, decimals) {
var k = Math.pow(10, decimals);
return '' + Math.round(n * k) / k;
};
// Fix for IE parseFloat(0.55).toFixed(0) = 0;
s = (decimals ? toFixedFix(n, decimals) : '' + Math.round(n)).split('.');
if (s[0].length > 3) {
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, thousands_sep);
}
if ((s[1] || '').length < decimals) {
s[1] = s[1] || '';
s[1] += new Array(decimals - s[1].length + 1).join('0');
}
return s.join(dec_point);
}
})(jQuery);