hwc-utils.js

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