-
Notifications
You must be signed in to change notification settings - Fork 796
/
main.jsx
290 lines (262 loc) · 8.86 KB
/
main.jsx
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
/**
* External dependencies
*/
import React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from '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 JetpackConnect from 'components/jetpack-connect';
import JumpStart from 'components/jumpstart';
import { getJumpStartStatus, isJumpstarting } from 'state/jumpstart';
import { getSiteConnectionStatus } from 'state/connection';
import {
setInitialState,
getSiteRawUrl,
getSiteAdminUrl,
getApiNonce,
getApiRootUrl,
userCanManageModules
} from 'state/initial-state';
import { areThereUnsavedModuleOptions, clearUnsavedOptionFlag } from 'state/modules';
import AtAGlance from 'at-a-glance/index.jsx';
import Discussion from 'discussion/index.jsx';
import Security from 'security/index.jsx';
import Traffic from 'traffic/index.jsx';
import Writing from 'writing/index.jsx';
import Apps from 'apps/index.jsx';
import Plans from 'plans/index.jsx';
import Footer from 'components/footer';
import SupportCard from 'components/support-card';
import NonAdminView from 'components/non-admin-view';
import JetpackNotices from 'components/jetpack-notices';
import AdminNotices from 'components/admin-notices';
import SearchPage from 'search/index.jsx';
import analytics from 'lib/analytics';
import restApi from 'rest-api';
import { getTracksUserData } from 'state/initial-state';
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( e ) {
const dialogText = __( 'There are unsaved settings in this tab that will be lost if you leave it. Proceed?' );
if ( this.props.areThereUnsavedModuleOptions ) {
e.returnValue = dialogText;
return dialogText;
}
},
/*
* 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.areThereUnsavedModuleOptions ) {
const confirmLeave = confirm( __( 'There are unsaved settings in this tab that will be lost if you leave it. Proceed?' ) );
if ( confirmLeave ) {
this.props.clearUnsavedOptionFlag();
} else {
return false;
}
}
},
initializeAnalyitics() {
const tracksUser = this.props.tracksUserData;
if ( tracksUser ) {
analytics.initialize(
tracksUser.userid,
tracksUser.username
);
}
},
shouldComponentUpdate: function( nextProps ) {
return nextProps.siteConnectionStatus !== this.props.siteConnectionStatus ||
nextProps.jumpStartStatus !== this.props.jumpStartStatus ||
nextProps.route.path !== this.props.route.path;
},
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 ) {
history.push( window.location.pathname + '?page=jetpack#/dashboard' );
}
},
renderMainContent: function( route ) {
// Track page views
analytics.tracks.recordEvent( 'jetpack_wpa_page_view', { path: route } );
if ( ! this.props.userCanManageModules ) {
return <NonAdminView { ...this.props } />
}
if ( ! this.props.siteConnectionStatus ) {
return <JetpackConnect />
}
if ( this.props.jumpStartStatus ) {
if ( '/' === route ) {
const history = createHistory();
history.push( window.location.pathname + '?page=jetpack#/jumpstart' );
} else if ( '/jumpstart' === route ) {
return <JumpStart />
}
}
let pageComponent,
navComponent = <Navigation route={ this.props.route }/>;
switch ( route ) {
case '/dashboard':
pageComponent = <AtAGlance siteRawUrl={ this.props.siteRawUrl } siteAdminUrl={ this.props.siteAdminUrl } />;
break;
case '/apps':
pageComponent = <Apps siteRawUrl={ this.props.siteRawUrl } />;
break;
case '/plans':
pageComponent = <Plans siteRawUrl={ this.props.siteRawUrl } siteAdminUrl={ this.props.siteAdminUrl } />;
break;
case '/settings':
case '/writing':
navComponent = <NavigationSettings route={ this.props.route } />;
pageComponent = <Writing route={ this.props.route } siteAdminUrl={ this.props.siteAdminUrl } />;
break;
case '/discussion':
navComponent = <NavigationSettings route={ this.props.route } />;
pageComponent = <Discussion route={ this.props.route } />;
break;
case '/security':
navComponent = <NavigationSettings route={ this.props.route } />;
pageComponent = <Security route={ this.props.route } siteAdminUrl={ this.props.siteAdminUrl } />;
break;
case '/traffic':
navComponent = <NavigationSettings route={ this.props.route } />;
pageComponent = <Traffic route={ this.props.route } />;
break;
case '/search':
navComponent = <NavigationSettings route={ this.props.route } />;
pageComponent = <SearchPage siteAdminUrl={ this.props.siteAdminUrl } />;
break;
default:
pageComponent = <AtAGlance siteRawUrl={ this.props.siteRawUrl } siteAdminUrl={ this.props.siteAdminUrl } />;
}
window.wpNavMenuClassChange();
return (
<div>
{ navComponent }
{ pageComponent }
</div>
);
},
render: function() {
return (
<div>
<Masthead/>
<div className="jp-lower">
<AdminNotices />
<JetpackNotices />
{ this.renderMainContent( this.props.route.path ) }
{
this.props.jumpStartStatus || '/apps' === this.props.route.path ?
null :
<SupportCard />
}
</div>
<Footer siteAdminUrl={ this.props.siteAdminUrl } />
</div>
);
}
} );
export default connect(
state => {
return {
jumpStartStatus: getJumpStartStatus( state ),
isJumpstarting: isJumpstarting( state ),
siteConnectionStatus: getSiteConnectionStatus( state ),
siteRawUrl: getSiteRawUrl( state ),
siteAdminUrl: getSiteAdminUrl( state ),
apiRoot: getApiRootUrl( state ),
apiNonce: getApiNonce( state ),
tracksUserData: getTracksUserData( state ),
areThereUnsavedModuleOptions: areThereUnsavedModuleOptions( state ),
userCanManageModules: userCanManageModules( state )
};
},
dispatch => bindActionCreators( { setInitialState, clearUnsavedOptionFlag }, dispatch )
)( 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',
'#/search'
],
dashboardRoutes = [
'#/',
'#/dashboard',
'#/apps',
'#/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();
} );
};