bgneal@312: tinyMCEPopup.requireLangPack();
bgneal@312: 
bgneal@312: var SearchReplaceDialog = {
bgneal@312: 	init : function(ed) {
bgneal@442: 		var t = this, f = document.forms[0], m = tinyMCEPopup.getWindowArg("mode");
bgneal@312: 
bgneal@442: 		t.switchMode(m);
bgneal@312: 
bgneal@312: 		f[m + '_panel_searchstring'].value = tinyMCEPopup.getWindowArg("search_string");
bgneal@312: 
bgneal@312: 		// Focus input field
bgneal@312: 		f[m + '_panel_searchstring'].focus();
bgneal@442: 		
bgneal@442: 		mcTabs.onChange.add(function(tab_id, panel_id) {
bgneal@442: 			t.switchMode(tab_id.substring(0, tab_id.indexOf('_')));
bgneal@442: 		});
bgneal@312: 	},
bgneal@312: 
bgneal@312: 	switchMode : function(m) {
bgneal@312: 		var f, lm = this.lastMode;
bgneal@312: 
bgneal@312: 		if (lm != m) {
bgneal@312: 			f = document.forms[0];
bgneal@312: 
bgneal@312: 			if (lm) {
bgneal@312: 				f[m + '_panel_searchstring'].value = f[lm + '_panel_searchstring'].value;
bgneal@312: 				f[m + '_panel_backwardsu'].checked = f[lm + '_panel_backwardsu'].checked;
bgneal@312: 				f[m + '_panel_backwardsd'].checked = f[lm + '_panel_backwardsd'].checked;
bgneal@312: 				f[m + '_panel_casesensitivebox'].checked = f[lm + '_panel_casesensitivebox'].checked;
bgneal@312: 			}
bgneal@312: 
bgneal@312: 			mcTabs.displayTab(m + '_tab',  m + '_panel');
bgneal@312: 			document.getElementById("replaceBtn").style.display = (m == "replace") ? "inline" : "none";
bgneal@312: 			document.getElementById("replaceAllBtn").style.display = (m == "replace") ? "inline" : "none";
bgneal@312: 			this.lastMode = m;
bgneal@312: 		}
bgneal@312: 	},
bgneal@312: 
bgneal@312: 	searchNext : function(a) {
bgneal@312: 		var ed = tinyMCEPopup.editor, se = ed.selection, r = se.getRng(), f, m = this.lastMode, s, b, fl = 0, w = ed.getWin(), wm = ed.windowManager, fo = 0;
bgneal@312: 
bgneal@312: 		// Get input
bgneal@312: 		f = document.forms[0];
bgneal@312: 		s = f[m + '_panel_searchstring'].value;
bgneal@312: 		b = f[m + '_panel_backwardsu'].checked;
bgneal@312: 		ca = f[m + '_panel_casesensitivebox'].checked;
bgneal@312: 		rs = f['replace_panel_replacestring'].value;
bgneal@312: 
bgneal@442: 		if (tinymce.isIE) {
bgneal@442: 			r = ed.getDoc().selection.createRange();
bgneal@442: 		}
bgneal@442: 
bgneal@312: 		if (s == '')
bgneal@312: 			return;
bgneal@312: 
bgneal@312: 		function fix() {
bgneal@312: 			// Correct Firefox graphics glitches
bgneal@442: 			// TODO: Verify if this is actually needed any more, maybe it was for very old FF versions? 
bgneal@312: 			r = se.getRng().cloneRange();
bgneal@312: 			ed.getDoc().execCommand('SelectAll', false, null);
bgneal@312: 			se.setRng(r);
bgneal@312: 		};
bgneal@312: 
bgneal@312: 		function replace() {
bgneal@442: 			ed.selection.setContent(rs); // Needs to be duplicated due to selection bug in IE
bgneal@312: 		};
bgneal@312: 
bgneal@312: 		// IE flags
bgneal@312: 		if (ca)
bgneal@312: 			fl = fl | 4;
bgneal@312: 
bgneal@312: 		switch (a) {
bgneal@312: 			case 'all':
bgneal@312: 				// Move caret to beginning of text
bgneal@312: 				ed.execCommand('SelectAll');
bgneal@312: 				ed.selection.collapse(true);
bgneal@312: 
bgneal@312: 				if (tinymce.isIE) {
bgneal@442: 					ed.focus();
bgneal@442: 					r = ed.getDoc().selection.createRange();
bgneal@442: 
bgneal@312: 					while (r.findText(s, b ? -1 : 1, fl)) {
bgneal@312: 						r.scrollIntoView();
bgneal@312: 						r.select();
bgneal@312: 						replace();
bgneal@312: 						fo = 1;
bgneal@312: 
bgneal@312: 						if (b) {
bgneal@312: 							r.moveEnd("character", -(rs.length)); // Otherwise will loop forever
bgneal@312: 						}
bgneal@312: 					}
bgneal@312: 
bgneal@312: 					tinyMCEPopup.storeSelection();
bgneal@312: 				} else {
bgneal@312: 					while (w.find(s, ca, b, false, false, false, false)) {
bgneal@312: 						replace();
bgneal@312: 						fo = 1;
bgneal@312: 					}
bgneal@312: 				}
bgneal@312: 
bgneal@312: 				if (fo)
bgneal@312: 					tinyMCEPopup.alert(ed.getLang('searchreplace_dlg.allreplaced'));
bgneal@312: 				else
bgneal@312: 					tinyMCEPopup.alert(ed.getLang('searchreplace_dlg.notfound'));
bgneal@312: 
bgneal@312: 				return;
bgneal@312: 
bgneal@312: 			case 'current':
bgneal@312: 				if (!ed.selection.isCollapsed())
bgneal@312: 					replace();
bgneal@312: 
bgneal@312: 				break;
bgneal@312: 		}
bgneal@312: 
bgneal@312: 		se.collapse(b);
bgneal@312: 		r = se.getRng();
bgneal@312: 
bgneal@312: 		// Whats the point
bgneal@312: 		if (!s)
bgneal@312: 			return;
bgneal@312: 
bgneal@312: 		if (tinymce.isIE) {
bgneal@442: 			ed.focus();
bgneal@442: 			r = ed.getDoc().selection.createRange();
bgneal@442: 
bgneal@312: 			if (r.findText(s, b ? -1 : 1, fl)) {
bgneal@312: 				r.scrollIntoView();
bgneal@312: 				r.select();
bgneal@312: 			} else
bgneal@312: 				tinyMCEPopup.alert(ed.getLang('searchreplace_dlg.notfound'));
bgneal@312: 
bgneal@312: 			tinyMCEPopup.storeSelection();
bgneal@312: 		} else {
bgneal@312: 			if (!w.find(s, ca, b, false, false, false, false))
bgneal@312: 				tinyMCEPopup.alert(ed.getLang('searchreplace_dlg.notfound'));
bgneal@312: 			else
bgneal@312: 				fix();
bgneal@312: 		}
bgneal@312: 	}
bgneal@312: };
bgneal@312: 
bgneal@312: tinyMCEPopup.onInit.add(SearchReplaceDialog.init, SearchReplaceDialog);