1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  *
  4  * Zimbra Collaboration Suite Web Client
  5  * Copyright (C) 2015, 2016 Synacor, Inc.
  6  *
  7  * The contents of this file are subject to the Common Public Attribution License Version 1.0 (the "License");
  8  * you may not use this file except in compliance with the License.
  9  * You may obtain a copy of the License at: https://www.zimbra.com/license
 10  * The License is based on the Mozilla Public License Version 1.1 but Sections 14 and 15
 11  * have been added to cover use of software over a computer network and provide for limited attribution
 12  * for the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B.
 13  *
 14  * Software distributed under the License is distributed on an "AS IS" basis,
 15  * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 16  * See the License for the specific language governing rights and limitations under the License.
 17  * The Original Code is Zimbra Open Source Web Client.
 18  * The Initial Developer of the Original Code is Zimbra, Inc.  All rights to the Original Code were
 19  * transferred by Zimbra, Inc. to Synacor, Inc. on September 14, 2015.
 20  *
 21  * All portions of the code are Copyright (C) 2015, 2016 Synacor, Inc. All Rights Reserved.
 22  *
 23  * ***** END LICENSE BLOCK *****
 24  */
 25 
 26 /**
 27  * Creates a dialog for
 28  * @constructor
 29  * @class
 30  * @author  Hem Aravind
 31  *
 32  * @extends	DwtDialog
 33  */
 34 ZmOneTimeCodesDialog = function(params) {
 35 	this.twoStepAuthCodesSpan = params.twoStepAuthCodesSpan;
 36 	this.twoStepAuthCodesViewLink = params.twoStepAuthCodesViewLink;
 37 	this.twoStepAuthCodesGenerateLink = params.twoStepAuthCodesGenerateLink;
 38 	var generateNewCodesButton = new DwtDialog_ButtonDescriptor(ZmOneTimeCodesDialog.GENERATE_NEW_CODES_BUTTON, ZmMsg.twoStepAuthGenerateNewCodes, DwtDialog.ALIGN_LEFT, this._getScratchCodes.bind(this, true));
 39 	var printButton = new DwtDialog_ButtonDescriptor(ZmOneTimeCodesDialog.PRINT_BUTTON, ZmMsg.print, DwtDialog.ALIGN_RIGHT, this._printListener.bind(this));
 40 	var closeButton = new DwtDialog_ButtonDescriptor(DwtDialog.DISMISS_BUTTON, ZmMsg.cancel, DwtDialog.ALIGN_RIGHT, this.popdown.bind(this));
 41 	var newParams = {
 42 		parent : appCtxt.getShell(),
 43 		title : ZmMsg.twoStepAuthOneTimeCodesTitle,
 44 		standardButtons: [DwtDialog.NO_BUTTONS],
 45 		extraButtons : [generateNewCodesButton, printButton, closeButton]
 46 	};
 47 	DwtDialog.call(this, newParams);
 48 	this.setContent(this._contentHtml());
 49 	this._setAllowSelection();
 50 };
 51 
 52 ZmOneTimeCodesDialog.prototype = new DwtDialog;
 53 ZmOneTimeCodesDialog.prototype.constructor = ZmOneTimeCodesDialog;
 54 
 55 ZmOneTimeCodesDialog.GENERATE_NEW_CODES_BUTTON = ++DwtDialog.LAST_BUTTON;
 56 ZmOneTimeCodesDialog.PRINT_BUTTON = ++DwtDialog.LAST_BUTTON;
 57 
 58 /**
 59  * Pops-up the dialog.
 60  */
 61 ZmOneTimeCodesDialog.prototype.popup =
 62 function() {
 63 	this._getScratchCodes();
 64 	DwtDialog.prototype.popup.call(this);
 65 };
 66 
 67 ZmOneTimeCodesDialog.prototype._getScratchCodes =
 68 function(isNew) {
 69 	var params = {
 70 		twoStepAuthCodesSpan : this.twoStepAuthCodesSpan,
 71 		twoStepAuthCodesViewLink : this.twoStepAuthCodesViewLink,
 72 		twoStepAuthCodesGenerateLink : this.twoStepAuthCodesGenerateLink
 73 	};
 74 	var callback = this._getScratchCodesCallback.bind(this);
 75 	ZmAccountsPage.getScratchCodes(isNew, params, callback);
 76 };
 77 
 78 ZmOneTimeCodesDialog.prototype._getScratchCodesCallback =
 79 function(scratchCode) {
 80 	this.setContent(this._contentHtml(scratchCode));
 81 };
 82 
 83 ZmOneTimeCodesDialog.prototype._printListener =
 84 function() {
 85 	var content = AjxTemplate.expand("prefs.Pages#OneTimeCodesPrint", {content : this._getContentDiv().innerHTML});
 86 	var win = window.open('', '_blank');
 87 	appCtxt.handlePopupBlocker(win);
 88 	win.document.write(content);
 89 	win.document.close();
 90 	win.focus();
 91 	win.print();
 92 };
 93 
 94 ZmOneTimeCodesDialog.prototype._contentHtml =
 95 function(oneTimeCodes) {
 96 	var data = {
 97 		id : this._htmlElId,
 98 		oneTimeCodes : oneTimeCodes
 99 	};
100 	return AjxTemplate.expand("prefs.Pages#OneTimeCodes", data);
101 };