All Downloads are FREE. Search and download functionalities are using the official Maven repository.

META-INF.resources.primefaces.keyfilter.1-keyfilter.js Maven / Gradle / Ivy

There is a newer version: 14.0.0-RC3
Show newest version
PrimeFaces.widget.KeyFilter = PrimeFaces.widget.BaseWidget.extend({
	
	/**
	 * Initializes the widget.
	 * 
	 * @param {object} cfg The widget configuration.
	 */
	init : function(cfg) {
		this.id = cfg.id;
		this.cfg = cfg;
	    this.target = PrimeFaces.expressions.SearchExpressionFacade.resolveComponentsAsSelector(this.cfg.target);

	    if (this.target.is(':input')) {
	    	this.applyKeyFilter(this.target, cfg);
	    } else {
	    	var nestedInput = $(':not(:submit):not(:button):input:visible:enabled:first', this.target);
	    	this.applyKeyFilter(nestedInput, cfg);
	    }
	    
	    this.removeScriptElement(this.id);
	},

	/**
	 * Applies the keyFilter to the given jQuery selector object.
	 * 
	 * @param {object} input A jQuery selector object.
	 * @param {object} cfg The widget configuration.
	 * @private
	 */
	applyKeyFilter : function(input, cfg) {
		if (this.cfg.regEx) {
			input.keyfilter(this.cfg.regEx);
		} else if(this.cfg.inputRegEx) {
			var inputRegEx = this.cfg.inputRegEx;
		 	var previousInputValue = '';
			input.on('input', function(e) {
				var ok = inputRegEx.test(this.value);
				if(ok) {
					previousInputValue = this.value;
				}
				else {
					this.value = previousInputValue;
				}
			});
		} else if (this.cfg.testFunction) {
			input.keyfilter(this.cfg.testFunction);
		} else if (this.cfg.mask) {
			input.keyfilter($.fn.keyfilter.defaults.masks[this.cfg.mask]);
		}

		if (cfg.preventPaste) {
			//disable paste
			input.bind('paste', function(e) {
				e.preventDefault();
			});
		}
	}
});




© 2015 - 2024 Weber Informatics LLC | Privacy Policy