      1 /*
      2  *  Copyright (C) 2009 WaveMaker Software, Inc.
      3  *
      4  *  This file is part of the WaveMaker Client Runtime.
      5  *
      6  *  Licensed under the Apache License, Version 2.0 (the "License");
      7  *  you may not use this file except in compliance with the License.
      8  *  You may obtain a copy of the License at
      9  *
     10  *      http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  *  Unless required by applicable law or agreed to in writing, software
     13  *  distributed under the License is distributed on an "AS IS" BASIS,
     14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  *  See the License for the specific language governing permissions and
     16  *  limitations under the License.
     17  */
     18 dojo.provide("wm.base.data.expression");
     19
     20 /**
     21 	@class
     22 	Static API for handling data expressions.
     23 	Data expressions are strings can contain valid JavaScript and
     24 	special macros.
     25 	Macros are expanded via preprocessing, and use this syntax:
     26 	<pre class="code">${<id>}</pre>
     27 	<id> supports dot notation, e.g. ${address.name.lastName}.<br/>
     28 	<br/>
     29 	<b>Example:</b>
     30 	<pre class="code">
     31 "Half of " + ${editor1.dataValue} + " is " + ${editor1.dataValue}/2.
     32
     33 <i>// Macros are replaced with quoted JSON and should not be inside of literal strings</i>
     34 "${lastName}, ${firstName}" <i>// bad</i>
     35 ${lastName} + ", " + ${firstName} <i>// good</i>
     36 </pre>
     37 */
     38 wm.expression = {
     39 	/**
     40 		Evaluate expression with given namespace root.
     41 		@param {String} inExpression Valid javascript that is evaluated in global scope. The expression can contain
     42 			macros.
     43 		@param {String} inRoot The root object under which id macros are evaluated.
     44 		@example
     45 var exp = '"Half of " + ${editor1.dataValue} + " is " + ${editor1.dataValue}/2.';
     46 wm.expression.getValue(exp, app.main);
     47 	*/
     48 	getValue: function(inExpression, inRoot) {
     49 		var v = wm.expression._getText(inExpression, inRoot);
     50 		return wm.evalJs(v);
     51 	},
     52 	getSources: function(inExpression) {
     53 		var re = wm.expression._getSourceRegEx
     54 		re.lastIndex = 0;
     55 		var m, sources=[];
     56 		while((m = re.exec(inExpression)) != null)
     57 			sources.push(m[1]);
     58 		return sources;
     59 	},
     60 	_getText: function(inExpression, inRoot) {
     61 		//return inExpression.replace(wm.expression._getSourceRegEx(), function(){
     62 		return inExpression.replace(wm.expression._getSourceRegEx, function(){
     63 			try {
     64 				var v = inRoot.getValue(arguments[1]);
     65 				// objects cannot be returned directly since they are eval'd.
     66 				if (v instanceof wm.Object || v === undefined)
     67 					v = "";
     68 				// do we want to automatically jsonify all values?
     69 				return dojo.toJson(v);
     70 			} catch(e) {}
     71 		});
     72 	},
     73 	_getSourceRegEx: new RegExp(/\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g)
     74 	//_getSourceRegEx: function() {
     75 	//	return new RegExp(/\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g);
     76 	//}
     77 }