Symbian.org/preview/script/lib/widget.js
changeset 0 54498df70f5d
child 10 07ac2f6a36a9
equal deleted inserted replaced
-1:000000000000 0:54498df70f5d
       
     1 /**
       
     2  * Widget object constructor
       
     3  * @param {void}
       
     4  *     widget()
       
     5  * @return {void}
       
     6  */ 
       
     7 function Widget()
       
     8 {
       
     9 	//	widget identifier, dummy value
       
    10 	this.identifier = 14021981;
       
    11 	this.isrotationsupported = true;
       
    12 	
       
    13 	//	widget event triggers
       
    14 	this.onshow = null;
       
    15 	this.onhide = null;
       
    16 	
       
    17 	/*
       
    18 	 * Custom extra functionalities to support
       
    19 	 */
       
    20 	this.path = document.location.pathname;
       
    21 	this.sysAPI = null;
       
    22 	this.onload = null;
       
    23 	this.opacity = 50;
       
    24 	this.interval = 20;
       
    25 	this.isFront = false;
       
    26 	this.preferenceArray = new Array();
       
    27 	this.preferenceKey  = 0;
       
    28 }
       
    29 
       
    30 
       
    31 /**
       
    32  * Launches the browser with the specified url 
       
    33  * @param {String} url
       
    34  *     openURL()
       
    35  * @return {Void}
       
    36  */ 
       
    37 Widget.prototype.openURL = function(url)
       
    38 {
       
    39    if (url) {
       
    40 	   window.open(url ,"New Widget Window",'height=200 width=250');
       
    41    }
       
    42 }
       
    43 
       
    44 
       
    45 /**
       
    46  * Returns previously stored preference associated with the specified key
       
    47  * @param {String} Key preference value to be fetch
       
    48  *     preferenceForKey()
       
    49  * @return {String} Value
       
    50  */ 
       
    51 Widget.prototype.preferenceForKey = function(key){
       
    52 	var name = "Nokia_WRT#" + this.path + "#" + key;
       
    53 	var result = readCookie(document, name);
       
    54 	return result;
       
    55 }
       
    56 
       
    57 
       
    58 /**
       
    59  * Stores the key associated with the specified preference
       
    60  * @param {String} Preference value to be stored 
       
    61  * @param {String} Key Preference value associated to
       
    62  *     setPreferenceForKey()
       
    63  * @return {Void}
       
    64  */ 
       
    65 Widget.prototype.setPreferenceForKey = function(preference, key)
       
    66 {
       
    67 	var value;
       
    68 	//Specifying null for the preference parameter removes the specified key from the preferences
       
    69 	if(key == null){
       
    70 		if(this.preferenceKey>0){
       
    71 			this.preferenceKey--;
       
    72 		}
       
    73 		//delete from cookies
       
    74 	}
       
    75 	value = "Nokia_WRT#"+this.path+"#"+key;
       
    76 	this.preferenceArray[this.preferenceKey] = value;
       
    77 	
       
    78 	createCookie(document,value,preference,240000);
       
    79 	this.preferenceKey++;
       
    80 	
       
    81 	//save cookie for cookies
       
    82 	updateMainCookie(document);
       
    83 }
       
    84 
       
    85 
       
    86 
       
    87 /**
       
    88  * Toggle between Tabbed navigation mode or Cursor mode
       
    89  * @param {Boolean} Value
       
    90  *     setNavigationEnabled()
       
    91  * @return {Void}
       
    92  */ 
       
    93 Widget.prototype.setNavigationEnabled = function(bool)
       
    94 {
       
    95 	//This function can not be used on preview browser
       
    96 }
       
    97 
       
    98 
       
    99 
       
   100 /**
       
   101  * Open S0-Application identified by UID along with the specified params
       
   102  * @param {Integer} Uid hexadecimal value to a specified application
       
   103  * @param {String} Value
       
   104  *     openApplication()
       
   105  * @return {Void}
       
   106  */ 
       
   107 Widget.prototype.openApplication = function(Uid, param)
       
   108 {
       
   109 	alert("openApplication function won't be simulated in this application");
       
   110 }
       
   111 
       
   112 
       
   113 
       
   114 /**
       
   115  * Prepares the Widget.to do transition to specified transitionState
       
   116  * @param {String} Value Transition state
       
   117  *     prepareForTransition()
       
   118  * @return {Void}
       
   119  */ 
       
   120 Widget.prototype.prepareForTransition = function(transitionState)
       
   121 {
       
   122     this.isFront = ("" + transitionState).toLowerCase() != "toback";
       
   123     window.document.getElementsByTagName("body")[0].style.opacity = "0.3";
       
   124 }
       
   125 
       
   126 
       
   127 
       
   128 
       
   129 /**
       
   130  * Does the animation to make the transition between the specified transitionState
       
   131  * @param {Void}
       
   132  *     performTransition()
       
   133  * @return {Void}
       
   134  */ 
       
   135 Widget.prototype.performTransition = function()
       
   136 {
       
   137     var _self = this;
       
   138     this.opacity = 0;
       
   139     this.interval = window.setInterval(function() {
       
   140       _self.opacity += 0.2;
       
   141       if (_self.opacity > 1) {
       
   142         _self.opacity = 1;
       
   143       }
       
   144       window.document.getElementsByTagName("body")[0].style.opacity = _self.opacity + "";
       
   145       if (_self.opacity >= 1) {
       
   146        window.clearInterval(_self.interval);
       
   147        window.document.getElementsByTagName("body")[0].style.opacity = "1";
       
   148       }
       
   149       //do nothing
       
   150     }, 50);  
       
   151       //do nothing
       
   152 }
       
   153 
       
   154 
       
   155 
       
   156 
       
   157 
       
   158 /**
       
   159  * Set the preferred screen orientation to landscape. 
       
   160  * The display will flip if the phone display orientation 
       
   161  * is portrait and the phone supports landscape mode.
       
   162  * @param {Void}
       
   163  *     setDisplayLandscape()
       
   164  * @return {Void}
       
   165  */ 
       
   166 Widget.prototype.setDisplayLandscape = function(){
       
   167 	try 
       
   168 	{
       
   169 		if (this.isrotationsupported && childToParent_Reference.Emulator.orientation_mode != 'landscape') 
       
   170 		{
       
   171 			childToParent_Reference.Emulator.changeOrientation(childToParent_Reference.$('DisplayOrientation'));
       
   172 		}
       
   173 	} 
       
   174 	catch (e) {}
       
   175 }
       
   176 
       
   177 
       
   178 
       
   179 
       
   180 /**
       
   181  * Set the preferred screen orientation to portrait.  
       
   182  * The display will flip if the phone display orientation 
       
   183  * is landscape and the phone supports portrait mode.
       
   184  * @param {Void}
       
   185  *     setDisplayPortrait()
       
   186  * @return {Void}
       
   187  */ 
       
   188 Widget.prototype.setDisplayPortrait = function()
       
   189 {
       
   190 	try 
       
   191 	{
       
   192 		if (this.isrotationsupported && childToParent_Reference.Emulator.orientation_mode != 'portrait') 
       
   193 		{
       
   194 			childToParent_Reference.Emulator.changeOrientation(childToParent_Reference.$('DisplayOrientation'));
       
   195 		}
       
   196 	} 
       
   197 	catch (e) {}
       
   198 }
       
   199 
       
   200 /**
       
   201  * Allows the definition of a function to be called 
       
   202  * when a Widget.is displayed 
       
   203  * @param {Void}
       
   204  *     onshow()
       
   205  * @return {Void}
       
   206  */ 
       
   207 Widget.prototype.onshow = function()
       
   208 {
       
   209 	// to be implemented
       
   210 }
       
   211 
       
   212 
       
   213 
       
   214 
       
   215 /**
       
   216  * Allows the definition of a function to be called 
       
   217  * when a Widget.sent into the background (hidden) 
       
   218  * @param {Void}
       
   219  *     onhide()
       
   220  * @return {Void}
       
   221  */ 
       
   222 Widget.prototype.onhide  = function()
       
   223 {
       
   224 	// to be implemented
       
   225 }
       
   226 
       
   227 
       
   228 
       
   229 /**
       
   230  * This function returns the System API if sysinfo is included in document embed
       
   231  */
       
   232 Widget.prototype.enableSystemApi = function()
       
   233 {
       
   234 	
       
   235 	//	Identify, and Attach System-Info-Object properties
       
   236 	try 
       
   237 	{
       
   238 		var parentIframeRef = window.parent.frames[0];
       
   239 		if(parentIframeRef) 
       
   240 		{
       
   241 			if (parentIframeRef.document.embeds.length > 0) {
       
   242 				for (var i = 0; i < parentIframeRef.document.embeds.length; i++) 
       
   243 				{
       
   244 					//match the system Info API embed tag
       
   245 					if (parentIframeRef.document.embeds[i].type == 'application/x-systeminfo-widget') 
       
   246 					{
       
   247 						new systemAPI(parentIframeRef.document.embeds[i]);
       
   248 						widget.sysAPI = parentIframeRef.document.embeds[i];
       
   249 					}
       
   250 				}
       
   251 			}
       
   252 		}
       
   253 	} 
       
   254 	catch (e) {
       
   255 		alert('Error in attachSysInfo: ' + e);
       
   256 	}	
       
   257 
       
   258 	//	Attach menu object to window
       
   259 	window.menu = new Menu();
       
   260 
       
   261 	// Attach window reference to the Parent Window	
       
   262 	window.parent.Emulator.parentToChild_Reference = window;
       
   263 
       
   264 	//	add event listener to window.focus
       
   265 	window.onfocus = function(){ menu.cancel();	}
       
   266 
       
   267 	//	add event listener to window.focus
       
   268 	window.onunload = function()
       
   269 	{ 
       
   270 		try
       
   271 		{
       
   272 			//	Trigger Callback of Widget.onHide function
       
   273 			if(typeof(widget.onhide) == 'function')
       
   274 			{
       
   275 				widget.onhide();
       
   276 			}
       
   277 		}
       
   278 		catch(e){ errorLog('widget.onhide: '+e.description, 0); }
       
   279 	}
       
   280 
       
   281 
       
   282 	/*
       
   283 	 * Used as a bridge between, Child widget & Parent window
       
   284 	 */
       
   285 	window.childToParent_Reference = window.parent;
       
   286 
       
   287 }
       
   288 
       
   289 /*
       
   290  * support functions for widget object
       
   291  */
       
   292 
       
   293 /**
       
   294  * This function stores cookie for all the cookies
       
   295  * to help finding cookies of the same document while clearing preferences
       
   296  * @param doucment -- Document object
       
   297  */
       
   298 function updateMainCookie(doucment){
       
   299 	var temp="";
       
   300 	name = "Nokia_WRT#"+widget.path;
       
   301 	for (var k = 0; k<widget.preferenceArray.length; k++){
       
   302 		temp = temp+"|"+widget.preferenceArray[k];
       
   303 	}
       
   304 	createCookie(document,name,temp,24000);
       
   305 }
       
   306 
       
   307 /**
       
   308  * This function creates cookie for the setPreferenceForKey function in order to save key-pref persistently
       
   309  * 
       
   310  * @param document -- Document object
       
   311  * @param name -- Name of the cookie
       
   312  * @param value -- value for the name cookie
       
   313  * @param days -- expire
       
   314  * 
       
   315  */
       
   316 
       
   317 function createCookie(document,name,value,days) {
       
   318     if (days) {
       
   319 		var date = new Date();
       
   320 		date.setTime(date.getTime()+(days*24*60*60*1000));
       
   321 		var expires = "; expires="+date.toGMTString();
       
   322 	}
       
   323 	else var expires = "";
       
   324 	document.cookie = name+"="+value+expires+"; path=/"
       
   325 } 
       
   326 
       
   327 /**
       
   328  * This function retrieves back the values from the cookies
       
   329  * @param document
       
   330  * @param name
       
   331  * @return
       
   332  */
       
   333 function readCookie(document , name) {
       
   334 	var nameEQ = name + "=";
       
   335 	var ca = document.cookie.split(';');
       
   336 	for(var i=0;i < ca.length;i++) {
       
   337 		var c = ca[i];
       
   338 		while (c.charAt(0)==' ') c = c.substring(1,c.length);
       
   339 		if (c.indexOf(nameEQ) == 0) {
       
   340 			return c.substring(nameEQ.length,c.length);
       
   341 		}
       
   342 	}
       
   343 	return undefined;
       
   344 }
       
   345 
       
   346 
       
   347 /*
       
   348  * /////////////////////////////////////////////////////////////////////////////////////
       
   349  * //////////////////////////////	  Ends here		  //////////////////////////////////
       
   350  * /////////////////////////////////////////////////////////////////////////////////////
       
   351  */
       
   352 
       
   353 function errorLog(str, flag)
       
   354 {
       
   355 //	alert(str);
       
   356 }
       
   357 
       
   358 /*
       
   359  * by John Resig
       
   360  * @reference: http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
       
   361  */
       
   362 function addEvent( obj, type, fn ){ 
       
   363    if (obj.addEventListener){ 
       
   364       obj.addEventListener( type, fn, false );
       
   365    }
       
   366    else if (obj.attachEvent){ 
       
   367       obj["e"+type+fn] = fn; 
       
   368       obj[type+fn] = function(){ obj["e"+type+fn]( window.event ); } 
       
   369       obj.attachEvent( "on"+type, obj[type+fn] ); 
       
   370    } 
       
   371 }
       
   372 
       
   373 /*
       
   374  * 		Create a new Widget Object when DOM ready
       
   375  * 
       
   376 */
       
   377 try 
       
   378 {
       
   379 	//	attach widget object to window
       
   380 	var widget = new Widget();
       
   381 	
       
   382 	//	attach the System-Info api specific functionality
       
   383 	addEvent(window, 'load', widget.enableSystemApi);
       
   384 } 
       
   385 catch (e) 
       
   386 {
       
   387 	alert('Exception: Widget object creation');
       
   388 }