Skip to content

Commit

Permalink
Merge branch 'obj'
Browse files Browse the repository at this point in the history
Conflicts:
	Source/Meio.Mask.Reverse.js
  • Loading branch information
fabiomcosta committed Jun 23, 2012
2 parents 2260034 + 19a5740 commit b8a0179
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 149 deletions.
316 changes: 168 additions & 148 deletions Source/Meio.Mask.Reverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,154 +20,174 @@ provides: [Meio.Mask.Reverse]

Meio.Mask.Reverse = new Class({

Extends: Meio.Mask,

options: {
autoSetSize: false,
autoEmpty: false,
alignText: true,
symbol: '',
precision: 2,
decimal: ',',
thousands: '.',
maxLength: 18
},

initialize: function(options){
this.parent(options);
var thousandsChar = this.options.thousands,
escapedThousandsChar = thousandsChar.escapeRegExp(),
escapedDecimalChar = this.options.decimal.escapeRegExp();
this.maxlength = this.options.maxLength;
this.reThousands = new RegExp('^([^\\D'+ escapedThousandsChar +']+)(\\d{3})');
this.reRemoveLeadingZeros = /^0+(.*)$/;
this.reDecimalNumber = /^\d$/;
this.thousandsReplaceStr = '$1' + thousandsChar + '$2';
this.reThousandsReplace = new RegExp(escapedThousandsChar, 'g');
this.reCleanup = new RegExp('[' + escapedThousandsChar + escapedDecimalChar + ']', 'g');
this.reRemoveNonNumbers = new RegExp('[^\\d' + escapedThousandsChar + escapedDecimalChar + ']', 'g');
},

link: function(element){
this.parent(element);
if (this.options.alignText) this.element.setStyle('text-align', 'right');
var elementValue = this.element.get('value');
if (elementValue === '' && !this.options.autoEmpty){
this.element.set('value', this.forceMask(elementValue, false));
}
return this;
},

focus: function(e, o){
var element = this.element,
elValue = element.get('value');
if (this.options.autoEmpty){
if (elValue === '') element.set('value', this.mask(elValue));
} else {
element.set('value', this.getValue(elValue, true));
}
this.parent(e, o);
},

blur: function(e, o){
this.parent(e, o);
var element = this.element,
value = this.getValue(element.get('value'));
if (this.options.autoEmpty && this.mask(value) == this.mask()) value = '';
element.set('value', value);
},

keypress: function(e, o){
if (this.ignore) return true;
e.preventDefault();

var state = this.getCurrentState(e, o), elementValue = state.value;

if (!this.testEvents(elementValue, state._char, e.code, o.isRemoveKey)) return true;
elementValue = this.forceMask(elementValue, true);
this.element.set('value', elementValue).setCaretPosition(elementValue.length);

return this.parent();
},

testEvents: function(elementValue, _char, code, isRemoveKey){
var args = [this.element, code, _char];
if (!isRemoveKey){
var elementValueLength = this.getValue(elementValue, false).length;
if (!(this.reDecimalNumber).test(_char) || (this.maxlength && elementValueLength > this.maxlength)){
this.fireEvent('invalid', args);
return false;
}
this.fireEvent('valid', args);
}
return true;
},

paste: function(e, o){
var element = this.element;
var elValue = element.get('value');
element.set('value', (elValue = this.forceMask(elValue, true))).setCaretPosition(elValue.length);
return true;
},

forceMask: function(str, applySymbol){
str = this.cleanup(str);
var precision = this.options.precision;
var zeros = precision + 1 - str.length;
if (zeros > 0) str = this.zeroize(str, zeros);
if (precision){
var decimalIndex = str.length - precision;
str = str.substring(0, decimalIndex) + this.options.decimal + str.substring(decimalIndex);
}
return this.getValue(this.maskThousands(str), applySymbol);
},

cleanup: function(str){
str = str.replace(this.reRemoveNonNumbers, '').replace(this.reCleanup, '');
return this.getValue(str).replace(this.reRemoveLeadingZeros, '$1');
},

mask: function(str){
str = this.unmask(str || '0').replace('.', this.options.decimal);
return this.getValue(this.maskThousands(str), true);
},

unmask: function(str){
return this.toNumber(this.getValue(str));
},

toNumber: function(str){
str = str.replace(this.reRemoveNonNumbers, '');
if (!isFinite(str)){
if (this.options.thousands) str = str.replace(this.reThousandsReplace, '');
var decimalChar = this.options.decimal;
if (decimalChar) str = str.replace(decimalChar, '.');
}
return str.toFloat().toFixed(this.options.precision);
},

getValue: function(str, applySymbol){
var symbol = this.options.symbol;
return (str.substring(0, symbol.length) === symbol) ?
applySymbol ? str : str.substring(symbol.length) :
applySymbol ? symbol + str : str;
},

maskThousands: function(str){
if (this.options.thousands){
while (this.reThousands.test(str)) str = str.replace(this.reThousands, this.thousandsReplaceStr);
}
return str;
},

zeroize: function(str, zeros){
while (zeros--) str = '0' + str;
return str;
},

shouldFocusNext: function(){
return this.getValue(this.element.get('value'), false).length >= this.options.maxLength;
}
Extends: Meio.Mask,

options: {
autoSetSize: false,
autoEmpty: false,
alignText: true,
symbol: '',
precision: 2,
decimal: ',',
thousands: '.',
maxLength: 18
},

initialize: function(options){
this.parent(options);
var thousandsChar = this.options.thousands,
escapedThousandsChar = thousandsChar.escapeRegExp(),
escapedDecimalChar = this.options.decimal.escapeRegExp();
this.maxlength = this.options.maxLength;
this.reThousands = new RegExp('^(-?[^\\D'+ escapedThousandsChar +']+)(\\d{3})');
this.reRemoveLeadingZeros = /^0+(.*)$/;
this.reDecimalNumber = /^[\d+-]$/;
this.thousandsReplaceStr = '$1' + thousandsChar + '$2';
this.reThousandsReplace = new RegExp(escapedThousandsChar, 'g');
this.reCleanup = new RegExp('[' + escapedThousandsChar + escapedDecimalChar + ']', 'g');
this.reRemoveNonNumbers = new RegExp('[^\\d' + escapedThousandsChar + escapedDecimalChar + ']', 'g');
},

link: function(element){
this.parent(element);
if (this.options.alignText) this.element.setStyle('text-align', 'right');
var elementValue = this.element.get('value');
if (elementValue === '' && !this.options.autoEmpty){
this.element.set('value', this.forceMask(elementValue, false));
}
return this;
},

focus: function(e, o){
var element = this.element,
elValue = element.get('value');
if (this.options.autoEmpty){
if (elValue === '') element.set('value', this.mask(elValue));
} else {
element.set('value', this.getValue(elValue, true));
}
this.parent(e, o);
},

blur: function(e, o){
this.parent(e, o);
var element = this.element,
value = this.getValue(element.get('value'));
if (this.options.autoEmpty && this.mask(value) == this.mask()) value = '';
element.set('value', value);
},

keypress: function(e, o){
if (this.ignore) return true;
e.preventDefault();

var state = this.getCurrentState(e, o), elementValue = state.value;

if (!this.testEvents(elementValue, state._char, e.code, o.isRemoveKey)) return true;
elementValue = this.forceMask(elementValue, true);

var forcePositive = state._char === '+';
var isNegative = this.isNegative(elementValue);
elementValue = forcePositive ? isNegative ? elementValue.substring(1) : elementValue : elementValue;

this.element.set('value', elementValue).setCaretPosition(elementValue.length);

return this.parent();
},

testEvents: function(elementValue, _char, code, isRemoveKey){
var args = [this.element, code, _char];
if (!isRemoveKey){
var elementValueLength = this.getValue(elementValue, false).length;
if (!(this.reDecimalNumber).test(_char) || (this.maxlength && elementValueLength > this.maxlength)){
this.fireEvent('invalid', args);
return false;
}
this.fireEvent('valid', args);
}
return true;
},

paste: function(e, o){
var element = this.element;
var elValue = element.get('value');
element.set('value', (elValue = this.forceMask(elValue, true))).setCaretPosition(elValue.length);
return true;
},

forceMask: function(str, applySymbol){
var negative = str.contains('-');
str = this.cleanup(str);
var precision = this.options.precision;
var zeros = precision + 1 - str.length;
if (zeros > 0) str = this.zeroize(str, zeros);
if (precision){
var decimalIndex = str.length - precision;
str = str.substring(0, decimalIndex) + this.options.decimal + str.substring(decimalIndex);
}
var value = this.getValue(this.maskThousands(str), applySymbol);
return this.negativize(value, negative);
},

cleanup: function(str){
str = str.replace(this.reRemoveNonNumbers, '').replace(this.reCleanup, '');
return this.getValue(str).replace(this.reRemoveLeadingZeros, '$1');
},

mask: function(str){
str = this.unmask(str || '0').replace('.', this.options.decimal);
return this.getValue(this.maskThousands(str), true);
},

unmask: function(str){
return this.toNumber(this.getValue(str));
},

toNumber: function(str){
var negative = this.isNegative(str);
str = str.replace(this.reRemoveNonNumbers, '');
if (!isFinite(str)){
if (this.options.thousands) str = str.replace(this.reThousandsReplace, '');
var decimalChar = this.options.decimal;
if (decimalChar) str = str.replace(decimalChar, '.');
}
var number = str.toFloat().toFixed(this.options.precision);
return this.negativize(number, negative);
},

getValue: function(str, applySymbol){
var negative = this.isNegative(str);
str = negative ? str.substring(1) : str;
var symbol = this.options.symbol;
var value = (str.substring(0, symbol.length) === symbol) ?
applySymbol ? str : str.substring(symbol.length) :
applySymbol ? symbol + str : str;
return this.negativize(value, negative);
},

maskThousands: function(str){
if (this.options.thousands){
while (this.reThousands.test(str)) str = str.replace(this.reThousands, this.thousandsReplaceStr);
}
return str;
},

zeroize: function(str, zeros){
while (zeros--) str = '0' + str;
return str;
},

isNegative : function(str) {
return str.indexOf('-') == 0;
},

negativize : function(str, negative) {
return negative ? '-' + str : str;
},

shouldFocusNext: function(){
return this.getValue(this.element.get('value'), false).length >= this.options.maxLength;
}
});

Meio.Mask.createMasks('Reverse', {
Expand Down
20 changes: 20 additions & 0 deletions Specs/moo.meio.mask.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,34 @@ test('should mask a string with the reverse.dollar mask', function(){
equals('12121212'.meiomask('reverse.dollar'), 'US$ 12,121,212.00');
});

test('should mask a string with the reverse.dollar mask (negative)', function(){
equals('-12121212'.meiomask('reverse.dollar'), '-US$ 12,121,212.00');
});

test('should mask a string with the reverse.integer mask', function(){
equals('345678.723'.meiomask('reverse.integer'), '345.679');
});

test('should mask a string with the reverse.integer mask (negative)', function(){
equals('-345678.723'.meiomask('reverse.integer'), '-345.679');
});

test('should mask a string with the reverse.decimal mask and', function(){
equals('345678.728'.meiomask('reverse.decimal', {precision: 5, symbol: 'R$ '}), 'R$ 345.678,72800');
});

test('should mask a string with the reverse.decimal mask and (negative)', function(){
equals('-345678.728'.meiomask('reverse.decimal', {precision: 5, symbol: 'R$ '}), '-R$ 345.678,72800');
});

test('should mask a string with the reverse.decimal mask', function(){
equals('345678.728'.meiomask('reverse.decimal'), '345.678,73');
});

test('should mask a string with the reverse.decimal mask (negative)', function(){
equals('-345678.728'.meiomask('reverse.decimal'), '-345.678,73');
});

test('should unmask the passed string', function(){
equals('12/12/2000'.meiounmask('Fixed', 'Date'), '12122000');
});
Expand All @@ -65,6 +81,10 @@ test('should unmask the passed string to a float number', function(){
equals('R$ 123,12'.meiounmask('reverse.reais'), 123.12);
});

test('should unmask the passed string to a float number (negative)', function(){
equals('-R$ 123,12'.meiounmask('reverse.reais'), -123.12);
});

test('should apply a precision: 5 mask to the 0 string', function(){
equals('0'.meiomask('Reverse', 'Decimal', {precision: 5}), '0,00000');
});
Expand Down
2 changes: 1 addition & 1 deletion Specs/user/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ <h1>Reverse</h1>
</fieldset>
<fieldset>
<label>reverse.reais Mask:</label>
<input style="width: 200px" type="text" data-meiomask-options="{autoTab: true}" data-meiomask="reverse.reais" />
<input style="width: 200px" type="text" data-meiomask-options="{autoTab: true}" data-meiomask="reverse.reais" value="-3.00" />
</fieldset>
<fieldset>
<label>reverse.euro Mask:</label>
Expand Down

0 comments on commit b8a0179

Please sign in to comment.