1- $ ( document ) . ready ( function ( ) {
2- const options = window . cameraWidgetOptions || { } ;
3- const openCameraBtn = $ ( '#openNativeCamera' ) ;
4- const fileInput = $ ( '#fileInput' ) ;
5- const previewImage = $ ( '#previewImage' ) ;
6-
7- // Gunakan opsi dari PHP
8- const uploadUrl = options . uploadUrl ;
9- const retakeText = options . retakeText || 'Ambil Ulang Gambar' ;
10- const onSuccess = options . onSuccess ;
11- const onError = options . onError ;
1+ $ ( document ) . ready ( function ( ) {
2+ const options = window . cameraWidgetOptions || { } ;
3+ const openCameraBtn = $ ( "#openNativeCamera" ) ;
4+ const fileInput = $ ( "#fileInput" ) ;
5+ const previewImage = $ ( "#previewImage" ) ;
126
13- openCameraBtn . on ( 'click' , function ( ) {
14- fileInput . click ( ) ;
15- } ) ;
7+ // Gunakan opsi dari PHP
8+ const uploadUrl = options . uploadUrl ;
9+ const retakeText = options . retakeText || "Ambil Ulang Gambar" ;
10+ const onSuccess = options . onSuccess ;
11+ const onError = options . onError ;
1612
17- fileInput . on ( 'change' , function ( event ) {
18- const file = event . target . files [ 0 ] ;
19- if ( ! file ) {
20- return ;
21- }
13+ openCameraBtn . on ( "click" , function ( ) {
14+ fileInput . click ( ) ;
15+ } ) ;
2216
23- const reader = new FileReader ( ) ;
24- reader . onload = function ( e ) {
25- openCameraBtn . text ( retakeText ) ;
26- previewImage . attr ( 'src' , e . target . result ) . show ( ) ;
17+ fileInput . on ( "change" , function ( event ) {
18+ const file = event . target . files [ 0 ] ;
19+ if ( ! file ) {
20+ return ;
21+ }
2722
28- const formData = new FormData ( ) ;
29- formData . append ( 'imageFile' , file ) ;
30- formData . append ( '_csrf' , yii . getCsrfToken ( ) ) ;
31-
32- $ . ajax ( {
33- url : uploadUrl ,
34- method : 'POST' ,
35- data : formData ,
36- processData : false ,
37- contentType : false ,
38- success : function ( response ) {
39- if ( onSuccess && typeof window [ onSuccess ] === 'function' ) {
40- window [ onSuccess ] ( response ) ;
41- } else {
42- console . log ( 'Gambar berhasil diunggah:' , response ) ;
43- }
44- } ,
45- error : function ( xhr , status , error ) {
46- if ( onError && typeof window [ onError ] === 'function' ) {
47- window [ onError ] ( error ) ;
48- } else {
49- console . error ( 'Error saat mengunggah gambar:' , error ) ;
50- }
51- }
52- } ) ;
53- } ;
54- reader . readAsDataURL ( file ) ;
55- } ) ;
56- } ) ;
23+ const reader = new FileReader ( ) ;
24+ reader . onload = function ( e ) {
25+ openCameraBtn . text ( retakeText ) ;
26+ previewImage . attr ( "src" , e . target . result ) . show ( ) ;
27+
28+ const formData = new FormData ( ) ;
29+ formData . append ( "imageFile" , file ) ;
30+ formData . append ( "_csrf" , yii . getCsrfToken ( ) ) ;
31+
32+ $ . ajax ( {
33+ url : uploadUrl ,
34+ method : "POST" ,
35+ data : formData ,
36+ processData : false ,
37+ contentType : false ,
38+ success : function ( response ) {
39+ if ( onSuccess && typeof window [ onSuccess ] === "function" ) {
40+ window [ onSuccess ] ( response ) ;
41+ } else {
42+ console . log ( "Gambar berhasil diunggah:" , response ) ;
43+ }
44+ } ,
45+ error : function ( xhr , status , error ) {
46+ if ( onError && typeof window [ onError ] === "function" ) {
47+ window [ onError ] ( error ) ;
48+ } else {
49+ console . error ( "Error saat mengunggah gambar:" , error ) ;
50+ }
51+ } ,
52+ } ) ;
53+ } ;
54+ reader . readAsDataURL ( file ) ;
55+ } ) ;
56+ } ) ;
0 commit comments