Skip to content
Browse files

Adding search and replace feature.

- Also change dialog so that focus is released first
  then value is passed forward
  • Loading branch information...
1 parent 0ea03c5 commit 130d7a3bfe60fa30f5ecade0f1ed0f3800494ca0 julmis committed
Showing with 232 additions and 5 deletions.
  1. +4 −3 lib/editor/dialog.js
  2. +87 −2 lib/editor/htmlarea.php
  3. +141 −0 lib/editor/popups/searchandreplace.php
View
7 lib/editor/dialog.js
@@ -38,6 +38,7 @@ Dialog._geckoOpenModal = function(url, action, init) {
case "link_std": x = 420; y = 230; break;
case "insert_image_std": x = 450; y = 240; break;
case "createanchor": x = 300; y = 140; break;
+ case "searchandreplace": x = 400; y = 250; break;
default: x = 50; y = 50;
}
@@ -66,14 +67,14 @@ Dialog._geckoOpenModal = function(url, action, init) {
}
// make up a function to be called when the Dialog ends.
Dialog._return = function (val) {
- if (val && action) {
- action(val);
- }
relwin(window);
// capture other frames
if(document.all) {
for (var i = 0; i < window.frames.length; relwin(window.frames[i++]));
}
+ if (val && action) {
+ action(val);
+ }
Dialog._modal = null;
};
};
View
89 lib/editor/htmlarea.php
@@ -146,7 +146,7 @@ function HTMLArea(textarea, config) {
"forecolor", "hilitecolor", "separator",
"inserthorizontalrule", "createanchor", "createlink", "unlink", "nolink", "separator",
"insertimage", "inserttable",
- "insertsmile", "insertchar",
+ "insertsmile", "insertchar", "search_replace",
<?php if (!empty($CFG->aspellpath) && file_exists($CFG->aspellpath) && !empty($CFG->editorspelling)) {
echo '"separator","spellcheck",';
} ?>
@@ -233,7 +233,8 @@ function cut_copy_paste(e, cmd, obj) {
echo 'spellcheck: ["Spell-check", "spell-check.gif", false, spellClickHandler ],'."\n";
}?>
insertsmile: ["Insert Smiley", "em.icon.smile.gif", false, function(e) {e.execCommand("insertsmile");} ],
- insertchar: [ "Insert Char", "icon_ins_char.gif", false, function(e) {e.execCommand("insertchar");} ]
+ insertchar: [ "Insert Char", "icon_ins_char.gif", false, function(e) {e.execCommand("insertchar");} ],
+ search_replace: [ "Search and replace", "ed_replace.gif", false, function(e) {e.execCommand("searchandreplace");} ]
};
// initialize tooltips from the I18N module and generate correct image path
@@ -1453,6 +1454,9 @@ function (str, l1, l2, l3) {
// Called when the user clicks on "InsertImage" button. If an image is already
// there, it will just modify it's properties.
HTMLArea.prototype._insertImage = function(image) {
+
+ // Make sure that editor has focus
+ this.focusEditor();
var editor = this; // for nested functions
var outparam = null;
if (typeof image == "undefined") {
@@ -1597,6 +1601,8 @@ function (str, l1, l2, l3) {
/// Moodle hack - insertSmile
HTMLArea.prototype._insertSmile = function() {
+ // Make sure that editor has focus
+ this.focusEditor();
var sel = this._getSelection();
var range = this._createRange(sel);
var editor = this; // for nested functions
@@ -1679,6 +1685,83 @@ function (str, l1, l2, l3) {
};
+HTMLArea.prototype._searchReplace = function() {
+
+ var editor = this;
+ var selectedtxt = "";
+ var strReplaced = '<?php print_string('itemsreplaced','editor'); ?>';
+ var strNotfound = '<?php print_string('searchnotfound','editor');?>';
+ var ile;
+
+ //in source mode mozilla show errors, try diffrent method
+ if (editor._editMode == "wysiwyg") {
+ selectedtxt = editor.getSelectedHTML();
+ } else {
+ if (HTMLArea.is_ie) {
+ selectedtxt = document.selection.createRange().text;
+ } else {
+ selectedtxt = getMozSelection(editor._textArea);
+ }
+ }
+
+ outparam = {
+ f_search : selectedtxt
+ };
+
+ //Call Search And Replace popup window
+ editor._popupDialog( "searchandreplace.php", function( entity ) {
+ if ( !entity ) {
+ //user must have pressed Cancel
+ return false;
+ }
+ var text = editor.getHTML();
+ var search = entity[0];
+ var replace = entity[1];
+ var delim = entity[2];
+ var regularx = entity[3];
+ var closesar = entity[4];
+ ile = 0;
+ if (search.length < 1) {
+ alert ("Enter a search word! \n search for: " + entity[0]);
+ } else {
+ if (regularx) {
+ var regX = new RegExp (search, delim) ;
+ var text = text.replace ( regX,
+ function (str, n) {
+ // Increment our counter variable.
+ ile++ ;
+ //return replace ;
+ return str.replace( regX, replace) ;
+ }
+ )
+
+ } else {
+ while (text.indexOf(search)>-1) {
+ pos = text.indexOf(search);
+ text = "" + (text.substring(0, pos) + replace + text.substring((pos + search.length), text.length));
+ ile++;
+ }
+ }
+
+ editor.setHTML(text);
+ editor.forceRedraw();
+ if (ile > 0) {
+ alert(ile + ' ' + strReplaced);
+ } else {
+ alert (strNotfound + "\n");
+ }
+ }
+ }, outparam);
+
+ function getMozSelection(txtarea) {
+ var selLength = txtarea.textLength;
+ var selStart = txtarea.selectionStart;
+ var selEnd = txtarea.selectionEnd;
+ if (selEnd==1 || selEnd==2) selEnd=selLength;
+ return (txtarea.value).substring(selStart, selEnd);
+ }
+};
+
/// Moodle hack's ends
//
// Category: EVENT HANDLERS
@@ -1755,6 +1838,7 @@ function (str, l1, l2, l3) {
case "insertimage": this._insertImage(); break;
case "insertsmile": this._insertSmile(); break;
case "insertchar": this._insertChar(); break;
+ case "searchandreplace": this._searchReplace(); break;
case "about" : this._popupDialog("about.html", null, this); break;
case "showhelp" : window.open(_editor_url + "reference.html", "ha_help"); break;
@@ -2367,6 +2451,7 @@ function hex(d) {
indentation = indentation.replace(format.regex.inremove, '') ;
}
+ line = line.replace(format.regex.tagopen, '\n$&');
line = line.replace(format.regex.tagclose, '$&\n');
line = line.replace(format.regex.tagsmain, '$&\n');
formatted += indentation + line;
View
141 lib/editor/popups/searchandreplace.php
@@ -0,0 +1,141 @@
+<?php // $Id$
+ require_once('../../../config.php');
+?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html>
+<head>
+<title><?php print_string("searchandreplace","editor");?></title>
+<script language="javascript" type="text/javascript" src="popup.js"></script>
+<script language="javascript" type="text/javascript">
+<!--
+function _CloseOnEsc(ev) {
+ ev || (ev = window.event) || (ev = editor._iframe.contentWindow.event);
+ if (ev.keyCode == 27) {
+ // update_parent();
+ window.close();
+ return;
+ }
+}
+
+//Initialize
+function Init() {
+
+ __dlg_init();
+
+ document.body.onkeypress = _CloseOnEsc;
+ var param = window.dialogArguments;
+ document.getElementById("f_search").value = param["f_search"];
+
+ document.getElementById("f_search").focus();
+ document.getElementById("f_search").select();
+
+};
+
+//Actions
+function onReplaceAll() {
+ var searchtxt = document.getElementById("f_search").value;
+
+ //Check a search string
+ if (searchtxt.length < 1 ) {
+ alert ("Search string is empty!");
+ return true;
+ }
+
+ var replacetxt = document.getElementById("f_replace").value;
+ var stringcase = (document.getElementById("f_case").checked) ? "g" : "gi";
+ var regularx = (document.getElementById("f_regx").checked) ? 1 : 0;
+ //var closesar = (document.getElementById("f_csar").checked) ? 1 : 0;
+ var closesar = 1;
+ var param = [ searchtxt , replacetxt, stringcase, regularx, closesar ];
+
+ //looks that not workin in ie :( need to fix!
+ if (closesar) {
+ __dlg_close(param);
+ window.close();
+ return false;
+ } else {
+ return true;
+ }
+};
+
+function onCancel() {
+ __dlg_close(null);
+ return false;
+};
+
+-->
+</script>
+
+<style type="text/css">
+html, body {
+ background: ButtonFace;
+ color: ButtonText;
+ font: 11px Tahoma,Verdana,sans-serif;
+ margin: 0px;
+ padding: 0px;
+}
+body { padding: 5px; }
+table {
+ font: 11px Tahoma,Verdana,sans-serif;
+}
+form p {
+ margin-top: 5px;
+ margin-bottom: 5px;
+}
+.fl { width: 9em; float: left; padding: 2px 5px; text-align: right; }
+.fr { width: 7em; float: left; padding: 2px 5px; text-align: right; }
+fieldset { padding: 0px 10px 5px 5px; }
+select, input, button { font: 11px Tahoma,Verdana,sans-serif; }
+button { width: 70px; }
+#buttons {
+ margin-top: 1em; border-top: 1px solid #999;
+ padding: 2px; text-align: right;
+}
+
+.space { padding: 2px; }
+
+.title { background: #ddf; color: #000; font-weight: bold; font-size: 120%; padding: 3px 10px; margin-bottom: 10px;
+border-bottom: 1px solid black; letter-spacing: 2px;
+}
+form { padding: 0px; margin: 0px; }
+</style>
+</head>
+<body onload="Init()">
+<div class="title"><?php print_string("searchandreplace","editor");?></div>
+<form>
+<table border="0" style="width: 100%;">
+ <tr>
+ <td class="label"><?php print_string("findwhat","editor");?>:</td>
+ <td align="left"><input type="text" id="f_search" style="width: 280px" /></td>
+ </tr>
+ <tr>
+ <td class="label"><?php print_string("replacewith","editor");?>:</td>
+ <td align="left"><input type="text" id="f_replace" style="width: 280px" /></td>
+ </tr>
+
+ </table>
+ <fieldset>
+ <legend><span style="font-weight: bold;"><?php print_string("options","editor");?>:</span></legend>
+<table border="0" style="width: 100%;">
+ <tr>
+ <td style="width: 20px;"><input type="checkbox" id="f_regx" checked="checked" /></td>
+ <td><label for="f_regx"><?php print_string("regularexpressions","editor");?></label></td>
+ </tr>
+ <tr>
+ <td style="width: 20px;"><input type="checkbox" id="f_case" checked="checked" /></td>
+ <td><label for="f_case"><?php print_string("matchcase","editor");?></label></td>
+ </tr>
+ <!-- <tr>
+ <td style="width: 20px;"><input type="checkbox" id="f_csar" checked="checked" /></td>
+ <td><label for="f_csar"><?php print_string("closeafterreplace","editor");?></label></td>
+ </tr> -->
+</table>
+</fieldset>
+<div id="buttons">
+ <button type="button" name="ok" onclick="return onReplaceAll();" style="width: 120px;"><?php print_string("replaceall","editor");?></button>
+ <button type="button" name="cancel" onclick="return onCancel();"><?php print_string("cancel","editor");?></button>
+</div>
+</form>
+</body>
+</html>

0 comments on commit 130d7a3

Please sign in to comment.
Something went wrong with that request. Please try again.