1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2007, 2008, 2009, 2010, 2011, 2013, 2014, 2016 Synacor, Inc.
  5  *
  6  * The contents of this file are subject to the Common Public Attribution License Version 1.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at: https://www.zimbra.com/license
  9  * The License is based on the Mozilla Public License Version 1.1 but Sections 14 and 15
 10  * have been added to cover use of software over a computer network and provide for limited attribution
 11  * for the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B.
 12  *
 13  * Software distributed under the License is distributed on an "AS IS" basis,
 14  * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 15  * See the License for the specific language governing rights and limitations under the License.
 16  * The Original Code is Zimbra Open Source Web Client.
 17  * The Initial Developer of the Original Code is Zimbra, Inc.  All rights to the Original Code were
 18  * transferred by Zimbra, Inc. to Synacor, Inc. on September 14, 2015.
 19  *
 20  * All portions of the code are Copyright (C) 2007, 2008, 2009, 2010, 2011, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * @overview
 26  */
 27 
 28 /**
 29  * Creates a "find-and-replace" dialog.
 30  * @class
 31  * This class represents a "find-and-replace" dialog.
 32  * 
 33  * @param	{DwtControl}	shell		the parent
 34  * @param	{String}	className		the class name
 35  * 
 36  * @extends		DwtDialog
 37  */
 38 ZmFindnReplaceDialog = function(shell, className) {
 39 	className = className || "ZmFindnReplaceDialog";
 40 	
 41 	var findBtn = new DwtDialog_ButtonDescriptor(ZmFindnReplaceDialog.FIND_BUTTON, 
 42 													  ZmMsg.find, DwtDialog.ALIGN_LEFT);
 43 	var replaceBtn = new DwtDialog_ButtonDescriptor(ZmFindnReplaceDialog.REPLACE_BUTTON, 
 44 													ZmMsg.replace, DwtDialog.ALIGN_LEFT);
 45 	var replaceAllBtn = new DwtDialog_ButtonDescriptor(ZmFindnReplaceDialog.REPLACE_ALL_BUTTON, 
 46 													ZmMsg.replaceAll, DwtDialog.ALIGN_LEFT);
 47 	
 48 	DwtDialog.call(this, {parent:shell, className:className, title:ZmMsg.findNReplaceTitle,
 49 						  standardButtons:[DwtDialog.CANCEL_BUTTON], extraButtons:[findBtn,replaceBtn,replaceAllBtn]});
 50 
 51 	this._findId = Dwt.getNextId();
 52 	this._replaceId = Dwt.getNextId();
 53 	this._dirId = Dwt.getNextId();
 54 	this._dirIdUp = Dwt.getNextId();
 55 	this._dirIdDown = Dwt.getNextId();
 56 	this._caseId = Dwt.getNextId();
 57 	this._wholeWordId = Dwt.getNextId();
 58 	this._messageId = Dwt.getNextId();
 59 	
 60 	var numberOfCols =1;
 61 		var html = [
 62          "<table><tr><td>",
 63          "<div style='padding:2px;' id='",this._messageId,"' style='padding-left:30px;'></div>",
 64          "<table border='0'>",
 65          "<tr><td class='Label' align='left'>",ZmMsg.findWhatLabel,"</td>",
 66          "<td colspan=2 id='",this._findId ,"'></td></tr>",
 67          "<tr><td class='Label' align='left'>",ZmMsg.replaceWithLabel,"</td>",
 68          "<td colspan=2 id='",this._replaceId ,"'></td></tr>",   
 69          "<tr><td class='Label' align='left'>",ZmMsg.directionLabel,"</td>",
 70          "<td colspan=2 id='",this._dirId ,"' align='left'>",
 71          	"<table cellpadding='3'><tr>",
 72          	"<td><input type='radio' id='",this._dirIdUp,"' name='",this._dirId,"' value='up'></td>","<td class='Label'>", "<label for='",this._dirIdUp,"'>", ZmMsg.upLabel, "</label>", "</td>",
 73          	"<td><input type='radio' id='",this._dirIdDown,"' name='",this._dirId,"' value='down' checked></td>","<td class='Label'>", "<label for='",this._dirIdDown,"'>", ZmMsg.downLabel, "</label>", "</td>",
 74          	"</tr></table>",
 75          "</td></tr>",        
 76          "<tr><td colspan='3'>",
 77 		   	"<table cellpadding='3'><tr>",
 78 		   	"<td class='Label' align='right'><input type='checkbox' id='",this._caseId,"'>",
 79 		   	"<td class='Label' align='left'>", "<label for='", this._caseId,"'>", ZmMsg.caseSensitive, "</label>", "</td>",
 80          	"</tr></table>",
 81 		 "</td></tr>",		         
 82          "</table>",          
 83          "</td></tr></table>"].join("");
 84      
 85 	this.setContent(html);
 86 	// set view
 87 	this.setView(this._createView());
 88 	this.registerCallback(ZmFindnReplaceDialog.FIND_BUTTON, this._handleFindButton, this);
 89 	this.registerCallback(ZmFindnReplaceDialog.REPLACE_BUTTON, this._handleReplaceButton, this);
 90 	this.registerCallback(ZmFindnReplaceDialog.REPLACE_ALL_BUTTON, this._handleReplaceAllButton, this);
 91 };
 92 
 93 ZmFindnReplaceDialog.prototype = new DwtDialog;
 94 ZmFindnReplaceDialog.prototype.constructor = ZmFindnReplaceDialog;
 95 
 96 
 97 ZmFindnReplaceDialog.FIND_BUTTON = ++DwtDialog.LAST_BUTTON;
 98 ZmFindnReplaceDialog.REPLACE_BUTTON = ++DwtDialog.LAST_BUTTON;
 99 ZmFindnReplaceDialog.REPLACE_ALL_BUTTON = ++DwtDialog.LAST_BUTTON;
100 
101 // Public methods
102 
103 /**
104  * Pops-up the dialog.
105  * 
106  * @param	{Hash}		editorInfo		a hash of editor info
107  * @param	{AjxCallback}	callback	the callback
108  */
109 ZmFindnReplaceDialog.prototype.popup =
110 function(editorInfo, callback) {
111 	this._editorInfo = editorInfo || {};
112 	this._callback = callback;
113     var findVal = "";
114     if(editorInfo) {
115         var editor = editorInfo.editor;
116         findVal = editor._getSelectedText();
117         if(findVal) {
118             findVal = AjxStringUtil.trim(findVal.toString());
119         }
120     }
121     this._findInput.setValue(findVal);
122     this._replaceInput.setValue("");    
123     DwtDialog.prototype.popup.call(this);
124 };
125 
126 ZmFindnReplaceDialog.prototype.popdown =
127 function() {
128 	if (this._acPageList) {
129 		this._acPageList.show(false);
130 	}
131 	DwtDialog.prototype.popdown.call(this);
132 };
133 
134 ZmFindnReplaceDialog._handleKeyPress = function(ev){
135     var inputField = DwtControl.getTargetControl(ev);
136 	var charCode = DwtKeyEvent.getCharCode(ev);
137 	if (charCode == 13 || charCode == 3) {		
138 		var dialog = inputField.parent.parent;
139 		dialog.replaceAction('none',true);
140 	    return false;
141 	}
142 	return true;	
143 };
144 
145 // Protected methods
146 
147 ZmFindnReplaceDialog.prototype._createView =
148 function() {
149 
150 	var view = new DwtComposite(this);
151 	var inputParams = {
152 		parent: view,
153 		type: DwtInputField.STRING,
154 		validationStyle: DwtInputField.CONTINUAL_VALIDATION
155 	}
156 
157 	// create common DWT controls
158 	this._findInput = new DwtInputField(inputParams);
159 	this._findInput.reparentHtmlElement(this._findId);
160 	this._replaceInput = new DwtInputField(inputParams);
161 	this._replaceInput.reparentHtmlElement(this._replaceId);
162 
163 	Dwt.setHandler(this._findInput.getInputElement(), DwtEvent.ONKEYPRESS, ZmFindnReplaceDialog._handleKeyPress);
164 	// create properties
165 	
166 	return view;
167 };
168 
169 ZmFindnReplaceDialog.prototype._handleFindButton =
170 function(event) {
171 	this.replaceAction('none',true);
172 };
173 
174 ZmFindnReplaceDialog.prototype._handleReplaceButton =
175 function() {
176 	this.replaceAction('current',false);
177 };
178 
179 ZmFindnReplaceDialog.prototype._handleReplaceAllButton =
180 function() {
181 	this.replaceAction('all',false);
182 };
183 
184 /**
185  * Shows an informational message.
186  * 
187  * @param	{String}	msg		the message
188  */
189 ZmFindnReplaceDialog.prototype.showInfoMsg =
190 function(msg) {
191 	if(!this.msgEl){
192 		this.msgEl = document.getElementById(this._messageId);
193 	}
194 	this.msgEl.innerHTML = msg;
195 };
196 
197 ZmFindnReplaceDialog.prototype.replaceAction =
198 function(mode,findOnly)
199 {
200 	var findVal = this._findInput.getValue();
201 	var replaceVal = (findOnly? null : this._replaceInput.getValue());
202 	var radioBtns = document.getElementById(this._dirIdUp);
203 	var casesensitiveVal = false;
204 	var backwardsVal = false;
205 	if(radioBtns && radioBtns.checked){
206 		backwardsVal = true;
207 	}
208 	this._caseCheckbox = document.getElementById(this._caseId);
209 	
210 	if(this._caseCheckbox && this._caseCheckbox.checked) {
211 		casesensitiveVal = true;
212 	}	
213 	
214 	var params = {
215 			searchstring: findVal,
216 			replacestring: replaceVal,
217 			replacemode : mode,
218 			casesensitive : casesensitiveVal,
219 			backwards : backwardsVal
220 		};
221 	if(this._editorInfo.editor){
222 		var editor = this._editorInfo.editor;
223 		if(AjxEnv.iIE){
224 		editor.focus();
225 		}
226 		editor.searchnReplace(params);			
227 	}
228 	
229 	if (this._callback) {		
230 		this._callback.run(params);		
231 	}
232 };
233 // Private methods
234 
235