1
+ jQuery ( document ) . ready ( function ( $ ) {
2
+ jQuery ( '.move-image-optml' ) . click ( function ( ) {
3
+ //get the id and send a jquery rest request to the server
4
+ var id = jQuery ( this ) . data ( 'id' ) ;
5
+ var action = jQuery ( this ) . data ( 'action' ) ;
6
+ moveImage ( id , action , jQuery ( this ) ) ;
7
+ } ) ;
8
+ } ) ;
9
+
10
+ function moveImage ( id , action , element , is_retry = false ) {
11
+ //add a loading indicator
12
+ element . parent ( ) . find ( '.spinner' ) . addClass ( 'is-active' ) ;
13
+ element . parent ( ) . addClass ( 'is-loading' ) ;
14
+ jQuery . ajax ( {
15
+ url : optimoleMediaListing . rest_url ,
16
+ type : 'POST' ,
17
+ headers : {
18
+ 'X-WP-Nonce' : optimoleMediaListing . nonce
19
+ } ,
20
+ data : {
21
+ action : action ,
22
+ status : is_retry ? 'check' : 'start' ,
23
+ id : id
24
+ } ,
25
+ success : function ( response ) {
26
+ if ( response . code === 'moved' ) {
27
+ element . parent ( ) . find ( '.spinner' ) . removeClass ( 'is-active' ) ;
28
+ element . parent ( ) . removeClass ( 'is-loading' ) ;
29
+ element . parent ( ) . find ( '.move-image-optml' ) . toggleClass ( 'hidden' ) ;
30
+
31
+ } else if ( response . code === 'error' ) {
32
+ element . parent ( ) . find ( '.spinner' ) . removeClass ( 'is-active' ) ;
33
+ element . parent ( ) . removeClass ( 'is-loading' ) ;
34
+ element . parent ( ) . text ( response . data ) ;
35
+ } else {
36
+ setTimeout ( function ( ) {
37
+ moveImage ( id , action , element , true ) ;
38
+ } , 1000 ) ;
39
+ }
40
+ }
41
+ } ) ;
42
+ }
0 commit comments