Skip to content

Instantly share code, notes, and snippets.

@apipkin
Created February 2, 2010 21:43
Show Gist options
  • Save apipkin/293069 to your computer and use it in GitHub Desktop.
Save apipkin/293069 to your computer and use it in GitHub Desktop.
YUI.add('form-filter', function(Y){
var Filter = function(config) {
config.node = config.host;
Filter.superclass.constructor.apply(this, arguments);
};
Y.mix(Filter,{
NAME : 'form-filter',
NS : 'filter',
ATTRS : {
host : {},
storedValues : [],
filters : [],
characters : []
},
FILTERS : {
NUMBERS : 'numbers', // allow numbers
SPECIAL : 'special', // allow special characters
LETTERS : 'letters' // allow all letters
}
});
Y.extend(Filter, Y.Base, {
CHARS : {
'numbers' : ['0','1','2','3','4','5','6','7','8','9'],
'special' : ['`','~','!','@','#','$','%','^','&','*','(',
')','_','-','+','=','[','{',']','}','\\','|',
';',':','\'','"',',','.','/','<','>','?'],
'letters' : ['a','b','c','d','e','f','g','h','i','j','k',
'l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','A','B','C','D','E','F','G',
'H','I','J','K','L','M','N','O','P','Q','R',
'S','T','U','V','W','X','Y','Z']
},
initializer : function(config) {
// attach the key press events
this.get('host').on('keydown', this._storeValue, this);
this.get('host').on('keyup', this._compareKeyToFilter, this);
// add filters from config
if(config.filters && config.filters.length) {
for(o in config.filters) {
this.addFilter(config.filters[o]);
}
}
// add chars from config
if(config.characters && config.characters.length) {
for(o in config.characters) {
this.addCharacter(config.characters[o]);
}
}
},
addFilter : function(filter) {
// finds filter in filters and add if not found
// adds filter's chars to characters
// finds filter in filters and removes
var index = this._searchFilter(filter);
if(index === -1) {
this.get('filters').push(filter);
}
// removes all chars associated with the filter
var filterChars = this.CHARS[filter];
if(filterChars) {
for(var i = 0; i < this.CHARS[filter].length; i++) {
this.addCharacter(this.CHARS[filter][i]);
}
}
},
removeFilter : function(filter) {
// finds filter in filters and removes
var index = this._searchFilter(filter);
if(index > -1) {
this.get('filters').splice(index,1);
}
// removes all chars associated with the filter
var filterChars = this.CHARS[filter];
if(filterChars) {
for(var i = 0; i < this.CHARS[filter].length; i++) {
this.removeCharacter(this.CHARS[filter][i]);
}
}
},
addCharacter : function(chr) {
// finds char in _chars and adds if not found
if(chr == '\\' || chr == ']') {
chr = '\\' + chr;
}
var index = this._searchChar(chr);
if(index === -1) {
if(chr == '-') {
this.get('characters').unshift(chr);
}else{
this.get('characters').push(chr);
}
}
},
removeCharacter : function(chr) {
// finds char in _chars and removes
if(chr == '\\' || chr == ']') {
chr = '\\' + chr;
}
var index = this._searchChar(chr);
if(index > -1) {
this.get('characters').splice(index,1);
}
},
_storeValue : function(e) {
if(this.get('characters').length) {
this.get('storedValues').push(this.get('host').get('value'));
}
},
_compareKeyToFilter : function(e) {
// allow ctrl + any char
if(e.ctrlKey) {
return;
}
if(this.get('characters').length) {
var regex = new RegExp('^[' + this.get('characters').join('') + ']*$');
var val = this.get('storedValues').pop();
if(!regex.test(this.get('host').get('value'))){
this.get('host').set('value',val);
}
}
},
_searchFilter : function(filter) {
return this._search(filter, this.get('filters'));
},
_searchChar : function(chr) {
Y.log(chr);
Y.log(this);
Y.log(this.get('filters'));
Y.log(this.get('characters'));
return this._search(chr, this.get('characters'));
},
_search : function(needle, haystack) {
for(var i = 0; i < haystack.length; i++) {
if(haystack[i] === needle) {
return i;
}
}
return -1;
}
});
Y.namespace('Form').Filter = Filter;
},'0.1',{requires:['base','node','event']});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment