hwc-utils.js

1       /**
2        * Sybase Hybrid App version 2.3
3        *
4        * Utils_CONT.js    - container maintained aspect
5        *
6        * This file will not be regenerated, so it is possible to modify it, but it
7        * is not recommended.
8        *
9        * The template used to create this file was compiled on Thu Jun 07 14:57:11 EDT 2012
10       *
11       * Copyright (c) 2012 Sybase Inc. All rights reserved.
12       */
13      /** @namespace */
14      hwc = (typeof hwc === "undefined" || !hwc) ? {} : hwc;      // SUP 'namespace'
15      
16      /**
17       * Container Utilities
18       */
19      (function(hwc, window, undefined) {
20      
21      /****************** PUBLIC CONSTANTS ************************/
22      /** @private */
23      hwc.versionURLParam = "version=2.2";
24      
25      
26      /****************** PUBLIC API ************************/
27      
28      /**
29       * The version number sent with the HTTP messages to the native code.
30       * Used for internal versioning only
31       * @private
32       * @return {String} the version string
33       */
34      hwc.getVersionURLParam = function() {
35          return hwc.versionURLParam;
36      };
37      
38      
39      /**
40       * Internal worker for initial HybridApp loading.
41       * Returns the response message or empty.
42       * @private
43       */
44      hwc.onHybridAppLoad_CONT = function() {
45         var response = hwc.getTransformData();
46         processDataMessage(response, false, true);
47      };
48      
49      /**
50       * Returns the transform data for the hybridapp.  Only a server-initiated app will have this data.
51       * @example
52       * TODO: Add an example
53       * @return the transform data.
54       * @public
55       * @memberOf hwc
56       */
57      hwc.getTransformData = function() {
58         var xmlhttp;
59         hwc.traceEnteringMethod("hwc.getTransformData");
60         
61         try {
62      	   if (hwc.isWindows()) {
63      		  xmlhttp = hwc.getXMLHTTPRequest();
64      		  xmlhttp.open("GET", "transform.xml", false);
65      		  xmlhttp.send("");
66      		  if (xmlhttp.status === 200 || xmlhttp.status === 0) {//Win32 returns 200 for OK, WM returns 0 for OK
67      			 return xmlhttp.responseText;
68      		  }
69      		}
70      		else
71      		{
72      			return hwc.getDataFromContainer("loadtransformdata");
73      		}
74      	} finally {
75      		hwc.traceLeavingMethod("hwc.getTransformData");
76      	}
77      };
78      
79      /**
80       * Internal worker for adding a single menu item.
81       * @private
82       */
83      hwc.addNativeMenuItem_CONT = function (menuStr ) {
84              hwc.postDataToContainer("addallmenuitems", "menuitems=" + encodeURIComponent(menuStr));
85      };
86      
87       /**
88        * Internal worker setting credential information.
89       * @private
90        */
91      hwc.handleCredentialChange_CONT = function(credInfo) {
92              var requestData = credInfo ? credInfo : "";
93              if (requestData) {
94                  if (!hwc.isWindows())  {
95                        hwc.postDataToContainer("formredirect", requestData);
96                  }
97              }
98      };
99      
100     /**
101      * Removes spaces from the specified string.
102      * @private
103      * @param str The specified string
104      * @param leftAndRightOnly When true removes leading and trailing spaces
105      * @return The trimmed string
106      * @memberOf hwc
107      */
108     hwc.trimSpaces = function(str, leftAndRightOnly) {
109         if (leftAndRightOnly) {
110             return str.replace(/^\s+|\s+$/g,"");
111         }
112         return str.replace(/\s+/g, '');
113     };
114     
115     /** @private 
116      * @memberOf hwc
117      */
118     hwc.parseBoolean = function(value) {
119        if (value) {
120           return hwc.trimSpaces(value, true).toLowerCase() === "true";
121        }
122        else {
123           return false;
124        }
125     };
126     
127     /**
128      * Extract the error message from a URL string. The parameter name of the error message should be "onErrorMsg".
129      *
130      * @param {String} errString The error string URL
131      * @return {String} error message
132      * @memberOf hwc
133      * @public
134      */
135     hwc.getOnErrorMessageFromNativeError = function getOnErrorMessageFromNativeError(errString) {
136     		hwc.traceEnteringMethod("hwc.getOnErrorMessageFromNativeError");
137     		try {
138     			if( hwc.isBlackBerry() ) {
139     				return unescape(hwc.getURLParamFromNativeError("onErrorMsg", errString));
140     			} else {
141     				// This is a temporary fix for a bug in the container that calls
142     				// encodeURIComponent on the whole query string for Android.  See
143     				// IR 676161-2.
144     				return hwc.getURLParamFromNativeError("onErrorMsg", errString);
145     			}
146     		} finally {
147     			hwc.traceLeavingMethod("hwc.getOnErrorMessageFromNativeError");
148     		}
149     };
150     
151     /**
152      * Extract the error call back method name from a URL string. The parameter name of the error call back method should be "onErrorCallback".
153      * @param {String} errString The error string URL
154      * @return {String} the error callback method name
155      * @memberOf hwc
156      * @public
157      */
158     hwc.getCallbackFromNativeError = function getCallbackFromNativeError(errString) {
159     	hwc.traceEnteringMethod("hwc.getCallbackFromNativeError");
160     	try {
161     		return hwc.getURLParamFromNativeError("onErrorCallback", errString);
162     	} finally {
163     		hwc.traceLeavingMethod("hwc.getCallbackFromNativeError");
164     	}
165     };
166     
167     /**
168      * Extract an error code from a URL string. The parameter name of the error code should be "errCode".
169      * @example
170      * TODO: CONFIRM THE RETURN DATATYPE
171      * @param {String} errString The error string URL
172      * @return {String} error code
173      * @memberOf hwc
174      * @public
175      */
176     hwc.getCodeFromNativeError = function getCodeFromNativeError( errString ) {
177     	hwc.traceEnteringMethod("hwc.getCodeFromNativeError");
178     	try {
179     		return hwc.getURLParamFromNativeError("errCode", errString);
180     	} finally {
181     		hwc.traceLeavingMethod("hwc.getCodeFromNativeError");
182     	}
183     };
184     
185     /**
186      * Extract a native message from a URL string. The parameter name of the native message should be "nativeErrMsg".
187      * @param {String} errString The error string URL
188      * @return {String} the native message
189      * @memberOf hwc
190      * @public
191      */
192     hwc.getNativeMessageFromNativeError = function getNativeMessageFromNativeError( errString ) {
193     	hwc.traceEnteringMethod("hwc.getNativeMessageFromNativeError");
194     	try {
195     		return hwc.getURLParamFromNativeError("nativeErrMsg", errString);
196     	} finally {
197     		hwc.traceLeavingMethod("hwc.getNativeMessageFromNativeError");
198     	}
199     };
200     
201     /**
202      * Extract a parameter value from a URL string with a given parameter name.
203      * @param {String} paramName The parameter name
204      * @param {String} url The containing URL of the parameter
205      * @return {String} The parameter value
206      * @memberOf hwc
207      * @public
208      */
209     hwc.getURLParamFromNativeError = function getURLParamFromNativeError(paramName, url) {
210             var indxofS, idxofE, pName, pValue, paramSection, ret, paramSectionsAmp, ampSections, idxofA;
211     
212     		hwc.traceEnteringMethod("hwc.getURLParamFromNativeError");
213     		try {
214     			if( hwc.isBlackBerry() ) {
215     				paramSection = url;
216     			} else {
217     				// This is a temporary fix for a bug in the container that calls
218     				// encodeURIComponent on the whole query string for Android.  See
219     				// IR 676161-2.
220     				paramSection = decodeURIComponent(url);
221     			}
222     			idxofA = paramSection.indexOf("&");
223     			if (idxofA > 0) {//there is one or more parameters in the & section
224     				paramSectionsAmp = paramSection.substring(idxofA + 1);
225     				ampSections = paramSection.split("&");
226     				if (ampSections.length === 1) {
227     					idxofE = paramSectionsAmp.indexOf("=");
228     					pName = paramSectionsAmp.substring(0, idxofE);
229     					if (pName.toLowerCase() === paramName.toLowerCase()) {
230     						pValue = paramSectionsAmp.substring(idxofE + 1);
231     						ret = decodeURIComponent( pValue);
232     						return ret;
233     					}
234     				} else {  //multiple parameters in the & section
235     					for (indxofS in ampSections) {
236     						idxofE = ampSections[indxofS].indexOf("=");
237     						pName = ampSections[indxofS].substring(0, idxofE);
238     						if (pName.toLowerCase() === paramName.toLowerCase()) {
239     							pValue = ampSections[indxofS].substring(idxofE + 1);
240     							ret = decodeURIComponent( pValue) ;
241     							return ret;
242     						}
243     					}
244     				}
245     				//ok did not find paramName in & section look for it at the start
246     				idxofE = paramSection.indexOf("=");
247     				pName = paramSection.substring(0, idxofE);
248     				if (pName.toLowerCase() === paramName.toLowerCase()) {
249     					pValue = paramSection.substring(idxofE + 1, idxofA);
250     					ret = decodeURIComponent( pValue );
251     					return ret;
252     				}
253     			} else { //only one param
254     				idxofE = paramSection.indexOf("=");
255     				pName = paramSection.substring(0, idxofE);
256     				if (pName.toLowerCase() === paramName.toLowerCase()) {
257     					pValue = paramSection.substring(idxofE + 1);
258     					ret = decodeURIComponent( pValue );
259     					return ret;
260     				}
261     			}
262     			return pValue;
263     		} finally {
264     			hwc.traceLeavingMethod("hwc.getURLParamFromNativeError");
265     		}
266     };
267     
268     /**
269      * Log the behavior of entering a JavaScript method.
270      * @param {String} methodName The target method name.
271      * @private
272      */
273     hwc.traceEnteringMethod = function (methodName) {
274     	if (hwc.getLoggingCurrentLevel() >= 4) { hwc.log("entering " + methodName + "()", "DEBUG", false); }
275     }
276     
277     /**
278      * Log the behavior of leaving a JavaScript method.
279      * @param {String} methodName The target method name.
280      * @private
281      */
282     hwc.traceLeavingMethod = function (methodName) {
283     	if (hwc.getLoggingCurrentLevel() >= 4) { hwc.log("exiting " + methodName + "()", "DEBUG", false); }
284     }
285     
286     })(hwc, window);
287