source: ether_statistics/web/resources/js/Wijmo.2.2.2/Wijmo-Complete/development-bundle/wijmo/jquery.wijmo.wijinputnumber.js @ 627

Last change on this file since 627 was 621, checked in by vmipsl, 12 years ago

wijmo

File size: 28.0 KB
Line 
1/*globals wijinputcore wijNumberTextProvider wijInputResult
2wijNumberFormat window jQuery*/
3/*
4 *
5 * Wijmo Library 2.2.2
6 * http://wijmo.com/
7 *
8 * Copyright(c) GrapeCity, Inc.  All rights reserved.
9 *
10 * Dual licensed under the Wijmo Commercial or GNU GPL Version 3 licenses.
11 * licensing@wijmo.com
12 * http://wijmo.com/license
13 *
14 *
15 * * Wijmo Inputnumber widget.
16 *
17 * Depends:
18 *      jquery-1.4.2.js
19 *      jquery.ui.core.js
20 *      jquery.ui.widget.js
21 *      jquery.ui.position.js
22 *      jquery.effects.core.js 
23 *      jquery.effects.blind.js
24 *      globalize.js
25 *      jquery.plugin.wijtextselection.js
26 *      jquery.wijmo.wijpopup.js
27 *      jquery.wijmo.wijinputcore.js
28 *
29 */
30(function ($) {
31        "use strict";
32
33        var wijNumberTextProvider, wijNumberFormat;
34
35        $.widget("wijmo.wijinputnumber", $.extend(true, {}, wijinputcore, {
36                options: {
37                        ///     <summary>
38                        ///     Determines the type of the number input.
39                        ///     Possible values are: 'numeric', 'percent', 'currency'.
40                        ///     </summary>
41                        type: 'numeric',
42                        ///     <summary>
43                        ///     Determines the default numeric value.
44                        ///     </summary>
45                        value: null,
46                        ///     <summary>
47                        ///     Determines the minimal value that can be entered for
48                        /// numeric/percent/currency inputs.
49                        ///     </summary>
50                        minValue: -1000000000,
51                        ///     <summary>
52                        ///     Determines the maximum value that can be entered for
53                        /// numeric/percent/currency inputs.
54                        ///     </summary>
55                        maxValue: 1000000000,
56                        ///     <summary>
57                        ///             Indicates whether the thousands group separator will be
58                        ///             inserted between between each digital group
59                        ///             (number of digits in thousands group depends on the
60                        ///             selected Culture).
61                        ///     </summary>
62                        showGroup: false,
63                        ///     <summary>
64                        ///             Indicates the number of decimal places to display.
65                        ///             Possible values are integer from -2 to 8. They are:
66                        ///             useDefault: -2,
67                        ///             asIs: -1,
68                        ///             zero: 0,
69                        ///             one: 1,
70                        ///             two: 2,
71                        ///             three: 3,
72                        ///             four: 4,
73                        ///             five: 5,
74                        ///             six: 6,
75                        ///             seven: 7,
76                        ///             eight: 8
77                        ///     </summary>
78                        decimalPlaces: 2,
79                        ///     <summary>
80                        ///             Determines how much to increase/decrease the input field.
81                        ///     </summary>
82                        increment: 1,
83                        /// <summary>
84                        /// The valueChanged event handler.
85                        /// A function called when the value of the input is changed.
86                        /// Default: null.
87                        /// Type: Function.
88                        /// Code example:
89                        /// $("#element").wijinputnumber({ valueChanged: function (e, arg) { } });
90                        /// </summary>
91                        ///
92                        /// <param name="e" type="Object">jQuery.Event object.</param>
93                        /// <param name="args" type="Object">
94                        /// The data with this event.
95                        /// args.value: The new value.
96                        ///</param>
97                        valueChanged: null,
98                        /// <summary>
99                        /// The valueBoundsExceeded event handler. A function called when
100                        /// the value of the input exceeds the valid range.
101                        /// Default: null.
102                        /// Type: Function.
103                        /// Code example:
104                        /// $("#element").wijinputnumber({ valueBoundsExceeded: function (e) { } });
105                        /// </summary>
106                        ///
107                        /// <param name="e" type="Object">jQuery.Event object.</param>
108                        valueBoundsExceeded: null
109                },
110
111                _createTextProvider: function () {
112                        this._textProvider = new wijNumberTextProvider(this, this.options.type);
113                        this._textProvider._nullvalue = this.element.val() === "" && this.options.value === null;
114                },
115
116                _beginUpdate: function () {
117                        var o = this.options;
118                        this.element.addClass('wijmo-wijinput-numeric');
119
120                        this.element.data({
121                                defaultValue: o.value,
122                                preValue: o.value
123                        }).attr({
124                                'aria-valuemin': o.minValue,
125                                'aria-valuemax': o.maxValue,
126                                'aria-valuenow': o.value || 0
127                        });
128                },
129
130                _onTriggerClicked: function () {
131                        this._popupComboList();
132                },
133
134                _setOption: function (key, value) {
135                        $.Widget.prototype._setOption.apply(this, arguments);
136                        wijinputcore._setOption.apply(this, arguments);
137
138                        switch (key) {
139                        case 'minValue':
140                                this.element.attr('aria-valuemin', value);
141                                this._updateText();
142                                break;
143
144                        case 'maxValue':
145                                this.element.attr('aria-valuemax', value);
146                                this._updateText();
147                                break;
148
149                        case 'value':
150                                this.setValue(value);
151                                this._updateText();
152                                break;
153
154                        case 'showGroup':
155                        case 'decimalPlaces':
156                        case 'culture':
157                                this._textProvider.updateStringFormat();
158                                this._updateText();
159                                break;
160                        }
161                },
162
163                _setData: function (val) {
164                        this.setValue(val);
165                },
166
167                _resetData: function () {
168                        var val = this.element.data('defaultValue');
169                        if (val === undefined || val === null) {
170                                val = this.element.data('elementValue');
171                                if (val === undefined || val === null && val === "") {
172                                        val = 0;
173                                }
174                        }
175
176                        this.setValue(val);
177                },
178
179                _validateData: function () {
180                        if (!this._textProvider.checkAndRepairBounds(true, false)) {
181                                this._updateText();
182                        }
183                },
184
185                _raiseDataChanged: function () {
186                        var v = this.options.value,
187                                prevValue = this.element.data('preValue');
188                        this.element.data('preValue', v);
189                        if (prevValue !== v) {
190                                this.element.attr('aria-valuenow', v);
191                                this._trigger('valueChanged', null, { value: v });
192                        }
193                },
194
195                getValue: function () {
196                        /// <summary>
197                        /// Gets the value.
198                        /// Code example:
199                        /// $(".selector").wijinputnumber("getValue")
200                        /// </summary>
201                        var val = this._textProvider.getValue();
202                        if (val === undefined || val === null) {
203                                val = this.getText(); 
204                        }
205                        return val;
206                },
207
208                setValue: function (val, exact) {
209                        /// <summary>
210                        /// Sets the value.
211                        /// Code example:
212                        /// $(".selector").wijinputnumber("setValue", 10, true)
213                        /// </summary>
214                        try {
215                                exact = !!exact;
216                                if (typeof val === 'boolean') {
217                                        val = val ? '1' : '0';
218                                } else if (typeof val === 'string') {
219                                        val = this._textProvider.tryParseValue(val);
220                                }
221
222                                if (this._textProvider.setValue(val)) {
223                                        this._updateText();
224                                } else {
225                                        if (exact) {
226                                                var prevVal = '', txt;
227                                                prevVal = this.getText();
228                                                this.setText(val);
229                                                val = val.trim();
230                                                txt = this.getText().trim();
231                                                if (txt !== val) {
232                                                        this.setText(prevVal);
233                                                }
234                                        } else {
235                                                this.setText(val);
236                                        }
237                                }
238
239                                return true;
240                        }
241                        catch (e) {
242                                return false;
243                        }
244                },
245
246                isValueNull: function () {
247                        /// <summary>
248                        /// Determines whether the value is in null state.
249                        /// Code example:
250                        /// $(".selector").wijinputnumber("isValueNull")
251                        /// </summary>
252                        try {
253                                return (this._textProvider).isValueNull();
254                        }
255                        catch (e) {
256                                return true;
257                        }
258                },
259
260                getPostValue: function () {
261                        /// <summary>
262                        /// Gets the text value when the container form is posted back to server.
263                        /// Code example:
264                        /// $(".selector").wijinputnumber("getPostValue")
265                        /// </summary>
266                        if (!this._isInitialized()) {
267                                return this.element.val(); 
268                        }
269                        if (this.options.showNullText && this.isValueNull()) {
270                                return "0"; 
271                        }
272
273                        var val = this.options.value ? this.options.value : 0;
274                        if (this.options.type === "percent") {
275                                val = (val / 100).toFixed(10);
276                        }
277
278                        return val.toString();
279                },
280
281                _updateText: function () {
282                        if (!this._isInitialized()) {
283                                return; 
284                        }
285
286                        this.options.value = this._textProvider.getValue();
287                        wijinputcore._updateText.apply(this, arguments);
288                        if (!this._textProvider.checkAndRepairBounds(false, false)) {
289                                this._trigger('valueBoundsExceeded');
290                        }
291                },
292
293                _doSpin: function (up, repeating) {
294                        up = !!up;
295                        repeating = !!repeating;
296
297                        if (!this._allowEdit()) {
298                                return; 
299                        }
300                        if (repeating && this.element.data('breakSpinner')) {
301                                return; 
302                        }
303                        var selRange = this.element.wijtextselection(),
304                                rh = new wijInputResult();
305                        if (this.element.data('focusNotCalledFirstTime') !== -9 && 
306                        (new Date().getTime() - this.element.data('focusNotCalledFirstTime')) < 600) {
307                                this.element.data('focusNotCalledFirstTime', -9);
308                                this.element.data('prevCursorPos', 0);
309                        }
310                        if (this.element.data('prevCursorPos') === -1) {
311                                this.element.data('prevCursorPos', selRange.start);
312                        } else {
313                                selRange.start = (this.element.data('prevCursorPos'));
314                        }
315                        rh.testPosition = selRange.start;
316                        this._textProvider[up ? 'incEnumPart' : 
317                        'decEnumPart'](selRange.start, rh, this.options.increment);
318                        this._updateText();
319                        this.element.data('prevCursorPos', rh.testPosition);
320                        this.selectText(rh.testPosition, rh.testPosition);
321                        if (repeating && !this.element.data('breakSpinner')) {
322                                window.setTimeout($.proxy(function () {
323                                        this._doSpin(up, true); 
324                                }, this), this._calcSpinInterval());
325                        }
326                }
327        }));
328
329
330        //==============================
331        wijNumberTextProvider = function (owner, t) {
332                this.inputWidget = owner;
333                this._type = t;
334                this._stringFormat = new wijNumberFormat(this._type,
335                        this.inputWidget.options.decimalPlaces, 
336                        this.inputWidget.options.showGroup, this._getCulture());
337                this._stringFormat._setValueFromJSFloat(this.getValue());
338        };
339
340        wijNumberTextProvider.prototype = {
341                _type: 'numeric',
342                _stringFormat: null,
343                _nullvalue: false,
344
345                _getCulture: function () {
346                        return this.inputWidget._getCulture();
347                },
348
349                getDecimalSeparator: function () {
350                        return this._getCulture().numberFormat['.'];
351                },
352
353                tryParseValue: function (value) {
354                        return this._stringFormat.tryParseValue(value);
355                },
356
357                toString: function () {
358                        if (this.inputWidget.options.showNullText && 
359                                !this.inputWidget.isFocused() && this.isValueNull()) {
360                                return this.inputWidget.options.nullText;
361                        }
362                        return this._stringFormat.getFormattedValue();
363                },
364
365                isValueNull: function () {
366                        var o = this.inputWidget.options,
367                                nullValue = o.minValue;
368                        //nullValue = Math.max(0, o.minValue);
369
370                        return null === o.value || undefined === o.value || nullValue === o.value || this._nullvalue;
371                },
372
373                set: function (input, rh) {
374                        this.clear();
375                        this.insertAt(input, 0, rh);
376                        return true;
377                },
378
379                clear: function () {
380                        this._stringFormat.clear();
381                },
382
383                checkAndRepairBounds: function (chkAndRepair, chkIsLessOrEqMin) {
384                        var result = true, minValue, maxValue;
385                        if (typeof (chkAndRepair) === 'undefined') {
386                                chkAndRepair = false; 
387                        }
388
389                        minValue = this.inputWidget.options.minValue;
390                        maxValue = this.inputWidget.options.maxValue;
391
392                        if (typeof (chkIsLessOrEqMin) !== 'undefined' && chkIsLessOrEqMin) {
393                                return this._stringFormat.checkMinValue(minValue, false, true);
394                        }
395
396                        if (!this._stringFormat.checkMinValue(minValue, chkAndRepair, false)) {
397                                result = false;
398                        }
399                        if (!this._stringFormat.checkMaxValue(maxValue, chkAndRepair)) {
400                                result = false; 
401                        }
402                        if (this.inputWidget.options.decimalPlaces >= 0) {
403                                this._stringFormat
404                                        .checkDigitsLimits(this.inputWidget.options.decimalPlaces);
405                        }
406
407                        return result;
408                },
409
410                countSubstring: function (txt, subStr) {
411                        var c = 0,
412                                pos = txt.indexOf(subStr);
413                        while (pos !== -1) {
414                                c++;
415                                pos = txt.indexOf(subStr, pos + 1);
416                        }
417                        return c;
418                },
419
420                getAdjustedPositionFromLeft: function (position) {
421                        var currentText = this._stringFormat._currentText, i, ch;
422                        for (i = 0; i < currentText.length; i++) {
423                                ch = currentText.charAt(i);
424                                if (!$.wij.charValidator.isDigit(ch) && 
425                                        (ch !== ',' && ch !== '.') || ch === '0') {
426                                        if (this._stringFormat.isZero()) {
427                                                if (position < i) {
428                                                        position++;
429                                                }
430                                        } else {
431                                                if (position <= i) {
432                                                        position++;
433                                                }
434                                        }
435                                } else {
436                                        break;
437                                }
438                        }
439
440                        return position;
441                },
442
443                getDecimalSeparatorPos: function () {
444                        var currentText = this._stringFormat._currentText;
445                        return currentText.indexOf(this.getDecimalSeparator());
446                },
447
448                insertAt: function (input, position, rh) {
449                        var nf = this._getCulture().numberFormat,
450                                pos, slicePos, currentText, beginText, endText, newBegText,
451                                leftPrevCh, leftCh;
452
453                        if (input === nf['.']) {
454                                input = nf['.']; 
455                        }
456                        if (!rh) {
457                                rh = new wijInputResult();
458                        } else {
459                                this._nullvalue = false;
460                        }
461                        if (input.length === 1) {
462                                if (input === '+') {
463                                        this._stringFormat.setPositiveSign();
464                                        this.checkAndRepairBounds(true, false);
465                                        return true;
466                                }
467                                if (input === '-' || input === ')' || input === '(') {
468                                        this._stringFormat.invertSign();
469                                        this.checkAndRepairBounds(true, false);
470                                        rh.testPosition = position;
471                                        if (this._stringFormat.isNegative()) {
472                                                rh.testPosition = position;
473                                        }
474                                        else {
475                                                rh.testPosition = position - 2;
476                                        }
477                                        return true;
478                                }
479                                if (!$.wij.charValidator.isDigit(input)) {
480                                        if (input === '.') {
481                                                pos = this.getDecimalSeparatorPos();
482                                                if (pos >= 0) {
483                                                        rh.testPosition = pos;
484                                                        return true;
485                                                }
486                                        }
487                                        if (input !== ',' && input !== '.' && input !== ')' &&
488                                                input !== '+' && input !== '-' && input !== '(' && 
489                                                input !== this.getDecimalSeparator()) {
490                                                if (this._type === 'percent' && input === nf.percent.symbol) {
491                                                        rh.testPosition = position;
492                                                        return true;
493                                                } else if (this._type === 'currency' && 
494                                                        input === nf.currency.symbol) {
495                                                        rh.testPosition = position;
496                                                        return true;
497                                                } else {
498                                                        return false;
499                                                }
500                                        }
501                                }
502                        }
503
504                        position = this.getAdjustedPositionFromLeft(position);
505                        slicePos = position;
506                        currentText = this._stringFormat._currentText;
507                        if (slicePos > currentText.length) {
508                                slicePos = currentText.length - 1;
509                        }
510                        // if (input.length === 1) {
511                        // if (currentText.charAt(slicePos) === input) {
512                        // rh.testPosition = slicePos;
513                        // return true;
514                        // }
515                        // }
516                        beginText = currentText.substring(0, slicePos);
517                        endText = currentText.substring(slicePos, currentText.length);
518                        if (this._stringFormat.isZero()) {
519                                endText = endText.replace(new RegExp('[0]'), '');
520                        }
521
522                        rh.testPosition = beginText.length + input.length - 1;
523                        this._stringFormat.deFormatValue(beginText + input + endText);                 
524                        //this.checkAndRepairBounds(true, false);
525                        try {
526                                if (input.length === 1) {
527                                        if (this.inputWidget.options.showGroup) {
528                                                newBegText = this._stringFormat
529                                                ._currentText.substring(0, beginText.length);
530                                                if (this.countSubstring(newBegText,
531                                                        this._stringFormat._groupSeparator) !==
532                                                        this.countSubstring(beginText, 
533                                                        this._stringFormat._groupSeparator)) {
534                                                        rh.testPosition = rh.testPosition + 1;
535                                                }
536                                        }
537                                        else {
538                                                leftPrevCh = beginText.charAt(beginText.length - 1);
539                                                leftCh = this._stringFormat._currentText
540                                                        .charAt(rh.testPosition - 1);
541                                                if (leftCh !== leftPrevCh) {
542                                                        rh.testPosition = rh.testPosition - 1;
543                                                }
544                                        }
545                                }
546                        }
547                        catch (e) {
548                        }
549
550                        return true;
551                },
552
553                removeAt: function (start, end, rh, skipCheck) {
554                        var nf = this._getCulture().numberFormat,
555                                curText, curInsertText, newBegText;
556
557                        if (!rh) {
558                                rh = new wijInputResult(); 
559                        }
560                        skipCheck = !!skipCheck;
561                        rh.testPosition = start;
562                        try {
563                                curText = this._stringFormat._currentText;
564                                if ((start === end) && curText.substring(start, end + 1) === 
565                                        this.getDecimalSeparator()) {
566                                        return false;
567                                }
568                                curInsertText = curText.slice(0, start) + curText.slice(end + 1);
569                                if (curInsertText === '') {
570                                        curInsertText = '0'; 
571                                }
572                                this._stringFormat.deFormatValue(curInsertText);
573                                this._nullvalue = false;
574                                if (start === end && this.inputWidget.options.showGroup) {
575                                        try {
576                                                newBegText = this._stringFormat._currentText
577                                                .substring(0, start);
578                                                if (this.countSubstring(newBegText,
579                                                        this._stringFormat._groupSeparator) !==
580                                                        this.countSubstring(curInsertText, 
581                                                        this._stringFormat._groupSeparator)) {
582                                                        rh.testPosition = rh.testPosition - 1;
583                                                        if (curText.indexOf(nf.currency.symbol) === rh.testPosition ||
584                                                         curText.indexOf(nf.percent.symbol) === rh.testPosition) {
585                                                                rh.testPosition = rh.testPosition + 1;
586                                                        }
587                                                }
588                                        }
589                                        catch (e1) {
590                                        }
591                                }
592
593                                // if (!skipCheck){
594                                // this.checkAndRepairBounds(true, false);
595                                // }
596                                return true;
597                        }
598                        catch (e2) {
599                        }
600
601                        // if (!skipCheck){
602                        // this.checkAndRepairBounds(true, false);
603                        // }
604                        return true;
605                },
606
607                incEnumPart: function (position, rh, val) {
608                        if (!rh) {
609                                rh = new wijInputResult(); 
610                        }
611                        this._stringFormat.increment(val);
612                        this._nullvalue = false;
613                        return this.checkAndRepairBounds(true, false);
614                },
615
616                decEnumPart: function (position, rh, val) {
617                        if (!rh) {
618                                rh = new wijInputResult(); 
619                        }
620                        this._stringFormat.decrement(val);
621                        this._nullvalue = false;
622                        return this.checkAndRepairBounds(true, false);
623                },
624
625                getValue: function () {
626                        return this._stringFormat.getJSFloatValue();
627                },
628
629                setValue: function (val) {
630                        try {
631                                this._stringFormat._setValueFromJSFloat(val);
632                                this.checkAndRepairBounds(true, false);
633                                return true;
634                        }
635                        catch (e) {
636                                return false;
637                        }
638                },
639
640                updateStringFormat: function () {
641                        var t = '0';
642                        if (typeof (this._stringFormat) !== 'undefined') {
643                                t = this._stringFormat._currentValueInString;
644                        }
645                        this._stringFormat = new wijNumberFormat(this._type,
646                                this.inputWidget.options.decimalPlaces, 
647                                this.inputWidget.options.showGroup, this._getCulture());
648                        this._stringFormat._currentValueInString = t;
649                }
650        };
651
652
653        //============================
654
655        wijNumberFormat = function (t, dp, g, c) {
656                this.type = t;
657                this.digitsPlaces = dp;
658                this.showGroup = g;
659                this.culture = c;
660        };
661
662        wijNumberFormat.prototype = {
663                _currentValueInString: '0',
664                _currentText: '0',
665                _groupSeparator: ' ',
666                type: 'numeric',
667                digitsPlaces: 0,
668                showGroup: false,
669                culture: null,
670
671                isNegtive: function (value) {
672                        return value.indexOf('-') !== -1 || value.indexOf('(') !== -1;
673                },
674
675                stripValue: function (value) {
676                        var nf = this.culture.numberFormat,
677                                isNegative = this.isNegtive(value),
678                                groupSep, decimalSep, r, reg, arr;
679
680                        value = value.replace('(', '');
681                        value = value.replace(')', '');
682                        value = value.replace('-', '');
683                        value = value.replace(nf.percent.symbol, '');
684                        value = value.replace(nf.currency.symbol, '');
685                        groupSep = nf[','];
686                        decimalSep = nf['.'];
687                        switch (this.type) {
688                        case 'percent':
689                                groupSep = nf.percent[','];
690                                decimalSep = nf.percent['.'];
691                                break;
692                        case 'currency':
693                                groupSep = nf.currency[','];
694                                decimalSep = nf.currency['.'];
695                                break;
696                        }
697                        this._groupSeparator = groupSep;
698                        r = new RegExp('[' + groupSep + ']', 'g');
699                        value = value.replace(r, '');
700                        r = new RegExp('[' + decimalSep + ']', 'g');
701                        value = value.replace(r, '.');
702                        r = new RegExp('[ ]', 'g');
703                        value = value.replace(r, '');
704                        try {
705                                reg = new RegExp('([\\d\\.])+');
706                                arr = reg.exec(value);
707                                if (arr) {
708                                        value = arr[0];
709                                }
710                                if (isNegative) {
711                                        value = '-' + value;
712                                }
713
714                                return value;
715                        }
716                        catch (e) {
717                        }
718
719                        return null;
720                },
721
722                tryParseValue: function (value) {
723                        value = this.stripValue(value);
724                        if (value === null) {
725                                return 0; 
726                        }
727
728                        try {
729                                value = parseFloat(value);
730                                if (isNaN(value)) {
731                                        value = 0; 
732                                }
733                        } catch (e) {
734                                value = 0;
735                        }
736
737                        return value;
738                },
739
740                deFormatValue: function (value) {
741                        value = this.stripValue(value);
742                        if (value === null) {
743                                return; 
744                        }
745
746                        this._currentValueInString = value;
747                        this._currentText = this.formatValue(value);
748                },
749
750                formatValue: function (value) {
751                        value = '' + value + '';
752
753                        var nf = this.culture.numberFormat,
754                                dp = this.digitsPlaces, groupSep = ' ', decimalSep = '.', 
755                                decimals = 2, isNegative = this.isNegtive(value),
756                                groupSizes = new Array(3), pattern, digitsString;
757                        groupSizes.push(3);
758                        pattern = 'n';
759                        switch (this.type) {
760                        case 'numeric':
761                                pattern = isNegative ? nf.pattern[0] : 'n';
762                                groupSep = nf[','];
763                                decimalSep = nf['.'];
764                                decimals = nf.decimals;
765                                groupSizes = nf.groupSizes;
766                                break;
767                        case 'percent':
768                                pattern = nf.percent.pattern[isNegative ? 0 : 1];
769                                groupSep = nf.percent[','];
770                                decimalSep = nf.percent['.'];
771                                decimals = nf.percent.decimals;
772                                groupSizes = nf.percent.groupSizes;
773                                break;
774                        case 'currency':
775                                pattern = nf.currency.pattern[isNegative ? 0 : 1];
776                                groupSep = nf.currency[','];
777                                decimalSep = nf.currency['.'];
778                                decimals = nf.currency.decimals;
779                                groupSizes = nf.currency.groupSizes;
780                                break;
781                        }
782
783                        if (dp !== -2) {
784                                decimals = dp; 
785                        }
786                        if (!this.showGroup) {
787                                groupSizes = [0]; 
788                        }
789
790                        value = value.replace(new RegExp('^[0]+'), '');
791                        digitsString = this.formatDigit(value, groupSep, 
792                                decimalSep, decimals, groupSizes);
793                        digitsString = digitsString.replace(new RegExp('^[0]+'), '');
794                        if (digitsString.indexOf(decimalSep) === 0) {
795                                digitsString = '0' + digitsString; 
796                        }
797                        if (digitsString === '') {
798                                digitsString = '0'; 
799                        }
800
801                        this._currentValueInString = value;
802                        this._currentText = this.applyFormatPattern(pattern, digitsString, 
803                                nf.percent.symbol, nf.currency.symbol);
804                        return this._currentText;
805                },
806
807                getFormattedValue: function () {
808                        return this.formatValue(this._currentValueInString);
809                },
810
811                getJSFloatValue: function () {
812                        try {
813                                if (this._currentValueInString === '') {
814                                        return 0;
815                                }
816                                return parseFloat(this._currentValueInString);
817                        }
818                        catch (e) {
819                                return Number.NaN;
820                        }
821                },
822
823                clear: function () {
824                        this._currentValueInString = '0';
825                        this._currentText = '0';
826                },
827
828                _setValueFromJSFloat: function (v) {
829                        try {
830                                this._currentValueInString = '' + v + '';
831                                this.formatValue(v);
832                                return true;
833                        }
834                        catch (e) {
835                                return false;
836                        }
837                },
838
839                isZero: function (val) {
840                        try {
841                                if (val === undefined) {
842                                        val = this._currentValueInString;
843                                }
844
845                                var test = val.replace('-', ''), dbl;
846                                test = test.replace('(', '');
847                                test = test.replace(')', '');
848                                if (!test.length) {
849                                        test = '0';
850                                }
851                                dbl = parseFloat(test);
852                                if (!isNaN(dbl) && !dbl) {
853                                        return true;
854                                }
855                        }
856                        catch (e) {
857                        }
858                        return false;
859                },
860
861                setPositiveSign: function () {
862                        this._currentValueInString = this._currentValueInString.replace('-', '');
863                        this._currentValueInString = this._currentValueInString.replace('(', '');
864                        this._currentValueInString = this._currentValueInString.replace(')', '');
865                },
866
867                isNegative: function () {
868                        return this._currentValueInString.indexOf('-') !== -1 || 
869                                this._currentValueInString.indexOf('(') !== -1;
870                },
871
872                invertSign: function () {
873                        var isNegative = this.isNegative();
874                        if (isNegative) {
875                                this.setPositiveSign();
876                        } else {
877                                this._currentValueInString = (!this._currentValueInString.length) ? 
878                                        '0' : '-' + this._currentValueInString;
879                        }
880                        if (this.isZero()) {
881                                this._currentValueInString = isNegative ? '0' : '-0';
882                        }
883                        this.formatValue(this._currentValueInString);
884                },
885
886                increment: function (val) {
887                        if (val === undefined) {
888                                val = 1; 
889                        }
890                        try {
891                                var arr = this._currentValueInString.split('.');
892                                this._currentValueInString = (arr[0] * 1 + val) + '' + 
893                                        ((arr.length > 1) ? ('.' + arr[1]) : '');
894                        }
895                        catch (e) {
896                        }
897                },
898
899                decrement: function (val) {
900                        if (val === undefined) {
901                                val = 1; 
902                        }
903                        try {
904                                var arr = this._currentValueInString.split('.');
905                                this._currentValueInString = (arr[0] * 1 - val) + '' + 
906                                        ((arr.length > 1) ? ('.' + arr[1]) : '');
907                        }
908                        catch (e) {
909                        }
910                },
911
912                checkDigitsLimits: function (aDigitsCount) {
913                        try {
914                                var arr = this._currentValueInString.split('.'), s, d, i, ch;
915                                if (!arr.length || (arr.length === 1 && arr[0] === '')) {
916                                        return;
917                                }
918                                s = '';
919                                if (arr.length > 1) {
920                                        s = arr[1];
921                                }
922                                d = '';
923                                for (i = 0; i < aDigitsCount; i++) {
924                                        ch = '0';
925                                        if (s.length > i) {
926                                                ch = s.charAt(i);
927                                        }
928                                        d = d + ch;
929                                }
930                                if (d.length > 0) {
931                                        this._currentValueInString = arr[0] + '.' + d;
932                                } else {
933                                        this._currentValueInString = arr[0];
934                                }
935                        }
936                        catch (e) {
937                        }
938                },
939
940                checkMinValue: function (val, chkAndRepair, chkIsLessOrEqMin) {
941                        if (typeof (chkIsLessOrEqMin) === 'undefined') {
942                                chkIsLessOrEqMin = false;
943                        }
944                        var result = true,
945                                arr, s1, s2, sv1, sv2;
946                        try {
947                                arr = this._currentValueInString.split('.');
948                                s1 = parseFloat((arr[0] === '' || arr[0] === '-') ? '0' : arr[0]);
949                                s2 = 0;
950
951                                if (arr.length > 1 && parseFloat(arr[1]) > 0) {
952                                        s2 = parseFloat('1.' + arr[1]);
953                                }
954                                if (s1 < 0 || arr[0] === '-') {
955                                        s2 = s2 * -1;
956                                }
957                                val = '' + val + '';
958                                arr = val.split('.');
959                                sv1 = parseFloat(arr[0]);
960                                sv2 = 0;
961                                if (arr.length > 1 && parseFloat(arr[1]) > 0) {
962                                        sv2 = parseFloat('1.' + arr[1]);
963                                }
964                                if (s1 > sv1) {
965                                        return true;
966                                }
967                                if (s1 < sv1 || (chkIsLessOrEqMin && s1 === sv1 && s2 <= sv2)) {
968                                        result = false;
969                                } else if (s1 === sv1 && s1 < 0 && s2 > sv2) {
970                                        result = false;
971                                } else if (s1 === sv1 && s1 >= 0 && s2 < sv2) {
972                                        result = false;
973                                }
974                                if (!result && chkAndRepair) {
975                                        this._currentValueInString = '' + val + '';
976                                }
977                        }
978                        catch (e) {
979                        }
980                        return result;
981                },
982
983                checkMaxValue: function (val, chkAndRepair) {
984                        var result = true, arr, s1, s2, sv1, sv2;
985                        try {
986                                arr = this._currentValueInString.split('.');
987                                s1 = parseFloat((arr[0] === '' || arr[0] === '-') ? '0' : arr[0]);
988                                s2 = 0;
989                                if (arr.length > 1 && parseFloat(arr[1]) > 0) {
990                                        s2 = parseFloat('1.' + arr[1]);
991                                }
992                                if (s1 < 0 || arr[0] === '-') {
993                                        s2 = s2 * -1;
994                                }
995                                val = '' + val + '';
996                                arr = val.split('.');
997                                sv1 = parseFloat(arr[0]);
998                                sv2 = 0;
999                                if (arr.length > 1 && parseFloat(arr[1]) > 0) {
1000                                        sv2 = parseFloat('1.' + arr[1]);
1001                                }
1002                                if (s1 < sv1) {
1003                                        return true;
1004                                }
1005                                if (s1 > sv1) {
1006                                        result = false;
1007                                }
1008                                if (s1 === sv1 && s1 >= 0 && s2 > sv2) {
1009                                        result = false;
1010                                }
1011                                if (s1 === sv1 && s1 < 0 && s2 < sv2) {
1012                                        result = false;
1013                                }
1014                                if (!result && chkAndRepair) {
1015                                        this._currentValueInString = '' + val + '';
1016                                }
1017                        }
1018                        catch (e) {
1019                        }
1020                        return result;
1021                },
1022
1023                applyFormatPattern: function (pattern, digitString, percentSymbol, 
1024                        currencySymbol) {
1025                        var result = pattern,
1026                                r = new RegExp('[n]', 'g');
1027                        result = result.replace(r, digitString);
1028                        r = new RegExp('[%]', 'g');
1029                        result = result.replace(r, percentSymbol);
1030                        r = new RegExp('[$]', 'g');
1031                        result = result.replace(r, currencySymbol);
1032                        return result;
1033                },
1034
1035                formatDigit: function (value, groupSep, decimalSep, decimals, groupSizes) {
1036                        var absValue = '' + value + '', decimalPos, result, 
1037                                groupSizeIndex, groupCount, ch, i;
1038                        absValue = absValue.replace('-', '');
1039                        absValue = absValue.replace('(', '');
1040                        absValue = absValue.replace(')', '');
1041                        decimalPos = absValue.indexOf(decimalSep);
1042                        if (decimalPos === -1) {
1043                                decimalPos = absValue.indexOf('.'); 
1044                        }
1045                        if (decimalPos === -1) {
1046                                decimalPos = absValue.indexOf(','); 
1047                        }
1048                        if (decimalPos === -1) {
1049                                decimalPos = absValue.length; 
1050                        }
1051
1052                        result = '';
1053                        groupSizeIndex = 0;
1054                        groupCount = 0; 
1055                       
1056                        for (i = absValue.length - 1; i >= 0; i--) {
1057                                ch = absValue.charAt(i);
1058                                if (i < decimalPos) {
1059                                        result = ch + result;
1060                                        groupCount++;
1061                                        if (groupCount === groupSizes[groupSizeIndex] * 1 && 
1062                                                groupSizes[groupSizeIndex] * 1 && i) {
1063                                                result = groupSep + result;
1064                                                groupCount = 0;
1065                                                if (groupSizes.length - 1 > groupSizeIndex) {
1066                                                        groupSizeIndex++;
1067                                                }
1068                                        }
1069                                }
1070                        }
1071                        if (decimals > 0) {
1072                                result = result + decimalSep;
1073                                for (i = 0; i < decimals; i++) {
1074                                        ch = '0';
1075                                        if (i + decimalPos + 1 < absValue.length) {
1076                                                ch = absValue.charAt(i + decimalPos + 1);
1077                                        }
1078                                        result = result + ch;
1079                                }
1080                        }
1081                        if (decimals === -1) {
1082                                if (decimalPos < absValue.length - 1) {
1083                                        result = result + decimalSep;
1084                                        result = result + absValue.substr(decimalPos + 1);
1085                                }
1086                        }
1087                        return result;
1088                }
1089        };
1090
1091}(jQuery));
Note: See TracBrowser for help on using the repository browser.