Repository navigation
Expand file tree
/
Copy pathmain.jsx
More file actions
333 lines (303 loc) · 10.4 KB
/
Copy pathmain.jsx
File metadata and controls
333 lines (303 loc) · 10.4 KB
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
/**
* External dependencies
*/
import React from 'react';
import { connect } from 'react-redux';
import includes from 'lodash/includes';
import { createHistory } from 'history';
import { withRouter } from 'react-router';
import { translate as __ } from 'i18n-calypso';
/**
* Internal dependencies
*/
import Masthead from 'components/masthead';
import Navigation from 'components/navigation';
import NavigationSettings from 'components/navigation-settings';
import SearchableSettings from 'settings/index.jsx';
import JetpackConnect from 'components/jetpack-connect';
import JumpStart from 'components/jumpstart';
import { getJumpStartStatus, isJumpstarting } from 'state/jumpstart';
import { getSiteConnectionStatus, isCurrentUserLinked, isSiteConnected } from 'state/connection';
import {
setInitialState,
getSiteRawUrl,
getSiteAdminUrl,
getApiNonce,
getApiRootUrl,
userCanManageModules
} from 'state/initial-state';
import { areThereUnsavedSettings, clearUnsavedSettingsFlag, showWelcomeForNewPlan } from 'state/settings';
import { getSearchTerm } from 'state/search';
import AtAGlance from 'at-a-glance/index.jsx';
import Plans from 'plans/index.jsx';
import Footer from 'components/footer';
import SupportCard from 'components/support-card';
import AppsCard from 'components/apps-card';
import NonAdminView from 'components/non-admin-view';
import JetpackNotices from 'components/jetpack-notices';
import AdminNotices from 'components/admin-notices';
import Tracker from 'components/tracker';
import analytics from 'lib/analytics';
import restApi from 'rest-api';
import { getTracksUserData } from 'state/initial-state';
import WelcomeNewPlan from 'components/welcome-new-plan';
const Main = React.createClass( {
componentWillMount: function() {
this.props.setInitialState();
restApi.setApiRoot( this.props.apiRoot );
restApi.setApiNonce( this.props.apiNonce );
this.initializeAnalyitics();
// Handles refresh, closing and navigating away from Jetpack's Admin Page
window.addEventListener( 'beforeunload', this.onBeforeUnload );
// Handles transition between routes handled by react-router
this.props.router.listenBefore( this.routerWillLeave );
},
/*
* Returns a string if there are unsaved module settings thus showing a confirm dialog to the user
* according to the `beforeunload` event handling specification
*/
onBeforeUnload() {
if ( this.props.areThereUnsavedSettings ) {
if ( confirm( __( 'There are unsaved settings in this tab that will be lost if you leave it. Proceed?' ) ) ) {
this.props.clearUnsavedSettingsFlag();
} else {
return false;
}
}
},
/*
* Shows a confirmation dialog if there are unsaved module settings.
*
* Return true or false according to the history.listenBefore specification which is part of react-router
*/
routerWillLeave() {
if ( this.props.areThereUnsavedSettings ) {
if ( confirm( __( 'There are unsaved settings in this tab that will be lost if you leave it. Proceed?' ) ) ) {
window.setTimeout( this.props.clearUnsavedSettingsFlag, 10 );
} else {
return false;
}
}
},
initializeAnalyitics() {
const tracksUser = this.props.tracksUserData;
if ( tracksUser ) {
analytics.initialize(
tracksUser.userid,
tracksUser.username
);
}
},
shouldComponentUpdate: function( nextProps ) {
// If user triggers Skip to main content or Skip to toolbar with keyboard navigation, stay in the same tab.
if ( includes( [ '/wpbody-content', '/wp-toolbar' ], nextProps.route.path ) ) {
return false;
}
return nextProps.siteConnectionStatus !== this.props.siteConnectionStatus ||
nextProps.jumpStartStatus !== this.props.jumpStartStatus ||
nextProps.isLinked !== this.props.isLinked ||
nextProps.route.path !== this.props.route.path ||
nextProps.searchTerm !== this.props.searchTerm ||
nextProps.newPlanActivated !== this.props.newPlanActivated;
},
componentDidUpdate( prevProps ) {
// Not taking into account development mode here because changing the connection
// status without reloading is possible only by disconnecting a live site not
// in development mode.
if ( prevProps.siteConnectionStatus !== this.props.siteConnectionStatus ) {
const $items = jQuery( '#toplevel_page_jetpack' ).find( 'ul.wp-submenu li' );
$items.find( 'a[href$="#/settings"]' ).hide();
$items.find( 'a[href$="admin.php?page=stats"]' ).hide();
}
},
componentWillReceiveProps( nextProps ) {
if ( nextProps.jumpStartStatus !== this.props.jumpStartStatus ||
nextProps.isJumpstarting !== this.props.isJumpstarting ) {
this.handleJumpstart( nextProps );
}
},
/**
*
* Takes care of redirection when
* - jumpstarting ( resseting options )
* - the jumpstart is complete
* @param {Object} nextProps The next props as received by componentWillReceiveProps
*/
handleJumpstart( nextProps ) {
const history = createHistory();
const willShowJumpStart = nextProps.jumpStartStatus;
const willBeJumpstarting = nextProps.isJumpstarting;
if ( ! this.props.jumpStartStatus && willShowJumpStart ) {
window.location.hash = 'jumpstart';
history.push( window.location.pathname + '?page=jetpack#/jumpstart' );
}
if ( this.props.jumpStartStatus && ! willShowJumpStart && ! willBeJumpstarting ) {
window.location.hash = 'dashboard';
history.push( window.location.pathname + '?page=jetpack#/dashboard' );
}
},
renderMainContent: function( route ) {
// Track page views
this.props.isSiteConnected && analytics.tracks.recordEvent( 'jetpack_wpa_page_view', { path: route } );
if ( ! this.props.userCanManageModules ) {
if ( ! this.props.siteConnectionStatus ) {
return false;
}
return (
<div aria-live="assertive">
<NonAdminView { ...this.props } />
</div>
);
}
if ( ! this.props.siteConnectionStatus ) {
return (
<div aria-live="assertive">
<JetpackConnect />
</div>
);
}
if ( this.props.isSiteConnected && this.props.jumpStartStatus ) {
if ( includes( [ '/', '/dashboard' ], route ) ) {
window.location.hash = 'jumpstart';
const history = createHistory();
history.push( window.location.pathname + '?page=jetpack#/jumpstart' );
} else if ( '/jumpstart' === route ) {
return (
<div aria-live="assertive">
<JumpStart />
</div>
);
}
}
let pageComponent,
navComponent = <Navigation route={ this.props.route }/>,
settingsNav = <NavigationSettings route={ this.props.route } siteRawUrl={ this.props.siteRawUrl } siteAdminUrl={ this.props.siteAdminUrl } />;
switch ( route ) {
case '/dashboard':
pageComponent = <AtAGlance siteRawUrl={ this.props.siteRawUrl } siteAdminUrl={ this.props.siteAdminUrl } />;
break;
case '/plans':
pageComponent = <Plans siteRawUrl={ this.props.siteRawUrl } siteAdminUrl={ this.props.siteAdminUrl } />;
break;
case '/settings':
case '/general':
case '/engagement':
case '/security':
case '/traffic':
case '/discussion':
case '/writing':
case '/sharing':
navComponent = settingsNav;
pageComponent = <SearchableSettings
route={ this.props.route }
siteAdminUrl={ this.props.siteAdminUrl }
siteRawUrl={ this.props.siteRawUrl }
searchTerm={ this.props.searchTerm } />;
break;
default:
// If no route found, kick them to the dashboard and do some url/history trickery
const history = createHistory();
history.replace( window.location.pathname + '?page=jetpack#/dashboard' );
pageComponent = <AtAGlance siteRawUrl={ this.props.siteRawUrl } siteAdminUrl={ this.props.siteAdminUrl } />;
}
window.wpNavMenuClassChange();
return (
<div aria-live="assertive">
{ navComponent }
{ pageComponent }
<WelcomeNewPlan
siteRawUrl={ this.props.siteRawUrl }
newPlanActivated={ this.props.newPlanActivated }
userCanManageModules={ this.props.userCanManageModules }
/>
</div>
);
},
render: function() {
return (
<div>
<Masthead route={ this.props.route } />
<div className="jp-lower">
<AdminNotices />
<JetpackNotices />
{ this.renderMainContent( this.props.route.path ) }
{ ! this.props.jumpStartStatus && <SupportCard path={ this.props.route.path } /> }
{ ! this.props.jumpStartStatus && <AppsCard /> }
</div>
<Footer siteAdminUrl={ this.props.siteAdminUrl } />
<Tracker analytics={ analytics } />
</div>
);
}
} );
export default connect(
state => {
return {
jumpStartStatus: getJumpStartStatus( state ),
isJumpstarting: isJumpstarting( state ),
siteConnectionStatus: getSiteConnectionStatus( state ),
isLinked: isCurrentUserLinked( state ),
siteRawUrl: getSiteRawUrl( state ),
siteAdminUrl: getSiteAdminUrl( state ),
searchTerm: getSearchTerm( state ),
apiRoot: getApiRootUrl( state ),
apiNonce: getApiNonce( state ),
tracksUserData: getTracksUserData( state ),
areThereUnsavedSettings: areThereUnsavedSettings( state ),
userCanManageModules: userCanManageModules( state ),
isSiteConnected: isSiteConnected( state ),
newPlanActivated: showWelcomeForNewPlan( state ),
};
},
( dispatch ) => ( {
setInitialState: () => {
return dispatch( setInitialState() );
},
clearUnsavedSettingsFlag: () => {
return dispatch( clearUnsavedSettingsFlag() );
}
} )
)( withRouter( Main ) );
/**
* Hack for changing the sub-nav menu core classes for 'settings' and 'dashboard'
*/
window.wpNavMenuClassChange = function() {
let hash = window.location.hash;
const settingRoutes = [
'#/settings',
'#/general',
'#/discussion',
'#/security',
'#/traffic',
'#/writing',
'#/sharing'
],
dashboardRoutes = [
'#/',
'#/dashboard',
'#/plans'
];
// Clear currents
jQuery( '.current' ).each( function( i, obj ) {
jQuery( obj ).removeClass( 'current' );
} );
hash = hash.split( '?' )[0];
if ( includes( dashboardRoutes, hash ) ) {
let subNavItem = jQuery( '#toplevel_page_jetpack' ).find( 'li' ).filter( function( index ) {
return index === 1;
} );
subNavItem[0].classList.add( 'current' );
} else if ( includes( settingRoutes, hash ) ) {
let subNavItem = jQuery( '#toplevel_page_jetpack' ).find( 'li' ).filter( function( index ) {
return index === 2;
} );
subNavItem[0].classList.add( 'current' );
}
const $body = jQuery( 'body' );
$body.on( 'click', 'a[href$="#/dashboard"], a[href$="#/settings"], .jp-dash-section-header__settings[href="#/security"], .dops-button[href="#/plans"]', function() {
window.scrollTo( 0, 0 );
} );
$body.on( 'click', '.jetpack-js-stop-propagation', function( e ) {
e.stopPropagation();
} );
};