law%netscape.com ff74a2d61d New and improved find dialog implementation
git-svn-id: svn://10.0.0.236/trunk@39618 18797224-902f-48f8-a5cc-f745e15eee43
1999-07-16 01:54:44 +00:00

146 lines
4.6 KiB
JavaScript

var finder; // Find component.
var data; // Search context (passed as argument).
var dialog; // Quick access to document/form elements.
function string2Bool( value )
{
return value != "false";
}
function bool2String( value )
{
if ( value ) {
return "true";
} else {
return "false";
}
}
function initDialog()
{
// Create dialog object and initialize.
dialog = new Object;
dialog.findKey = document.getElementById("dialog.findKey");
dialog.caseSensitive = document.getElementById("dialog.caseSensitive");
dialog.wrap = document.getElementById("dialog.wrap");
dialog.searchBackwards = document.getElementById("dialog.searchBackwards");
dialog.find = document.getElementById("dialog.find");
dialog.cancel = document.getElementById("dialog.cancel");
dialog.enabled = false;
}
function loadDialog()
{
// Set initial dialog field contents.
dialog.findKey.setAttribute( "value", data.searchString );
if ( data.caseSensitive ) {
dialog.caseSensitive.setAttribute( "checked", "" );
} else {
dialog.caseSensitive.removeAttribute( "checked" );
}
if ( data.wrapSearch ) {
dialog.wrap.setAttribute( "checked", "" );
} else {
dialog.wrap.removeAttribute( "checked" );
}
if ( data.wearchBackwards ) {
dialog.searchBackwards.setAttribute( "checked", "" );
} else {
dialog.searchBackwards.removeAttribute( "checked" );
}
// disable the OK button if no text
if (dialog.findKey.getAttribute("value") == "") {
dialog.find.setAttribute( "disabled", "" );
}
dialog.findKey.focus();
}
function loadData()
{
// Set data attributes per user input.
data.searchString = dialog.findKey.value;
data.caseSensitive = dialog.caseSensitive.checked;
data.wrapSearch = dialog.wrap.checked;
data.searchBackwards = dialog.searchBackwards.checked;
}
function onLoad()
{
// Init dialog.
initDialog();
// Get find component.
finder = Components.classes[ "component://netscape/appshell/component/find" ].getService();
finder = finder.QueryInterface( Components.interfaces.nsIFindComponent );
if ( !finder ) {
alert( "Error accessing find component\n" );
window.close();
return;
}
// Save search context.
data = window.arguments[0];
// Tell search context about this dialog.
data.findDialog = window;
// Turn "data" into weak reference.
data.ConvertToWeakReference();
// Fill dialog.
loadDialog();
// Give focus to search text field.
dialog.findKey.focus();
}
function onUnload() {
// Turn "data" back to owning reference (since xpconnect
// will do Release() regardless).
data.ConvertToOwningReference();
// Disconnect context from this dialog.
data.findDialog = null;
}
function find()
{
// Transfer dialog contents to data elements.
loadData();
// Search.
finder.FindNext( data );
}
function cancel()
{
// Close the window.
window.close();
}
function onTyping( key )
{
if ( key == 13 && dialog.enabled ) {
find();
} else {
if ( dialog.enabled ) {
// Disable OK if they delete all the text.
if ( dialog.findKey.value == "" ) {
dialog.enabled = false;
dialog.find.setAttribute( "disabled", "" );
}
} else {
// Enable OK once the user types something.
if ( dialog.findKey.value != "" ) {
dialog.enabled = true;
dialog.find.removeAttribute( "disabled" );
}
}
}
}