PDA

View Full Version : Help: Search Widget


indiglopanda
2007.05.25, 02:46 PM
Last week I started a widget based off of the Google search widget that cobbles together a search query url and sends it to the browser. This is my first widget, so I'm probably just missing something fairly simple.

I'm running into a problem after the first search has been performed. It seems like it is having trouble reading out of the .plist file, but I'm not sure if that's the culprit or if it's something else.

html:
<html>

<head>
<style type="text/css">
@import "uodirectory.css";
</style>

<script type="text/javascript" src="uodirectory.js">
</script>
<script type='text/javascript' src='/System/Library/WidgetResources/AppleClasses/AppleInfoButton.js' charset='utf-8'/>

<script type='text/javascript' src='/System/Library/WidgetResources/AppleClasses/AppleAnimator.js' charset='utf-8'/>

<script type='text/javascript' src='/System/Library/WidgetResources/AppleClasses/AppleButton.js' charset='utf-8'/>
</head>

<body onload="setup();">
<div id="front">

<img src="Default.png" />

<form>
<input type='search' id='search' onkeydown='keydown(event,this.value);' results="10" />
</form>

<div id="infoButton"></div>

</div>

<div id="back">

<img src="backside.png" />

<span id="searchby">
<label>Search By:</label>
<select id="by">
<option value="name">Name</option>
<option value="email">Email</option>
</select>
</span>

<span id="searchfor">
<label>Search For:</label>
<select id="mode">
<option value="staff">Faculty/Staff</option>
<option value="student">Students</option>
<option value="dept">Departments</option>
</select>
</span>

<div id="doneButton"></div>

</div>


</body>
</html>


javascript: (the alerts add a ton of clutter, but are only there in an attempt to narrow down where my problems are stemming from and will be removed)
/////////////////////////////
//VARIABLES
/////////////////////////////
var gDoneButton;
var gInfoButton;
var d;
var b;
var m;

////////////////////////////
//ONLOAD SETUP
////////////////////////////
function setup(){
alert("\nfunction setup called.");

//set up info button and done button (apple classes)
gDoneButton = new AppleGlassButton(document.getElementById("doneButton"), "Done", hidePrefs);
gInfoButton = new AppleInfoButton(document.getElementById("infoButton"), document.getElementById("front"), "black", "black", showPrefs);

//read in preferences from Info.plist
if (window.widget){
b = widget.preferenceForKey("b");
m = widget.preferenceForKey("m");
alert("\nvariables loaded:\nb=" + b + "\nm=" + m);
}

//set up "search by" according to preference read from Info.plist
if (b == "name"){
document.getElementById("by").selectedIndex = 0;
alert("\nset search by to: name");
}
else if (b == "email"){
document.getElementById("by").selectedIndex = 1;
alert("\nset search by to: email");
}
else {
alert("\nsearch by error, b="+b);
}

//set up "search for" according to preference read from Info.plist
if (m == "staff"){
document.getElementById("mode").selectedIndex = 0;
d = "person";
alert("\nset search for to: staff");
}
else if (m == "student"){
document.getElementById("mode").selectedIndex = 1;
d = "person";
alert("\nset search for to: student");
}
else if (m == "dept"){
document.getElementById("mode").selectedIndex = 2;
d = "dept";
alert("\nset search for to: dept");
}
else {
alert("\nsearch for error, m="+m);
}

}

////////////////////////////
//FUNCTIONS AHOY!!!
////////////////////////////

function keydown (event, input) //when enter is pressed:
{ //call format, passing input
if (event.keyCode == 13) // enter or return
{
alert("\nenter/return detected, passing \"" +input+"\" to format...")
format(input);
}
}

function format(input) //takes input and reformat spaces into "+" signs,
{ //passes output to function search.
var search_array=input.split(" ");
var i = 0;
var output = "";
while (i < search_array.length){
if (i == 0){
output = output+search_array[i];
}
if (i > 0){
output = output+"+"+search_array[i];
}
i += 1;
}
alert("\nformat complete. passing \""+output+"\" to search...");
search(output);
}


function search (input) //Takes output from format and assembles URL from
{ //options set on the backside and the search string.
var url="http://directory.uoregon.edu/telecom/directory.jsp?m="+m+"&b="+b+"&d="+d+"&s="+input;

alert("\nOpening: "+url);

if (window.widget)
widget.openURL (url);

}

function showPrefs(){
alert("\nflip to back.");
var front = document.getElementById("front");
var back = document.getElementById("back");
var infoButton = document.getElementById("infoButton");
infoButton.style.display="none";
window.resizeTo(284,182);
if (window.widget){
widget.prepareForTransition("ToBack");
}

front.style.display="none";
back.style.display="block";

if (window.widget)
setTimeout ('widget.performTransition();', 0);

}

function hidePrefs(){
alert("\nflip to front.")
var front = document.getElementById("front");
var back = document.getElementById("back");

if (window.widget){
widget.setPreferenceForKey(document.getElementById ("by").value, "b");
widget.setPreferenceForKey(document.getElementById ("mode").value, "m");
alert("\nsetting keys:");
alert("\nb="+document.getElementById("by").value);
alert("\nm="+document.getElementById("mode").value);
}

if (window.widget)
widget.prepareForTransition("ToFront");

window.resizeTo(261,42);
back.style.display="none";
front.style.display="block";
infoButton.style.display="block";

if (window.widget)
setTimeout ('widget.performTransition();', 0);

}

plist:
<plist version="1.0">
<dict>
<key>CFBundleDisplayName</key>
<string>UO Directory</string>
<key>CFBundleIdentifier</key>
<string>com.apple.widget.uodirectory</string>
<key>CFBundleName</key>
<string>UO Directory</string>
<key>CFBundleVersion</key>
<string>0.2</string>
<key>CloseBoxInsetX</key>
<integer>5</integer>
<key>CloseBoxInsetY</key>
<integer>0</integer>
<key>MainHTML</key>
<string>uodirectory.html</string>
<key>b</key>
<string>email</string>
<key>d</key>
<string>person</string>
<key>m</key>
<string>staff</string>
</dict>
</plist>

Thanks in advance for any help. I appreciate it greatly.

Taxxodium
2007.05.25, 04:41 PM
Can you be more specific about the problem? Just saying that a problem occurs after the first search is not meaningful to anyone.

Also, you may want to do url = encode(url) on the url...