Added Find / Replace functionality

This commit is contained in:
Len Kranendonk 2003-05-09 11:41:02 +00:00
parent 249e35d9fc
commit 942ff1f80f
3 changed files with 176 additions and 2 deletions

View file

@ -42,8 +42,9 @@ this.toolbar = [
['fontsize'],
// ['fontstyle'],
// ['linebreak'],
['find','separator'],
['bold','italic','underline','separator'],
// ['strikethrough','subscript','superscript','separator'],
['strikethrough','subscript','superscript','separator'],
['justifyleft','justifycenter','justifyright','separator'],
['OrderedList','UnOrderedList','Outdent','Indent','separator'],
['forecolor','backcolor','separator'],
@ -84,7 +85,8 @@ this.fontstyles = [ // make sure these exist in the header of page the conte
this.btnList = {
// buttonName: commandID, title, onclick, image,
"smileys": ['Smileys', 'Insert Smiley', 'editor_action(this.id)', 'ed_smiley.gif'],
"find": ['Find', 'Find', 'editor_action(this.id)', 'ed_find.gif'],
"smileys": ['Smileys', 'Insert Smiley', 'editor_action(this.id)', 'ed_smiley.gif'],
"bold": ['Bold', 'Bold', 'editor_action(this.id)', 'ed_format_bold.gif'],
"italic": ['Italic', 'Italic', 'editor_action(this.id)', 'ed_format_italic.gif'],
"underline": ['Underline', 'Underline', 'editor_action(this.id)', 'ed_format_underline.gif'],
@ -320,6 +322,14 @@ function editor_action(button_id) {
if (myText) { editor_insertHTML(objname, myText); }
}
// Find function
if (cmdID == 'Find') {
setGlobalVar ("_editor_field",objname);
var findRange = editor_obj.contentWindow.document.body.createTextRange();
showModelessDialog(_editor_url + "popups/find.html",window, "resizable: no; help: no; status: no; scroll: no; ");
return;
}
// Custom1
else if (cmdID == 'custom1') {
alert("Hello, I am custom button 1!");
@ -1161,3 +1171,33 @@ function editor_cMenu(obj,menuIdx,objname) {
}
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
// insert by lvn : find + insertTable
// insert by lvn : check editor changes
/* ---------------------------------------------------------------------- *\
Function : setGlobalVar
Description : set a variable with a global scope
Usage : setGlobalVar(varName, value);
Arguments : varName - name of the global variable to set
value - value of the global variable to set
\* ---------------------------------------------------------------------- */
function setGlobalVar(varName, value) {
if (this.cache == null) {this.cache = new Object();}
this.cache[varName] = value;
}
/* ---------------------------------------------------------------------- *\
Function : getGlobalVar
Description : get a variable in a global scope
Usage : value = getGlobalVar(varName);
Arguments : varName - name of the global variable to get
value - value of the global variable to get
\* ---------------------------------------------------------------------- */
function getGlobalVar(varName, value) {
if (this.cache == null) {
return null;
} else {
return this.cache[varName];
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 900 B

View file

@ -0,0 +1,134 @@
<HTML id=dlgFind STYLE="width: 385px; height: 165px; ">
<HEAD>
<TITLE>Find & Replace</TITLE>
<style>
html, body, button, div, input, select, fieldset, td { font-family: MS Shell Dlg; font-size: 8pt;};
</style>
<script>
window.setInterval(checkInputString, 500);
opener = window.dialogArguments;
var editor_obj = opener.document.all["_" + opener.getGlobalVar('_editor_field') + "_editor"];
function checkInputString() {
// close when switched to textedit
if (opener.getGlobalVar('_editor_field') == "_editor_disabled") {
window.close();
}
}
</script>
<SCRIPT defer>
var TxtRange;
TxtRange = dialogArguments.document.selection.createRange();
function _CloseOnEsc() {
if (event.keyCode == 27) { window.close(); return; }
}
window.onerror = HandleError
function HandleError(message, url, line) {
var str = "An error has occurred in this dialog." + "\n\n"
+ "Error: " + line + "\n" + message;
alert(str);
window.close();
return true;
}
// event handlers
function Init() {document.body.onkeypress = _CloseOnEsc;}
//Match Case and Whole Words
function matchtype(){
var retval = 0;
var matchcase = 0;
var matchword = 0;
if (document.formSearch.MatchCase.checked) matchcase = 4;
if (document.formSearch.MatchWholeWord.checked) matchword = 2;
retval = matchcase + matchword;
return(retval);
}
//Checks for Find field not empty
function checkInputString(){
if (document.formSearch.stringSearch.value.length < 1) {
alert("Nothing to search.\nPlease enter some text in the field labeled Find what:");
return false;
} else {
return true;
}
}
// Finds the entered text
function FindTxt(){
if (checkInputString()) {
var searchval = document.formSearch.stringSearch.value;
TxtRange.collapse(false);
if (TxtRange.findText(searchval, 1000000000, matchtype())) {TxtRange.select();}
else {
var startAgain = confirm("Finished Searching the document. Would you like to start again from the top of the page?");
if (startAgain) {
TxtRange.expand("textedit");
TxtRange.collapse();
TxtRange.select();
FindTxt();
}
}
}
}
// replaces the first found instance of the entered textwith the desired text
function ReplaceTxt(){
if (checkInputString()) {
if (document.formSearch.MatchCase.checked){
if (TxtRange.text == document.formSearch.stringSearch.value) TxtRange.text = document.formSearch.stringReplace.value
}
else {
if (TxtRange.text.toLowerCase() == document.formSearch.stringSearch.value.toLowerCase())
TxtRange.text = document.formSearch.stringReplace.value
}
FindTxt();
}
}
// replaces all instances of the entered text with the desired text
function ReplaceAllTxt(){
if (checkInputString()) {
var searchval = document.formSearch.stringSearch.value;
var wordcount = 0;
var msg = "";
TxtRange.expand("textedit");
TxtRange.collapse();
TxtRange.select();
while (TxtRange.findText(searchval, 1000000000, matchtype())){
TxtRange.select();
TxtRange.text = document.formSearch.stringReplace.value;
wordcount++;
}
if (wordcount == 0) msg = "Word not found. Nothing replaced."
else msg = wordcount + " word(s) were replaced.";
alert(msg);
}
}
</script>
</HEAD>
<BODY id=bdy onload="Init()" style="background: threedface; color: windowtext;" scroll=no>
<FORM NAME="formSearch" method="post" action="">
<TABLE CELLSPACING="0" cellpadding="5" border="0">
<TR><TD VALIGN="top" align="left" nowrap>
<label for="stringSearch">Find what:</label><br>
<INPUT TYPE=TEXT SIZE=40 NAME=stringSearch id="stringSearch" style="width : 280px; height: 22px;"><br>
<label for="stringReplace">Replace with:</label><br>
<INPUT TYPE=TEXT SIZE=40 NAME=stringReplace id="stringReplace" style="width : 280px; height: 22px;"><br>
<INPUT TYPE=Checkbox SIZE=40 NAME=MatchWholeWord ID="MatchWholeWord"><label for="MatchWholeWord">Match whole word only</label><br>
<INPUT TYPE=Checkbox SIZE=40 NAME=MatchCase ID="MatchCase"><label for="MatchCase">Match case</label>
</td>
<td rowspan="2" valign="top">
<button name="Find" style="width:75px; height:22px; margin-top:15px" onClick="FindTxt();">Find Next</button><br>
<button name="Replace" style="width:75px; height:22px; margin-top:7px" onClick="ReplaceTxt();">Replace</button><br>
<button name="Replaceall" style="width:75px; height:22px; margin-top:7px" onClick="ReplaceAllTxt();">Replace All</button><br>
<button name="Cancel" style="width:75px; height:22px; margin-top:7px" onClick="window.close();">Close</button><br>
</td>
</tr>
</table>
</FORM>
</BODY>
</HTML>