mypackage_wrt/preview/script/lib/widget.js
changeset 42 20be4dd42b12
equal deleted inserted replaced
41:315255cd1aef 42:20be4dd42b12
       
     1 /**
       
     2  * widget object constructor
       
     3  * @param {void}
       
     4  *     widget()
       
     5  * @return {void}
       
     6  */ 
       
     7 
       
     8 if (typeof window.widget == "undefined" || !window.widget) {
       
     9 	window.widget = {
       
    10 		author : 'Nokia WRT Emulation Library',
       
    11 		//	widget identifier, dummy value
       
    12 		identifier: 14021981,
       
    13 		isrotationsupported: true,
       
    14 		
       
    15 		//	widget event triggers
       
    16 		onshow: null,
       
    17 		onhide: null,
       
    18 		
       
    19 		sysInfo: [],
       
    20 		onload: null,
       
    21 		opacity: 50,
       
    22 		interval: 20,
       
    23 		isFront: false,
       
    24 		preferenceArray: [],
       
    25 		preferenceKey: 0
       
    26 	};
       
    27 	
       
    28 	
       
    29 	/**
       
    30 	 * Launches the browser with the specified url
       
    31 	 * @param {String} url
       
    32 	 *     openURL()
       
    33 	 * @return {Void}
       
    34 	 */
       
    35 	widget.openURL = function(url){
       
    36 		if (url) {
       
    37 			window.open(url, "New Widget Window", 'height=200 width=250');
       
    38 		}
       
    39 	}
       
    40 	
       
    41 	
       
    42 	/**
       
    43 	 * Returns previously stored preference associated with the specified key
       
    44 	 * @param {String} Key preference value to be fetch
       
    45 	 *     preferenceForKey()
       
    46 	 * @return {String} Value
       
    47 	 */
       
    48 	widget.preferenceForKey = function(name){
       
    49 		return _BRIDGE_REF.nokia.helper.readCookie(name);
       
    50 	}
       
    51 	
       
    52 	
       
    53 	/**
       
    54 	 * Stores the key associated with the specified preference
       
    55 	 * @param {String} Preference value to be stored
       
    56 	 * @param {String} Key Preference value associated to
       
    57 	 *     setPreferenceForKey()
       
    58 	 * @return {Void}
       
    59 	 */
       
    60 	widget.setPreferenceForKey = function(preference, key){
       
    61 		_BRIDGE_REF.nokia.helper.createCookie(key, preference);
       
    62 	}
       
    63 	
       
    64 	
       
    65 	
       
    66 	/**
       
    67 	 * Toggle between Tabbed navigation mode or Cursor mode
       
    68 	 * @param {Boolean} Value
       
    69 	 *     setNavigationEnabled()
       
    70 	 * @return {Void}
       
    71 	 */
       
    72 	widget.setNavigationEnabled = function(bool){
       
    73 		//This function can not be used on preview browser
       
    74 	}
       
    75 	
       
    76 	
       
    77 	
       
    78 	/**
       
    79 	 * Open S0-Application identified by UID along with the specified params
       
    80 	 * @param {Integer} Uid hexadecimal value to a specified application
       
    81 	 * @param {String} Value
       
    82 	 *     openApplication()
       
    83 	 * @return {Void}
       
    84 	 */
       
    85 	widget.openApplication = function(Uid, param){
       
    86 		alert("openApplication function won't be simulated in this application");
       
    87 	}
       
    88 	
       
    89 	
       
    90 	
       
    91 	/**
       
    92 	 * Prepares the Widget.to do transition to specified transitionState
       
    93 	 * @param {String} Value Transition state
       
    94 	 *     prepareForTransition()
       
    95 	 * @return {Void}
       
    96 	 */
       
    97 	widget.prepareForTransition = function(transitionState){
       
    98 		this.isFront = ("" + transitionState).toLowerCase() != "toback";
       
    99 		window.document.getElementsByTagName("body")[0].style.opacity = "0.3";
       
   100 	}
       
   101 	
       
   102 	
       
   103 	
       
   104 	
       
   105 	/**
       
   106 	 * Does the animation to make the transition between the specified transitionState
       
   107 	 * @param {Void}
       
   108 	 *     performTransition()
       
   109 	 * @return {Void}
       
   110 	 */
       
   111 	widget.performTransition = function(){
       
   112 		var _self = this;
       
   113 		this.opacity = 0;
       
   114 		this.interval = window.setInterval(function(){
       
   115 			_self.opacity += 0.2;
       
   116 			if (_self.opacity > 1) {
       
   117 				_self.opacity = 1;
       
   118 			}
       
   119 			window.document.getElementsByTagName("body")[0].style.opacity = _self.opacity + "";
       
   120 			if (_self.opacity >= 1) {
       
   121 				window.clearInterval(_self.interval);
       
   122 				window.document.getElementsByTagName("body")[0].style.opacity = "1";
       
   123 			}
       
   124 			//do nothing
       
   125 		}, 50);
       
   126 		//do nothing
       
   127 	}
       
   128 	
       
   129 	
       
   130 	
       
   131 	
       
   132 	
       
   133 	/**
       
   134 	 * Set the preferred screen orientation to landscape.
       
   135 	 * The display will flip if the phone display orientation
       
   136 	 * is portrait and the phone supports landscape mode.
       
   137 	 * @param {Void}
       
   138 	 *     setDisplayLandscape()
       
   139 	 * @return {Void}
       
   140 	 */
       
   141 	widget.setDisplayLandscape = function(){
       
   142 		try {
       
   143 			if (this.isrotationsupported && _BRIDGE_REF.nokia.emulator.orientationSupports()) {
       
   144 				_BRIDGE_REF.nokia.emulator.setMode('landscape');
       
   145 			}
       
   146 		} 
       
   147 		catch (e) {
       
   148 		}
       
   149 	}
       
   150 	
       
   151 	
       
   152 	
       
   153 	
       
   154 	/**
       
   155 	 * Set the preferred screen orientation to portrait.
       
   156 	 * The display will flip if the phone display orientation
       
   157 	 * is landscape and the phone supports portrait mode.
       
   158 	 * @param {Void}
       
   159 	 *     setDisplayPortrait()
       
   160 	 * @return {Void}
       
   161 	 */
       
   162 	widget.setDisplayPortrait = function(){
       
   163 		try {
       
   164 			if (this.isrotationsupported && _BRIDGE_REF.nokia.emulator.orientationSupports()) {
       
   165 				_BRIDGE_REF.nokia.emulator.setMode('portrait');
       
   166 			}
       
   167 		} 
       
   168 		catch (e) {
       
   169 		}
       
   170 	}
       
   171 	
       
   172 	/**
       
   173 	 * Allows the definition of a function to be called
       
   174 	 * when a Widget.is displayed
       
   175 	 * @param {Void}
       
   176 	 *     onshow()
       
   177 	 * @return {Void}
       
   178 	 */
       
   179 	widget.onshow = function(){
       
   180 		// to be implemented
       
   181 	}
       
   182 	
       
   183 	
       
   184 	
       
   185 	
       
   186 	/**
       
   187 	 * Allows the definition of a function to be called
       
   188 	 * when a Widget.sent into the background (hidden)
       
   189 	 * @param {Void}
       
   190 	 *     onhide()
       
   191 	 * @return {Void}
       
   192 	 */
       
   193 	widget.onhide = function(){
       
   194 		// to be implemented
       
   195 	}
       
   196 	
       
   197 	
       
   198 	
       
   199 	/**
       
   200 	 * This function returns the System API if sysinfo is included in document embed
       
   201 	 */
       
   202 	widget.enableSystemApi = function(){
       
   203 	
       
   204 		//	Identify, and Attach System-Info-Object properties
       
   205 		try {
       
   206 			var parentIframeRef = window.parent.frames[0];
       
   207 			if (typeof parentIframeRef == 'object') {
       
   208 				if (parentIframeRef.document.embeds.length > 0) {
       
   209 					for (var i = 0; i < parentIframeRef.document.embeds.length; i++) {
       
   210 						//match the system Info API embed tag
       
   211 						if (parentIframeRef.document.embeds[i].type == 'application/x-systeminfo-widget') {
       
   212 							new systemAPI(parentIframeRef.document.embeds[i]);
       
   213 //							widget.sysInfo = parentIframeRef.document.embeds[i];
       
   214 							
       
   215 							// hide the <embed> object
       
   216 							parentIframeRef.document.embeds[i].style.display='none';
       
   217 							
       
   218 							// push the reference object into widget
       
   219 							widget.sysInfo.push(parentIframeRef.document.embeds[i]);
       
   220 						}
       
   221 					}
       
   222 				}
       
   223 			}
       
   224 		} 
       
   225 		catch (e) {
       
   226 			alert('Error in attachSysInfo: ' + e);
       
   227 		}
       
   228 	}
       
   229 	
       
   230 	/**
       
   231 	 * 
       
   232 	 */
       
   233 	
       
   234 	widget.triggerListener = function(provider, eventType, data){
       
   235 		if(widget.sysInfo.length){
       
   236 			for(var i=0; i<widget.sysInfo.length; i++){
       
   237 				if(provider == "power"){
       
   238 					switch(eventType){
       
   239 						case "chargerconnected" : 
       
   240 												  widget.sysInfo[i].chargerconnected = data;
       
   241 												  if(widget.sysInfo[i].onchargerconnected != null){
       
   242 													setTimeout(widget.sysInfo[i].onchargerconnected, 0);
       
   243 												  }else{
       
   244 														console.info("System API-1.0 Notice -- no listeners defined for eventType:"+eventType);
       
   245 												  }
       
   246 												  break;
       
   247 
       
   248 						case "chargelevel" 		:
       
   249 												  widget.sysInfo[i].chargelevel = data;
       
   250 												  if(widget.sysInfo[i].onchargelevel != null){
       
   251 													setTimeout(widget.sysInfo[i].onchargelevel, 0);
       
   252 												  }else{
       
   253 														console.info("System API-1.0 Notice -- no listeners defined for eventType:"+eventType);
       
   254 												  }
       
   255 												 break;
       
   256 					}
       
   257 				}
       
   258 			}
       
   259 		}else{
       
   260 			console.info("System API-1.0 Notice -- no listeners defined for eventType:"+eventType);
       
   261 		}
       
   262 	}
       
   263 	
       
   264 	//	make TRUE widget.js script loaded
       
   265 	window.parent.NOKIA.scriptsLoaded.widget = true;
       
   266 }
       
   267 
       
   268 (function(){
       
   269 
       
   270 	//	attach the System-Info api specific functionality
       
   271 	_BRIDGE_REF.helper.addEvent(window, 'load', function(){
       
   272 		widget.enableSystemApi();
       
   273 		
       
   274 	});
       
   275 
       
   276 	if (_BRIDGE_REF.nokia) {
       
   277 		_BRIDGE_REF.nokia.menu.lsk_event = function(){
       
   278 			_BRIDGE_REF.nokia.emulator.child.menu.show();
       
   279 		};
       
   280 		
       
   281 		//	Add THIS window Reference on FRAME WINDOW
       
   282 		//	NOKIA.emulator.child object reference
       
   283 		_BRIDGE_REF.nokia.emulator.child = window;
       
   284 		_BRIDGE_REF.nokia.menu.init();
       
   285 	}	
       
   286 })()