Skip to content

Commit

Permalink
Fixed an old behavior that i didnt like. The class used to need an in…
Browse files Browse the repository at this point in the history
…put element to initialize, now i can link it later, so now i dont need a dummy input to mask strings.

changed the name of the method that removes the mask from an input to unlink. This shouldnt be used by normal users thats no biggy. If you used it on your code, just rename it to unlink and everything should work just fine.
general cleanups.
  • Loading branch information
fabiomcosta committed Jul 25, 2010
1 parent 43d7ee6 commit bf5a7c3
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 110 deletions.
22 changes: 11 additions & 11 deletions Source/Meio.Mask.Extras.js
Expand Up @@ -18,7 +18,7 @@ provides: [Meio.Mask.Extras]

(function(){

var meiomask = 'meiomask', dummyInput = new Element('input', {'type': 'text'});
var meiomask = 'meiomask';

var upperCamelize = function(str){
return str.camelCase().capitalize();
Expand All @@ -44,8 +44,7 @@ provides: [Meio.Mask.Extras]

var executeFunction = function(functionName, args){
var co = getClassOptions.apply(null, args);
dummyInput.set('value', '');
return new co.klass(dummyInput, co.options)[functionName](this);
return new co.klass(co.options)[functionName](this);
};

String.implement({
Expand All @@ -60,7 +59,7 @@ provides: [Meio.Mask.Extras]
Element.Properties.meiomask = {
set: function(){
var args = getClassOptions.apply(null, arguments);
return this.store(meiomask, new args.klass(this, args.options));
return this.store(meiomask, new args.klass(args.options).link(this));
},
// returns the mask object
get: function(){
Expand All @@ -69,7 +68,7 @@ provides: [Meio.Mask.Extras]
// removes completely the mask from this input
erase: function(){
var mask = this.retrieve(meiomask);
if (mask) mask.remove();
if (mask) mask.unlink();
return this;
}
};
Expand All @@ -90,6 +89,13 @@ provides: [Meio.Mask.Extras]
}
};

Element.implement({
meiomask: function(mask, type, options){
return this.set(meiomask, mask, type, options);
}
});


// fix for maxlength property
var maxLength = document.createElement('input').getAttribute('maxLength');
if (maxLength != null) Element.Properties.maxlength = Element.Properties.maxLength = {
Expand All @@ -98,11 +104,5 @@ provides: [Meio.Mask.Extras]
return maxlength == maxLength ? null : maxlength;
}
};

Element.implement({
meiomask: function(mask, type, options){
return this.set(meiomask, mask, type, options);
}
});

})();
117 changes: 61 additions & 56 deletions Source/Meio.Mask.Fixed.js
Expand Up @@ -27,21 +27,25 @@ Meio.Mask.Fixed = new Class({
removeInvalidTrailingChars: true
},

initialize: function(element, options){
this.parent(element, options);
initialize: function(options){
this.parent(options);
this.maskArray = this.options.mask.split('');
this.maskMold = this.options.mask.replace(Meio.Mask.rulesRegex, this.options.placeholder);
this.maskMoldArray = this.maskMold.split('');
var value = this.element.get('value');
if (value) this.maskMoldArray = this.mask(value).split('');
this.validIndexes = [];
if (this.options.autoSetSize) this.setSize();
this.maskArray.each(function(c, i){
if (!this.isFixedChar(c)) this.validIndexes.push(i);
if (!this.isFixedChar(c)) this.validIndexes.push(i);
}, this);
this.createUnmaskRegex();
},

link: function(element){
this.parent(element);
var elementValue = this.element.get('value');
if (elementValue != '') this.maskMoldArray = this.mask(elementValue).split('');
if (this.options.autoSetSize) this.setSize();
},

focus: function(e, o){
this.element.set('value', this.maskMoldArray.join(''));
if (this.options.selectOnFocus && this.element.select) this.element.select();
Expand All @@ -57,7 +61,7 @@ Meio.Mask.Fixed = new Class({
this.element.set('value', '');
}
return true;
}
}
if (this.options.removeInvalidTrailingChars) this.removeInvalidTrailingChars(elementValue);
return true;
},
Expand Down Expand Up @@ -139,53 +143,15 @@ Meio.Mask.Fixed = new Class({
return true;
},

mask: function(str){
return this.applyMask(str).value.join('');
},

unmask: function(str){
return this.unmaskRegex ? str.replace(this.unmaskRegex, '') : str;
},

applyMask: function(elementValue, newRangeStart){
var elementValueArray = elementValue.split(''),
maskArray = this.maskArray,
maskMold = this.maskMoldArray,
eli = 0,
returnFromTestEntry;

while (eli < maskMold.length){
if (!elementValueArray[eli]){
elementValueArray[eli] = maskMold[eli];
} else if (Meio.Mask.rules[maskArray[eli]]){
if (!(returnFromTestEntry = this.testEntry(eli, elementValueArray[eli]))){
elementValueArray.splice(eli, 1);
continue;
} else {
if (typeof returnFromTestEntry == 'string') elementValueArray[eli] = returnFromTestEntry;
}
newStartRange = eli;
} else if (maskArray[eli] != elementValueArray[eli]){
elementValueArray.splice(eli, 0, maskMold[eli]);
} else {
elementValueArray[eli] = maskMold[eli];
}
eli++;
}

// makes sure the value is not bigger than the mask definition
return {value: elementValueArray.slice(0, this.maskMold.length), rangeStart: newRangeStart + 1};
},

removeInvalidTrailingChars: function(elementValue){
removeInvalidTrailingChars: function(elementValue){
var truncateIndex = elementValue.length,
placeholder = this.options.placeholder,
i = elementValue.length - 1,
cont;
while (i >= 0){
cont = false;
while (this.isFixedChar(elementValue.charAt(i)) && elementValue.charAt(i) !== placeholder){
if (i === 0) truncateIndex = 0;
if (i == 0) truncateIndex = 0;
cont = true;
i--;
}
Expand All @@ -199,26 +165,19 @@ Meio.Mask.Fixed = new Class({
this.element.set('value', elementValue.substring(0, truncateIndex));
},

testEntry: function(index, _char){
var maskArray = this.maskArray,
rule = Meio.Mask.rules[maskArray[index]],
ret = (rule && rule.regex.test(_char));
return (rule.check && ret) ? rule.check(this.element.get('value'), index, _char) : ret;
},

testEvents: function(index, _char, code, isRemoveKey){
var maskArray = this.maskArray,
rule = Meio.Mask.rules[maskArray[index]],
returnFromTestEntry;
if (!isRemoveKey){
var args = [this.element, code, _char];
if (!rule || !(returnFromTestEntry = this.testEntry(index, _char))){
if (!rule || !(returnFromTestEntry = this.testEntry(this.element.get('value'), index, _char))){
this.fireEvent('invalid', args);
return false;
}
this.fireEvent('valid', args);
}
return returnFromTestEntry || true;
return (returnFromTestEntry != null) ? returnFromTestEntry : true;
},

shouldFocusNext: function(){
Expand All @@ -229,7 +188,53 @@ Meio.Mask.Fixed = new Class({
var fixedCharsArray = [].combine(this.options.mask.replace(Meio.Mask.rulesRegex, '').split(''));
var chars = (fixedCharsArray.join('') + this.options.placeholder).escapeRegExp() ;
this.unmaskRegex = chars ? new RegExp('[' + chars + ']', 'g') : null;
},

testEntry: function(str, index, _char){
var maskArray = this.maskArray,
rule = Meio.Mask.rules[maskArray[index]],
ret = (rule && rule.regex.test(_char));
return (rule.check && ret) ? rule.check(str, index, _char) : ret;
},

applyMask: function(str, newRangeStart){
var strArray = str.split(''),
maskArray = this.maskArray,
maskMold = this.maskMoldArray,
rules = Meio.Mask.rules,
eli = 0,
returnFromTestEntry;

while (eli < maskMold.length){
if (!strArray[eli]){
strArray[eli] = maskMold[eli];
} else if (rules[maskArray[eli]]){
if (!(returnFromTestEntry = this.testEntry(str, eli, strArray[eli]))){
strArray.splice(eli, 1);
continue;
} else {
if (typeof returnFromTestEntry == 'string') strArray[eli] = returnFromTestEntry;
}
newStartRange = eli;
} else if (maskArray[eli] != strArray[eli]){
strArray.splice(eli, 0, maskMold[eli]);
} else {
strArray[eli] = maskMold[eli];
}
eli++;
}

return {value: strArray.slice(0, this.maskMold.length), rangeStart: newRangeStart + 1};
},

mask: function(str){
return this.applyMask(str).value.join('');
},

unmask: function(str){
return this.unmaskRegex ? str.replace(this.unmaskRegex, '') : str;
}

});


Expand Down
19 changes: 11 additions & 8 deletions Source/Meio.Mask.Reverse.js
Expand Up @@ -31,22 +31,26 @@ Meio.Mask.Reverse = new Class({
maxLength: 18
},

initialize: function(element, options){
this.parent(element, options);
initialize: function(options){
this.parent(options);
var thousandsChar = this.options.thousands,
escapedThousandsChars = thousandsChar.escapeRegExp(),
escapedDecimalChar = this.options.decimal.escapeRegExp();
if (this.options.alignText) this.element.setStyle('text-align', 'right');
this.maxlength = this.maxlength || this.options.maxLength;
this.maxlength = this.options.maxLength;
this.thousandsRegex = /(\d+)(\d{3})/;
this.removeLeadingZerosRegex = /^0+(.*)$/;
this.decimalNumberRegex = /^\d$/;
this.thousandsReplaceStr = '$1' + thousandsChar + '$2';
this.thousandsReplaceRegex = new RegExp(escapedThousandsChars, 'g');
this.cleanupRegex = new RegExp('[' + escapedThousandsChars + 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.mask(elementValue));
this.element.set('value', this.forceMask(elementValue, false));
}
},

Expand Down Expand Up @@ -87,7 +91,7 @@ Meio.Mask.Reverse = new Class({
var args = [this.element, code, _char];
if (!isRemoveKey){
var elementValueLength = this.getValue(elementValue, false).length;
if (!(this.decimalNumberRegex).test(_char) || elementValueLength > this.maxlength){
if (!(this.decimalNumberRegex).test(_char) || (this.maxlength && elementValueLength > this.maxlength)){
this.fireEvent('invalid', args);
return false;
}
Expand Down Expand Up @@ -120,8 +124,7 @@ Meio.Mask.Reverse = new Class({
},

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

unmask: function(str){
Expand Down

0 comments on commit bf5a7c3

Please sign in to comment.