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