OSCON/Main.js
changeset 11 aaba47256eea
parent 7 97dcd250e5be
equal deleted inserted replaced
10:07ac2f6a36a9 11:aaba47256eea
       
     1 // ////////////////////////////////////////////////////////////////////////////
       
     2 // Symbian Foundation Example Code
       
     3 //
       
     4 // This software is in the public domain. No copyright is claimed, and you 
       
     5 // may use it for any purpose without license from the Symbian Foundation.
       
     6 // No warranty for any purpose is expressed or implied by the authors or
       
     7 // the Symbian Foundation. 
       
     8 // ////////////////////////////////////////////////////////////////////////////
       
     9 
       
    10 var uiManager;
       
    11 var home;
       
    12 var mainView;
       
    13 var osconIcalUrl = new Array();
       
    14 var icalData = new Array();
       
    15 var icalReader = new Array();
       
    16 var osconDays = new Array();
       
    17 var http;
       
    18 var downloadDayIndex = -1;
       
    19 
       
    20 // Called from the onload event handler to initialize the widget.
       
    21 function init() {
       
    22 	
       
    23 	for (var i = 0; i < 5; i++) {
       
    24 		var osconDay = new Date()
       
    25 		osconDay.setFullYear(2009, 6, 20 + i);
       
    26 		osconDays.push(osconDay);
       
    27 		osconIcalUrl.push("OSCON200907" + (20+i) + ".ics");
       
    28 		icalData.push(null);
       
    29 		icalReader.push(null);
       
    30 	}
       
    31 	
       
    32     // set tab-navigation mode and show softkeys
       
    33     // (only if we are in the WRT environment)
       
    34     if (window.widget) {
       
    35 		//create about menu
       
    36 	
       
    37         widget.setNavigationEnabled(false);
       
    38         menu.showSoftkeys();
       
    39     }
       
    40 	
       
    41 	// create UI manager
       
    42 	uiManager = new UIManager();
       
    43 
       
    44 	home = new ListView(null,null);
       
    45 	
       
    46 	var homeViewImage = new ImageLabel(null, null, "oscon-home.png");
       
    47 	// hack to center image
       
    48 	homeViewImage.contentElement.style.textAlign = "center";
       
    49 	home.addControl(homeViewImage);
       
    50 
       
    51 	var homeViewImage2 = new ImageLabel(null, null, "logo.png");
       
    52 	// hack to center image
       
    53 	homeViewImage2.contentElement.style.textAlign = "center";
       
    54 	home.addControl(homeViewImage2);
       
    55 	
       
    56 	mainView = new ListView(null, "<img src=oscon-home.png>");
       
    57 	
       
    58 	
       
    59 	for (var i = 0; i < osconDays.length; i++) {
       
    60 		var button = new NavigationButton(i, "day"+(i+1)+".png", dateToString(osconDays[i]));
       
    61 		mainView.addControl(button);
       
    62 		button.addEventListener("ActionPerformed", function(event){
       
    63 			var clickedButton = event.source;
       
    64 			var clickedId = clickedButton.id;
       
    65 			showDay(clickedId, osconDays[clickedId], null);
       
    66 			});
       
    67 	}	
       
    68 
       
    69 	home.show();
       
    70 	mainView.previousView = null;	
       
    71 	uiManager.showNotification(-1, "wait", "Please wait...", -1);
       
    72 	setTimeout(function(){	uiManager.hideNotification();mainView.show();}, 1000);
       
    73 }
       
    74 
       
    75 
       
    76 function showDay(dayIndex, date) {
       
    77 	downloadDayIndex = dayIndex;
       
    78 	if ( icalReader[dayIndex] == null ) {
       
    79 		setTimeout(function(){	downloadIcalData(downloadDayIndex);}, 100);
       
    80 	} else {
       
    81 		showList(date, null);
       
    82 	}
       
    83 }
       
    84 
       
    85 function downloadIcalData(dayIndex) {
       
    86 	downloadDayIndex = dayIndex;
       
    87 
       
    88 	http = new Ajax();
       
    89     http.onreadystatechange = function() { downloadStateChanged(); };
       
    90 
       
    91     // initiate the request
       
    92     http.open("GET", osconIcalUrl[downloadDayIndex], true);
       
    93     http.send(null);
       
    94 }
       
    95 
       
    96 function downloadStateChanged(){
       
    97 	// complete request?
       
    98     if (http.readyState == 4) {
       
    99         // attempt to get response status
       
   100         var responseStatus = null;
       
   101         try {
       
   102             responseStatus = http.status;
       
   103         } catch (noStatusException) {}
       
   104         
       
   105 		// are we being prompted for login?
       
   106 		icalData[downloadDayIndex] = http.responseText;
       
   107 		try {
       
   108 			dataAvailable(downloadDayIndex);
       
   109 //			savePreferences();
       
   110 		}catch(x) {
       
   111 			uiManager.showNotification(5000, "warning", "Error processing feed");
       
   112 		}
       
   113 		downloadInProgress = false;
       
   114     }
       
   115 }
       
   116 
       
   117 function dataAvailable(downloadDayIndex){
       
   118 	uiManager.showNotification(-1, "wait", "Parsing info...", -1);
       
   119 	// parse iCal
       
   120 	var reader = new iCalReader(); // Construction of the reader object.
       
   121 	reader.prepareData(icalData[downloadDayIndex]); // Prepare and set the data for the parser.
       
   122 	reader.parse(); // Parse the data.
       
   123 	reader.sort(); // Sort the data.
       
   124 	icalReader[downloadDayIndex] =  reader;
       
   125 	showList(osconDays[downloadDayIndex], null);
       
   126 	uiManager.hideNotification();
       
   127 }
       
   128 
       
   129 function showList(day, session) {
       
   130 	var list = new ListView(null, "<img src=oscon-home.png>");
       
   131 	if (day) {
       
   132 		var button = new ImageLabel(null, dateToString(day), "day"+(downloadDayIndex+1)+".png");
       
   133 		list.addControl(button);
       
   134 	}
       
   135 	if (session) {
       
   136 		var button = new ImageLabel(null, sessionTimeToString(session) + ", " + dateToString(session) , "session.png");
       
   137 		list.addControl(button);
       
   138 	}
       
   139 	
       
   140 	var myCalReader = icalReader[downloadDayIndex];
       
   141 	var events = myCalReader.getCalendar().getEvents(); // Get all events.
       
   142 	var num = myCalReader.getCalendar().getNrOfEvents();
       
   143 
       
   144 	var addedSessions = new Array();
       
   145 
       
   146 	
       
   147 	for(var i=0; i<num; i++) { // Loop through all events.
       
   148 	
       
   149 		var event = myCalReader.getCalendar().getEventAtIndex(i); // A single event.        
       
   150 		
       
   151 		// Get Javascript date for start and end time.
       
   152 		var startDate = event.getStartDate();
       
   153 		var altStartDate = null;
       
   154 		try {
       
   155 			altStartDate = event.getAltStartDate();
       
   156 		} catch(z) {
       
   157 			altStartDate = startDate; 
       
   158 		}
       
   159 		var timeZone = event.getTimeZone();
       
   160 		
       
   161 		if ( day ) {
       
   162 			if ( ! dayMatches(day, startDate) ) {
       
   163 				continue;
       
   164 			} else {
       
   165 				var haveit = false;
       
   166 				// if session is not in already, add it
       
   167 				for (var j = 0 ; j < addedSessions.length ; j++ ) {
       
   168 					if ( sessionMatches(addedSessions[j], altStartDate ) ) {
       
   169 						haveit = true;
       
   170 						break;
       
   171 					}
       
   172 				}
       
   173 				if (!haveit) {
       
   174 					// add it
       
   175 					addedSessions.push(altStartDate);
       
   176 					var button = new NavigationButton(altStartDate.getTime(), "session.png", "Session @ "+ sessionTimeToString(altStartDate));
       
   177 					button.addEventListener("ActionPerformed", function(event){
       
   178 						var clickedButton = event.source;
       
   179 						var clickedId = clickedButton.id;
       
   180 						var ed = new Date();
       
   181 						ed.setTime(clickedId);
       
   182 						showList(null, ed);
       
   183 					});
       
   184 					list.addControl(button);
       
   185 				}
       
   186 				continue;
       
   187 			}
       
   188 		}
       
   189 		else if ( ! sessionMatches(session, startDate) ) {
       
   190 			continue;
       
   191 		}
       
   192 
       
   193 
       
   194 		// add events
       
   195 		var endDate = event.getEndDate();
       
   196 		var location = event.getProperty("LOCATION");
       
   197 		var summary = event.getProperty("SUMMARY");
       
   198 		var description = event.getProperty("DESCRIPTION");
       
   199 		var url = event.getProperty("URL");
       
   200 
       
   201 		var buf = "";
       
   202 		buf += "<div class=\"FeedItemDate\">" ;
       
   203 		if ( location != null ) {
       
   204 			buf += location + ", ";
       
   205 		}
       
   206 		buf += sessionTimeToString(startDate) +"-" + sessionTimeToString(endDate) + " " + timeZone + "</div>";
       
   207 		buf += "<div class=\"FeedItemDescription\">" + description + "</div>";
       
   208 		if (url != null) {
       
   209 	        buf += "<div class=\"FeedItemLink\">";
       
   210             buf += "<a href=\"JavaScript:void(0)\" onclick=\"openURL('" + url + "'); return false;\">";
       
   211             buf += "Read more...";
       
   212             buf += "</a>";
       
   213 	        buf += "</div>";
       
   214 		}
       
   215 		
       
   216 		var cp = new ContentPanel(null, null, null, true);
       
   217 
       
   218 		// initialize feed item control
       
   219 		cp.setCaption(summary);
       
   220 		cp.setContent(buf);
       
   221 		cp.setExpanded(false);
       
   222 		list.addControl(cp); 	
       
   223 	} // End for each event.
       
   224 	list.previousView = uiManager.currentView;
       
   225 	mainView.previousView = null;
       
   226 	list.show();
       
   227 }
       
   228 
       
   229 
       
   230 // Loads widget preferences.
       
   231 function loadPreferences() {
       
   232     if (window.widget) {
       
   233         // load settings from widget preferences store
       
   234         icalData = widget.preferenceForKey("icalData");
       
   235     }
       
   236 }
       
   237 
       
   238 // Loads widget preferences.
       
   239 function savePreferences() {
       
   240     if (window.widget) {
       
   241         // save settings in widget preferences store
       
   242         widget.setPreferenceForKey(icalData, "icalData");
       
   243     }
       
   244 }
       
   245 
       
   246 
       
   247 
       
   248 function setDefaultFontSizeForScreenSize(){
       
   249 	// first check if there is a preference present
       
   250     if (window.widget) {
       
   251 		var saved = widget.preferenceForKey("fontsize");
       
   252 		if ( widget.preferenceForKey("fontsize") ) {
       
   253 			setCssBodyFontSize(parseInt(saved));
       
   254 		}
       
   255 		else {
       
   256 			// no preference available, check screen size
       
   257 			if (window.screen.width > 400 || window.screen.height > 400) {
       
   258 				// hi res screen, use large font
       
   259 				setCssBodyFontSize(18);
       
   260 			}
       
   261 			else {
       
   262 				// lo res screen, use small font
       
   263 				setCssBodyFontSize(14);
       
   264 			}
       
   265 		}
       
   266 	}
       
   267 }
       
   268 
       
   269 
       
   270 function nocache(url) {
       
   271     if (url.indexOf("?") == -1) {
       
   272         url += "?";
       
   273     } else {
       
   274         url += "&";
       
   275     }
       
   276     url += "nocache=" + (new Date().getTime());
       
   277 	return url;
       
   278 }
       
   279 
       
   280 
       
   281 function sessionMatches(session, startDate) {
       
   282 	var m_date = session.getDate()==startDate.getDate();
       
   283 	var m_year = session.getFullYear()==startDate.getFullYear();
       
   284 	var m_month = session.getMonth()==startDate.getMonth();
       
   285 	var m_hour = session.getHours()==startDate.getHours();
       
   286 	var m_minute = session.getMinutes()==startDate.getMinutes();
       
   287 	return m_date && m_month && m_year && m_hour && m_minute; 
       
   288 }
       
   289 
       
   290 function dayToString(day) {
       
   291 	return day.toDateString(); 	
       
   292 }
       
   293 
       
   294 function dayMatches(day, startDate){
       
   295 	var m_date = day.getDate()==startDate.getDate();
       
   296 	var m_year = day.getFullYear()==startDate.getFullYear();
       
   297 	var m_month = day.getMonth()==startDate.getMonth();
       
   298 	return m_date && m_month && m_year; 
       
   299 }
       
   300 
       
   301 function sessionTimeToString(session) {
       
   302 	return ""+session.getHours()+":"+pad(session.getMinutes(),2); 	
       
   303 }
       
   304 
       
   305 function dateToString(day) {
       
   306 	var full = day.toDateString();
       
   307 	// remove year as it doesn't fit on small screens
       
   308 	return full.substring(0, full.length-4); 	
       
   309 }
       
   310 
       
   311 function pad(num, digits) {
       
   312 	var str = "" + num;
       
   313 	while ( str.length < digits ) {
       
   314 		str = "0" + str;
       
   315 	}
       
   316 	return str;
       
   317 }
       
   318 
       
   319 // Opens a URL in a separate browser window
       
   320 function openURL(url) {
       
   321     if (window.widget) {
       
   322         // in WRT
       
   323         widget.openURL(url);
       
   324     } else {
       
   325         // outside WRT
       
   326         window.open(url, "NewWindow");
       
   327     }
       
   328 }
       
   329