From 2f980ca76ca1c2399e216fa1b94862c4932db3dd Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Tue, 28 Feb 2017 10:20:30 +0100 Subject: [PATCH] Fix saving backup codes by using a correct data uri Signed-off-by: Christoph Wurst --- apps/twofactor_backupcodes/js/settingsview.js | 162 +++++++++++++----- 1 file changed, 121 insertions(+), 41 deletions(-) diff --git a/apps/twofactor_backupcodes/js/settingsview.js b/apps/twofactor_backupcodes/js/settingsview.js index 753644fb21..101db0c8e7 100644 --- a/apps/twofactor_backupcodes/js/settingsview.js +++ b/apps/twofactor_backupcodes/js/settingsview.js @@ -1,59 +1,100 @@ /* global Backbone, Handlebars, OC, _ */ -(function (OC, Handlebars, $, _) { +(function(OC, Handlebars, $, _) { 'use strict'; OC.Settings = OC.Settings || {}; OC.Settings.TwoFactorBackupCodes = OC.Settings.TwoFactorBackupCodes || {}; var TEMPLATE = '
' - + '{{#unless enabled}}' - + '' - + '{{else}}' - + '

' - + '{{#unless codes}}' - + t('twofactor_backupcodes', 'Backup codes have been generated. {{used}} of {{total}} codes have been used.') - + '{{else}}' - + t('twofactor_backupcodes', 'These are your backup codes. Please save and/or print them as you will not be able to read the codes again later') - + '

    ' - + '{{#each codes}}' - + '
  • {{this}}
  • ' - + '{{/each}}' - + '
' - + '' + t('twofactor_backupcodes', 'Save backup codes') + '' - + '' - + '{{/unless}}' - + '

' - + '

' - + '' - + '

' - + '

' - + t('twofactor_backupcodes', 'If you regenerate backup codes, you automatically invalidate old codes.') - + '

' - + '{{/unless}}' - + '' + t('twofactor_backupcodes', 'Generate backup codes') + '' + + '{{else}}' + + '

' + + '{{#unless codes}}' + + t('twofactor_backupcodes', 'Backup codes have been generated. {{used}} of {{total}} codes have been used.') + + '{{else}}' + + t('twofactor_backupcodes', 'These are your backup codes. Please save and/or print them as you will not be able to read the codes again later') + + '

    ' + + '{{#each codes}}' + + '
  • {{this}}
  • ' + + '{{/each}}' + + '
' + + '' + t('twofactor_backupcodes', 'Save backup codes') + '' + + '' + + '{{/unless}}' + + '

' + + '

' + + '' + + '

' + + '

' + + t('twofactor_backupcodes', 'If you regenerate backup codes, you automatically invalidate old codes.') + + '

' + + '{{/unless}}' + + '"; }, ''); }, - _load: function () { + + /** + * Load codes from the server + * + * @returns {undefined} + */ + _load: function() { this._loading = true; var url = OC.generateUrl('/apps/twofactor_backupcodes/settings/state'); var loading = $.ajax(url, { - method: 'GET', + method: 'GET' }); - $.when(loading).done(function (data) { + $.when(loading).done(function(data) { this._enabled = data.enabled; this._total = data.total; this._used = data.used; }.bind(this)); - $.when(loading).always(function () { + $.when(loading).always(function() { this._loading = false; this.render(); }.bind(this)); }, - _onGenerateBackupCodes: function () { + + /** + * Event handler to generate the codes + * + * @returns {undefined} + */ + _onGenerateBackupCodes: function() { if (OC.PasswordConfirmation.requiresPasswordConfirmation()) { OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this._onGenerateBackupCodes, this)); return; @@ -101,25 +174,32 @@ var url = OC.generateUrl('/apps/twofactor_backupcodes/settings/create'); $.ajax(url, { method: 'POST' - }).done(function (data) { + }).done(function(data) { this._enabled = data.state.enabled; this._total = data.state.total; this._used = data.state.used; this._codes = data.codes; this.render(); - }.bind(this)).fail(function () { + }.bind(this)).fail(function() { OC.Notification.showTemporary(t('twofactor_backupcodes', 'An error occurred while generating your backup codes')); $('#generate-backup-codes').removeClass('icon-loading-small'); }); }, - _onPrintBackupCodes: function () { - var data = this._getDownloadData(); + + /** + * Event handler to print the codes + * + * @returns {undefined} + */ + _onPrintBackupCodes: function() { + var data = this._getPrintData(); var newTab = window.open('', t('twofactor_backupcodes', 'Nextcloud backup codes')); newTab.document.write('

' + t('twofactor_backupcodes', 'Nextcloud backup codes') + '

'); newTab.document.write(data); newTab.print(); newTab.close(); } + }); OC.Settings.TwoFactorBackupCodes.View = View;