1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 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) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * @overview
 26  * This file contains classes for a message dialog.
 27  */
 28 
 29 /**
 30  * @class
 31  * 
 32  * Creates a new message dialog. This class represents a reusable message dialog box.
 33  * Messages can be informational, warning, or critical.
 34  * 
 35  * @author Ross Dargahi
 36  * 
 37  * @param {hash}		params			a hash of parameters
 38  * @param {DwtComposite}	params.parent		the parent widget (the shell)
 39  * @param {string}	params.className 		the CSS class
 40  * @param {array}	params.buttons				the buttons to show. Defaults to {@link DwtDialog.OK_BUTTON} button
 41  * @param {array}	params.extraButtons	  	a list of {@link DwtDialog_ButtonDescriptor} objects describing custom buttons to add to the dialog
 42  * @param {String} params.helpText  shows a left aligned help button with the text specified in this param.
 43  * 
 44  * @extends	DwtDialog
 45  */
 46 DwtMessageDialog = function(params) {
 47 	if (arguments.length == 0) { return; }
 48 	params = Dwt.getParams(arguments, DwtMessageDialog.PARAMS);
 49 	this._msgCellId = Dwt.getNextId("MessageDialog_");
 50 	params.standardButtons = params.buttons || [DwtDialog.OK_BUTTON];
 51 
 52 	if (params.helpText) {
 53 		var helpButton = new DwtDialog_ButtonDescriptor(DwtMessageDialog.HELP_BUTTON, params.helpText, DwtDialog.ALIGN_LEFT);
 54 		params.extraButtons = params.extraButtons || [];
 55 		params.extraButtons.push(helpButton);
 56 		DwtDialog.call(this, params);
 57 		this.registerCallback(DwtMessageDialog.HELP_BUTTON, function() {
 58 			ZmZimbraMail.helpLinkCallback(this._helpURL);
 59 		},this);
 60 	} else {
 61 		DwtDialog.call(this, params);
 62 	}
 63 	
 64 	this.setContent(this._contentHtml());
 65 	this._msgCell = document.getElementById(this._msgCellId);
 66 	this.addEnterListener(new AjxListener(this, this._enterListener));
 67 	this._setAllowSelection();
 68 
 69 	if (AjxEnv.isSafari) {
 70 		this.setAttribute('aria-labelledby',
 71 		                  this._titleEl.id + ' ' + this._msgCellId);
 72 	} else {
 73 		this.setAttribute('aria-describedby', this._msgCellId);
 74 	}
 75 };
 76 
 77 DwtMessageDialog.PARAMS = ["parent", "className", "buttons", "extraButtons", "id"];
 78 
 79 DwtMessageDialog.prototype = new DwtDialog;
 80 DwtMessageDialog.prototype.constructor = DwtMessageDialog;
 81 DwtMessageDialog.prototype.isDwtMessageDialog = true;
 82 DwtMessageDialog.prototype.role = 'alertdialog';
 83 
 84 DwtMessageDialog.prototype.toString = function() {
 85 	return "DwtMessageDialog";
 86 };
 87 
 88 /**
 89  * Defines the "critical" style.
 90  */
 91 DwtMessageDialog.CRITICAL_STYLE = 1;
 92 /**
 93  * Defines the "info" style.
 94  */
 95 DwtMessageDialog.INFO_STYLE = 2;
 96 /**
 97  * Defines the "warning" style.
 98  */
 99 DwtMessageDialog.WARNING_STYLE = 3;
100 /**
101  * Defines a style with no icon.
102  */
103 DwtMessageDialog.PLAIN_STYLE = 4;
104 
105 DwtMessageDialog.TITLE = {};
106 DwtMessageDialog.TITLE[DwtMessageDialog.CRITICAL_STYLE] = AjxMsg.criticalMsg;
107 DwtMessageDialog.TITLE[DwtMessageDialog.INFO_STYLE] = AjxMsg.infoMsg;
108 DwtMessageDialog.TITLE[DwtMessageDialog.WARNING_STYLE] = AjxMsg.warningMsg;
109 DwtMessageDialog.TITLE[DwtMessageDialog.PLAIN_STYLE] = AjxMsg.infoMsg;
110 
111 DwtMessageDialog.ICON = {};
112 DwtMessageDialog.ICON[DwtMessageDialog.CRITICAL_STYLE] = "Critical_32";
113 DwtMessageDialog.ICON[DwtMessageDialog.INFO_STYLE] = "Information_32";
114 DwtMessageDialog.ICON[DwtMessageDialog.WARNING_STYLE] = "Warning_32";
115 
116 DwtMessageDialog.HELP_BUTTON = "Help";
117 // Public methods
118 
119 /**
120  * Returns a string representation of the object.
121  * 
122  * @return		{string}		a string representation of the object
123  */
124 DwtMessageDialog.prototype.toString = 
125 function() {
126 	return "DwtMessageDialog";
127 };
128 
129 /**
130 * Sets the message style (info/warning/critical) and content.
131 *
132 * @param {string}	msgStr		the message text
133 * @param {constant}	style		the style (see <code>DwtMessageDialog.*_STYLE</code> constants)
134 * @param {string}	title		the dialog box title
135 */
136 DwtMessageDialog.prototype.setMessage =
137 function(msgStr, style, title) {
138 	this._message = msgStr || "";
139 	this._style = style || this._getDefaultStyle();
140 
141 	this.setTitle(title || DwtMessageDialog.TITLE[this._style]);
142 
143 	if (msgStr) {
144         var html = [];
145 		var i = 0;
146 		html[i++] = "<table role='presentation' cellspacing=0 cellpadding=0 border=0 width=100% height=100%><tr>";
147 		if (DwtMessageDialog.ICON[this._style]) {
148 			html[i++] = "<td valign='top'>"
149 			html[i++] = AjxImg.getImageHtml({
150 				imageName: DwtMessageDialog.ICON[this._style],
151 				attrStr: "id='" +  this._msgCellId + "_Image''",
152 				altText: DwtMessageDialog.TITLE[this._style]
153 			});
154 			html[i++] = "</td>";
155 		}
156 		html[i++] = "<td class='DwtMsgArea' id='" +  this._msgCellId +"_Msg'>";
157 		html[i++] = msgStr;
158 		html[i++] = "</td></tr></table>";
159 		this._msgCell.innerHTML = html.join("");
160 	} else {
161 		this._msgCell.innerHTML = "";
162 	}
163 };
164 
165 /**
166  * Sets the message style (info/warning/critical) and content.
167  *
168  * @param {string}	url		the url of the help
169  */
170 DwtMessageDialog.prototype.setHelpURL =
171 function(url) {
172 	this._helpURL = url;
173 }
174 
175 DwtMessageDialog.prototype.setSize =
176 function(width, height) {
177 	var msgCell = document.getElementById(this._msgCellId);
178 	if (msgCell && (width || height)) {
179 		Dwt.setSize(msgCell, width, height);
180 	}
181 };
182 
183 DwtMessageDialog.prototype._getDefaultStyle = function() {
184 	return DwtMessageDialog.INFO_STYLE;
185 }
186 
187 /**
188  * Resets the message dialog. This should be performed to "reuse" the dialog.
189  * 
190  */
191 DwtMessageDialog.prototype.reset = 
192 function() {
193 	this._msgCell.innerHTML = "";
194 	this._helpURL = "";
195 	DwtDialog.prototype.reset.call(this);
196 };
197 
198 /**
199  * Handles the dialog key action. If the user hits the "Esc" key and no "Cancel" button is present,
200  * the key action is treated it as a press of the "OK" button.
201  * 
202  * @param	{DwtKeyMap}		actionCode	the key action code
203  * @param	{DwtKeyEvent}	ev	the key event
204  * 
205  * @private
206  */
207 DwtMessageDialog.prototype.handleKeyAction =
208 function(actionCode, ev) {
209 	return DwtDialog.prototype.handleKeyAction.call(this, actionCode, ev);
210 };
211 
212 // Private methods
213 
214 /**
215  * @private
216  */
217 DwtMessageDialog.prototype._contentHtml = 
218 function() {
219 	return "<div id='" + this._msgCellId + "' class='DwtMsgDialog'></div>";
220 };
221 
222 /**
223  * @private
224  */
225 DwtMessageDialog.prototype._enterListener =
226 function(ev) {
227 	this._runEnterCallback();
228 };
229