      1 if (!window.RichFaces) {
      2     window.RichFaces = {};
      3 }
      4
      5 (function($, rf) {
      6
      7     rf.ui = rf.ui || {};
      8
      9     var evaluate = function(selector) {
     10         var result = selector;
     11         try {
     12             result = eval(selector);
     13         } catch (e) {
     14             //do nothing
     15         }
     16         return result;
     17     };
     18
     19     var evaluateJQuery = function(element, selector) {
     20         var result = element || evaluate(selector);
     21         if (!(result instanceof $)) {
     22             result = $(result || "");
     23         }
     24
     25         return result;
     26     };
     27
     28     var createEventHandlerFunction = function(opts) {
     29         var newFunction = new Function("event", opts.query);
     30
     31         return function() {
     32             var selector = evaluateJQuery(null, opts.selector);
     33             if (opts.attachType != "live") {
     34                 selector[opts.attachType || "bind"](opts.event, null, newFunction);
     35             } else { // $.live is deprecated in jQuery 1.9+
     36                 $(document).on(opts.event, selector.selector, null, newFunction);
     37             }
     38         };
     39     };
     40
     41     var createDirectQueryFunction = function(opts) {
     42         var queryFunction = new Function("options", "arguments[1]." + opts.query);
     43
     44         return function() {
     45             var element;
     46             var options;
     47
     48             if (arguments.length == 1) {
     49     	        if (!opts.selector) {
     50 		        	//if selector is not present take selector as default
     51 		         	element = arguments[0];
     52 	        	}else{
     53 		        	//if selector is allready presen from rich jQuery component take options as default.
     54 			        options = arguments[0];
     55 	        	}
     56
     57             } else {
     58                 //function(element, options) { ...query()... }
     59                 element = arguments[0];
     60                 options = arguments[1];
     61             }
     62
     63             var selector = evaluateJQuery(element, opts.selector);
     64             queryFunction.call(this, options, selector);
     65         };
     66     };
     67
     68     var createQueryFunction = function(options) {
     69         if (options.event) {
     70             return createEventHandlerFunction(options);
     71         } else {
     72             return createDirectQueryFunction(options);
     73         }
     74     };
     75
     76     var query = function(options) {
     77         if (options.timing == 'immediate') {
     78             createQueryFunction(options).call(this);
     79         } else {
     80             $(document).ready(createQueryFunction(options));
     81         }
     82     };
     83
     84     rf.ui.jQueryComponent = {
     85
     86         createFunction: createQueryFunction,
     87
     88         query: query
     89
     90     };
     91
     92 }(RichFaces.jQuery, RichFaces));
     93