1+ /* global Backbone, Handlebars, OC, _ */
2+
3+ ( function ( OC , Handlebars , $ , _ ) {
4+ 'use strict' ;
5+
6+ OC . Settings = OC . Settings || { } ;
7+ OC . Settings . TwoFactorBackupCodes = OC . Settings . TwoFactorBackupCodes || { } ;
8+
9+ var TEMPLATE = '<div>'
10+ + '{{#unless enabled}}'
11+ + '<button id="generate-backup-codes">' + t ( 'twofactor_backupcodes' , 'Generate backup codes' ) + '</button>'
12+ + '{{else}}'
13+ + '<p>'
14+ + '{{#unless codes}}'
15+ + t ( 'twofactor_backupcodes' , 'Backup codes have been generated. {{used}} of {{total}} codes have been used.' )
16+ + '{{else}}'
17+ + 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' )
18+ + '<ul>'
19+ + '{{#each codes}}'
20+ + '<li class="backup-code">{{this}}</li>'
21+ + '{{/each}}'
22+ + '</ul>'
23+ + '<a href="{{download}}" class="button" download="Nextcloud-backup-codes.txt">' + t ( 'twofactor_backupcodes' , 'Save backup codes' ) + '</a>'
24+ + '<button id="print-backup-codes" class="button">' + t ( 'twofactor_backupcodes' , 'Print backup codes' ) + '</button>'
25+ + '{{/unless}}'
26+ + '</p>'
27+ + '<p>'
28+ + '<button id="generate-backup-codes">' + t ( 'twofactor_backupcodes' , 'Regenerate backup codes' ) + '</button>'
29+ + '</p>'
30+ + '<p>'
31+ + t ( 'twofactor_backupcodes' , 'If you regenerate backup codes, you automatically invalidate old codes.' )
32+ + '</p>'
33+ + '{{/unless}}'
34+ + '</div' ;
35+
36+ var View = OC . Backbone . View . extend ( {
37+ _template : undefined ,
38+ template : function ( data ) {
39+ if ( ! this . _template ) {
40+ this . _template = Handlebars . compile ( TEMPLATE ) ;
41+ }
42+ return this . _template ( data ) ;
43+ } ,
44+ _loading : undefined ,
45+ _enabled : undefined ,
46+ _total : undefined ,
47+ _used : undefined ,
48+ _codes : undefined ,
49+ events : {
50+ 'click #generate-backup-codes' : '_onGenerateBackupCodes' ,
51+ 'click #print-backup-codes' : '_onPrintBackupCodes' ,
52+ } ,
53+ initialize : function ( ) {
54+ this . _load ( ) ;
55+ } ,
56+ render : function ( ) {
57+ this . $el . html ( this . template ( {
58+ enabled : this . _enabled ,
59+ total : this . _total ,
60+ used : this . _used ,
61+ codes : this . _codes ,
62+ download : this . _getDownloadDataHref ( )
63+ } ) ) ;
64+ } ,
65+ _getDownloadDataHref : function ( ) {
66+ if ( ! this . _codes ) {
67+ return '' ;
68+ }
69+ return 'data:text/plain,' + encodeURIComponent ( _ . reduce ( this . _codes , function ( prev , code ) {
70+ return prev + code + "\r\n" ;
71+ } , '' ) ) ;
72+ } ,
73+ _load : function ( ) {
74+ this . _loading = true ;
75+
76+ var url = OC . generateUrl ( '/apps/twofactor_backupcodes/settings/state' ) ;
77+ var loading = $ . ajax ( url , {
78+ method : 'GET' ,
79+ } ) ;
80+
81+ $ . when ( loading ) . done ( function ( data ) {
82+ this . _enabled = data . enabled ;
83+ this . _total = data . total ;
84+ this . _used = data . used ;
85+ } . bind ( this ) ) ;
86+ $ . when ( loading ) . always ( function ( ) {
87+ this . _loading = false ;
88+ this . render ( ) ;
89+ } . bind ( this ) ) ;
90+ } ,
91+ _onGenerateBackupCodes : function ( ) {
92+ // Hide old codes
93+ this . _enabled = false ;
94+ this . render ( ) ;
95+ $ ( '#generate-backup-codes' ) . addClass ( 'icon-loading-small' ) ;
96+ var url = OC . generateUrl ( '/apps/twofactor_backupcodes/settings/create' ) ;
97+ $ . ajax ( url , {
98+ method : 'POST'
99+ } ) . done ( function ( data ) {
100+ this . _enabled = data . state . enabled ;
101+ this . _total = data . state . total ;
102+ this . _used = data . state . used ;
103+ this . _codes = data . codes ;
104+ this . render ( ) ;
105+ } . bind ( this ) ) . fail ( function ( ) {
106+ OC . Notification . showTemporary ( 'An error occurred while generating your backup codes' ) ;
107+ $ ( '#generate-backup-codes' ) . removeClass ( 'icon-loading-small' ) ;
108+ } ) ;
109+ } ,
110+ _onPrintBackupCodes : function ( ) {
111+ var url = this . _getDownloadDataHref ( ) ;
112+ window . open ( url , 'Nextcloud backpu codes' ) ;
113+ window . print ( ) ;
114+ window . close ( ) ;
115+ }
116+ } ) ;
117+
118+ OC . Settings . TwoFactorBackupCodes . View = View ;
119+
120+ } ) ( OC , Handlebars , $ , _ ) ;
0 commit comments