summaryrefslogtreecommitdiff
blob: a8141684026db3a980e229aeb75434661379f9c3 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* global jetpack_post_by_email:true, ajaxurl, pbeVars */

( function( $ ) {
	var $pbeDisable,
		$pbeEmail,
		$pbeEmailWrapper,
		$pbeEnable,
		$pbeError,
		$pbeInfo,
		$pbeRegenerate,
		$pbeSpinner;

	jetpack_post_by_email = {
		init: function() {
			$pbeEnable.click( jetpack_post_by_email.enable );
			$pbeRegenerate.click( jetpack_post_by_email.regenerate );
			$pbeDisable.click( jetpack_post_by_email.disable );
		},

		enable: function() {
			$pbeEnable.attr( 'disabled', 'disabled' );
			$pbeError.fadeOut();
			$pbeSpinner.fadeIn();

			var data = {
				action: 'jetpack_post_by_email_enable',
				pbe_nonce: pbeVars.nonces.enable,
			};

			$.post( ajaxurl, data, jetpack_post_by_email.handle_enabled );
		},

		handle_enabled: function( response ) {
			$pbeRegenerate.removeAttr( 'disabled' );
			$pbeDisable.removeAttr( 'disabled' );

			if ( response.success ) {
				$pbeEnable.fadeOut( 400, function() {
					$pbeEnable.removeAttr( 'disabled' );
					$pbeEmail.val( response.data );
					$pbeInfo.fadeIn();
				} );
			} else {
				$pbeError.text( response.data );
				$pbeError.fadeIn();
				$pbeEnable.removeAttr( 'disabled' );
			}

			$pbeSpinner.fadeOut();
		},

		regenerate: function() {
			$pbeRegenerate.attr( 'disabled', 'disabled' );
			$pbeDisable.attr( 'disabled', 'disabled' );
			$pbeError.fadeOut();
			$pbeSpinner.fadeIn();

			var data = {
				action: 'jetpack_post_by_email_regenerate',
				pbe_nonce: pbeVars.nonces.regenerate,
			};

			$.post( ajaxurl, data, jetpack_post_by_email.handle_regenerated );
		},

		handle_regenerated: function( response ) {
			if ( response.success ) {
				$pbeEmailWrapper.fadeOut( 400, function() {
					$pbeEmail.val( response.data );
					$pbeEmailWrapper.fadeIn();
				} );
			} else {
				$pbeError.text( response.data );
				$pbeError.fadeIn();
			}

			$pbeRegenerate.removeAttr( 'disabled' );
			$pbeDisable.removeAttr( 'disabled' );
			$pbeSpinner.fadeOut();
		},

		disable: function() {
			$pbeRegenerate.attr( 'disabled', 'disabled' );
			$pbeDisable.attr( 'disabled', 'disabled' );
			$pbeError.fadeOut();
			$pbeSpinner.fadeIn();

			var data = {
				action: 'jetpack_post_by_email_disable',
				pbe_nonce: pbeVars.nonces.disable,
			};

			$.post( ajaxurl, data, jetpack_post_by_email.handle_disabled );
		},

		handle_disabled: function( response ) {
			if ( response.success ) {
				$pbeEnable.removeAttr( 'disabled' );
				$pbeInfo.fadeOut( 400, function() {
					$pbeRegenerate.removeAttr( 'disabled' );
					$pbeDisable.removeAttr( 'disabled' );
					$pbeEnable.fadeIn();
				} );
			} else {
				$pbeRegenerate.removeAttr( 'disabled' );
				$pbeDisable.removeAttr( 'disabled' );

				$pbeError.text( response.data );
				$pbeError.fadeIn();
			}

			$pbeSpinner.fadeOut();
		},
	};

	$( function() {
		$pbeDisable = $( '#jp-pbe-disable' );
		$pbeEmail = $( '#jp-pbe-email' );
		$pbeEmailWrapper = $( '#jp-pbe-email-wrapper' );
		$pbeEnable = $( '#jp-pbe-enable' );
		$pbeError = $( '#jp-pbe-error' );
		$pbeInfo = $( '#jp-pbe-info' );
		$pbeRegenerate = $( '#jp-pbe-regenerate' );
		$pbeSpinner = $( '#jp-pbe-spinner' );

		jetpack_post_by_email.init();
	} );
} )( jQuery );