jquery-number-rails 2.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NzFhODJhNmQzZmMxNGVjMDg3Y2Q0MGEzY2ZhY2VkMjMwNDc4MjZiZg==
5
+ data.tar.gz: !binary |-
6
+ OGU5YTAzYTQ3NDNiNmM0MzdhMDgxZGIyMGIxOGRkM2MyNzAyYzgzOA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NzYyYjc0MWVmOWJkOWU5N2JhMDgxMzQ5NzliODM1ZDAzMWMzMmFmODllNmRj
10
+ MTViMjk1NTk0YjlhMzEzMWJkNzlhZTcwMjA4ZTg5YzdmNjc2ZmEwZGZiYmE4
11
+ ZWExOWZkNmI3NGY3Yzc0M2ZlMjc2NTQ5NDg2YzFlMDNhY2JiMDM=
12
+ data.tar.gz: !binary |-
13
+ MGVmYWIxMzJhNDkyMTRkZDBiMjU5ZjY3NGVlOTJmN2NlMDc1YzgwNWI2Yzdk
14
+ ZDRiNGIwMjU5NjMwYTZmZTBkZmZmZDVhZjc0Yzc4ZWIxZmM2NjAxNTEwYmY0
15
+ N2MwZmEyZmIxZDU5YmQxYjZkNDlhMDI2MmMwY2ZkOTNmNmJiMjY=
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .ruby-version
19
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jquery-number-rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Digital Fusion, http://teamdf.com/
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # jquery-number-rails
2
+
3
+ jQuery Number Plugin By Sam Sehnert, Digital Fusion 2012 now integrated for the Rails asset pipeline.
4
+
5
+ See [jquery-number](https://github.com/teamdf/jquery-number) for more details.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'jquery-number-rails'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install jquery-number-rails
20
+
21
+ Then require `jquery-number` in your `app/assets/javascripts/application.js` file:
22
+
23
+ //= require jquery-number
24
+
25
+ ## Usage
26
+
27
+ See [jquery-number](https://github.com/teamdf/jquery-number).
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jquery-number-rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'jquery-number-rails'
8
+ spec.version = JQuery::Number::Rails::VERSION
9
+ spec.authors = ['Kostya Yegorov']
10
+ spec.email = %w(kostya@skycure.com)
11
+ spec.description = %q{jQuery Number for the Rails asset pipeline}
12
+ spec.summary = %q{This is a jQuery plugin which allows developers to easily format numbers for display use. Allows users to replace numbers inline in a document, or return a formatted number for other uses. By Sam Sehnert, Digital Fusion 2012}
13
+ spec.homepage = 'https://github.com/skycure/jquery-number-rails'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = %w(lib)
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake', '~> 0'
23
+ end
@@ -0,0 +1,13 @@
1
+ require 'jquery-number-rails/version'
2
+
3
+ module JQuery
4
+ module Numbers
5
+ module Rails
6
+ if ::Rails.version < '3.1'
7
+ require 'jquery-number-rails/railtie'
8
+ else
9
+ require 'jquery-number-rails/engine'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ module JQuery
2
+ module Number
3
+ module Rails
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module JQuery
2
+ module Number
3
+ module Rails
4
+ class Railtie < ::Rails::Railtie;
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module JQuery
2
+ module Number
3
+ module Rails
4
+ VERSION = '2.1.3'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ //= require ./jquery.number.js
@@ -0,0 +1,699 @@
1
+ /**
2
+ * jQuery number plug-in 2.1.3
3
+ * Copyright 2012, Digital Fusion
4
+ * Licensed under the MIT license.
5
+ * http://opensource.teamdf.com/license/
6
+ *
7
+ * A jQuery plugin which implements a permutation of phpjs.org's number_format to provide
8
+ * simple number formatting, insertion, and as-you-type masking of a number.
9
+ *
10
+ * @author Sam Sehnert
11
+ * @docs http://www.teamdf.com/web/jquery-number-format-redux/196/
12
+ */
13
+ (function($){
14
+
15
+ "use strict";
16
+
17
+ /**
18
+ * Method for selecting a range of characters in an input/textarea.
19
+ *
20
+ * @param int rangeStart : Where we want the selection to start.
21
+ * @param int rangeEnd : Where we want the selection to end.
22
+ *
23
+ * @return void;
24
+ */
25
+ function setSelectionRange( rangeStart, rangeEnd )
26
+ {
27
+ // Check which way we need to define the text range.
28
+ if( this.createTextRange )
29
+ {
30
+ var range = this.createTextRange();
31
+ range.collapse( true );
32
+ range.moveStart( 'character', rangeStart );
33
+ range.moveEnd( 'character', rangeEnd-rangeStart );
34
+ range.select();
35
+ }
36
+
37
+ // Alternate setSelectionRange method for supporting browsers.
38
+ else if( this.setSelectionRange )
39
+ {
40
+ this.focus();
41
+ this.setSelectionRange( rangeStart, rangeEnd );
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Get the selection position for the given part.
47
+ *
48
+ * @param string part : Options, 'Start' or 'End'. The selection position to get.
49
+ *
50
+ * @return int : The index position of the selection part.
51
+ */
52
+ function getSelection( part )
53
+ {
54
+ var pos = this.value.length;
55
+
56
+ // Work out the selection part.
57
+ part = ( part.toLowerCase() == 'start' ? 'Start' : 'End' );
58
+
59
+ if( document.selection ){
60
+ // The current selection
61
+ var range = document.selection.createRange(), stored_range, selectionStart, selectionEnd;
62
+ // We'll use this as a 'dummy'
63
+ stored_range = range.duplicate();
64
+ // Select all text
65
+ //stored_range.moveToElementText( this );
66
+ stored_range.expand('textedit');
67
+ // Now move 'dummy' end point to end point of original range
68
+ stored_range.setEndPoint( 'EndToEnd', range );
69
+ // Now we can calculate start and end points
70
+ selectionStart = stored_range.text.length - range.text.length;
71
+ selectionEnd = selectionStart + range.text.length;
72
+ return part == 'Start' ? selectionStart : selectionEnd;
73
+ }
74
+
75
+ else if(typeof(this['selection'+part])!="undefined")
76
+ {
77
+ pos = this['selection'+part];
78
+ }
79
+ return pos;
80
+ }
81
+
82
+ /**
83
+ * Substitutions for keydown keycodes.
84
+ * Allows conversion from e.which to ascii characters.
85
+ */
86
+ var _keydown = {
87
+ codes : {
88
+ 188 : 44,
89
+ 109 : 45,
90
+ 190 : 46,
91
+ 191 : 47,
92
+ 192 : 96,
93
+ 220 : 92,
94
+ 222 : 39,
95
+ 221 : 93,
96
+ 219 : 91,
97
+ 173 : 45,
98
+ 187 : 61, //IE Key codes
99
+ 186 : 59, //IE Key codes
100
+ 189 : 45 //IE Key codes
101
+ },
102
+ shifts : {
103
+ 96 : "~",
104
+ 49 : "!",
105
+ 50 : "@",
106
+ 51 : "#",
107
+ 52 : "$",
108
+ 53 : "%",
109
+ 54 : "^",
110
+ 55 : "&",
111
+ 56 : "*",
112
+ 57 : "(",
113
+ 48 : ")",
114
+ 45 : "_",
115
+ 61 : "+",
116
+ 91 : "{",
117
+ 93 : "}",
118
+ 92 : "|",
119
+ 59 : ":",
120
+ 39 : "\"",
121
+ 44 : "<",
122
+ 46 : ">",
123
+ 47 : "?"
124
+ }
125
+ };
126
+
127
+ /**
128
+ * jQuery number formatter plugin. This will allow you to format numbers on an element.
129
+ *
130
+ * @params proxied for format_number method.
131
+ *
132
+ * @return : The jQuery collection the method was called with.
133
+ */
134
+ $.fn.number = function( number, decimals, dec_point, thousands_sep ){
135
+
136
+ // Enter the default thousands separator, and the decimal placeholder.
137
+ thousands_sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
138
+ dec_point = (typeof dec_point === 'undefined') ? '.' : dec_point;
139
+ decimals = (typeof decimals === 'undefined' ) ? 0 : decimals;
140
+
141
+ // Work out the unicode character for the decimal placeholder.
142
+ var u_dec = ('\\u'+('0000'+(dec_point.charCodeAt(0).toString(16))).slice(-4)),
143
+ regex_dec_num = new RegExp('[^'+u_dec+'0-9]','g'),
144
+ regex_dec = new RegExp(u_dec,'g');
145
+
146
+ // If we've specified to take the number from the target element,
147
+ // we loop over the collection, and get the number.
148
+ if( number === true )
149
+ {
150
+ // If this element is a number, then we add a keyup
151
+ if( this.is('input:text') )
152
+ {
153
+ // Return the jquery collection.
154
+ return this.on({
155
+
156
+ /**
157
+ * Handles keyup events, re-formatting numbers.
158
+ *
159
+ * @param object e : the keyup event object.s
160
+ *
161
+ * @return void;
162
+ */
163
+ 'keydown.format' : function(e){
164
+
165
+ // Define variables used in the code below.
166
+ var $this = $(this),
167
+ data = $this.data('numFormat'),
168
+ code = (e.keyCode ? e.keyCode : e.which),
169
+ chara = '', //unescape(e.originalEvent.keyIdentifier.replace('U+','%u')),
170
+ start = getSelection.apply(this,['start']),
171
+ end = getSelection.apply(this,['end']),
172
+ val = '',
173
+ setPos = false;
174
+
175
+ // Webkit (Chrome & Safari) on windows screws up the keyIdentifier detection
176
+ // for numpad characters. I've disabled this for now, because while keyCode munging
177
+ // below is hackish and ugly, it actually works cross browser & platform.
178
+
179
+ // if( typeof e.originalEvent.keyIdentifier !== 'undefined' )
180
+ // {
181
+ // chara = unescape(e.originalEvent.keyIdentifier.replace('U+','%u'));
182
+ // }
183
+ // else
184
+ // {
185
+ if (_keydown.codes.hasOwnProperty(code)) {
186
+ code = _keydown.codes[code];
187
+ }
188
+ if (!e.shiftKey && (code >= 65 && code <= 90)){
189
+ code += 32;
190
+ } else if (!e.shiftKey && (code >= 69 && code <= 105)){
191
+ code -= 48;
192
+ } else if (!e.shiftKey && code == 110){
193
+ chara = dec_point;
194
+ } else if (e.shiftKey && _keydown.shifts.hasOwnProperty(code)){
195
+ //get shifted keyCode value
196
+ chara = _keydown.shifts[code];
197
+ }
198
+
199
+ if( chara == '' ) chara = String.fromCharCode(code);
200
+ // }
201
+
202
+
203
+
204
+
205
+ // Stop executing if the user didn't type a number key, a decimal character, or backspace.
206
+ if( code !== 8 && chara != dec_point && !chara.match(/[0-9]/) )
207
+ {
208
+ var ct = true;
209
+ if (chara.match(/[-]/)) {
210
+ if (!data.isNegative) {
211
+ // User is trying to make this field a negative number
212
+ data.isNegative = true;
213
+ ct = false;
214
+ }
215
+ }
216
+ if (ct) {
217
+ // We need the original keycode now...
218
+ var key = (e.keyCode ? e.keyCode : e.which);
219
+ if( // Allow control keys to go through... (delete, etc)
220
+ key == 46 || key == 8 || key == 9 || key == 27 || key == 13 ||
221
+ // Allow: Ctrl+A, Ctrl+R
222
+ ( (key == 65 || key == 82 ) && ( e.ctrlKey || e.metaKey ) === true ) ||
223
+ // Allow: Ctrl+V, Ctrl+C
224
+ ( (key == 86 || key == 67 ) && ( e.ctrlKey || e.metaKey ) === true ) ||
225
+ // Allow: home, end, left, right
226
+ ( (key >= 35 && key <= 39) )
227
+ ){
228
+ return;
229
+ }
230
+ // But prevent all other keys.
231
+ e.preventDefault();
232
+ return false;
233
+ }
234
+ }
235
+
236
+ // The whole lot has been selected, or if the field is empty...
237
+ if( start == 0 && end == this.value.length || $this.val() == 0 )
238
+ {
239
+ if( code === 8 )
240
+ {
241
+ // Blank out the field, but only if the data object has already been instanciated.
242
+ start = end = 1;
243
+ this.value = '';
244
+
245
+ // Reset the cursor position.
246
+ data.init = (decimals>0?-1:0);
247
+ data.c = (decimals>0?-(decimals+1):0);
248
+ setSelectionRange.apply(this, [0,0]);
249
+ }
250
+ else if( chara === dec_point )
251
+ {
252
+ start = end = 1;
253
+ this.value = '0'+ dec_point + (new Array(decimals+1).join('0'));
254
+
255
+ // Reset the cursor position.
256
+ data.init = (decimals>0?1:0);
257
+ data.c = (decimals>0?-(decimals+1):0);
258
+ }
259
+ else
260
+ {
261
+ // Reset the cursor position.
262
+ data.init = (decimals>0?-1:0);
263
+ data.c = (decimals>0?-(decimals):0);
264
+ }
265
+ }
266
+
267
+ // Otherwise, we need to reset the caret position
268
+ // based on the users selection.
269
+ else
270
+ {
271
+ data.c = end-this.value.length;
272
+ }
273
+
274
+ // If they are trying to delete the negative sign
275
+ if (code == 8 && start <= 1 && data.isNegative)
276
+ {
277
+ e.preventDefault();
278
+ data.isNegative = false;
279
+ data.c--;
280
+ setPos = this.value.length+data.c;
281
+ }
282
+ // If the start position is before the decimal point,
283
+ // and the user has typed a decimal point, we need to move the caret
284
+ // past the decimal place.
285
+ else if( decimals > 0 && chara == dec_point && start == this.value.length-decimals-1 )
286
+ {
287
+ data.c++;
288
+ data.init = Math.max(0,data.init);
289
+ e.preventDefault();
290
+
291
+ // Set the selection position.
292
+ setPos = this.value.length+data.c;
293
+ }
294
+
295
+ // If the user is just typing the decimal place,
296
+ // we simply ignore it.
297
+ else if( chara == dec_point )
298
+ {
299
+ data.init = Math.max(0,data.init);
300
+ e.preventDefault();
301
+ }
302
+
303
+ // If hitting the delete key, and the cursor is behind a decimal place,
304
+ // we simply move the cursor to the other side of the decimal place.
305
+ else if( decimals > 0 && code == 8 && start == this.value.length-decimals )
306
+ {
307
+ e.preventDefault();
308
+ data.c--;
309
+
310
+ // Set the selection position.
311
+ setPos = this.value.length+data.c;
312
+ }
313
+
314
+ // If hitting the delete key, and the cursor is to the right of the decimal
315
+ // (but not directly to the right) we replace the character preceeding the
316
+ // caret with a 0.
317
+ else if( decimals > 0 && code == 8 && start > this.value.length-decimals )
318
+ {
319
+ if( this.value === '' ) return;
320
+
321
+ // If the character preceeding is not already a 0,
322
+ // replace it with one.
323
+ if( this.value.slice(start-1, start) != '0' )
324
+ {
325
+ val = this.value.slice(0, start-1) + '0' + this.value.slice(start);
326
+ $this.val(val.replace(regex_dec_num,'').replace(regex_dec,dec_point));
327
+ }
328
+
329
+ e.preventDefault();
330
+ data.c--;
331
+
332
+ // Set the selection position.
333
+ setPos = this.value.length+data.c;
334
+ }
335
+
336
+ // If the delete key was pressed, and the character immediately
337
+ // before the caret is a thousands_separator character, simply
338
+ // step over it.
339
+ else if( code == 8 && this.value.slice(start-1, start) == thousands_sep )
340
+ {
341
+ e.preventDefault();
342
+ data.c--;
343
+
344
+ // Set the selection position.
345
+ setPos = this.value.length+data.c;
346
+ }
347
+
348
+ // If the caret is to the right of the decimal place, and the user is entering a
349
+ // number, remove the following character before putting in the new one.
350
+ else if(
351
+ decimals > 0 &&
352
+ start == end &&
353
+ this.value.length > decimals+1 &&
354
+ start > this.value.length-decimals-1 && isFinite(+chara) &&
355
+ !e.metaKey && !e.ctrlKey && !e.altKey && chara.length === 1
356
+ )
357
+ {
358
+ // If the character preceeding is not already a 0,
359
+ // replace it with one.
360
+ if( end === this.value.length )
361
+ {
362
+ val = this.value.slice(0, start-1);
363
+ }
364
+ else
365
+ {
366
+ val = this.value.slice(0, start)+this.value.slice(start+1);
367
+ }
368
+
369
+ // Reset the position.
370
+ this.value = val;
371
+ setPos = start;
372
+ }
373
+
374
+ // If we need to re-position the characters.
375
+ if( setPos !== false )
376
+ {
377
+ //console.log('Setpos keydown: ', setPos );
378
+ setSelectionRange.apply(this, [setPos, setPos]);
379
+ }
380
+
381
+ // Store the data on the element.
382
+ $this.data('numFormat', data);
383
+
384
+ },
385
+
386
+ /**
387
+ * Handles keyup events, re-formatting numbers.
388
+ *
389
+ * @param object e : the keyup event object.s
390
+ *
391
+ * @return void;
392
+ */
393
+ 'keyup.format' : function(e){
394
+
395
+ // Store these variables for use below.
396
+ var $this = $(this),
397
+ data = $this.data('numFormat'),
398
+ code = (e.keyCode ? e.keyCode : e.which),
399
+ start = getSelection.apply(this,['start']),
400
+ setPos;
401
+
402
+ // Stop executing if the user didn't type a number key, a decimal, or a comma.
403
+ if( this.value === '' || (code < 48 || code > 57) && (code < 96 || code > 105 ) && code !== 8 && code !== 110 ) return;
404
+
405
+ // Re-format the textarea.
406
+ $this.val($this.val());
407
+
408
+ if( decimals > 0 )
409
+ {
410
+ // If we haven't marked this item as 'initialised'
411
+ // then do so now. It means we should place the caret just
412
+ // before the decimal. This will never be un-initialised before
413
+ // the decimal character itself is entered.
414
+ if( data.init < 1 )
415
+ {
416
+ start = this.value.length-decimals-( data.init < 0 ? 1 : 0 );
417
+ data.c = start-this.value.length;
418
+ data.init = 1;
419
+
420
+ $this.data('numFormat', data);
421
+ }
422
+
423
+ // Increase the cursor position if the caret is to the right
424
+ // of the decimal place, and the character pressed isn't the delete key.
425
+ else if( start > this.value.length-decimals && code != 8 )
426
+ {
427
+ data.c++;
428
+
429
+ // Store the data, now that it's changed.
430
+ $this.data('numFormat', data);
431
+ }
432
+ }
433
+
434
+ //console.log( 'Setting pos: ', start, decimals, this.value.length + data.c, this.value.length, data.c );
435
+
436
+ if (!$this.get(0).value.length) {
437
+ // If they delete the entire contents of the text field, remove the 'negative' variable
438
+ data.isNegative = false;
439
+ } else if (data.isNegative) {
440
+ // Otherwise, we add the - sign to the beginning of the field if it's negative
441
+ $this.get(0).value = '-' + this.value;
442
+ }
443
+
444
+ // Set the selection position.
445
+ setPos = this.value.length+data.c;
446
+ setSelectionRange.apply(this, [setPos, setPos]);
447
+ },
448
+
449
+ /**
450
+ * Reformat when pasting into the field.
451
+ *
452
+ * @param object e : jQuery event object.
453
+ *
454
+ * @return false : prevent default action.
455
+ */
456
+ 'paste.format' : function(e){
457
+
458
+ // Defint $this. It's used twice!.
459
+ var $this = $(this),
460
+ original = e.originalEvent,
461
+ val = null;
462
+
463
+ // Get the text content stream.
464
+ if (window.clipboardData && window.clipboardData.getData) { // IE
465
+ val = window.clipboardData.getData('Text');
466
+ } else if (original.clipboardData && original.clipboardData.getData) {
467
+ val = original.clipboardData.getData('text/plain');
468
+ }
469
+
470
+ // Do the reformat operation.
471
+ $this.val(val);
472
+
473
+ // Stop the actual content from being pasted.
474
+ e.preventDefault();
475
+ return false;
476
+ }
477
+
478
+ })
479
+
480
+ // Loop each element (which isn't blank) and do the format.
481
+ .each(function(){
482
+
483
+ var $this = $(this).data('numFormat',{
484
+ c : -(decimals+1),
485
+ decimals : decimals,
486
+ thousands_sep : thousands_sep,
487
+ dec_point : dec_point,
488
+ regex_dec_num : regex_dec_num,
489
+ regex_dec : regex_dec,
490
+ init : false
491
+ });
492
+
493
+ // Return if the element is empty.
494
+ if( this.value === '' ) return;
495
+
496
+ // Otherwise... format!!
497
+ $this.val($this.val());
498
+ });
499
+ }
500
+ else
501
+ {
502
+ // return the collection.
503
+ return this.each(function(){
504
+ var $this = $(this),
505
+ isNegative = $this.text().match(/^-/) ? -1 : 1,
506
+ num = +$this.text().replace(regex_dec_num,'').replace(regex_dec,'.') * isNegative;
507
+ $this.number( !isFinite(num) ? 0 : +num, decimals, dec_point, thousands_sep );
508
+ });
509
+ }
510
+ }
511
+
512
+ // Add this number to the element as text.
513
+ return this.text( $.number.apply(window,arguments) );
514
+ };
515
+
516
+ //
517
+ // Create .val() hooks to get and set formatted numbers in inputs.
518
+ //
519
+
520
+ // We check if any hooks already exist, and cache
521
+ // them in case we need to re-use them later on.
522
+ var origHookGet = null, origHookSet = null;
523
+
524
+ // Check if a text valHook already exists.
525
+ if( $.isPlainObject( $.valHooks.text ) )
526
+ {
527
+ // Preserve the original valhook function
528
+ // we'll call this for values we're not
529
+ // explicitly handling.
530
+ if( $.isFunction( $.valHooks.text.get ) ) origHookGet = $.valHooks.text.get;
531
+ if( $.isFunction( $.valHooks.text.set ) ) origHookSet = $.valHooks.text.set;
532
+ }
533
+ else
534
+ {
535
+ // Define an object for the new valhook.
536
+ $.valHooks.text = {};
537
+ }
538
+
539
+ /**
540
+ * Define the valHook to return normalised field data against an input
541
+ * which has been tagged by the number formatter.
542
+ *
543
+ * @param object el : The raw DOM element that we're getting the value from.
544
+ *
545
+ * @return mixed : Returns the value that was written to the element as a
546
+ * javascript number, or undefined to let jQuery handle it normally.
547
+ */
548
+ $.valHooks.text.get = function( el ){
549
+
550
+ // Get the element, and its data.
551
+ var $this = $(el), num,
552
+ data = $this.data('numFormat');
553
+
554
+ // Does this element have our data field?
555
+ if( !data )
556
+ {
557
+ // Check if the valhook function already existed
558
+ if( $.isFunction( origHookGet ) )
559
+ {
560
+ // There was, so go ahead and call it
561
+ return origHookGet(el);
562
+ }
563
+ else
564
+ {
565
+ // No previous function, return undefined to have jQuery
566
+ // take care of retrieving the value
567
+ return undefined;
568
+ }
569
+ }
570
+ else
571
+ {
572
+ // Remove formatting, and return as number.
573
+ if( el.value === '' ) return '';
574
+
575
+ // If the first character is a minus sign,
576
+ // we assume the number is negative.
577
+ if (el.value.match(/^-/)) {
578
+ data.isNegative = true;
579
+ }
580
+
581
+ // Convert to a number.
582
+ num = +(el.value
583
+ .replace( data.regex_dec_num, '' )
584
+ .replace( data.regex_dec, '.' ));
585
+
586
+ // If we've got a finite number, return it.
587
+ // Otherwise, simply return 0.
588
+ num = ( isFinite( num ) ? num : 0 );
589
+
590
+ // If it's a negative number, times by -1.
591
+ if (num != 0 && data.isNegative) {
592
+ num *= -1;
593
+ }
594
+
595
+ // Return as a string... thats what we're
596
+ // used to with .val()
597
+ return ''+num;
598
+ }
599
+ };
600
+
601
+ /**
602
+ * A valhook which formats a number when run against an input
603
+ * which has been tagged by the number formatter.
604
+ *
605
+ * @param object el : The raw DOM element (input element).
606
+ * @param float : The number to set into the value field.
607
+ *
608
+ * @return mixed : Returns the value that was written to the element,
609
+ * or undefined to let jQuery handle it normally.
610
+ */
611
+ $.valHooks.text.set = function( el, val )
612
+ {
613
+ // Get the element, and its data.
614
+ var $this = $(el),
615
+ data = $this.data('numFormat');
616
+
617
+ // Does this element have our data field?
618
+ if( !data )
619
+ {
620
+
621
+ // Check if the valhook function already exists
622
+ if( $.isFunction( origHookSet ) )
623
+ {
624
+ // There was, so go ahead and call it
625
+ return origHookSet(el,val);
626
+ }
627
+ else
628
+ {
629
+ // No previous function, return undefined to have jQuery
630
+ // take care of retrieving the value
631
+ return undefined;
632
+ }
633
+ }
634
+ else
635
+ {
636
+ if(val == '')
637
+ {
638
+ return el.value = '';
639
+ }
640
+ // Otherwise, don't worry about other valhooks, just run ours.
641
+ return el.value = $.number( val, data.decimals, data.dec_point, data.thousands_sep );
642
+ }
643
+ };
644
+
645
+ /**
646
+ * The (modified) excellent number formatting method from PHPJS.org.
647
+ * http://phpjs.org/functions/number_format/
648
+ *
649
+ * @modified by Sam Sehnert (teamdf.com)
650
+ * - don't redefine dec_point, thousands_sep... just overwrite with defaults.
651
+ * - don't redefine decimals, just overwrite as numeric.
652
+ * - Generate regex for normalizing pre-formatted numbers.
653
+ *
654
+ * @param float number : The number you wish to format, or TRUE to use the text contents
655
+ * of the element as the number. Please note that this won't work for
656
+ * elements which have child nodes with text content.
657
+ * @param int decimals : The number of decimal places that should be displayed. Defaults to 0.
658
+ * @param string dec_point : The character to use as a decimal point. Defaults to '.'.
659
+ * @param string thousands_sep : The character to use as a thousands separator. Defaults to ','.
660
+ *
661
+ * @return string : The formatted number as a string.
662
+ */
663
+ $.number = function( number, decimals, dec_point, thousands_sep ){
664
+ // Set the default values here, instead so we can use them in the replace below.
665
+ thousands_sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
666
+ dec_point = (typeof dec_point === 'undefined') ? '.' : dec_point;
667
+ decimals = !isFinite(+decimals) ? 0 : Math.abs(decimals);
668
+
669
+ // Work out the unicode representation for the decimal place and thousand sep.
670
+ var u_dec = ('\\u'+('0000'+(dec_point.charCodeAt(0).toString(16))).slice(-4));
671
+ var u_sep = ('\\u'+('0000'+(thousands_sep.charCodeAt(0).toString(16))).slice(-4));
672
+
673
+ // Fix the number, so that it's an actual number.
674
+ number = (number + '')
675
+ .replace('\.', dec_point) // because the number if passed in as a float (having . as decimal point per definition) we need to replace this with the passed in decimal point character
676
+ .replace(new RegExp(u_sep,'g'),'')
677
+ .replace(new RegExp(u_dec,'g'),'.')
678
+ .replace(new RegExp('[^0-9+\-Ee.]','g'),'');
679
+
680
+ var n = !isFinite(+number) ? 0 : +number,
681
+ s = '',
682
+ toFixedFix = function (n, decimals) {
683
+ var k = Math.pow(10, decimals);
684
+ return '' + Math.round(n * k) / k;
685
+ };
686
+
687
+ // Fix for IE parseFloat(0.55).toFixed(0) = 0;
688
+ s = (decimals ? toFixedFix(n, decimals) : '' + Math.round(n)).split('.');
689
+ if (s[0].length > 3) {
690
+ s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, thousands_sep);
691
+ }
692
+ if ((s[1] || '').length < decimals) {
693
+ s[1] = s[1] || '';
694
+ s[1] += new Array(decimals - s[1].length + 1).join('0');
695
+ }
696
+ return s.join(dec_point);
697
+ }
698
+
699
+ })(jQuery);
@@ -0,0 +1,12 @@
1
+ /*! jQuery number 2.1.3 (c) github.com/teamdf/jquery-number | opensource.teamdf.com/license */
2
+ (function(h){function r(f,a){if(this.createTextRange){var c=this.createTextRange();c.collapse(true);c.moveStart("character",f);c.moveEnd("character",a-f);c.select()}else if(this.setSelectionRange){this.focus();this.setSelectionRange(f,a)}}function s(f){var a=this.value.length;f=f.toLowerCase()=="start"?"Start":"End";if(document.selection){a=document.selection.createRange();var c;c=a.duplicate();c.expand("textedit");c.setEndPoint("EndToEnd",a);c=c.text.length-a.text.length;a=c+a.text.length;return f==
3
+ "Start"?c:a}else if(typeof this["selection"+f]!="undefined")a=this["selection"+f];return a}var q={codes:{188:44,109:45,190:46,191:47,192:96,220:92,222:39,221:93,219:91,173:45,187:61,186:59,189:45,110:46},shifts:{96:"~",49:"!",50:"@",51:"#",52:"$",53:"%",54:"^",55:"&",56:"*",57:"(",48:")",45:"_",61:"+",91:"{",93:"}",92:"|",59:":",39:'"',44:"<",46:">",47:"?"}};h.fn.number=function(f,a,c,k){k=typeof k==="undefined"?",":k;c=typeof c==="undefined"?".":c;a=typeof a==="undefined"?0:a;var j="\\u"+("0000"+
4
+ c.charCodeAt(0).toString(16)).slice(-4),o=RegExp("[^"+j+"0-9]","g"),p=RegExp(j,"g");if(f===true)return this.is("input:text")?this.on({"keydown.format":function(b){var d=h(this),e=d.data("numFormat"),g=b.keyCode?b.keyCode:b.which,m="",i=s.apply(this,["start"]),n=s.apply(this,["end"]),l="";l=false;if(q.codes.hasOwnProperty(g))g=q.codes[g];if(!b.shiftKey&&g>=65&&g<=90)g+=32;else if(!b.shiftKey&&g>=69&&g<=105)g-=48;else if(b.shiftKey&&q.shifts.hasOwnProperty(g))m=q.shifts[g];if(m=="")m=String.fromCharCode(g);
5
+ if(g!==8&&m!=c&&!m.match(/[0-9]/)){d=b.keyCode?b.keyCode:b.which;if(d==46||d==8||d==9||d==27||d==13||(d==65||d==82)&&(b.ctrlKey||b.metaKey)===true||(d==86||d==67)&&(b.ctrlKey||b.metaKey)===true||d>=35&&d<=39)return;b.preventDefault();return false}if(i==0&&n==this.value.length||d.val()==0)if(g===8){i=n=1;this.value="";e.init=a>0?-1:0;e.c=a>0?-(a+1):0;r.apply(this,[0,0])}else if(m===c){i=n=1;this.value="0"+c+Array(a+1).join("0");e.init=a>0?1:0;e.c=a>0?-(a+1):0}else{if(this.value.length===0){e.init=
6
+ a>0?-1:0;e.c=a>0?-a:0}}else e.c=n-this.value.length;if(a>0&&m==c&&i==this.value.length-a-1){e.c++;e.init=Math.max(0,e.init);b.preventDefault();l=this.value.length+e.c}else if(m==c){e.init=Math.max(0,e.init);b.preventDefault()}else if(a>0&&g==8&&i==this.value.length-a){b.preventDefault();e.c--;l=this.value.length+e.c}else if(a>0&&g==8&&i>this.value.length-a){if(this.value==="")return;if(this.value.slice(i-1,i)!="0"){l=this.value.slice(0,i-1)+"0"+this.value.slice(i);d.val(l.replace(o,"").replace(p,
7
+ c))}b.preventDefault();e.c--;l=this.value.length+e.c}else if(g==8&&this.value.slice(i-1,i)==k){b.preventDefault();e.c--;l=this.value.length+e.c}else if(a>0&&i==n&&this.value.length>a+1&&i>this.value.length-a-1&&isFinite(+m)&&!b.metaKey&&!b.ctrlKey&&!b.altKey&&m.length===1){this.value=l=n===this.value.length?this.value.slice(0,i-1):this.value.slice(0,i)+this.value.slice(i+1);l=i}l!==false&&r.apply(this,[l,l]);d.data("numFormat",e)},"keyup.format":function(b){var d=h(this),e=d.data("numFormat");b=b.keyCode?
8
+ b.keyCode:b.which;var g=s.apply(this,["start"]);if(!(this.value===""||(b<48||b>57)&&(b<96||b>105)&&b!==8)){d.val(d.val());if(a>0)if(e.init<1){g=this.value.length-a-(e.init<0?1:0);e.c=g-this.value.length;e.init=1;d.data("numFormat",e)}else if(g>this.value.length-a&&b!=8){e.c++;d.data("numFormat",e)}d=this.value.length+e.c;r.apply(this,[d,d])}},"paste.format":function(b){var d=h(this),e=b.originalEvent,g=null;if(window.clipboardData&&window.clipboardData.getData)g=window.clipboardData.getData("Text");
9
+ else if(e.clipboardData&&e.clipboardData.getData)g=e.clipboardData.getData("text/plain");d.val(g);b.preventDefault();return false}}).each(function(){var b=h(this).data("numFormat",{c:-(a+1),decimals:a,thousands_sep:k,dec_point:c,regex_dec_num:o,regex_dec:p,init:false});this.value!==""&&b.val(b.val())}):this.each(function(){var b=h(this),d=+b.text().replace(o,"").replace(p,".");b.number(!isFinite(d)?0:+d,a,c,k)});return this.text(h.number.apply(window,arguments))};var t=null,u=null;if(h.isPlainObject(h.valHooks.text)){if(h.isFunction(h.valHooks.text.get))t=
10
+ h.valHooks.text.get;if(h.isFunction(h.valHooks.text.set))u=h.valHooks.text.set}else h.valHooks.text={};h.valHooks.text.get=function(f){var a=h(f).data("numFormat");if(a){if(f.value==="")return"";f=+f.value.replace(a.regex_dec_num,"").replace(a.regex_dec,".");return""+(isFinite(f)?f:0)}else if(h.isFunction(t))return t(f)};h.valHooks.text.set=function(f,a){var c=h(f).data("numFormat");if(c)return f.value=h.number(a,c.decimals,c.dec_point,c.thousands_sep);else if(h.isFunction(u))return u(f,a)};h.number=
11
+ function(f,a,c,k){k=typeof k==="undefined"?",":k;c=typeof c==="undefined"?".":c;a=!isFinite(+a)?0:Math.abs(a);var j="\\u"+("0000"+c.charCodeAt(0).toString(16)).slice(-4),o="\\u"+("0000"+k.charCodeAt(0).toString(16)).slice(-4);f=(f+"").replace(".",c).replace(RegExp(o,"g"),"").replace(RegExp(j,"g"),".").replace(RegExp("[^0-9+-Ee.]","g"),"");f=!isFinite(+f)?0:+f;j="";j=function(p,b){var d=Math.pow(10,b);return""+Math.round(p*d)/d};j=(a?j(f,a):""+Math.round(f)).split(".");if(j[0].length>3)j[0]=j[0].replace(/\B(?=(?:\d{3})+(?!\d))/g,
12
+ k);if((j[1]||"").length<a){j[1]=j[1]||"";j[1]+=Array(a-j[1].length+1).join("0")}return j.join(c)}})(jQuery);
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jquery-number-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Kostya Yegorov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: jQuery Number for the Rails asset pipeline
42
+ email:
43
+ - kostya@skycure.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - jquery-number-rails.gemspec
54
+ - lib/jquery-number-rails.rb
55
+ - lib/jquery-number-rails/engine.rb
56
+ - lib/jquery-number-rails/railtie.rb
57
+ - lib/jquery-number-rails/version.rb
58
+ - vendor/assets/javascripts/jquery-number/index.js
59
+ - vendor/assets/javascripts/jquery-number/jquery.number.js
60
+ - vendor/assets/javascripts/jquery-number/jquery.number.min.js
61
+ homepage: https://github.com/skycure/jquery-number-rails
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.3.0
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: This is a jQuery plugin which allows developers to easily format numbers
85
+ for display use. Allows users to replace numbers inline in a document, or return
86
+ a formatted number for other uses. By Sam Sehnert, Digital Fusion 2012
87
+ test_files: []