File size: 4,465 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import page from '@automattic/calypso-router';
import { debounce } from 'lodash';
import { stringify } from 'qs';
import { createElement } from 'react';
import noSitesIllustration from 'calypso/assets/images/illustrations/illustration-nosites.svg';
import EmptyContent from 'calypso/components/empty-content';
import { login } from 'calypso/lib/paths';
import { isUserLoggedIn } from 'calypso/state/current-user/selectors';
// This is a custom AsyncLoad component for devdocs that includes a
// `props.component`-aware placeholder. It still needs to be imported as
// `AsyncLoad` though–see https://github.com/Automattic/babel-plugin-transform-wpcalypso-async/blob/HEAD/index.js#L12
import { setSelectedSiteId } from 'calypso/state/ui/actions';
import AsyncLoad from './devdocs-async-load';
import SingleDocComponent from './doc';
import DocsComponent from './main';
import Sidebar from './sidebar';
import DevWelcome from './welcome';
import WizardComponent from './wizard-component';

const devdocs = {
	/*
	 * Documentation is rendered on #primary and doesn't expect a sidebar to exist
	 * so #secondary needs to be cleaned up
	 */
	sidebar: function ( context, next ) {
		context.secondary = createElement( Sidebar, {
			path: context.path,
		} );

		context.store.dispatch( setSelectedSiteId( null ) );
		next();
	},

	/*
	 * Controller for page listing multiple developer docs
	 */
	devdocs: function ( context, next ) {
		function onSearchChange( searchTerm ) {
			const query = context.query;

			if ( searchTerm ) {
				query.term = searchTerm;
			} else {
				delete query.term;
			}

			const queryString = stringify( query ).replace( /%20/g, '+' ).trim();

			let newUrl = context.pathname;

			if ( queryString ) {
				newUrl += '?' + queryString;
			}

			page.replace( newUrl, context.state, false, false );
		}

		context.primary = createElement( DocsComponent, {
			term: context.query.term,
			// we debounce with wait time of 0, so that the search doesn’t happen
			// in the same tick as the keyUp event and possibly cause typing lag
			onSearchChange: debounce( onSearchChange, 0 ),
		} );
		next();
	},

	/*
	 * Controller for single developer document
	 */
	singleDoc: function ( context, next ) {
		context.primary = createElement( SingleDocComponent, {
			path: context.params.path,
			term: context.query.term,
			sectionId: Object.keys( context.hash )[ 0 ],
		} );
		next();
	},

	// UI components
	design: function ( context, next ) {
		context.primary = <AsyncLoad component={ context.params.component } require="./design" />;
		next();
	},

	wizard: function ( context, next ) {
		context.primary = <WizardComponent stepName={ context.params.stepName } />;
		next();
	},

	// App Blocks
	blocks: function ( context, next ) {
		context.primary = (
			<AsyncLoad component={ context.params.component } require="./design/blocks" />
		);
		next();
	},

	playground: function ( context, next ) {
		context.primary = (
			<AsyncLoad component={ context.params.component } require="./design/playground" />
		);
		next();
	},

	wpComponentsGallery( context, next ) {
		context.primary = <AsyncLoad require="./design/wordpress-components-gallery" />;
		next();
	},

	selectors: function ( context, next ) {
		context.primary = (
			<AsyncLoad
				require="./docs-selectors"
				search={ context.query.search }
				selector={ context.params.selector }
			/>
		);
		next();
	},

	typography: function ( context, next ) {
		context.primary = (
			<AsyncLoad component={ context.params.component } require="./design/typography" />
		);
		next();
	},

	illustrations: function ( context, next ) {
		context.primary = (
			<AsyncLoad component={ context.params.component } require="./design/illustrations" />
		);
		next();
	},

	pleaseLogIn: function ( context, next ) {
		const redirectTo = window.location.origin + '/devdocs/welcome';
		if ( ! isUserLoggedIn( context.store.getState() ) ) {
			context.primary = createElement( EmptyContent, {
				title: 'Log In to start hacking',
				line: 'Required to access the WordPress.com API',
				action: 'Log In to WordPress.com',
				actionURL: login( { redirectTo } ),
				secondaryAction: 'Register',
				secondaryActionURL: '/start/account',
				illustration: noSitesIllustration,
			} );
			next();
		} else {
			page( redirectTo );
		}
	},

	// Welcome screen
	welcome: function ( context, next ) {
		context.primary = createElement( DevWelcome, {} );
		next();
	},
};

export default devdocs;