1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2007, 2008, 2009, 2010, 2012, 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, 2012, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * Creates a password field.
 26  * @constructor
 27  * @class
 28  * 
 29  * @param	{hash}		params		a hash of parameters
 30  * @param {DwtComposite}      params.parent			the parent widget
 31  * @param {string}      params.initialValue		the initial value of the field
 32  * @param {number}      params.size				size of the input field (in characters)
 33  * @param {number}      params.rows				the number of rows (more than 1 means textarea)
 34  * @param {boolean}      params.forceMultiRow		if <code>true</code>, forces use of textarea even if rows == 1
 35  * @param {number}      params.maxLen			the maximum length (in characters) of the input
 36  * @param {constant}      params.errorIconStyle		the error icon style
 37  * @param {constant}      params.validationStyle	the validation type
 38  * @param  {function}     params.validator			the custom validation function
 39  * @param {Object}      params.validatorCtxtObj		the object context for validation function
 40  * @param {string}      params.className			the CSS class
 41  * @param {constant}      params.posStyle			the positioning style (see {@link DwtControl})
 42  * @param {boolean}      params.required          if <code>true</code>, mark as required.
 43  * @param {string}      params.hint				a hint to display in the input field when the value is empty.
 44  * @param {string}      params.id				an explicit ID to use for the control's DIV element
 45  * @param {string}      params.inputId			an explicit ID to use for the control's INPUT element
 46  * 
 47  * @extends		DwtInputField
 48  */
 49 DwtPasswordField = function(params) {
 50 	if (arguments.length == 0) return;
 51 
 52 	params = params || { parent: DwtShell.getShell(window) };
 53 	params.type = DwtInputField.PASSWORD;
 54 	DwtInputField.call(this, params);
 55 
 56 	this._tabGroup = new DwtTabGroup(this._htmlElId);
 57 
 58 	// TODO: templatize DwtInputField -- then we don't need to explicitly call _createHtml
 59 	this._createHtml();
 60 };
 61 DwtPasswordField.prototype = new DwtInputField;
 62 DwtPasswordField.prototype.constructor = DwtPasswordField;
 63 
 64 //
 65 // Data
 66 //
 67 
 68 DwtPasswordField.prototype.TEMPLATE = "dwt.Widgets#DwtPasswordField";
 69 
 70 //
 71 // Public methods
 72 //
 73 
 74 DwtPasswordField.prototype.getTabGroupMember = function() {
 75 	return this._tabGroup;
 76 };
 77 
 78 /**
 79  * Shows the password.
 80  * 
 81  * @param	{boolean}	show		if <code>true</code>, show the password
 82  */
 83 DwtPasswordField.prototype.setShowPassword = function(show) {
 84 	this._showCheckbox.setSelected(show);
 85 	this.setInputType(show ? DwtInputField.STRING : DwtInputField.PASSWORD);
 86 };
 87 
 88 //
 89 // Protected methods
 90 //
 91 
 92 DwtPasswordField.prototype._createHtml = function(templateId) {
 93 	var data = { id: this._htmlElId };
 94 	this._createHtmlFromTemplate(templateId || this.TEMPLATE, data);
 95 };
 96 
 97 DwtPasswordField.prototype._createHtmlFromTemplate =
 98 function(templateId, data) {
 99 	this._tabGroup.removeAllMembers();
100 
101 	// save old contents
102 	var fragment = document.createDocumentFragment();
103 	var child = this.getHtmlElement().firstChild;
104 	while (child) {
105 		var sibling = child.nextSibling;
106 		fragment.appendChild(child);
107 		child = sibling;
108 	};
109 
110 	// create HTML and append content
111 	DwtInputField.prototype._createHtmlFromTemplate.apply(this, arguments);
112 	var inputEl = document.getElementById(data.id+"_input");
113 	inputEl.appendChild(fragment);
114 	this._tabGroup.addMember(this.getInputElement());
115 
116 	var showCheckboxEl = document.getElementById(data.id+"_show_password");
117 	if (showCheckboxEl) {
118 		this._showCheckbox = new DwtCheckbox({parent:this});
119 		this._showCheckbox.setText(AjxMsg.showPassword);
120 		this._showCheckbox.addSelectionListener(new AjxListener(this, this._handleShowCheckbox));
121 		this._showCheckbox.replaceElement(showCheckboxEl);
122 		this._tabGroup.addMember(this._showCheckbox);
123 	}
124 };
125 
126 DwtPasswordField.prototype._handleShowCheckbox = function(event) {
127 	this.setShowPassword(event.detail);
128 };
129 
130 /**
131 * Overrides DwtInputField getValue to not do the leading/trailing spaces trimming.
132 *
133 * @return {string} the value
134 */
135 DwtPasswordField.prototype.getValue =
136 function() {
137 	return this._inputField.value;
138 };
139 
140