1
1
var nfc = {
2
- // (A) INITIALIZE WEB NFC
3
- ndef : null , ctrl : null , // ndef object
4
- onread : null , onwrite : null , onerror : null , // functions to run on read, write, error
5
- init : ( ) => {
6
- nfc . stop ( ) ;
2
+ // (A) PROPERTIES
3
+ ndef : null , ctrl : null , // ndef object
4
+ ondone : null , onerror : null , // functions to call on complete/error
5
+
6
+ // (B) INIT WEB NFC SCANNER/WRITER
7
+ init : ( ondone , onerror ) => {
8
+ // (B1) ATTACH HTML
9
+ if ( document . getElementById ( "nfc-wrapA" ) == null ) {
10
+ let nScan = document . createElement ( "div" ) ;
11
+ nScan . id = "nfc-wrapA" ;
12
+ nScan . className = "d-none tran-zoom bg-dark" ;
13
+ nScan . innerHTML = `<div id="nfc-wrapB">
14
+ <div id="nfc-wrapC" class="bg-success">
15
+ <div><i class="icon-feed"></i></div>
16
+ <div>READY - SCAN YOUR NFC TAG</div>
17
+ </div>
18
+ <button type="button" class="mt-4 btn btn-danger d-flex-inline" onclick="nfc.hide()">
19
+ <i class="ico-sm icon-cross"></i> Cancel
20
+ </button>
21
+ </div>` ;
22
+ document . body . appendChild ( nScan ) ;
23
+ }
24
+
25
+ // (B2) SET "POST ACTIONS"
26
+ nfc . ondone = ondone ;
27
+ nfc . onerror = onerror ;
28
+ } ,
29
+
30
+ // (C) START - MISSION START
31
+ start : ( ) => {
7
32
nfc . ctrl = new AbortController ( ) ;
8
33
nfc . ndef = new NDEFReader ( ) ;
9
34
} ,
10
35
11
- // (B ) STOP - MISSION ABORT
36
+ // (D ) STOP - MISSION ABORT
12
37
stop : ( ) => { if ( nfc . ndef != null ) {
13
38
nfc . ctrl . abort ( ) ;
14
39
nfc . ndef = null ;
15
40
nfc . ctrl = null ;
16
41
} } ,
17
42
18
- // (C) STANDBY - SCAN & DO NOTHING
19
- standby : ( ) => {
20
- nfc . init ( ) ;
21
- nfc . ndef . onreading = null ;
22
- nfc . ndef . onreadingerror = null ;
23
- nfc . ndef . scan ( { signal : nfc . ctrl . signal } ) ;
43
+ // (E) SHOW NFC WRITER/SCANNER
44
+ show : ( ) => cb . transit ( ( ) => {
45
+ document . getElementById ( "nfc-wrapA" ) . classList . remove ( "d-none" ) ;
46
+ document . body . classList . add ( "overflow-hidden" ) ;
47
+ } ) ,
48
+
49
+ // (F) HIDE NFC WRITER/SCANNER
50
+ hide : ( ) => {
51
+ cb . transit ( ( ) => {
52
+ document . getElementById ( "nfc-wrapA" ) . classList . add ( "d-none" ) ;
53
+ document . body . classList . remove ( "overflow-hidden" ) ;
54
+ } ) ;
24
55
} ,
25
56
26
- // (D) SCAN NFC TAG
27
- scan : ( ) => {
28
- nfc . init ( ) ;
29
- nfc . ndef . scan ( { signal : nfc . ctrl . signal } )
30
- . then ( ( ) => {
31
- if ( nfc . onread != null ) { nfc . ndef . onreading = nfc . onread ; }
32
- if ( nfc . onerror != null ) { nfc . ndef . onreadingerror = nfc . onerror ; }
33
- } )
34
- . catch ( err => { if ( nfc . onerror != null ) { nfc . onerrorerr ( err ) ; } } ) ;
57
+ // (G) GENERAL ERROR HANDLER
58
+ catcher : err => {
59
+ nfc . stop ( ) ; nfc . hide ( ) ;
60
+ cb . modal ( "ERROR" , err . msg ) ;
61
+ if ( nfc . onerror ) { nfc . onerror ( err ) ; }
35
62
} ,
36
63
37
- // (E ) WRITE NFC TAG
64
+ // (H ) WRITE NFC TAG
38
65
write : data => {
39
- nfc . init ( ) ;
66
+ nfc . stop ( ) ; nfc . start ( ) ; nfc . show ( ) ;
40
67
nfc . ndef . write ( data , { signal : nfc . ctrl . signal } )
41
- . then ( ( ) => { if ( nfc . onwrite != null ) { nfc . onwrite ( ) ; } } )
42
- . catch ( err => { if ( nfc . onerror != null ) { nfc . onerrorerr ( err ) ; } } ) ;
68
+ . then ( ( ) => {
69
+ nfc . stop ( ) ; nfc . hide ( ) ;
70
+ cb . toast ( true , "Success" , "NFC Tag Created" ) ;
71
+ if ( nfc . ondone ) { nfc . ondone ( ) ; }
72
+ } )
73
+ . catch ( nfc . catcher ) ;
43
74
} ,
44
75
45
- // (F) CREATE READ-ONLY NFC TAG
76
+ // (I) SCAN NFC TAG
77
+ scan : ( ) => {
78
+ nfc . stop ( ) ; nfc . start ( ) ; nfc . show ( ) ;
79
+ nfc . ndef . scan ( { signal : nfc . ctrl . signal } )
80
+ . then ( ( ) => {
81
+ nfc . ndef . onreading = evt => {
82
+ nfc . stop ( ) ; nfc . hide ( ) ;
83
+ const decoder = new TextDecoder ( ) ;
84
+ nfc . ondone ( decoder . decode ( evt . message . records [ 0 ] . data ) ) ;
85
+ } ;
86
+ nfc . ndef . onreadingerror = nfc . catcher ;
87
+ } )
88
+ . catch ( nfc . catcher ) ;
89
+ } ,
90
+
91
+ // (J) CREATE READ-ONLY NFC TAG
46
92
readonly : ( ) => {
47
- nfc . init ( ) ;
93
+ nfc . stop ( ) ; nfc . start ( ) ; nfc . show ( ) ;
48
94
nfc . ndef . makeReadOnly ( { signal : nfc . ctrl . signal } )
49
- . then ( ( ) => { if ( nfc . onwrite != null ) { nfc . onwrite ( ) ; } } )
50
- . catch ( err => { if ( nfc . onerror != null ) { nfc . onerrorerr ( err ) ; } } ) ;
95
+ . then ( ( ) => {
96
+ nfc . stop ( ) ; nfc . hide ( ) ;
97
+ cb . toast ( true , "Success" , "NFC Tag Locked" ) ;
98
+ if ( nfc . ondone ) { nfc . ondone ( ) ; }
99
+ } )
100
+ . catch ( nfc . catcher ) ;
51
101
}
52
102
} ;
0 commit comments