summaryrefslogtreecommitdiff
blob: 72499bb222b9393e0ef8e615697d90653644d19d (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* global wp, jetpackL10n, jQuery */

(function( $, modules, currentVersion, jetpackL10n ) {

	///////////////////////////////////////
	// INIT
	///////////////////////////////////////

	var originPoint,
		data;

	$( document ).ready(function () {

		data = {
			'jumpstartModules'      : jetpackL10n.jumpstart_modules,
			'jumpstartModSlug'      : jetpackL10n.jumpstart_modules,
			'jumpstartNonce'        : jetpackL10n.activate_nonce,
			'jumpstartStatsURLS'    : jetpackL10n.jumpstart_stats_urls,
			'nuxAdminStatsURLS'     : jetpackL10n.admin_stats_urls,
			'showJumpstart'         : jetpackL10n.show_jumpstart,
			'adminNonce'            : jetpackL10n.admin_nonce
		};

		initEvents();
		loadModules( 'Performance-Security', 'mod-nux', '#nux-performance-security' );
		loadModules( 'Traffic', 'mod-nux', '#nux-traffic' );
		if('1' === data.showJumpstart) {
			loadModules( 'Jumpstart', 'mod-jumpstart', '#jp-config-list' );
		}

		/*
		We are listening to see if we need to refresh the data.
		We'd need to refresh the data only if the page is navigated to
		via the back or forward browser buttons.  We do this so the
		browser cache isn't out of sync with the real data generated by the
		AJAX event.
		 */
		onload = function() {
			if ( window.location.hash.substr( '#refresh' ) ) {
				refreshData();
			}
		};

		jumpStartAJAX();
		adminAJAX();
	});

	///////////////////////////////////////
	// FUNCTIONS
	///////////////////////////////////////

	function closeShadeToggle() {
		// Clicking outside modal, or close X closes modal
		$( '.shade, .modal .close' ).on( 'click', function () {
			$( '.shade, .modal' ).hide();
			$( '.manage-right' ).removeClass( 'show' );
			originPoint.focus();
			$( '.modal' )[0].removeAttribute( 'tabindex' );
			$( 'body' ).css( 'overflow', 'auto' );
			return false;
		});

		$( window ).on( 'keydown', function( e ) {
			// If pressing ESC close the modal
			if ( 27 === e.keyCode ) {
				$( '.shade, .modal' ).hide();
				$( '.manage-right' ).removeClass( 'show' );
				originPoint.focus();
				$( '.modal' )[0].removeAttribute( 'tabindex' );
				$( 'body' ).css( 'overflow', 'auto' );
			}
		});
	}

	function initEvents () {
		// Only show module table if Jumpstart isn't there
		if ( ! data.showJumpstart ) {
			$( '.nux-intro' ).show();
		}

		// Show preconfigured list of features to enable via "Jump-start"
		$( '.jp-config-list-btn' ).click(function(){
			$( '#jp-config-list' ).toggle();
			recalculateModuleHeights();

			//Log Jump Start event "learn more" in MC Stats
			new Image().src = data.jumpstartStatsURLS.learnmore;
		});

		// Hide the successful connection message after a little bit
		setTimeout( function(){
			jQuery( '.jetpack-message:not( .stay-visible, .jetpack-err )' ).hide( 600 );
		}, 6000);

		// Modal events
		$( document ).ready(function () {
			initModalEvents();
		});

		// Debounce the resize event
		var pauseResize = false;
		window.onresize = function() {
			if ( !pauseResize ) {
				pauseResize = true;
				recalculateModuleHeights();
				setTimeout(function () {
					pauseResize = false;
				}, 100 );
			}
		};

		// This function will track the number of clicks on the "See the other X Jetpack features"
		$( '.full-features-btn' ).click( function() {
			new Image().src = data.nuxAdminStatsURLS.learnmore+'-full-features-btn';
		});
	}

	function initModalEvents() {
		var $modal = $( '.modal' );
		$( '.module h3, .feature a, .configs a, .more-info, .feat h4' ).on( 'click keypress', function (e) {
			// Only show modal on enter when keypress recorded (accessibility)
			if ( e.keyCode && 13 !== e.keyCode ) {
				return;
			}

			e.preventDefault();

			$( '.shade' ).show();

			// Show loading message on init
			$modal.html( wp.template( 'modalLoading' )( {} ) ).fadeIn();
			// Load & populate with content
			var $this = $( this ).parent(),
				index = $this.data( 'index' ),
				name = $this.data( 'name' );
			
			$modal.empty().html( wp.template( 'modal' )( $.extend( modules[index], { name: name } ) ) );
			// Save the focused element, then shift focus to the modal window.
			originPoint = this;
			$modal[0].setAttribute( 'tabindex', '0' );
			$modal.focus();

			// Disallow scroll
			$( 'body' ).css( 'overflow', 'hidden' );

			closeShadeToggle();

			// Modal header links
			$( '.modal header li a.title' ).on( 'click', function () {
				$( '.modal header li a.title' ).removeClass( 'active' );
				$( this ).addClass( 'active' );
				return false;
			});
		});
	}


	/*
	Load Modules for a template
	@param string: The module tag you'd like to filter by
	@param string: The template name
	@param string: The target element to display the template
	 */
	function loadModules( prop, template, location ) {
		// Mapping prior to sorting improves performance by over 50%
		var html = '',
			result = [],
			val = '',
			i,
			length,
			renderingmodules = [];

		// create the map
		for ( i = 0, length = modules.length; i < length; i++ ) {
			if( modules[i].feature.indexOf(prop) !== -1 ) {
				val = modules[i].name.toLowerCase();
				result.push( {
					index: i,
					value: val,
					order: modules[i].recommendation_order
				});
			}
		}

		// Sort modules by recommendation order
		result.sort(function( a, b ) {
			if (a.order === b.order ) {
				return 0;
			}
			return ( a.order < b.order ) ? -1 : 1;
		});

		// copy values in right order
		for ( i = 0, length = result.length; i < length; i++ ) {
			renderingmodules.push( modules[result[i].index] );
			renderingmodules[i].index =  result[i].index; // make sure we set the index to the right order*/
		}


		// Render modules.  Don't show active in Jumpstart.
		for ( i = 0; i < renderingmodules.length; i++ ) {
			if ( 'Jumpstart' === prop && ! renderingmodules[i].activated ) {
				html += wp.template( template )( renderingmodules[i] );
			} else if ( 'Jumpstart' !== prop )  {
				html += wp.template( template )( renderingmodules[i] );
			}
		}

		$( location ).append( html );

		// track Jump Start views
		if('Jumpstart' === prop) {
			new Image().src = data.jumpstartStatsURLS.viewed;
		}

		recalculateModuleHeights();
		initModalEvents();
	}

	function recalculateModuleHeights () {
		// Resize module heights based on screen resolution
		var module = $( '.jp-jumpstart, .module, .jp-support-column-left .widget-text' ),
			tallest = 0,
			thisHeight;

		// Remove heights
		module.css( 'height', 'auto' );

		// Determine new height
		module.each(function() {

			thisHeight = $( this ).outerHeight();

			if ( thisHeight > tallest ) {
				tallest = thisHeight;
			}
		});

		// Apply new height plus 20 pixels
		module.css( 'height', ( parseInt( tallest, 10 ) + 5 ) + 'px' );
	}

	/*
	Handles the jump start ajax requests.

	Dismissing the Jump Start area will set an option, so it will never be seen again
	Initiating Jump Start will activate all modules that are recommended and set a sharing options while doing so.
	For either request, if update_option has failed, look for an error in the console.
	@todo delete the "reset everything" call - meant for testing only.
	 */
	function jumpStartAJAX() {

		// Will dismiss the Jump Start area, and set wp option in callback
		$( '.dismiss-jumpstart' ).click(function(){
			$( '#jump-start-area' ).hide( 600 );

			data.disableJumpStart = true;
			data.action = 'jetpack_jumpstart_ajax';

			$.post( jetpackL10n.ajaxurl, data, function (response) {
				// If there's no response, something bad happened
				if ( ! response ) {
					//console.log( 'Option "jetpack_dismiss_jumpstart" not updated.' );
				}

				window.location.hash = 'refresh';
			});

			$( '.nux-intro' ).show();

			// Log Jump Start event in MC Stats
			new Image().src = data.jumpstartStatsURLS.dismiss;

			return false;
		});

		// Activate all Jump-start modules
		$( '#jump-start' ).click(function () {
			var module, dataName, configURL, checkBox;

			$( '.jumpstart-spinner' ).show().css( 'display', 'block' );
			$( '#jump-start' ).hide();
			$( '.dismiss-jumpstart' ).hide();

			data.jumpStartActivate = 'jump-start-activate';
			data.action = 'jetpack_jumpstart_ajax';

			$( '#jp-config-list' ).hide();

			$.post( jetpackL10n.ajaxurl, data, function (response) {
				// If there's no response, option 'sharing-services' was not updated.
				if ( ! response ) {
					//console.log( 'Option "sharing-services" not updated. Either you already had sharing buttons enabled, or something is broken.' );
				}

				module = data.jumpstartModules;

				// Only target Jump Start modules
				_.each( module, function( mod ) {
					dataName = $( 'label[for="active-' + mod.module_slug + '"]' + '.plugin-action__label' );
					configURL = mod.configure_url;
					checkBox = $( 'input[id="active-' + mod.module_slug + '"]' );
					
					$( '#toggle-' + mod.module_slug ).addClass( 'activated' );
					dataName.html( 'ACTIVE' );
					$( checkBox ).prop( 'checked', true );
				});

				$( '.jumpstart-spinner, .jstart, #jumpstart-cta' ).hide();
				$( '.jumpstart-message' ).toggle();
				$( '#jump-start-area' ).delay( 5000 ).hide( 600 );

				// Log Jump Start event in MC Stats
				new Image().src = data.jumpstartStatsURLS.jumpstarted;

				$( '.nux-intro' ).show();

				window.location.hash = 'refresh';
			});

			return false;
		});

		/*
			RESET EVERYTHING (for testing only)
			@todo remove
		 */

		$( '#jump-start-deactivate' ).click(function () {
			$( '.jumpstart-spinner' ).show();

			data.jumpStartDeactivate = 'jump-start-deactivate';
			data.action = 'jetpack_jumpstart_ajax';

			$.post( jetpackL10n.ajaxurl, data, function ( response ) {
				//$('#jumpstart-cta').html(response);
				$( '#deactivate-success' ).html( response );
				$( '.jumpstart-spinner' ).hide();
				window.location.hash = '';

			});

			return false;
		});
	}

	/*
	Handles the module activation ajax actions
	 */
	function adminAJAX() {
		$( '.nux-in' ).on( 'keypress click', '.form-toggle', function( e ){
			if ( e.which !== 13 && e.type !== 'click' ) {
				return;
			}
			var thisElementId = e.target.id,
				thisLabel = $( 'label[for="' + thisElementId + '"]' + '.plugin-action__label'),
				index;

			data.action         = 'jetpack_admin_ajax';
			data.thisModuleSlug = thisElementId.replace( 'active-', '' );
			data.toggleModule   = 'nux-toggle-module';

			index = $( '#toggle-' + data.thisModuleSlug ).data( 'index' );

			thisLabel.hide();
			$( '.module-spinner-' + data.thisModuleSlug ).show();

			$.post( jetpackL10n.ajaxurl, data, function ( response ) {
				if ( 0 !== response ) {

					// Log NUX Admin event in MC Stats
					if( true === response.activated ){
						new Image().src = data.nuxAdminStatsURLS.enabled+','+'enabled-'+data.thisModuleSlug;
					}else{
						new Image().src = data.nuxAdminStatsURLS.deactivated+','+'deactivated-'+data.thisModuleSlug;
					}

					$( '.module-spinner-' + response.module ).hide();

					// This is a hacky way around not showing the config link when activated.
					response.noConfig = _.indexOf( [ 'photon', 'enhanced-distribution' ], response.module );

					// Preserves the modal index so it can be rendered properly after the data has changed
					response.index = index;

					$( '#toggle-' + response.module ).replaceWith( wp.template( 'mod-nux' )( response ) );

					// Refreshes the modal element data
					_.extend( _.findWhere( modules, { module: response.module } ), response );

					// Manual element alteration for Manage, since it's not part of the template
					if ( 'manage' === data.thisModuleSlug ) {
						if ( response.activated ) {
							$( '#manage-row .module-action' ).hide();
							$( '#manage-row' ).addClass( 'activated' );
						} else {
							thisLabel.show().html( 'INACTIVE' );
							$( '#manage-row' ).removeClass( 'activated' );
						}

						$( '.manage-cta-inactive' ).toggle();
						$( '.manage-cta-active' ).toggle();
						return;
					}

					initModalEvents();
					window.location.hash = 'refresh';
				}

			}, 'json' );
		});
	}

	/*
	This function will refresh any data elements that we've
	changed via AJAX.  Necessary when page is visited via back/forward
	browsing.
	 */
	function refreshData() {
		// Clean up
		$( '#nux-performance-security, #nux-traffic' ).empty();
		$( '#jump-start-area' ).hide();
		$( '.nux-intro' ).show();

		data.action      = 'jetpack_admin_ajax_refresh';
		data.refreshData = 'refresh';
		$.post( jetpackL10n.ajaxurl, data, function ( response ) {
			modules = _.values( response );
			loadModules( 'Performance-Security', 'mod-nux', '#nux-performance-security' );
			loadModules( 'Traffic', 'mod-nux', '#nux-traffic' );
		}, 'json' );
	}
})( jQuery, jetpackL10n.modules, jetpackL10n.currentVersion, jetpackL10n );