Skip to content

Instantly share code, notes, and snippets.

@128keaton
Created September 8, 2017 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 128keaton/31c6887cb59ae0651b5e60e7c8cff38d to your computer and use it in GitHub Desktop.
Save 128keaton/31c6887cb59ae0651b5e60e7c8cff38d to your computer and use it in GitHub Desktop.
main.js from Yahoo! Weather Widget
const dayList = { "Sunday":"Sun", "Monday":"Mon", "Tuesday":"Tue", "Wednesday":"Wed", "Thursday":"Thu", "Friday":"Fri", "Saturday":"Sat" };
const conditionList = [ "Tornado", "Tropical Storm", "Hurricane", "Strong Thunderstorms", "Thunderstorms", "Rain and Snow", "Rain and Sleet", "Freezing Rain", "Freezing Drizzle", "Drizzle", "Freezing Rain", "Showers", "Rain", "Flurries", "Snow Showers", "Blowing Snow", "Snow", "Hail", "Sleet", "Dust", "Fog", "Haze", "Smoke", "Breezy", "Windy", "Frigid", "Cloudy", "Mostly Cloudy", "Mostly Cloudy", "Partly Cloudy", "Partly Cloudy", "Clear", "Sunny", "Mostly Clear", "Mostly Sunny", "Mixed Rain and Hail", "Hot", "Isolated Thunderstorms", "Scattered Thunderstorms", "Scattered Showers", "Heavy Rain", "Scattered Snow Showers", "Heavy Snow", "Blizzard", "No Feed", "Scattered Showers", "Scattered Snow Showers", "Scattered Thunderstorms" ];
const maxPopupTextWidth = 148;
const msFifteenMinutes = 900000;
const msOneHour = 3600000;
const temperatureExpiration = 1800000; // 30 minutes
const forecastExpiration = 345600000; // 4 days
var gConnectionTries = 0;
var gControlList = [ windowSizeButton, unitsToggleButton, locationSelectionButtonFrame, forecastOffsiteLinkButtonFrame, currentOffsiteLinkButtonFrame ];
var gControlsVisible = false;
var gRunningAnimation = null;
var gBumpStamp = null;
var gCollapsedPosition = null;
var gLastPosition = null;
var gLastMousePositionInWindow;
var gMousePositionInControl = false;
var gMouseIsOver, gMouseIsDown;
var gIsNightTime, gLastIsNightTime;
var gBaseColor, gTextColor, gTextHighlightColor;
var gCcBaseColor = "#5883c8";;
var gCcTextColor = "#ffffff";
var gCcTextHighlightColor = "#265080";
var gShadowColor = "#000000";
var gLargeShadowColor = "rgba( 0, 0, 0, 0.5)";
var gSmallShadowOffset, gLargeShadowOffset;
var gOldPrefs = [ ];
var vitalityDoc = XMLDOM.parse( filesystem.readFile( "vitality.xml" ) );
var defaultWeatherDOM = XMLDOM.parse( filesystem.readFile( "Empty Feed.xml" ) );
var weatherDOM = null;
initWidgetDB( );
initWidgetState( );
// -------------------------------------------------------------------------------------------
function animateBase( )
{
var percent = ( animator.milliseconds - this.startTime ) / this.duration;
this.endAlertOpacity
temperatureWarningIcon.opacity = this.endAlertOpacity == 0 ? 255 * ( 1 - percent ) : 255 * percent;
var width = animator.ease( this.startWidth, this.endWidth, percent, animator.kEaseOut );
var height = animator.ease( this.startHeight, this.endHeight, percent, animator.kEaseOut );
setBaseSize( width, height );
if( this.finalState == "open" && ( mainWindow.vOffset + base.vOffset + base.height ) > this.screenHeight )
{
if( !gBumpStamp && !gCollapsedPosition )
{
gBumpStamp = mainWindow.vOffset;
}
mainWindow.vOffset = this.screenHeight - base.height - base.vOffset;
}
else if( gCollapsedPosition )
{
if( mainWindow.vOffset < gCollapsedPosition )
{
mainWindow.vOffset = this.screenHeight - base.height - base.vOffset;
}
else
{
mainWindow.vOffset = gCollapsedPosition;
gCollapsedPosition = null;
}
}
if( animator.milliseconds >= ( this.startTime + this.duration ) )
{
setBaseSize( this.endWidth, this.endHeight );
temperatureWarningIcon.opacity = this.endAlertOpacity;
if( gBumpStamp )
{
gCollapsedPosition = gBumpStamp;
gBumpStamp = null;
}
gLastPosition = mainWindow.vOffset;
return false;
}
return true;
}
// -------------------------------------------------------------------------------------------
function buttonMouseEnter( )
{
gMouseIsOver = this;
if( gMouseIsDown == this )
{
var img = this.firstChild;
while( img )
{
img.src = img.src.replace( / 0/, " 1" );
img = img.nextSibling;
}
}
}
// -------------------------------------------------------------------------------------------
function buttonMouseExit( )
{
gMouseIsOver = null;
if( gMouseIsDown == this )
{
var img = this.firstChild;
while( img )
{
img.src = img.src.replace( / 1/, " 0" );
img = img.nextSibling;
}
}
}
// -------------------------------------------------------------------------------------------
function buttonMouseDown( )
{
gMouseIsDown = this;
var img = this.firstChild;
while( img )
{
img.src = img.src.replace( / 0/, " 1" );
img = img.nextSibling;
}
}
// -------------------------------------------------------------------------------------------
function buttonMouseUp( )
{
gMouseIsDown = null;
if( gMouseIsOver == this )
{
var img = this.firstChild;
while( img )
{
img.src = img.src.replace( / 1/, " 0" );
img = img.nextSibling;
}
}
}
// -------------------------------------------------------------------------------------------
function calculateDockTextWidth( string )
{
var tempText = new Text( );
tempText.style.fontFamily = "'Helvetica', 'Arial'";
tempText.style.fontWeight = "bold";
tempText.style.fontSize = "12px"; // default, largest
tempText.data = string;
// set the temporary element with the size and the data and decrease until it fits
var loops = 0;
while( tempText.width > 66 && loops < 10 )
{
tempText.style.fontSize = parseInt( tempText.style.fontSize ) - 1 + "px";
loops++; // sanity check for excessively long names
}
return tempText.style.fontSize;
}
// -------------------------------------------------------------------------------------------
function ccText( data, highlight )
{
var text = new Text( );
text.data = data;
text.style.fontFamily = "Arial";
text.style.fontSize = "11px";
text.style.fontWeight = "bold";
text.style.color = highlight ? gTextHighlightColor : gTextColor;
text.highlight = highlight;
return text;
}
// -------------------------------------------------------------------------------------------
function changeLocation( )
{
locationSearchFocusRing.visible = true;
locationOkFocusFrame.visible = false;
if( warningFrame.visible )
{
warningText.visible = false;
}
forecastTabFrame.visible = false;
currentConditionsTabFrame.visible = false;
locationSelectorTabFrame.visible = true;
locationSearchTextField.editable = true;
locationSearchTextField.focus( );
if( preferences.trayState.value == "closed" )
{
windowSizeButton.onClick( );
}
}
// -------------------------------------------------------------------------------------------
function cleanLocName( locName )
{
return locName.match( /([^,\/]*).*/ )[1];
}
// -------------------------------------------------------------------------------------------
function clearCurrentLocation( )
{
// print( "clearCurrentLocation" );
locationDB.exec( "DELETE FROM locationMeta WHERE locId = '" + preferences.locId.value + "'");
locationDB.exec( "DELETE FROM location WHERE locId = '" + preferences.locId.value + "'");
var result = locationDB.query( "SELECT * FROM locationMeta" );
var row = result.getRow( );
if( row )
{
preferences.locId.value = row["locId"];
preferences.locName.value = row["locName"];
selectRecentLocation( );
}
result.dispose( );
}
// -------------------------------------------------------------------------------------------
function clearRecentLocations( )
{
locationDB.exec( "DELETE FROM locationMeta WHERE locId != '" + preferences.locId.value + "'");
locationDB.exec( "DELETE FROM location WHERE locId != '" + preferences.locId.value + "'");
}
// -------------------------------------------------------------------------------------------
function colorizeBase( color )
{
var img = base.firstChild;
while( img != "[object Frame]" )
{
img.colorize = color;
img = img.nextSibling;
}
}
// -------------------------------------------------------------------------------------------
function controlOnMouseEnter( )
{
if( gRunningAnimation )
{
for( var i = 0; i < gRunningAnimation.length; i++ )
{
if( this == gRunningAnimation[i].owner )
{
gRunningAnimation[i].kill( );
}
}
}
this.opacity = 255;
gMousePositionInControl = this;
}
// -------------------------------------------------------------------------------------------
function controlOnMouseExit( )
{
this.opacity = 128;
gMousePositionInControl = null;
}
// -------------------------------------------------------------------------------------------
function controlsBaseMouseMoved( )
{
// This only fires when outside of the controls, so we must be outside of the controls.
// gMousePositionInControl = null;
var currentMousePositionInWindow = system.event.screenX + " " + system.event.screenY;
// FIXME Workaround for onMouseMoved seemingly firing at random on certain notebooks.
if( currentMousePositionInWindow != gLastMousePositionInWindow )
{
gLastMousePositionInWindow = currentMousePositionInWindow;
controlsTimer.ticking = true;
controlsTimer.reset( );
if( !gControlsVisible )
{
fadeControls( 128, gControlList );
}
}
}
// -------------------------------------------------------------------------------------------
function controlsTimerFired( )
{
if( !gMousePositionInControl )
{
fadeControls( 0, gControlList );
}
controlsTimer.ticking = false;
}
// -------------------------------------------------------------------------------------------
function convertTime( timeIn )
{
timeIs = timeIn.split( " " )[0];
amPmIs = timeIn.split( " " )[1].toUpperCase();
timeHour = timeIs.split( ":" )[0];
timeMinute = timeIs.split( ":" )[1];
if( amPmIs == "AM" && timeHour == "12" )
{
return "00:" + timeMinute;
}
else if( amPmIs == "PM" && timeHour == "12" )
{
return "12:" + timeMinute;
}
else if( amPmIs == "PM" )
{
return ( 12 + Number( timeHour ) ) + ":" + timeMinute;
}
else
{
return timeIs;
}
}
// -------------------------------------------------------------------------------------------
function displayFoundLocationsMenu( )
{
this.src = "Resources/UI/Pop Up Large 1.png";
var point = this.convertPointToWindow( 0, 0 );
popupMenu( this.menu, point.x + 3, point.y + this.height - 4 );
this.src = "Resources/UI/Pop Up Large 0.png";
}
// -------------------------------------------------------------------------------------------
function displayRecentLocationsMenu( )
{
controlsTimer.ticking = false;
var items = [];
var item = new MenuItem( );
item.title = "Change Location...";
item.onSelect = changeLocation;
items.push( item );
var item = new MenuItem( );
item.title = "-";
items.push( item );
var item = new MenuItem( );
item.title = "Recent Locations";
item.enabled = false;
items.push( item );
var result = locationDB.query( "SELECT * FROM locationMeta" );
var row;
while( ( row = result.getRow( ) ) != null )
{
var item = new MenuItem( );
item.title = ( row["useLocDisplayName"] == 1 ) ? row["locDisplayName"] + " - " + row["locName"] : row["locName"];
item.locName = row["locName"];
item.locId = row["locId"];
item.onSelect = selectRecentLocation;
item.checked = ( preferences.locId.value == item.locId );
items.push( item );
}
result.dispose( );
var item = new MenuItem( );
item.title = "-";
items.push( item );
var item = new MenuItem( );
item.title = "Clear Current Location";
if( items.length > 5 ) // There's more than one recent location, so enable item
{
item.onSelect = clearCurrentLocation;
}
else
{
item.enabled = false;
}
items.push( item );
var item = new MenuItem( );
item.title = "Clear Recent Locations";
if( items.length > 6 ) // There's more than one recent location, so enable item
{
item.onSelect = clearRecentLocations;
}
else
{
item.enabled = false;
}
items.push( item );
var point = this.convertPointToWindow( 0, 0 );
popupMenu( items, point.x, point.y + this.height - 1 );
controlsTimer.ticking = true;
}
// -------------------------------------------------------------------------------------------
function fadeControls( toOpacity, elementList )
{
var aDuration = 250;
var fades = new Array( );
var doneFunction = fadeControlsDone;
for( var i = 0; i < elementList.length; i++ )
{
fades.push( new FadeAnimation( elementList[i], toOpacity, aDuration, animator.kEaseOut, doneFunction ) );
doneFunction = function( ){ };
}
gRunningAnimation = fades;
animator.start( fades );
}
// -------------------------------------------------------------------------------------------
function fadeControlsDone( )
{
gRunningAnimation = null;
if( this.owner.opacity == 0 )
{
gControlsVisible = false;
}
else
{
gControlsVisible = true;
}
if( gMousePositionInControl )
{
gMousePositionInControl.opacity = 255;
}
}
// -------------------------------------------------------------------------------------------
function fetchData( forceCache )
{
if( preferences.unitsPref.value == "1" )
{
var units = "C";
var unitsQuery = "ut=c&ud=k&us=k&up=m&ur=m";
}
else
{
var units = "F";
var unitsQuery = "ut=f&ud=mi&us=mph&up=in&ur=in";
}
var nowTime = Date.parse( new Date( ) );
var fetchType, fetchCache, fetchFirst;
var result = locationDB.query( "SELECT * FROM location WHERE locId = '" + preferences.locId.value + "' AND units = '" + units + "'" );
var results = result.getAll( );
// If no results (new location or units) or an hour has passed since last time stamp or timestamp is in the future because of clock changes, refresh.
if( results.length == 0 )
{
fetchType = "forecast";
fetchCache = false;
fetchFirst = true;
}
else if( nowTime >= Number( results[0]["fcTimestamp"] ) + msOneHour || Number( results[0]["fcTimestamp"] ) > nowTime )
{
fetchType = "forecast";
fetchCache = false;
}
else if( nowTime >= Number( results[0]["ccTimestamp"] ) + msFifteenMinutes )
{
fetchType = "cc";
fetchCache = false;
}
else
{
fetchCache = true;
}
if( forceCache && results.length != 0 )
{
fetchCache = true;
}
if( results.length != 0 )
{
weatherDOM = XMLDOM.parse( results[0]["storedXML"] );
}
if( fetchCache )
{
updateWeather( true );
}
else
{
var url = new URL();
url.timeout = 15;
url.feedType = fetchType;
url.fetchFirst = fetchFirst;
if( results.length == 0 )
{
url.createNewEntry = true;
}
if( fetchType == "forecast" )
{
var forecast = "&dayf=5";
}
else
{
var forecast = "";
}
url.location = "http://yahoowidget.weather.com/weather/local/" + preferences.locId.value + "?cc=*" + forecast + "&" + unitsQuery + "&prod=bd_select&par=yahoowidgetxml";
url.fetchAsync( fetchDataComplete );
gConnectionTries++;
}
result.dispose( );
}
// -------------------------------------------------------------------------------------------
function fetchDataComplete( url )
{
var urlData = url.responseData;
var newWeatherDOM;
if ( url.response != 200 || url.result == "Could not load URL")
{
if( url.fetchFirst ) // Failed on the first attempt? We need to put some kind of data in the DB to display.
{
var units = ( preferences.unitsPref.value == "1" ) ? "C" : "F";
var defaultXML = sqlEscape( defaultWeatherDOM.toXML( ) );
locationDB.exec( "INSERT INTO location ( locId, units, fcTimestamp, ccTimestamp, storedXML ) VALUES ( '" + preferences.locId.value + "', '" + units + "', '0', '0', '" + defaultXML + "' )" );
}
fetchData( true ); // Pull the most recent cached data.
// start checking every so often for a connection. But after 5 tries, give up.
if( gConnectionTries == 1 )
{
connectionTester.ticking = true;
}
else if( gConnectionTries >= 5 )
{
connectionTester.ticking = false;
}
}
else
{
setWarning( false );
gConnectionTries = 0;
connectionTester.ticking = false; // just in case
failureCount = 0;
try
{
newWeatherDOM = XMLDOM.parse( urlData );
var node = newWeatherDOM.firstChild;
if( node.nodeType == 8 ) // Don't store the large TOS comment that comes with the XML
{
node.data = ""
}
}
catch( e )
{
// parse failed
newWeatherDOM = null;
}
// Got new weather DOM
var locId = preferences.locId.value;
var units = newWeatherDOM.evaluate( "string( /weather/head/ut )" );
var timestamp = Date.parse( new Date( ) );
if( url.feedType == "forecast" )
{
var timeField = "fcTimestamp = '" + timestamp + "', ccTimestamp = '" + timestamp + "'";
}
else
{
var timeField = "ccTimestamp = '" + timestamp + "'";
}
try
{
if( url.createNewEntry )
{
locationDB.exec( "INSERT INTO location ( locId, units, fcTimestamp, ccTimestamp, storedXML ) VALUES ( '" + locId + "', '" + units + "', " + timestamp + ", " + timestamp + ", '" + sqlEscape( newWeatherDOM.toXML( ) ) + "' )" );
weatherDOM = newWeatherDOM;
}
else
{
if( url.feedType == "forecast" )
{
weatherDOM = newWeatherDOM;
}
else
{
// Pull current conditions into cached DOM.
var existingNode = weatherDOM.evaluate( "weather/cc" ).item( 0 );
var newNode = weatherDOM.importNode( newWeatherDOM.evaluate( "weather/cc" ).item( 0 ), true );
var targetNode = weatherDOM.evaluate( "weather" ).item( 0 );
targetNode.replaceChild( newNode, existingNode );
// Pull location node into cached DOM (updates current local time, sunr and suns).
var existingNode = weatherDOM.evaluate( "weather/loc" ).item( 0 );
var newNode = weatherDOM.importNode( newWeatherDOM.evaluate( "weather/loc" ).item( 0 ), true );
var targetNode = weatherDOM.evaluate( "weather" ).item( 0 );
targetNode.replaceChild( newNode, existingNode );
}
locationDB.exec( "UPDATE location SET " + timeField + ", storedXML = '" + sqlEscape( weatherDOM.toXML( ) ) + "' WHERE locId = '" + locId + "' AND units = '" + units + "'" );
}
}
catch( e )
{
log( "Error: fetchDataComplete", e, e.errCode, e.errMsg );
}
updateWeather( );
}
}
// -------------------------------------------------------------------------------------------
function forecastFrame( day, hi, low, icon, forceSun )
{
var obj = new Frame( );
obj.width = 50;
obj.height = 94;
var images = getIcons( icon, "small", forceSun );
obj.appendChild( images.sun );
obj.appendChild( images.cloud );
var text = new Text( );
text.hOffset = 25;
text.vOffset = 14;
text.hAlign = "center";
text.data = day;
text.style.fontFamily = "Arial";
text.style.fontSize = "11px";
text.style.fontWeight = "bold";
text.style.color = gTextColor;
obj.appendChild( text );
var text = new Text( );
text.hOffset = 25;
text.vOffset = 37;
text.hAlign = "center";
text.data = isNaN( hi ) ? "--" : " " + hi + "\u00b0";
text.style.fontFamily = "Arial";
text.style.fontSize = "12px";
text.style.fontWeight = "bold";
text.style.color = gTextColor;
obj.appendChild( text );
var text = new Text( );
text.hOffset = 25;
text.vOffset = 87;
text.hAlign = "center";
text.data = isNaN( low ) ? "--" : " " + low + "\u00b0";
text.style.fontFamily = "Arial";
text.style.fontSize = "11px";
text.style.fontWeight = "bold";
text.style.color = gTextHighlightColor;
text.highlight = true;
obj.appendChild( text );
var tooltipValue = isNaN( icon ) ? "Unknown" : conditionList[icon];
if( preferences.showToolTips.value == "1" )
{
obj.tooltip = obj.savedTooltip = tooltipValue;
}
else
{
obj.savedTooltip = tooltipValue;
}
return obj;
}
// -------------------------------------------------------------------------------------------
function getIcons( code, size, forceSun )
{
var sun = new Image( );
var cloud = new Image( );
var baseColor, textColor, textHighlightColor, cloudVoffsetLarge, cloudVoffsetMedium;
var sunSource = null;
if( size == "small" )
{
var prefix = "Resources/Small/";
var sunOffsetH = -6;
sun.hOffset = cloud.hOffset = 25;
sun.vOffset = cloud.vOffset = 41;
sun.hAlign = cloud.hAlign = "center";
cloudVoffsetLarge = cloudVoffsetMedium = 41;
}
else
{
var prefix = "Resources/Big/";
var sunOffsetH = 0;
cloudVoffsetLarge = -26;
cloudVoffsetMedium = -16;
}
if( gIsNightTime && !forceSun )
{
var moonIconCode = weatherDOM.evaluate( "string( /weather/cc/moon/icon )" );
if( !isNaN( moonIconCode ) )
{
sunSource = prefix + "Moons/" + moonIconCode + ".png";
}
}
else
{
sunSource = prefix + "Sun.png";
}
switch( code )
{
case "-": // Unknown Weather
sun.src = prefix + "Unknown.png";
baseColor = "#5883c8";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "0": // Tornado
case "1": // Tropical Storm
case "2": // Hurricane
case "3": // Strong Thunderstorms
case "4": // Thunderstorms
cloud.src = prefix + "Thunderstorms.png";
cloud.vOffset = cloudVoffsetLarge;
baseColor = "#63758b";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "5": // Rain and Snow
case "6": // Rain and Sleet
case "7": // Freezing Rain
cloud.src = prefix + "Wintry_Mix.png";
cloud.vOffset = cloudVoffsetLarge;
baseColor = "#96a6b3";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "8": // Freezing Drizzle
cloud.src = prefix + "Freezing_Drizzle.png";
cloud.vOffset = cloudVoffsetLarge;
baseColor = "#96a6b3";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "9": // Drizzle
cloud.src = prefix + "Drizzle.png";
cloud.vOffset = cloudVoffsetLarge;
baseColor = "#5c7287";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "10": // Freezing Rain
cloud.src = prefix + "Freezing_Rain.png";
cloud.vOffset = cloudVoffsetLarge;
baseColor = "#96a6b3";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "11": // Showers
cloud.src = prefix + "Showers.png";
cloud.vOffset = cloudVoffsetLarge;
baseColor = "#6e8aa2";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "12": // Rain
case "40": // Heavy Rain
cloud.src = prefix + "Rain.png";
cloud.vOffset = cloudVoffsetLarge;
baseColor = "#5a7085";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "13": // Flurries
cloud.src = prefix + "Flurries.png";
cloud.vOffset = cloudVoffsetLarge;
baseColor = "#bccde1";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "14": // Snow Showers
cloud.src = prefix + "Snow_Showers.png";
cloud.vOffset = cloudVoffsetLarge;
baseColor = "#bccde1";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "15": // Blowing Snow
cloud.src = prefix + "Blowing_Snow.png";
baseColor = "#bccde1";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "16": // Snow
cloud.src = prefix + "Snow.png";
cloud.vOffset = cloudVoffsetLarge;
baseColor = "#bccde1";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "17": // Hail
cloud.src = prefix + "Hail.png";
cloud.vOffset = cloudVoffsetLarge;
baseColor = "#9aa8b5";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "18": // Sleet
cloud.src = prefix + "Sleet.png";
cloud.vOffset = cloudVoffsetMedium;
baseColor = "#9aa8b5";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "19": // Dust
cloud.src = prefix + "Dust.png";
baseColor = "#898077";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "20": // Fog
cloud.src = prefix + "Fog.png";
baseColor = "#5883c8";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "21": // Haze
cloud.src = prefix + "Haze.png";
baseColor = "#5883c8";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "22": // Smoke
cloud.src = prefix + "Smoke.png";
baseColor = "#7c7d7f";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "23": // Breezy
cloud.src = prefix + "Breezy.png";
baseColor = "#5883c8";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "24": // Windy
cloud.src = prefix + "Windy.png";
baseColor = "#5883c8";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "25": // Frigid
cloud.src = prefix + "Frigid.png";
baseColor = "#cfdcef";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "26": // Cloudy (no sun/moon)
cloud.src = prefix + "Cloudy.png";
baseColor = "#8698ae";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "27": // Mostly Cloudy - night
sun.src = sunSource;
cloud.src = prefix + "Mostly_Cloudy.png";
baseColor = "#738fa7";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "28": // Mostly Cloudy - day
sun.src = sunSource;
cloud.src = prefix + "Mostly_Cloudy.png";
baseColor = "#738fa7";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "29": // Partly Cloudy - night
sun.src = sunSource;
cloud.src = prefix + "Partly_Cloudy.png";
baseColor = "#88aac6";
textColor = "#ffffff";
textHighlightColor = "#265080";
sun.hOffset += sunOffsetH;
break
case "30": // Partly Cloudy - day
sun.src = sunSource;
cloud.src = prefix + "Partly_Cloudy.png";
baseColor = "#88aac6";
textColor = "#ffffff";
textHighlightColor = "#265080";
sun.hOffset += sunOffsetH;
break
case "31": // Clear - night
sun.src = sunSource;
baseColor = "#5883c8";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "32": // Sunny - day
sun.src = sunSource;
baseColor = "#5883c8";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "33": // Mostly Clear - night
sun.src = sunSource;
cloud.src = prefix + "Mostly_Clear.png";
baseColor = "#5883c8";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "34": // Mostly Sunny - day
sun.src = sunSource;
cloud.src = prefix + "Mostly_Clear.png";
baseColor = "#5883c8";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "35": // Mixed Rain and Hail
cloud.src = prefix + "Rain_and_Hail.png";
cloud.vOffset = cloudVoffsetLarge;
baseColor = "#7a8b9d";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "36": // Hot
sun.src = sunSource;
baseColor = "#c08951";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "37": // Isolated Thunderstorms
sun.src = sunSource;
cloud.src = prefix + "Isolated_Thunderstorms.png";
cloud.vOffset = cloudVoffsetMedium;
baseColor = "#748194";
textColor = "#ffffff";
textHighlightColor = "#265080";
sun.hOffset += sunOffsetH;
break
case "38": // Scattered Thunderstorms - day
sun.src = sunSource;
cloud.src = prefix + "Scattered_Thunderstorms.png";
cloud.vOffset = cloudVoffsetMedium;
baseColor = "#727f92";
textColor = "#ffffff";
textHighlightColor = "#265080";
sun.hOffset += sunOffsetH;
break
case "47": // Scattered Thunderstorms - night
sun.src = sunSource;
cloud.src = prefix + "Scattered_Thunderstorms.png";
cloud.vOffset = cloudVoffsetMedium;
baseColor = "#727f92";
textColor = "#ffffff";
textHighlightColor = "#265080";
sun.hOffset += sunOffsetH;
break
case "39": // Scattered Showers - day
sun.src = sunSource;
cloud.src = prefix + "Scattered_Showers.png";
cloud.vOffset = cloudVoffsetMedium;
baseColor = "#66849e";
textColor = "#ffffff";
textHighlightColor = "#265080";
sun.hOffset += sunOffsetH;
break
case "41": // Scattered Snow Showers - day
sun.src = sunSource;
cloud.src = prefix + "Scattered_Snow_Showers.png";
cloud.vOffset = cloudVoffsetMedium;
baseColor = "#b2c4dc";
textColor = "#ffffff";
textHighlightColor = "#265080";
sun.hOffset += sunOffsetH;
break
case "42": // Heavy Snow
case "43": // Blizzard
cloud.src = prefix + "Heavy_Snow.png";
cloud.vOffset = cloudVoffsetLarge;
baseColor = "#bccae4";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "44": // No Feed
baseColor = "#5883c8";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
case "45": // Scattered Showers - night
sun.src = sunSource;
cloud.src = prefix + "Scattered_Showers.png";
cloud.vOffset = cloudVoffsetMedium;
baseColor = "#66819e";
textColor = "#ffffff";
textHighlightColor = "#265080";
sun.hOffset += sunOffsetH;
break
case "46": // Scattered Snow Showers - night
sun.src = sunSource;
cloud.src = prefix + "Scattered_Snow_Showers.png";
cloud.vOffset = cloudVoffsetMedium;
baseColor = "#bbcadf";
textColor = "#ffffff";
textHighlightColor = "#265080";
sun.hOffset += sunOffsetH;
break
default:
baseColor = "#5883c8";
textColor = "#ffffff";
textHighlightColor = "#265080";
break
}
if( size == "small" )
{
var images = {};
images.sun = sun;
images.cloud = cloud;
return images;
}
else if( size == "big" )
{
suppressUpdates( );
sun.hOffset = 0;
if( gIsNightTime )
{
gCcBaseColor = "#000000";
gCcTextColor = "#ffffff";
gCcTextHighlightColor = "#b0ccef";
}
else
{
gCcBaseColor = baseColor;
gCcTextColor = textColor;
gCcTextHighlightColor = textHighlightColor;
}
var existingContent = currentConditionsImageFrame.firstChild;
while( existingContent != null )
{
var nextSibling = existingContent.nextSibling;
currentConditionsImageFrame.removeChild( existingContent );
existingContent = nextSibling;
}
if( preferences.backgroundType.value == "Default" )
{
setTheme( );
}
if( sun.src )
{
currentConditionsImageFrame.appendChild( sun );
}
if( cloud.src )
{
currentConditionsImageFrame.appendChild( cloud );
}
// Update vitality
if( widget.dockOpen )
{
var doc = vitalityDoc;
var dockBase = doc.getElementById( "base" );
dockBase.setAttribute( "colorize", gCcBaseColor );
var dockSun = doc.getElementById( "sun" );
if( sun.src )
{
dockSun.setAttribute( "src", sun.src );
}
else
{
dockSun.removeAttribute( "src" );
}
var dockCloud = doc.getElementById( "cloud" );
if( cloud.src )
{
dockCloud.setAttribute( "src", cloud.src );
}
else
{
dockCloud.removeAttribute( "src" );
}
var dockTemperature = doc.getElementById( "temperature" );
dockTemperature.setAttribute( "data", currentTemperatureText.data );
var dockLocName = doc.getElementById( "locName" );
// set the proper text size
dockLocName.setAttribute( "data", preferences.locMenuName.value );
var finalStyle = "font-family: 'Helvetica', 'Arial'; font-weight: bold; color: white; -kon-shadow: 1px 1px #000; font-size: " + calculateDockTextWidth( preferences.locMenuName.value );
dockLocName.setAttribute( "style", finalStyle );
widget.setDockItem( doc, "fade" );
}
resumeUpdates( );
}
}
// -------------------------------------------------------------------------------------------
function getLumi( theHex )
{
var theRed = parseInt( theHex.substr( 1, 2 ), 16 );
var theGreen = parseInt( theHex.substr( 3, 2 ), 16 );
var theBlue = parseInt( theHex.substr( 5, 2 ), 16 );
return Math.round( ( ( theRed * .3 ) + ( theGreen * .59 ) + ( theBlue * .11 ) ) );
}
// -------------------------------------------------------------------------------------------
function initWidgetDB( )
{
locationDB = new SQLite( );
locationDB.open( system.widgetDataFolder + "/location data.db" );
var defaultXML = sqlEscape( defaultWeatherDOM.toXML( ) );
try
{
locationDB.exec( "CREATE TABLE location ( locId TEXT, units TEXT, fcTimestamp TEXT, ccTimestamp TEXT, storedXML TEXT )" );
locationDB.exec( "CREATE TABLE locationMeta ( locId TEXT, locName TEXT, locDisplayName TEXT, useLocDisplayName INTEGER )" );
if( preferences.recentLocationsPreference.value == "3.0" )
{
locationDB.exec( "INSERT INTO locationMeta ( locId, locName, locDisplayName ) VALUES ( '94089', 'Sunnyvale, CA', 'Sunnyvale' )" );
locationDB.exec( "INSERT INTO location ( locId, units, fcTimestamp, ccTimestamp, storedXML ) VALUES ( '94089', 'F', 0, 0, '" + defaultXML + "' )" );
}
}
catch( e )
{
if( e.errCode != "1" ) // An error other than the table already existing?
{
Alert( "Error: Unable to create weather database." );
}
}
// Check to see if preferences from previous weather version is present
if( preferences.prefsVersion.value != "3.0" )
{
if( preferences.recentLocationsPreference.value != "3.0" )
{
var units = ( preferences.unitsPref.value == "1" ) ? "C" : "F";
var previousLocations = eval( "[" + preferences.recentLocationsPreference.value + "]" );
mainWindow.hOffset -= 7;
mainWindow.vOffset -= 17;
preferences.backgroundType.value = "Default";
preferences.locId.value = preferences.cityValuePref.value;
preferences.locName.value = preferences.userDisplayPref.value;
for( var i = 0; i < previousLocations.length; i++ )
{
var locId = sqlEscape( previousLocations[i].locationCode );
var locName = sqlEscape( previousLocations[i].cityName );
var locDisplayName = previousLocations[i].displayName;
var useLocDisplayName = 0;
if( locDisplayName == "" )
{
locDisplayName = sqlEscape( cleanLocName( locName ) );
}
else
{
locDisplayName = sqlEscape( locDisplayName );
useLocDisplayName = 1;
}
locationDB.exec( "INSERT INTO locationMeta ( locId, locName, locDisplayName, useLocDisplayName ) VALUES ( '" + locId + "', '" + locName + "', '" + locDisplayName + "', " + useLocDisplayName + " )" );
locationDB.exec( "INSERT INTO location ( locId, units, fcTimestamp, ccTimestamp, storedXML ) VALUES ( '" + locId + "', '" + units + "', '0', '0', '" + defaultXML + "' )" );
}
}
preferences.prefsVersion.value = "3.0";
}
}
// -------------------------------------------------------------------------------------------
function initWidgetState( )
{
setPopupText( locationSelectionFrame, "" );
// ---------
windowSizeButton.onClick = function( )
{
var finalState, finalImage;
var sizer = new CustomAnimation( 1, animateBase );
sizer.duration = 250;
sizer.startWidth = base.width - 18;
sizer.startHeight = base.height - 18;
if (system.platform == "windows")
{
sizer.screenHeight = screen.availHeight + screen.availTop;
}
else
{
sizer.screenHeight = screen.height;
}
if( mainWindow.vOffset > gLastPosition + 5 || mainWindow.vOffset < gLastPosition - 5 )
{
gLastPosition = gCollapsedPosition = gBumpStamp = null;
}
if( sizer.startHeight == 56 )
{
sizer.endWidth = 252;
sizer.endHeight = 197;
sizer.endAlertOpacity = 0;
sizer.finalState = "open";
finalImage = "Resources/UI/Disclosure Arrow Up.png";
windowSizeButton.tooltip = "Click for Collapsed View";
}
else
{
sizer.endWidth = 192;
sizer.endHeight = 56;
sizer.endAlertOpacity = 255;
sizer.finalState = "closed";
finalImage = "Resources/UI/Disclosure Arrow Down.png";
windowSizeButton.tooltip = "Click for Expanded View";
}
windowSizeButton.src = finalImage;
animator.runUntilDone( sizer );
preferences.trayState.value = sizer.finalState;
// Made the window small while the location selector was open? Close it.
if( sizer.finalState == "closed" && locationSelectorTabFrame.visible )
{
locationCancelButtonFrame.onClick( );
}
}
windowSizeButton.onMouseEnter = controlOnMouseEnter;
windowSizeButton.onMouseExit = controlOnMouseExit;
// ---------
unitsToggleButton.onMouseEnter = controlOnMouseEnter;
unitsToggleButton.onMouseExit = controlOnMouseExit;
unitsToggleButton.onClick = function( fromPrefs )
{
print( preferences.unitsPref.value, fromPrefs );
if( fromPrefs == "0" || ( preferences.unitsPref.value == "1" && fromPrefs != "1" ) )
{
// Switch to Fahrenheit
currentUnitsText.data = "F"
preferences.unitsPref.value = "0";
}
else
{
// Switch to Celsius
currentUnitsText.data = "C"
preferences.unitsPref.value = "1";
}
fetchData( );
}
// ---------
locationSelectionButtonFrame.onMouseEnter = controlOnMouseEnter;
locationSelectionButtonFrame.onMouseExit = controlOnMouseExit;
// ---------
forecastOffsiteLinkButtonFrame.onMouseEnter = controlOnMouseEnter;
forecastOffsiteLinkButtonFrame.onMouseExit = controlOnMouseExit;
setPopupText( forecastOffsiteLinkFrame, "Extended Forecast from weather.com" );
// ---------
currentOffsiteLinkButtonFrame.onMouseEnter = controlOnMouseEnter;
currentOffsiteLinkButtonFrame.onMouseExit = controlOnMouseExit;
setPopupText( currentOffsiteLinkFrame, "Extended Forecast from weather.com" );
// ---------
setButtonText( locationCancelFrame, "Cancel", 80 );
setButtonText( locationOkFrame, "OK", 80 );
if( system.platform == "windows" )
{
var button1 = locationOkFrame;
var button2 = locationCancelFrame;
}
else
{
var button1 = locationCancelFrame;
var button2 = locationOkFrame;
}
button2.hOffset = button2.parentNode.width - 6;
button1.hOffset = button2.hOffset - button2.width - 6;
// ---------
forecastTabButtonInactive.onClick = function( )
{
forecastTabFrame.visible = true;
currentConditionsTabFrame.visible = false;
preferences.selectedTab.value = "forecast";
forecastOffsiteLinkButtonFrame.onClick = function( )
{
var url = new URL( );
url.location = "http://geo.yahoo.com/t;_ylc=X1MDMjAyMjIwOTY5MwRfcgMy";
url.fetchAsync( nullFunction );
var addr = "http://www.weather.com/outlook/health/general/extended/";
addr += preferences.locId.value;
addr += "?cm_ven=Yahoo&promo=extendedforecast&site=www.yahoo.com&cm_ite=CityPage&par=yahoo&cm_pla=forecastpage&cm_cat=www.yahoo.com";
openURL( addr );
}
if( warningFrame.visible )
{
warningText.visible = true;
}
}
// ---------
currentConditionsTabButtonInactive.onClick = function( )
{
forecastTabFrame.visible = false;
currentConditionsTabFrame.visible = true;
preferences.selectedTab.value = "current";
currentOffsiteLinkButtonFrame.onClick = function( )
{
var url = new URL( );
url.location = "http://geo.yahoo.com/t;_ylc=X1MDMjAyMjIwOTY5MwRfcgMy";
url.fetchAsync( nullFunction );
var addr = "http://www.weather.com/outlook/health/general/tenday/";
addr += preferences.locId.value;
addr += "?from=_topnav_health";
openURL( addr );
}
if( warningFrame.visible )
{
warningText.visible = true;
}
}
// ---------
// Location tab
// ---------
locationCancelButtonFrame.onClick = function( )
{
locationSelectorTabFrame.visible = false;
locationSearchTextField.data = "";
locationSearchTextField.editable = false;
locationPopupFrame.lastChild.data = "";
locationPopup.menu = null;
locationPopup.onMouseDown = null;
locationPopup.locId = null;
locationPopup.locName = null;
locationOkButtonFrame.onClick = null;
if( warningFrame.visible )
{
warningText.visible = true;
}
if( preferences.selectedTab.value == "current" )
{
currentConditionsTabFrame.visible = true;
}
else
{
forecastTabFrame.visible = true;
}
}
locationSearchMagButton.onMouseEnter = function( )
{
gMouseIsOver = this;
if( gMouseIsDown == this )
{
this.src = "Resources/UI/Search Field/Mag Cap 1.png";
}
}
locationSearchMagButton.onMouseExit = function( )
{
gMouseIsOver = null;
if( gMouseIsDown == this )
{
this.src = "Resources/UI/Search Field/Mag Cap 0.png";
}
}
locationSearchMagButton.onMouseDown = function( )
{
gMouseIsDown = this;
this.src = "Resources/UI/Search Field/Mag Cap 1.png";
}
locationSearchMagButton.onMouseUp = function( )
{
gMouseIsDown = null;
if( gMouseIsOver == this )
{
this.src = "Resources/UI/Search Field/Mag Cap 0.png";
}
}
// ---------
locationCancelButtonFrame.onMouseEnter = buttonMouseEnter;
locationCancelButtonFrame.onMouseExit = buttonMouseExit;
locationCancelButtonFrame.onMouseDown = buttonMouseDown;
locationCancelButtonFrame.onMouseUp = buttonMouseUp;
// ---------
locationSearchTextField.onKeyPress = function( )
{
if( system.event.keyString == "Return" || system.event.keyString == "Enter" || system.event.keyString == "Tab" )
{
this.rejectKeyPress( );
if( system.event.keyString == "Return" || system.event.keyString == "Enter" )
{
if( locationSearchFocusRing.visible )
{
locationSearch( );
}
else
{
locationOkButtonFrame.onClick( );
}
}
}
else if( !locationSearchFocusRing.visible )
{
locationSearchFocusRing.visible = true;
locationOkFocusFrame.visible = false;
}
}
// ---------
locationSearchMagButton.onClick = locationSearch;
locationSearchMagButton.savedTooltip = "Click to find matching cities";
if( preferences.showToolTips.value == "1" )
{
locationSearchMagButton.tooltip = locationSearchMagButton.savedTooltip;
}
// ---------
mainWindow.onGainFocus = function( )
{
if( gMousePositionInControl )
{
gMousePositionInControl.opacity = 255;
}
controlsTimer.ticking = true;
}
mainWindow.onLoseFocus = function( )
{
fadeControls( 0, gControlList );
controlsTimer.ticking = false;
}
// ---------
windowSizeButton.savedTooltip = "Click for Expanded View";
if( preferences.showToolTips.value == "1" )
{
windowSizeButton.tooltip = windowSizeButton.savedTooltip;
}
// Restore previously saved state
setThemeBackground( );
colorizeBase( "#4d749f" );
setBaseSize( 192, 56 );
if( preferences.trayState.value == "open" )
{
windowSizeButton.onClick( );
}
if( preferences.selectedTab.value == "current" )
{
currentConditionsTabButtonInactive.onClick( );
}
else
{
forecastTabButtonInactive.onClick( );
}
if( preferences.useLocDisplayName.value == 1 )
{
var locName = preferences.locDisplayName.value;
}
else
{
var locName = preferences.locName.value;
}
if( preferences.unitsPref.value == "1" )
{
currentUnitsText.data = "C";
}
mainWindow.visible = true;
selectRecentLocation( );
}
// -------------------------------------------------------------------------------------------
function isNightTime( )
{
try
{
var riseData = convertTime( weatherDOM.evaluate( "string( /weather/loc/sunr )" ) );
var setData = convertTime( weatherDOM.evaluate( "string( /weather/loc/suns )" ) );
var curData = convertTime( weatherDOM.evaluate( "string( /weather/loc/tm )" ) );
var curTime = new Date( );
curTime.setHours( curData.split( ":" )[0] );
curTime.setMinutes( curData.split( ":" )[1] );
var riseTime = new Date( );
riseTime.setHours( riseData.split( ":" )[0] );
riseTime.setMinutes( riseData.split( ":" )[1] );
var setTime = new Date( );
setTime.setHours( setData.split( ":" )[0] );
setTime.setMinutes( setData.split( ":" )[1] );
if( Number( setData.split( ":" )[0] ) < Number( riseData.split( ":" )[0] ) )
{
if( curData.split( ":" )[0] < setData.split( ":" )[0] )
{
riseTime.setTime( riseTime.getTime( ) - ( 1000 * 60 * 60 * 24 ) );
}
else
{
setTime.setTime( setTime.getTime( ) + ( 1000 * 60 * 60 * 24 ) );
}
}
var theSet = setTime.getTime( );
var theRise = riseTime.getTime( );
var theCur = curTime.getTime( );
if( theCur > theRise && theCur < theSet )
{
return false;
}
else
{
return true;
}
}
catch( e )
{
var dateNow = new Date( );
var hourStamp = dateNow.getHours( );
if( hourStamp > 6 && hourStamp < 20 )
{
return false;
}
else
{
return true;
}
}
}
// -------------------------------------------------------------------------------------------
function locationSearch( )
{
locationSearchLabel.visible = true;
var url = new URL();
url.timeout = 15;
url.location = "http://yahoowidget.weather.com/weather/search/search?where=" + escape( locationSearchTextField.data );
url.fetchAsync( locationSearchComplete );
}
// -------------------------------------------------------------------------------------------
function locationSearchComplete( url )
{
if( locationSelectorTabFrame.visible )
{
if ( url.response != 200 || url.result == "Could not load URL")
{
locationSearchLabel.visible = false;
locationPopupFrame.lastChild.data = "No Locations Found";
}
else
{
gConnectionTries = 0;
connectionTester.ticking = false; // just in case
failureCount = 0;
try
{
var resultDOM = XMLDOM.parse( url.responseData );
}
catch( e )
{
// parse failed
var resultDOM = null;
}
var locNodes = resultDOM.evaluate( "/search/loc" );
var items = [];
if( locNodes.length == 0 )
{
var item = new MenuItem( );
item.title = locationPopupFrame.lastChild.data = "No Locations Found";
item.enabled = false;
items.push( item );
locationPopup.locId = null;
locationPopup.locName = null;
locationPopup.lastChecked = null;
locationPopup.onMouseDown = null;
locationOkButtonFrame.onMouseDown = null;
locationOkButtonFrame.onMouseEnter = null;
locationOkButtonFrame.onMouseExit = null;
locationOkButtonFrame.onMouseDown = null;
locationOkButtonFrame.onMouseUp = null;
locationSearchFocusRing.visible = true;
locationOkFocusFrame.visible = false;
}
else
{
for( var i = 0; i < locNodes.length; i++ )
{
var item = new MenuItem( );
item.title = locNodes.item( i ).firstChild.data;
item.locId = locNodes.item( i ).evaluate( "string( @id )" );
if( i == 0 )
{
locationPopup.locId = item.locId;
locationPopup.locName = locationPopupFrame.lastChild.data = item.title;
locationPopup.lastChecked = item;
item.checked = true;
locationOkButtonFrame.onClick = selectNewLocation;
locationOkButtonFrame.onMouseEnter = buttonMouseEnter;
locationOkButtonFrame.onMouseExit = buttonMouseExit;
locationOkButtonFrame.onMouseDown = buttonMouseDown;
locationOkButtonFrame.onMouseUp = buttonMouseUp;
locationSearchFocusRing.visible = false;
locationOkFocusFrame.visible = true;
}
item.onSelect = selectFoundLocation;
items.push( item );
}
locationPopup.menu = items;
locationPopup.onMouseDown = displayFoundLocationsMenu;
}
locationSearchLabel.visible = false;
}
}
}
// -------------------------------------------------------------------------------------------
function nullFunction( )
{
// Async connection bit bucket.
}
// -------------------------------------------------------------------------------------------
function prefChanged( pref )
{
if( preferences[pref].value != gOldPrefs[pref] )
{
return true;
}
}
// -------------------------------------------------------------------------------------------
function selectFoundLocation( )
{
this.checked = true;
locationPopup.locId = this.locId;
locationPopup.locName = locationPopupFrame.lastChild.data = this.title;
if( locationPopup.lastChecked )
{
locationPopup.lastChecked.checked = false;
locationPopup.lastChecked = this;
}
}
// -------------------------------------------------------------------------------------------
function selectNewLocation( )
{
var result = locationDB.query( "SELECT * FROM locationMeta WHERE locId = '" + locationPopup.locId + "'" );
var results = result.getAll( );
// New location? Create the default meta entry.
if( results.length == 0 )
{
locationDB.exec( "INSERT INTO locationMeta ( locId, locName, useLocDisplayName ) VALUES ( '" + locationPopup.locId + "', '" + sqlEscape( locationPopup.locName ) + "', 0 )");
}
result.dispose( );
preferences.locId.value = locationPopup.locId;
preferences.locName.value = locationPopup.locName;
locationCancelButtonFrame.onClick( );
selectRecentLocation( );
}
// -------------------------------------------------------------------------------------------
function selectRecentLocation( forceCache )
{
var locId, locName;
if( this == "[object MenuItem]" )
{
locId = preferences.locId.value = this.locId;
locName = preferences.locName.value = this.locName;
}
else
{
locId = preferences.locId.value;
locName = preferences.locName.value;
}
var result = locationDB.query( "SELECT * FROM locationMeta WHERE locId = '" + locId + "'" );
var results = result.getAll( );
preferences.useLocDisplayName.value = results[0]["useLocDisplayName"];
preferences.locDisplayName.value = results[0]["locDisplayName"];
if( preferences.useLocDisplayName.value == 1 )
{
locName = results[0]["locDisplayName"];
}
else
{
locName = cleanLocName( locName );
}
result.dispose( );
preferences.locMenuName.value = locName;
fetchData( forceCache );
weatherFetchTimer.reset( );
}
// -------------------------------------------------------------------------------------------
function setBaseSize( width, height )
{
base.width = width + 18;
base.height = height + 18;
currentConditionsTooltipFrame.width = width;
baseTopRight.hOffset = baseMiddleRight.hOffset = baseBottomRight.hOffset = base.width;
baseBottomLeft.vOffset = baseBottomMiddle.vOffset = baseBottomRight.vOffset = base.height;
baseTopMiddle.width = baseMiddle.width = baseBottomMiddle.width = base.width - 64;
baseMiddleLeft.height = baseMiddle.height = baseMiddleRight.height = base.height - 64;
baseHighlightFrame.width = baseHighlight.width = base.width - 16;
baseHighlightFrame.height = base.height - 20;
currentConditionsRightFrame.hOffset = base.width + 23;
}
// -------------------------------------------------------------------------------------------
function setButtonText( obj, text, width )
{
var focusFrame = obj.firstChild;
var capFocusL = focusFrame.firstChild;
var middleFocus = focusFrame.firstChild.nextSibling;
var capFocusR = focusFrame.lastChild;
var imageFrame = obj.firstChild.nextSibling;
var capImageL = imageFrame.firstChild;
var middleImage = imageFrame.firstChild.nextSibling;
var capImageR = imageFrame.lastChild;
var textObj = obj.lastChild;
textObj.data = text;
if( !width )
{
width = textObj.width + capImageL.width + capImageR.width;
}
middleImage.width = middleFocus.width = width - capImageL.width - capImageL.hOffset - capImageR.width;
capImageR.hOffset = capFocusR.hOffset = middleImage.hOffset + middleImage.width;
textObj.hOffset = obj.width / 2;
}
// -------------------------------------------------------------------------------------------
function setPopupText( obj, text, maxWidth )
{
var imageFrame = obj.firstChild;
var middleImage = imageFrame.firstChild.nextSibling;
var capImage = imageFrame.lastChild;
var textObj = obj.lastChild;
textObj.width = null;
textObj.data = text;
if( maxWidth && textObj.width > maxWidth )
{
textObj.width = maxWidth;
}
middleImage.width = textObj.width;
capImage.hOffset = middleImage.width + 5;
}
// -------------------------------------------------------------------------------------------
function setTheme( )
{
var shadowedTextFrames = [ forecastTabButton, currentConditionsTabButtonInactive, forecastOffsiteLinkFrame, forecastTabButtonInactive, currentConditionsTabButton, currentOffsiteLinkFrame, locationSelectionFrame, unitsToggleFrame, warningFrame ];
var largeShadowedText = [ currentTemperatureText, degreeSymbolText ];
switch( preferences.backgroundType.value )
{
case "Dark Glass":
gBaseColor = null;
gTextColor = "#ffffff";
gTextHighlightColor = "#b0ccef";
break;
case "Colorized":
case "None":
if( gIsNightTime )
{
gBaseColor = preferences.nightColor.value;
gTextColor = preferences.nightTextColor.value;
gTextHighlightColor = preferences.nightTextColor.value;
}
else
{
gBaseColor = preferences.dayColor.value;
gTextColor = preferences.dayTextColor.value;
gTextHighlightColor = preferences.dayTextColor.value;
}
break;
default:
gBaseColor = gCcBaseColor;
gTextColor = gCcTextColor;
gTextHighlightColor = gCcTextHighlightColor;
break;
}
suppressUpdates( );
colorizeBase( gBaseColor );
// Deal with shadowed text (all are in primary color)
if( preferences.hideTextShadow.value == "1" )
{
gShadowColor = null;
gSmallShadowOffset = null;
gLargeShadowOffset = null;
}
else
{
if( getLumi( gTextColor ) > 128 )
{
gShadowColor = "#000000";
gLargeShadowColor = "rgba( 0, 0, 0, 0.5)";
gSmallShadowOffset = "0px 1px";
gLargeShadowOffset = "2px 2px";
}
else
{
gShadowColor = "#ffffff";
gLargeShadowColor = "rgba( 255, 255, 255, 0.5)";
gSmallShadowOffset = "0px -1px";
gLargeShadowOffset = "-2px -2px";
}
}
for( var i = 0; i < shadowedTextFrames.length; i++ )
{
var text = shadowedTextFrames[i].lastChild;
text.style.color = gTextColor;
text.style.KonShadowColor = gShadowColor;
text.style.KonShadowOffset = gSmallShadowOffset;
}
for( var i = 0; i < largeShadowedText.length; i++ )
{
var text = largeShadowedText[i];
text.style.color = gTextColor;
text.style.KonShadowColor = gLargeShadowColor;
text.style.KonShadowOffset = gLargeShadowOffset;
}
// Deal with forecast tab
var forecastFrame = forecastContentFrame.firstChild.firstChild;
while( forecastFrame != null )
{
var obj = forecastFrame.firstChild;
while( obj != null )
{
if( obj == "[object Text]" )
{
obj.style.color = obj.highlight ? gTextHighlightColor : gTextColor;
}
obj = obj.nextSibling;
}
forecastFrame = forecastFrame.nextSibling;
}
// Deal with current conditions tab
var obj = currentConditionsContentFrame.firstChild.firstChild;
while( obj != null )
{
if( obj == "[object Text]" )
{
obj.style.color = obj.highlight ? gTextHighlightColor : gTextColor;
}
obj = obj.nextSibling;
}
// Deal with location picker
var obj = locationSelectorTabFrame.firstChild;
while( obj != null )
{
if( obj == "[object Text]" )
{
obj.style.color = obj.highlight ? gTextHighlightColor : gTextColor;
}
obj = obj.nextSibling;
}
resumeUpdates( );
}
// -------------------------------------------------------------------------------------------
function setThemeBackground( )
{
if( preferences.backgroundType.value != "Dark Glass" )
{
var themePath = "Resources/Base/Colorized/";
}
else
{
var themePath = "Resources/Base/Dark Glass/";
}
baseTopLeft.src = themePath + "Top Left.png";
baseTopMiddle.src = themePath + "Top Middle.png";
baseTopRight.src = themePath + "Top Right.png";
baseMiddleLeft.src = themePath + "Middle Left.png";
baseMiddle.src = themePath + "Middle.png";
baseMiddleRight.src = themePath + "Middle Right.png";
baseBottomLeft.src = themePath + "Bottom Left.png";
baseBottomMiddle.src = themePath + "Bottom Middle.png";
baseBottomRight.src = themePath + "Bottom Right.png";
var img = base.firstChild;
while( img != "[object Frame]" )
{
img.visible = ( preferences.backgroundType.value != "None" );
img.colorize = null;
img = img.nextSibling;
}
baseHighlight.visible = ( preferences.backgroundType.value != "None" && preferences.backgroundType.value != "Dark Glass" );
}
// -------------------------------------------------------------------------------------------
function setWarning( visible, message )
{
warningText.visible = visible;
warningFrame.visible = visible;
temperatureWarningIcon.visible = visible;
if( visible && message )
{
message = message.split( "|" );
warningText.data = message[0];
if( message.length > 1 )
{
var ttMessage = message[1];
}
else
{
var ttMessage = message[0];
}
temperatureWarningIcon.tooltip = footerWarningIcon.tooltip = ttMessage;
}
else
{
temperatureWarningIcon.tooltip = footerWarningIcon.tooltip = null;
}
forecastOffsiteLinkFrame.visible = !visible;
currentOffsiteLinkFrame.visible = !visible;
}
// -------------------------------------------------------------------------------------------
function sqlEscape( a )
{
if( a )
{
return a.replace( /'/g, "''" );
}
else
{
return "";
}
}
// -------------------------------------------------------------------------------------------
function updateWeather( fromCache )
{
suppressUpdates( );
if( fromCache )
{
var units = ( preferences.unitsPref.value == "1" ) ? "C" : "F";
var lsupText = "";
var result = locationDB.query( "SELECT storedXML, ccTimestamp, fcTimestamp FROM location WHERE locId = '" + preferences.locId.value + "' AND units = '" + units + "'" );
var results = result.getRow( );
if( !results )
{
result.dispose( );
fetchData( true );
updateWeather( );
return;
}
else
{
// Pulled weatherDOM from cache
weatherDOM = XMLDOM.parse( results["storedXML"] );
// Check for expired data
var nowTime = Date.parse( new Date( ) );
var updateCache = false;
if( nowTime >= Number( results["ccTimestamp"] ) + temperatureExpiration && weatherDOM.evaluate( "string( /weather/cc/nodata )" ) != "true" )
{
// Temperature expired
lsupText = weatherDOM.evaluate( "string( /weather/cc/lsup )" );
var existingNode = weatherDOM.evaluate( "weather/cc" ).item( 0 );
var newNode = weatherDOM.importNode( defaultWeatherDOM.evaluate( "weather/cc" ).item( 0 ), true );
// Keep the last update time, but flush sunrise/set
newNode.firstChild.firstChild.data = lsupText;
var sunriseNode = weatherDOM.evaluate( "/weather/loc/sunr" );
sunriseNode.item( 0 ).firstChild.data = "N/A";
var sunsetNode = weatherDOM.evaluate( "/weather/loc/suns" );
sunsetNode.item( 0 ).firstChild.data = "N/A";
var targetNode = weatherDOM.evaluate( "weather" ).item( 0 );
targetNode.replaceChild( newNode, existingNode );
updateCache = true;
}
if( nowTime >= Number( results["fcTimestamp"] ) + forecastExpiration && weatherDOM.evaluate( "string( /weather/dayf/nodata )" ) != "true" )
{
// Forecast expired
lsupText = weatherDOM.evaluate( "string( /weather/dayf/lsup )" );
// Remove forecast data
var existingNode = weatherDOM.evaluate( "weather/dayf" ).item( 0 );
var newNode = weatherDOM.importNode( defaultWeatherDOM.evaluate( "weather/dayf" ).item( 0 ), true );
// Keep the last update time
newNode.firstChild.firstChild.data = lsupText;
var targetNode = weatherDOM.evaluate( "weather" ).item( 0 );
targetNode.replaceChild( newNode, existingNode );
updateCache = true;
}
if( updateCache )
{
locationDB.exec( "UPDATE location SET storedXML = '" + sqlEscape( weatherDOM.toXML( ) ) + "' WHERE locId = '" + preferences.locId.value + "' AND units = '" + units + "'" );
}
result.dispose( );
}
}
var ccNode = weatherDOM.evaluate( "/weather/cc" ).item( 0 );
var dayfNode = weatherDOM.evaluate( "/weather/dayf/day" );
var fetchedTemp = ccNode.evaluate( "string( tmp )" );
var fetchedConditions = ccNode.evaluate( "string( icon )" );
var fetchedLastUpdate = ccNode.evaluate( "string( lsup )" );
var ccNoData = weatherDOM.evaluate( "string( /weather/cc/nodata )" );
var dayfNoData = weatherDOM.evaluate( "string( /weather/dayf/nodata )" );
if( lsupText == "" )
{
lsupText = fetchedLastUpdate;
}
gIsNightTime = isNightTime( );
if( gIsNightTime != gLastIsNightTime )
{
// Re-theme for day/night change
setTheme( );
gLastIsNightTime = gIsNightTime;
}
currentTemperatureText.data = ( fetchedTemp == "N/A" ? "--" : fetchedTemp );
temperatureWarningIcon.hOffset = currentTemperatureText.hOffset - currentTemperatureText.width;
getIcons( fetchedConditions, "big" )
// Render Current Conditions panel
if( ccNode )
{
var ccFrame = new Frame( );
ccFrame.width = 252;
var tText = ccNode.evaluate( "string( t )" );
var text = new ccText( ( tText == "N/A" ? "--" : tText ) );
text.hAlign = "center";
text.hOffset = ccFrame.width / 2;
text.vOffset = 15;
ccFrame.appendChild( text );
// ----
var text = new ccText( "Humidity", true );
text.hOffset = 9;
text.vOffset = 36;
ccFrame.appendChild( text );
var hmidText = ccNode.evaluate( "string( hmid )" );
var text = new ccText( ( hmidText == "N/A" ? "--" : hmidText + "%" ) );
text.hAlign = "right";
text.hOffset = 116;
text.vOffset = 36;
ccFrame.appendChild( text );
var text = new ccText( "Pressure", true );
text.hOffset = 9;
text.vOffset = 55;
ccFrame.appendChild( text );
var barText = ccNode.evaluate( "string( bar/r )" );
var text = new ccText( ( barText == "N/A" ? "--" : barText + ( preferences.unitsPref.value == "1" ? ' mb' : '"' ) ) );
text.hAlign = "right";
text.hOffset = 116;
text.vOffset = 55;
ccFrame.appendChild( text );
var text = new ccText( "Wind Chill", true );
text.hOffset = 9;
text.vOffset = 74;
ccFrame.appendChild( text );
var flikText = ccNode.evaluate( "string( flik )" );
var text = new ccText( ( flikText == "N/A" ? "--" : flikText + "\u00b0" + weatherDOM.evaluate( "string( /weather/head/ut )" ) ) );
text.hAlign = "right";
text.hOffset = 116;
text.vOffset = 74;
ccFrame.appendChild( text );
// ----
var text = new ccText( "Wind", true );
text.hOffset = 133;
text.vOffset = 36;
ccFrame.appendChild( text );
var windSpeed = ccNode.evaluate( "string( wind/s )" );
if( isNaN( windSpeed ) )
{
var text = new ccText( "--" );
}
else
{
var text = new ccText( ccNode.evaluate( "string( wind/t )" ) + " " + ccNode.evaluate( "string( wind/s )" ) + " " + weatherDOM.evaluate( "string( /weather/head/us )" ) );
}
text.hAlign = "right";
text.hOffset = 242;
text.vOffset = 36;
ccFrame.appendChild( text );
var text = new ccText( "Sunrise", true );
text.hOffset = 133;
text.vOffset = 55;
ccFrame.appendChild( text );
var sunrText = weatherDOM.evaluate( "string( /weather/loc/sunr )" );
var text = new ccText( ( sunrText == "N/A" ? "--" : sunrText.toLowerCase( ) ) );
text.hAlign = "right";
text.hOffset = 242;
text.vOffset = 55;
ccFrame.appendChild( text );
var text = new ccText( "Sunset", true );
text.hOffset = 133;
text.vOffset = 74;
ccFrame.appendChild( text );
var sunsText = weatherDOM.evaluate( "string( /weather/loc/suns )" );
var text = new ccText( ( sunsText == "N/A" ? "--" : sunsText.toLowerCase( ) ) );
text.hAlign = "right";
text.hOffset = 242;
text.vOffset = 74;
ccFrame.appendChild( text );
// ----
var text = new ccText( "Last Update: " + fetchedLastUpdate.replace( / AM /, " am " ).replace( / PM /, " pm " ) );
text.hAlign = "center";
text.hOffset = ccFrame.width / 2;
text.vOffset = 90;
text.style.fontSize = "10px";
text.style.opacity = 0.75;
ccFrame.appendChild( text );
currentConditionsContentFrame.removeChild( currentConditionsContentFrame.firstChild );
currentConditionsContentFrame.appendChild( ccFrame );
}
// Render 5-day Forecast panel
var fiveDayForecastFrame = new Frame( );
fiveDayForecastFrame.width = 252;
if( dayfNoData == "true" )
{
for( var i = 0; i < 5; i++ )
{
var node = dayfNode.item( i );
var day = dayList[node.evaluate( "string( @t )" )];
var hi = node.evaluate( "string( hi )" );
var low = node.evaluate( "string( low )" );
var icon = node.evaluate( "string( part/icon )" );
var newForecastFrame = forecastFrame( day, hi, low, icon, true );
if( fiveDayForecastFrame.lastChild != null )
{
var lastForecastFrame = fiveDayForecastFrame.lastChild;
newForecastFrame.hOffset = lastForecastFrame.hOffset + lastForecastFrame.width;
}
fiveDayForecastFrame.appendChild( newForecastFrame );
}
currentConditionsTooltipFrame.tooltip = null;
var text = new ccText( "No data available" );
text.hAlign = "center";
text.hOffset = fiveDayForecastFrame.width / 2;
text.vOffset = 15;
fiveDayForecastFrame.appendChild( text );
}
else if( dayfNode.length != 0 )
{
var node = dayfNode.item( 0 );
var hi = node.evaluate( "string( hi )" );
var timeOfDay = ( hi == "N/A" ) ? "n" : "d";
var day = ( timeOfDay == "n" ) ? "Tonight" : "Today";
var low = node.evaluate( "string( low )" );
var icon = node.evaluate( "string( part[@p='" + timeOfDay + "']/icon )" );
var newForecastFrame = forecastFrame( day, hi, low, icon );
fiveDayForecastFrame.appendChild( newForecastFrame );
var currentConditionsTooltip = day + ": " + newForecastFrame.savedTooltip + ". ";
if( timeOfDay == "d" )
{
currentConditionsTooltip += "High " + hi + "\u00b0" + ", ";
}
currentConditionsTooltip += "Low " + low + "\u00b0";
currentConditionsTooltipFrame.savedTooltip = currentConditionsTooltip;
if( preferences.showToolTips.value == "1" )
{
currentConditionsTooltipFrame.tooltip = currentConditionsTooltip;
}
else
{
currentConditionsTooltipFrame.tooltip = null;
}
for( var i = 1; i < 5; i++ )
{
var node = dayfNode.item( i );
var day = dayList[node.evaluate( "string( @t )" )];
var hi = node.evaluate( "string( hi )" );
var low = node.evaluate( "string( low )" );
var icon = node.evaluate( "string( part/icon )" ); // This is pulling from the first forecast, for day.
var newForecastFrame = forecastFrame( day, hi, low, icon, true );
if( fiveDayForecastFrame.lastChild != null )
{
var lastForecastFrame = fiveDayForecastFrame.lastChild;
newForecastFrame.hOffset = lastForecastFrame.hOffset + lastForecastFrame.width;
}
fiveDayForecastFrame.appendChild( newForecastFrame );
}
}
else
{
var text = new ccText( "No data available" );
text.hAlign = "center";
text.hOffset = fiveDayForecastFrame.width / 2;
text.vOffset = 15;
fiveDayForecastFrame.appendChild( text );
}
forecastContentFrame.removeChild( forecastContentFrame.firstChild );
forecastContentFrame.appendChild( fiveDayForecastFrame );
setPopupText( locationSelectionFrame, preferences.locMenuName.value, maxPopupTextWidth );
if( ccNoData == "true" || dayfNoData == "true" )
{
setWarning( true, "Unable to connect|Unable to connect. Last update " + lsupText.replace( / AM /, " am " ).replace( / PM /, " pm " ) );
}
else
{
setWarning( false );
}
resumeUpdates( );
}
// -------------------------------------------------------------------------------------------
widget.onDockOpened = function( )
{
fetchData( );
}
// -------------------------------------------------------------------------------------------
widget.onPreferencesChanged = function( )
{
if( prefChanged( "backgroundType" ) )
{
setThemeBackground( );
setTheme( );
}
if( ( preferences.backgroundType.value == "Colorized" || preferences.backgroundType.value == "None" ) &&
( prefChanged( "dayColor" ) ||
prefChanged( "nightColor" ) ||
prefChanged( "dayTextColor" ) ||
prefChanged( "nightTextColor" ) )
)
{
setTheme( );
}
else if( prefChanged( "hideTextShadow" ) )
{
setTheme( );
}
if( prefChanged( "useLocDisplayName" ) || prefChanged( "locDisplayName" ) )
{
locationDB.exec( "UPDATE locationMeta SET locDisplayName = '" + sqlEscape( preferences.locDisplayName.value ) + "', useLocDisplayName = " + preferences.useLocDisplayName.value + " WHERE locId = '" + preferences.locId.value + "'" );
if( preferences.useLocDisplayName.value == 1 )
{
preferences.locMenuName.value = preferences.locDisplayName.value;
setPopupText( locationSelectionFrame, preferences.locDisplayName.value, maxPopupTextWidth );
var dockText = preferences.locDisplayName.value;
}
else
{
preferences.locMenuName.value = cleanLocName( preferences.locName.value );
setPopupText( locationSelectionFrame, preferences.locMenuName.value, maxPopupTextWidth );
var dockText = preferences.locMenuName.value;
}
var doc = vitalityDoc;
var dockLocName = doc.getElementById( "locName" );
dockLocName.setAttribute( "data", dockText );
var finalStyle = "font-family: 'Helvetica', 'Arial'; font-weight: bold; color: white; -kon-shadow: 1px 1px #000; font-size: " + calculateDockTextWidth( dockText );
dockLocName.setAttribute( "style", finalStyle );
widget.setDockItem( doc, "fade" );
}
if( prefChanged( "unitsPref" ) )
{
unitsToggleButton.onClick( preferences.unitsPref.value );
}
if( prefChanged( "showToolTips" ) )
{
var forecastFrame = forecastContentFrame.firstChild.firstChild;
if( preferences.showToolTips.value == "1" )
{
while( forecastFrame != null )
{
forecastFrame.tooltip = forecastFrame.savedTooltip;
forecastFrame = forecastFrame.nextSibling;
}
currentConditionsTooltipFrame.tooltip = currentConditionsTooltipFrame.savedTooltip;
windowSizeButton.tooltip = windowSizeButton.savedTooltip;
locationSearchMagButton.tooltip = locationSearchMagButton.savedTooltip;
}
else
{
while( forecastFrame != null )
{
forecastFrame.tooltip = null;
forecastFrame = forecastFrame.nextSibling;
}
currentConditionsTooltipFrame.tooltip = null;
windowSizeButton.tooltip = null;
locationSearchMagButton.tooltip = null;
}
}
}
// -------------------------------------------------------------------------------------------
widget.onWakeFromSleep = function( )
{
weatherFetchTimer.ticking = false;
sleepPauseTimer.ticking = true;
}
// -------------------------------------------------------------------------------------------
widget.onWillChangePreferences = function( )
{
gOldPrefs = [ ];
for( var i in preferences )
{
gOldPrefs[ i ] = preferences[ i ].value;
}
}
// -------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------
// Debugging & testing functions
// -------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------
function dumpDB( )
{
var result = locationDB.query( "SELECT * FROM location" );
var results = result.getAll( );
print( ">>> location" );
for( var i = 0; i < results.length; i++ )
{
print( i, results[i]["locId"], results[i]["units"], results[i]["fcTimestamp"], results[i]["ccTimestamp"], results[i]["storedXML"].replace(/\r|\n|\t| +/g, "") );
}
print( "<<< location" );
result.dispose( );
var result = locationDB.query( "SELECT * FROM locationMeta" );
var results = result.getAll( );
print( "***>>> locationMeta" );
for( var i = 0; i < results.length; i++ )
{
print( i, results[i]["locId"], results[i]["locName"], results[i]["locDisplayName"], results[i]["useLocDisplayName"] );
}
print( "***<<< locationMeta" );
result.dispose( );
}
// -------------------------------------------------------------------------------------------
function test( fetchedConditions, tNightTime, fetchedTemp )
{
var fields = [];
var options = [ "N/A" ];
var optionValues = [ "N/A" ];
for( var i = 0; i < conditionList.length; i++ )
{
options.push( conditionList[i] );
optionValues.push( i );
}
var field = new FormField( );
field.name = "icon";
field.type = "popup";
field.title = "Weather Condition";
field.option = options;
field.optionValue = optionValues;
field.defaultValue = fetchedConditions;
fields.push( field );
var field = new FormField( );
field.name = "nightTime";
field.type = "popup";
field.title = "Time of Day";
field.option = [ "Day", "Night" ];
field.optionValue = [ 0, 1 ];
field.defaultValue = tNightTime;
fields.push( field );
var field = new FormField( );
field.name = "temperature";
field.type = "text";
field.title = "Temperature";
field.defaultValue = fetchedTemp ? fetchedTemp : "72";
fields.push( field );
var results = form( fields, "Weather Test", "Submit" );
if( results )
{
var fetchedConditions = Number( results[0] );
var tNightTime = results[1];
var fetchedTemp = results[2];
gIsNightTime = ( tNightTime == 1 );
setTheme( );
gLastIsNightTime = gIsNightTime;
if( fetchedTemp == "N/A" || fetchedTemp == "" )
{
degreeSymbolText.visible = false;
fetchedTemp = "--";
}
else
{
degreeSymbolText.visible = true;
}
currentTemperatureText.data = fetchedTemp;
getIcons( String( fetchedConditions ), "big" );
// Render Current Conditions panel
var ccFrame = new Frame( );
ccFrame.width = 252;
if( fetchedConditions != "N/A" )
{
var conditionText = "--";
}
else
{
var conditionText = conditionList[fetchedConditions];
}
var text = new ccText( conditionText );
text.hAlign = "center";
text.hOffset = ccFrame.width / 2;
text.vOffset = 14;
ccFrame.appendChild( text );
// ----
var text = new ccText( "Humidity", true );
text.hOffset = 9;
text.vOffset = 36;
ccFrame.appendChild( text );
var text = new ccText( "20%" );
text.hOffset = 80;
text.vOffset = 36;
ccFrame.appendChild( text );
var text = new ccText( "Pressure", true );
text.hOffset = 9;
text.vOffset = 55;
ccFrame.appendChild( text );
var text = new ccText( '30"' );
text.hOffset = 80;
text.vOffset = 55;
ccFrame.appendChild( text );
var text = new ccText( "Wind Chill", true );
text.hOffset = 9;
text.vOffset = 74;
ccFrame.appendChild( text );
var text = new ccText( "50\u00b0F" );
text.hOffset = 80;
text.vOffset = 74;
ccFrame.appendChild( text );
// ----
var text = new ccText( "Wind", true );
text.hOffset = 133;
text.vOffset = 36;
ccFrame.appendChild( text );
var text = new ccText( "N 8 mph " );
text.hAlign = "right";
text.hOffset = 241;
text.vOffset = 36;
ccFrame.appendChild( text );
var text = new ccText( "Sunrise", true );
text.hOffset = 133;
text.vOffset = 55;
ccFrame.appendChild( text );
var text = new ccText( "8:00 am" );
text.hAlign = "right";
text.hOffset = 241;
text.vOffset = 55;
ccFrame.appendChild( text );
var text = new ccText( "Sunset", true );
text.hOffset = 133;
text.vOffset = 74;
ccFrame.appendChild( text );
var text = new ccText( "8:00 pm" );
text.hAlign = "right";
text.hOffset = 241;
text.vOffset = 74;
ccFrame.appendChild( text );
// ----
var text = new ccText( "Last Update: (this is static text)" );
text.hAlign = "center";
text.hOffset = ccFrame.width / 2;
text.vOffset = 90;
text.style.fontSize = "10px";
text.style.opacity = 0.75;
ccFrame.appendChild( text );
currentConditionsContentFrame.removeChild( currentConditionsContentFrame.firstChild );
currentConditionsContentFrame.appendChild( ccFrame );
// ----
var fiveDayForecastFrame = new Frame( );
var hi = fetchedTemp;
var timeOfDay = ( hi == "N/A" ) ? "n" : "d";
var day = ( timeOfDay == "n" ) ? "Tonight" : "Today";
var low = 50;
var icon = fetchedConditions;
var newForecastFrame = forecastFrame( day, hi, low, String( icon ) );
fiveDayForecastFrame.appendChild( newForecastFrame );
for( var i = 1; i < 5; i++ )
{
icon++;
if( icon == conditionList.length )
icon = 0;
var day = dayList[i];
var newForecastFrame = forecastFrame( day, 80, 40, String( icon ), true );
if( fiveDayForecastFrame.lastChild != null )
{
var lastForecastFrame = fiveDayForecastFrame.lastChild;
newForecastFrame.hOffset = lastForecastFrame.hOffset + lastForecastFrame.width;
}
fiveDayForecastFrame.appendChild( newForecastFrame );
}
forecastContentFrame.removeChild( forecastContentFrame.firstChild );
forecastContentFrame.appendChild( fiveDayForecastFrame );
test( fetchedConditions, tNightTime, fetchedTemp );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment