summaryrefslogtreecommitdiff
blob: 2bbaa7db10f9cd47972d1786069af2f53df65e9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* global ajaxurl */
jQuery( document ).ready( function($) {
	var update = function( cooldown ) {
		var self = $( '.jetpack_sync_reindex_control' ),
			data;

		self
			.find( '.jetpack_sync_reindex_control_action' )
			.attr( 'disabled', true );

		self
			.find( '.jetpack_sync_reindex_control_status' )
			.html( '…' );

		if ( 'DONE' === self.data( 'status' ) ) {
			data = { action:'jetpack-sync-reindex-trigger' };
		} else {
			data = { action:'jetpack-sync-reindex-status' };
		}

		$.getJSON(
			ajaxurl,
			data,
			function( response ) {
				var self = $( '.jetpack_sync_reindex_control' ),
					strings,
					status;

				if ( 0 === self.length ) {
					return;
				}

				strings = self.data( 'strings' );
				status = strings[response.status].status;

				if ( 'INDEXING' === response.status ) {
					status += ' (' + Math.floor( 100 * response.posts.imported / response.posts.total ) + '%)';
				}

				self
					.data( 'status', response.status );

				self
					.find( '.jetpack_sync_reindex_control_action' )
					.val( strings[response.status].action );

				self
					.find( '.jetpack_sync_reindex_control_status' )
					.text( status );

				setTimeout( function() {
					$( '.jetpack_sync_reindex_control' )
						.find( '.jetpack_sync_reindex_control_action' )
						.attr( 'disabled', false );
				}, cooldown );
			}
		);
	};

	$( '.jetpack_sync_reindex_control' )
		.find( '.jetpack_sync_reindex_control_action' )
		.live( 'click', function( event ) {
			event.preventDefault();
			update( 5000 );
		} );

	update( 1000 );
} );