autonumeric-rails 1.9.27 → 1.9.33

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module Autonumeric
2
2
  module Rails
3
- VERSION = '1.9.27'
3
+ VERSION = '1.9.33'
4
4
  end
5
5
  end
@@ -6,8 +6,10 @@ describe 'Autonumeric-rails', type: :feature, js: true do
6
6
 
7
7
  let(:params) { {aSign: 'USD ', mDec: 1}.to_json }
8
8
 
9
- before { visit "/#{url}#{record_id}" }
10
- before { wait_for_jquery }
9
+ before {
10
+ visit "/#{url}#{record_id}"
11
+ wait_for_jquery
12
+ }
11
13
 
12
14
  context 'Through form helper' do
13
15
  let(:url) { 'static_fields' }
@@ -5,12 +5,7 @@
5
5
 
6
6
  <script>
7
7
  jQuery(function() {
8
- var new_field;
9
- new_field = $('<input>').attr('id', 'record_field1').attr('name', 'record[field1]');
10
- new_field.attr('data-autonumeric', 'true').data('autonumeric', 'true');
11
- <% if @record.field1 %>
12
- new_field.val('<%= @record.field1.to_s %>');
13
- <% end %>
8
+ var new_field = '<input id="record_field1" name="record[field1]" data-autonumeric="true" value="<%= @record.field1.to_s %>">';
14
9
  $('form').prepend(new_field);
15
10
 
16
11
  $(document).trigger('refresh_autonumeric');
@@ -21,6 +21,10 @@ require 'headless'
21
21
  Capybara.ignore_hidden_elements = false
22
22
 
23
23
  RSpec.configure do |config|
24
+ # Focus
25
+ config.run_all_when_everything_filtered = true
26
+ config.filter_run focus: true
27
+
24
28
  # Use fixtures
25
29
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
26
30
 
@@ -2,7 +2,7 @@
2
2
  * autoNumeric.js
3
3
  * @author: Bob Knothe
4
4
  * @author: Sokolov Yura
5
- * @version: 1.9.27 - 2014-12-07 GMT 5:30 PM
5
+ * @version: 1.9.33 - 2015-02-15 GMT 11:00 PM
6
6
  *
7
7
  * Created by Robert J. Knothe on 2010-10-25. Please report any bugs to https://github.com/BobKnothe/autoNumeric
8
8
  * Created by Sokolov Yura on 2010-11-07
@@ -36,7 +36,7 @@
36
36
  "use strict";
37
37
  /*jslint browser: true*/
38
38
  /*global jQuery: false*/
39
- /* Cross browser routine for getting selected range/cursor position
39
+ /*Cross browser routine for getting selected range/cursor position
40
40
  */
41
41
  function getElementSelection(that) {
42
42
  var position = {};
@@ -104,7 +104,6 @@
104
104
  */
105
105
  function autoCode($this, settings) {
106
106
  runCallbacks($this, settings);
107
- settings.oEvent = null;
108
107
  settings.tagList = ['b', 'caption', 'cite', 'code', 'dd', 'del', 'div', 'dfn', 'dt', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ins', 'kdb', 'label', 'li', 'output', 'p', 'q', 's', 'sample', 'span', 'strong', 'td', 'th', 'u', 'var'];
109
108
  var vmax = settings.vMax.toString().split('.'),
110
109
  vmin = (!settings.vMin && settings.vMin !== 0) ? [] : settings.vMin.toString().split('.');
@@ -185,36 +184,17 @@
185
184
  /**
186
185
  * places or removes brackets on negative values
187
186
  */
188
- function negativeBracket(s, nBracket, oEvent) { /** oEvent = settings.oEvent */
189
- nBracket = nBracket.split(',');
190
- if (oEvent === 'set' || oEvent === 'focusout') {
187
+ function negativeBracket(s, nBracket, hasFocus) {
188
+ nBracket = nBracket.split(',');
189
+ if (hasFocus === false && s.charAt(0) === '-') {
191
190
  s = s.replace('-', '');
192
191
  s = nBracket[0] + s + nBracket[1];
193
- } else if ((oEvent === 'get' || oEvent === 'focusin' || oEvent === 'pageLoad') && s.charAt(0) === nBracket[0]) {
192
+ } else if (hasFocus === true && s.charAt(0) === nBracket[0]) {
194
193
  s = s.replace(nBracket[0], '-');
195
194
  s = s.replace(nBracket[1], '');
196
195
  }
197
196
  return s;
198
197
  }
199
- /**
200
- * truncate decimal part of a number
201
- */
202
- function truncateDecimal(s, aDec, mDec) {
203
- if (aDec && mDec) {
204
- var parts = s.split(aDec);
205
- /** truncate decimal part to satisfying length
206
- * cause we would round it anyway */
207
- if (parts[1] && parts[1].length > mDec) {
208
- if (mDec > 0) {
209
- parts[1] = parts[1].substring(0, mDec);
210
- s = parts.join(aDec);
211
- } else {
212
- s = parts[0];
213
- }
214
- }
215
- }
216
- return s;
217
- }
218
198
  /**
219
199
  * prepare number string to be converted to real number
220
200
  */
@@ -273,21 +253,6 @@
273
253
  }
274
254
  return s;
275
255
  }
276
- /**
277
- * checking that number satisfy format conditions
278
- * and lays between settings.vMin and settings.vMax
279
- * and the string length does not exceed the digits in settings.vMin and settings.vMax
280
- */
281
- function autoCheck(s, settings) {
282
- s = autoStrip(s, settings);
283
- s = truncateDecimal(s, settings.aDec, settings.mDec);
284
- s = fixNumber(s, settings.aDec, settings.aNeg);
285
- var value = +s;
286
- if (settings.oEvent === 'set' && (value < settings.vMin || value > settings.vMax)) {
287
- $.error("The value (" + value + ") from the 'set' method falls outside of the vMin / vMax range");
288
- }
289
- return value >= settings.vMin && value <= settings.vMax;
290
- }
291
256
  /**
292
257
  * private function to check for empty value
293
258
  */
@@ -324,17 +289,17 @@
324
289
  var ivSplit = iv.split(settings.aDec);
325
290
  if (settings.altDec && ivSplit.length === 1) {
326
291
  ivSplit = iv.split(settings.altDec);
327
- } /** assigns the whole number to the a varibale (s) */
292
+ } /** assigns the whole number to the a variable (s) */
328
293
  var s = ivSplit[0];
329
294
  if (settings.aSep) {
330
- while (digitalGroup.test(s)) { /** re-inserts the thousand sepparator via a regualer expression */
295
+ while (digitalGroup.test(s)) { /** re-inserts the thousand separator via a regular expression */
331
296
  s = s.replace(digitalGroup, '$1' + settings.aSep + '$2');
332
297
  }
333
298
  }
334
299
  if (settings.mDec !== 0 && ivSplit.length > 1) {
335
300
  if (ivSplit[1].length > settings.mDec) {
336
301
  ivSplit[1] = ivSplit[1].substring(0, settings.mDec);
337
- } /** joins the whole number with the deciaml value */
302
+ } /** joins the whole number with the decimal value */
338
303
  iv = s + settings.aDec + ivSplit[1];
339
304
  } else { /** if whole numbers only */
340
305
  iv = s;
@@ -347,8 +312,9 @@
347
312
  iv = settings.aNeg + iv;
348
313
  }
349
314
  }
350
- if (settings.oEvent === 'set' && testNeg < 0 && settings.nBracket !== null) { /** removes the negative sign and places brackets */
351
- iv = negativeBracket(iv, settings.nBracket, settings.oEvent);
315
+ if (settings.setEvent && testNeg < 0 && settings.nBracket !== null) { /** removes the negative sign and places brackets */
316
+ iv = negativeBracket(iv, settings.nBracket, settings.hasFocus);
317
+ settings.setEvent = false;
352
318
  }
353
319
  return iv;
354
320
  }
@@ -389,9 +355,11 @@
389
355
  if ((+iv > 0 && settings.lZero !== 'keep') || (iv.length > 0 && settings.lZero === 'allow')) { /** trims leading zero's if needed */
390
356
  iv = iv.replace(/^0*(\d)/, '$1');
391
357
  }
392
- var dPos = iv.lastIndexOf('.'), /** virtual decimal position */
393
- vdPos = (dPos === -1) ? iv.length - 1 : dPos, /** checks decimal places to determine if rounding is required */
394
- cDec = (iv.length - 1) - vdPos; /** check if no rounding is required */
358
+ var dPos = iv.lastIndexOf('.'),
359
+ /** virtual decimal position */
360
+ vdPos = (dPos === -1) ? iv.length - 1 : dPos,
361
+ /** checks decimal places to determine if rounding is required */
362
+ cDec = (iv.length - 1) - vdPos; /** check if no rounding is required */
395
363
  if (cDec <= settings.mDec) {
396
364
  ivRounded = iv; /** check if we need to pad with zeros */
397
365
  if (cDec < rDec) {
@@ -469,6 +437,40 @@
469
437
  ivRounded = truncateZeros(ivArray.join('')); /** return rounded value */
470
438
  return (+ivRounded === 0) ? ivRounded : nSign + ivRounded;
471
439
  }
440
+ /**
441
+ * truncate decimal part of a number
442
+ */
443
+ function truncateDecimal(s, settings, paste) {
444
+ var aDec = settings.aDec,
445
+ mDec = settings.mDec;
446
+ s = (paste === 'paste') ? autoRound(s, settings) : s;
447
+ if (aDec && mDec) {
448
+ var parts = s.split(aDec);
449
+ /** truncate decimal part to satisfying length
450
+ * cause we would round it anyway */
451
+ if (parts[1] && parts[1].length > mDec) {
452
+ if (mDec > 0) {
453
+ parts[1] = parts[1].substring(0, mDec);
454
+ s = parts.join(aDec);
455
+ } else {
456
+ s = parts[0];
457
+ }
458
+ }
459
+ }
460
+ return s;
461
+ }
462
+ /**
463
+ * checking that number satisfy format conditions
464
+ * and lays between settings.vMin and settings.vMax
465
+ * and the string length does not exceed the digits in settings.vMin and settings.vMax
466
+ */
467
+ function autoCheck(s, settings) {
468
+ s = autoStrip(s, settings);
469
+ s = truncateDecimal(s, settings);
470
+ s = fixNumber(s, settings.aDec, settings.aNeg);
471
+ var value = +s;
472
+ return value >= settings.vMin && value <= settings.vMax;
473
+ }
472
474
  /**
473
475
  * Holder object for field properties
474
476
  */
@@ -552,13 +554,13 @@
552
554
  /**
553
555
  * set part of number to value keeping position of cursor
554
556
  */
555
- setValueParts: function (left, right) {
557
+ setValueParts: function (left, right, paste) {
556
558
  var settingsClone = this.settingsClone,
557
559
  parts = this.normalizeParts(left, right),
558
560
  new_value = parts.join(''),
559
561
  position = parts[0].length;
560
562
  if (autoCheck(new_value, settingsClone)) {
561
- new_value = truncateDecimal(new_value, settingsClone.aDec, settingsClone.mDec);
563
+ new_value = truncateDecimal(new_value, settingsClone, paste);
562
564
  if (position > new_value.length) {
563
565
  position = new_value.length;
564
566
  }
@@ -615,7 +617,7 @@
615
617
  oldParts = this.valuePartsBeforePaste;
616
618
  delete this.valuePartsBeforePaste; /** try to strip pasted value first */
617
619
  parts[0] = parts[0].substr(0, oldParts[0].length) + autoStrip(parts[0].substr(oldParts[0].length), this.settingsClone);
618
- if (!this.setValueParts(parts[0], parts[1])) {
620
+ if (!this.setValueParts(parts[0], parts[1], 'paste')) {
619
621
  this.value = oldParts.join('');
620
622
  this.setPosition(oldParts[0].length, false);
621
623
  }
@@ -829,7 +831,6 @@
829
831
  }
830
832
  return $(obj);
831
833
  }
832
-
833
834
  function getHolder($that, settings, update) {
834
835
  var data = $that.data('autoNumeric');
835
836
  if (!data) {
@@ -847,10 +848,11 @@
847
848
  init: function (options) {
848
849
  return this.each(function () {
849
850
  var $this = $(this),
850
- settings = $this.data('autoNumeric'), /** attempt to grab 'autoNumeric' settings, if they don't exist returns "undefined". */
851
+ settings = $this.data('autoNumeric'),
852
+ /** attempt to grab 'autoNumeric' settings, if they don't exist returns "undefined". */
851
853
  tagData = $this.data(); /** attempt to grab HTML5 data, if they don't exist we'll get "undefined".*/
852
854
  if (typeof settings !== 'object') { /** If we couldn't grab settings, create them from defaults and passed options. */
853
- settings = $.extend({}, $.fn.autoNumeric.defaults, tagData, options); /** Merge defaults, tagData and options */
855
+ settings = $.extend({}, $.fn.autoNumeric.defaults, tagData, options, {setEvent: false, hasFocus: false}); /** Merge defaults, tagData and options */
854
856
  if (settings.aDec === settings.aSep) {
855
857
  $.error("autoNumeric will not function properly when the decimal character aDec: '" + settings.aDec + "' and thousand separator aSep: '" + settings.aSep + "' are the same character");
856
858
  return this;
@@ -865,7 +867,7 @@
865
867
  $.error("The <" + $this.prop('tagName').toLowerCase() + "> is not supported by autoNumeric()");
866
868
  return this;
867
869
  }
868
- if (settings.runOnce === false && settings.aForm) {/** routine to format default value on page load */
870
+ if (settings.runOnce === false && settings.aForm) { /** routine to format default value on page load */
869
871
  if ($this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])')) {
870
872
  var setValue = true;
871
873
  if ($this[0].value === '' && settings.wEmpty === 'empty') {
@@ -876,7 +878,7 @@
876
878
  $this[0].value = settings.aSign;
877
879
  setValue = false;
878
880
  }
879
- if (setValue && $this[0].value.indexOf(settings.aSep) === -1) {
881
+ if (setValue && $this[0].value === $this.prop('defaultValue')) {
880
882
  $this.autoNumeric('set', $this.val());
881
883
  }
882
884
  }
@@ -902,7 +904,6 @@
902
904
  holder.inVal = $this.val();
903
905
  }*/
904
906
  holder.init(e);
905
- holder.settings.oEvent = 'keydown';
906
907
  if (holder.skipAllways(e)) {
907
908
  holder.processed = true;
908
909
  return true;
@@ -920,7 +921,6 @@
920
921
  var holder = getHolder($this),
921
922
  processed = holder.processed;
922
923
  holder.init(e);
923
- holder.settings.oEvent = 'keypress';
924
924
  if (holder.skipAllways(e)) {
925
925
  return true;
926
926
  }
@@ -938,7 +938,6 @@
938
938
  $this.on('keyup.autoNumeric', function (e) {
939
939
  var holder = getHolder($this);
940
940
  holder.init(e);
941
- holder.settings.oEvent = 'keyup';
942
941
  var skip = holder.skipAllways(e);
943
942
  holder.kdCode = 0;
944
943
  delete holder.valuePartsBeforePaste;
@@ -961,10 +960,10 @@
961
960
  });
962
961
  $this.on('focusin.autoNumeric', function () {
963
962
  var holder = getHolder($this);
964
- holder.settingsClone.oEvent = 'focusin';
963
+ holder.settingsClone.hasFocus = true;
965
964
  if (holder.settingsClone.nBracket !== null) {
966
965
  var checkVal = $this.val();
967
- $this.val(negativeBracket(checkVal, holder.settingsClone.nBracket, holder.settingsClone.oEvent));
966
+ $this.val(negativeBracket(checkVal, holder.settingsClone.nBracket, holder.settingsClone.hasFocus));
968
967
  }
969
968
  holder.inVal = $this.val();
970
969
  var onempty = checkEmpty(holder.inVal, holder.settingsClone, true);
@@ -982,7 +981,7 @@
982
981
  settingsClone = holder.settingsClone,
983
982
  value = $this.val(),
984
983
  origValue = value;
985
- holder.settingsClone.oEvent = 'focusout';
984
+ holder.settingsClone.hasFocus = false;
986
985
  var strip_zero = ''; /** added to control leading zero */
987
986
  if (settingsClone.lZero === 'allow') { /** added to control leading zero */
988
987
  settingsClone.allowLeading = false;
@@ -1002,16 +1001,13 @@
1002
1001
  if (groupedValue === null) {
1003
1002
  groupedValue = autoGroup(value, settingsClone);
1004
1003
  }
1005
- if (groupedValue !== origValue) {
1006
- $this.val(groupedValue);
1007
- }
1008
- if (groupedValue !== holder.inVal) {
1004
+ if (groupedValue !== holder.inVal || groupedValue !== origValue) {
1009
1005
  $this.change();
1006
+ $this.val(groupedValue);
1010
1007
  delete holder.inVal;
1011
1008
  }
1012
1009
  if (settingsClone.nBracket !== null && $this.autoNumeric('get') < 0) {
1013
- holder.settingsClone.oEvent = 'focusout';
1014
- $this.val(negativeBracket($this.val(), settingsClone.nBracket, settingsClone.oEvent));
1010
+ $this.val(negativeBracket($this.val(), settingsClone.nBracket, settingsClone.hasFocus));
1015
1011
  }
1016
1012
  });
1017
1013
  }
@@ -1064,7 +1060,7 @@
1064
1060
  }
1065
1061
  /** routine to handle page re-load from back button */
1066
1062
  if (testValue !== $this.attr('value') && $this.prop('tagName').toLowerCase() === 'input' && settings.runOnce === false) {
1067
- value = (settings.nBracket !== null) ? negativeBracket($this.val(), settings.nBracket, 'pageLoad') : value;
1063
+ value = (settings.nBracket !== null) ? negativeBracket($this.val(), settings.nBracket, settings.hasFocus) : value;
1068
1064
  value = autoStrip(value, settings);
1069
1065
  }
1070
1066
  /** allows locale decimal separator to be a comma */
@@ -1076,7 +1072,7 @@
1076
1072
  return '';
1077
1073
  }
1078
1074
  value = checkValue(value, settings);
1079
- settings.oEvent = 'set';
1075
+ settings.setEvent = true;
1080
1076
  value.toString();
1081
1077
  if (value !== '') {
1082
1078
  value = autoRound(value, settings);
@@ -1104,7 +1100,7 @@
1104
1100
  $.error("You must initialize autoNumeric('init', {options}) prior to calling the 'get' method");
1105
1101
  return this;
1106
1102
  }
1107
- settings.oEvent = 'get';
1103
+ settings.setEvent = false;
1108
1104
  var getValue = '';
1109
1105
  /** determine the element type then use .eq(0) selector to grab the value of the first element in selector */
1110
1106
  if ($this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])')) { /**added hidden type */
@@ -1119,7 +1115,7 @@
1119
1115
  return '';
1120
1116
  }
1121
1117
  if (settings.nBracket !== null && getValue !== '') {
1122
- getValue = negativeBracket(getValue, settings.nBracket, settings.oEvent);
1118
+ getValue = negativeBracket(getValue, settings.nBracket, true);
1123
1119
  }
1124
1120
  if (settings.runOnce || settings.aForm === false) {
1125
1121
  getValue = autoStrip(getValue, settings);
@@ -1141,11 +1137,20 @@
1141
1137
  str = $this.serialize(),
1142
1138
  parts = str.split('&'),
1143
1139
  formIndex = $('form').index($this),
1140
+ inputIndex = [],
1144
1141
  i = 0;
1145
1142
  for (i; i < parts.length; i += 1) {
1146
1143
  var miniParts = parts[i].split('='),
1147
1144
  $field = $('form:eq(' + formIndex + ') input[name="' + decodeURIComponent(miniParts[0]) + '"]'),
1148
1145
  settings = $field.data('autoNumeric');
1146
+ if ($field.length > 1) {
1147
+ if (inputIndex[decodeURIComponent(miniParts[0])] === undefined) {
1148
+ inputIndex[decodeURIComponent(miniParts[0])] = 0;
1149
+ } else {
1150
+ inputIndex[decodeURIComponent(miniParts[0])]++;
1151
+ }
1152
+ $field = $field.eq(inputIndex[decodeURIComponent(miniParts[0])]);
1153
+ }
1149
1154
  if (typeof settings === 'object') {
1150
1155
  if (miniParts[1] !== null) {
1151
1156
  miniParts[1] = $field.autoNumeric('get');
@@ -1164,11 +1169,20 @@
1164
1169
  var isAutoNumeric = false,
1165
1170
  $this = autoGet($(this)),
1166
1171
  formFields = $this.serializeArray(),
1167
- formIndex = $('form').index($this);
1172
+ formIndex = $('form').index($this),
1173
+ inputIndex = [];
1168
1174
  /*jslint unparam: true*/
1169
1175
  $.each(formFields, function (i, field) {
1170
1176
  var $field = $('form:eq(' + formIndex + ') input[name="' + decodeURIComponent(field.name) + '"]'),
1171
1177
  settings = $field.data('autoNumeric');
1178
+ if ($field.length > 1) {
1179
+ if (inputIndex[decodeURIComponent(field.name)] === undefined) {
1180
+ inputIndex[decodeURIComponent(field.name)] = 0;
1181
+ } else {
1182
+ inputIndex[decodeURIComponent(field.name)]++;
1183
+ }
1184
+ $field = $field.eq(inputIndex[decodeURIComponent(field.name)]);
1185
+ }
1172
1186
  if (typeof settings === 'object') {
1173
1187
  if (field.value !== '') {
1174
1188
  field.value = $field.autoNumeric('get').toString();
@@ -1197,8 +1211,7 @@
1197
1211
  }
1198
1212
  $.error('Method "' + method + '" is not supported by autoNumeric()');
1199
1213
  };
1200
-
1201
- /* Make defaults public */
1214
+ /** Make defaults public */
1202
1215
  $.fn.autoNumeric.defaults = {
1203
1216
  /** allowed numeric values
1204
1217
  * please do not modify
@@ -1295,7 +1308,6 @@
1295
1308
  */
1296
1309
  aForm: true,
1297
1310
  /** future use */
1298
- onSomeEvent: function () { }
1311
+ onSomeEvent: function () {}
1299
1312
  };
1300
-
1301
- }(jQuery));
1313
+ }(jQuery));
@@ -1,2 +1,2 @@
1
- //= require autoNumeric-1.9.27.js
1
+ //= require autoNumeric-1.9.33.js
2
2
  //= require autonumeric_ujs.js
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * autonumeric_ujs.js
3
3
  * @author: randoum
4
- * @version: 1.9.27 - 2014-12-22
4
+ * @version: 1.9.33 - 2015-03-04
5
5
  *
6
6
  * Created by Randoum on 2013-08-15. Please report any bugs to https://github.com/randoum/autonumeric-rails
7
7
  *
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autonumeric-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.27
4
+ version: 1.9.33
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-12-22 00:00:00.000000000 Z
12
+ date: 2015-03-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jquery-rails
@@ -254,7 +254,7 @@ files:
254
254
  - spec/spec_helper.rb
255
255
  - spec/support/shared_examples.rb
256
256
  - spec/support/wait_for_jquery.rb
257
- - vendor/assets/javascripts/autoNumeric-1.9.27.js
257
+ - vendor/assets/javascripts/autoNumeric-1.9.33.js
258
258
  - vendor/assets/javascripts/autonumeric.js
259
259
  - vendor/assets/javascripts/autonumeric_ujs.js
260
260
  homepage: https://github.com/randoum/autonumeric-rails