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