This repository has been archived by the owner on Sep 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
load.js
408 lines (335 loc) · 14.4 KB
/
load.js
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
( function() {
'use strict';
var Endpoint, initializedDeferreds = {},
wpApiSettings = window.wpApiSettings || {};
window.wp = window.wp || {};
wp.api = wp.api || {};
// If wpApiSettings is unavailable, try the default.
if ( _.isEmpty( wpApiSettings ) ) {
wpApiSettings.root = window.location.origin + '/wp-json/';
}
Endpoint = Backbone.Model.extend( {
defaults: {
apiRoot: wpApiSettings.root,
versionString: wp.api.versionString,
schema: null,
models: {},
collections: {}
},
/**
* Initialize the Endpoint model.
*/
initialize: function() {
var model = this, deferred;
Backbone.Model.prototype.initialize.apply( model, arguments );
deferred = jQuery.Deferred();
model.schemaConstructed = deferred.promise();
model.schemaModel = new wp.api.models.Schema( null, {
apiRoot: model.get( 'apiRoot' ),
versionString: model.get( 'versionString' )
} );
// When the model loads, resolve the promise.
model.schemaModel.once( 'change', function() {
model.constructFromSchema();
deferred.resolve( model );
} );
if ( model.get( 'schema' ) ) {
// Use schema supplied as model attribute.
model.schemaModel.set( model.schemaModel.parse( model.get( 'schema' ) ) );
} else if (
! _.isUndefined( sessionStorage ) &&
( _.isUndefined( wpApiSettings.cacheSchema ) || wpApiSettings.cacheSchema ) &&
sessionStorage.getItem( 'wp-api-schema-model' + model.get( 'apiRoot' ) + model.get( 'versionString' ) )
) {
// Used a cached copy of the schema model if available.
model.schemaModel.set( model.schemaModel.parse( JSON.parse( sessionStorage.getItem( 'wp-api-schema-model' + model.get( 'apiRoot' ) + model.get( 'versionString' ) ) ) ) );
} else {
model.schemaModel.fetch( {
/**
* When the server returns the schema model data, store the data in a sessionCache so we don't
* have to retrieve it again for this session. Then, construct the models and collections based
* on the schema model data.
*/
success: function( newSchemaModel ) {
// Store a copy of the schema model in the session cache if available.
if ( ! _.isUndefined( sessionStorage ) && ( _.isUndefined( wpApiSettings.cacheSchema ) || wpApiSettings.cacheSchema ) ) {
try {
sessionStorage.setItem( 'wp-api-schema-model' + model.get( 'apiRoot' ) + model.get( 'versionString' ), JSON.stringify( newSchemaModel ) );
} catch ( error ) {
// Fail silently, fixes errors in safari private mode.
}
}
},
// Log the error condition.
error: function( err ) {
window.console.log( err );
}
} );
}
},
constructFromSchema: function() {
var routeModel = this, modelRoutes, collectionRoutes, schemaRoot, loadingObjects,
/**
* Set up the model and collection name mapping options. As the schema is built, the
* model and collection names will be adjusted if they are found in the mapping object.
*
* Localizing a variable wpApiSettings.mapping will over-ride the default mapping options.
*
*/
mapping = wpApiSettings.mapping || {
models: {
'Categories': 'Category',
'Comments': 'Comment',
'Pages': 'Page',
'PagesMeta': 'PageMeta',
'PagesRevisions': 'PageRevision',
'Posts': 'Post',
'PostsCategories': 'PostCategory',
'PostsRevisions': 'PostRevision',
'PostsTags': 'PostTag',
'Schema': 'Schema',
'Statuses': 'Status',
'Tags': 'Tag',
'Taxonomies': 'Taxonomy',
'Types': 'Type',
'Users': 'User'
},
collections: {
'PagesMeta': 'PageMeta',
'PagesRevisions': 'PageRevisions',
'PostsCategories': 'PostCategories',
'PostsMeta': 'PostMeta',
'PostsRevisions': 'PostRevisions',
'PostsTags': 'PostTags'
}
};
/**
* Iterate thru the routes, picking up models and collections to build. Builds two arrays,
* one for models and one for collections.
*/
modelRoutes = [];
collectionRoutes = [];
schemaRoot = routeModel.get( 'apiRoot' ).replace( wp.api.utils.getRootUrl(), '' );
loadingObjects = {};
/**
* Tracking objects for models and collections.
*/
loadingObjects.models = {};
loadingObjects.collections = {};
_.each( routeModel.schemaModel.get( 'routes' ), function( route, index ) {
// Skip the schema root if included in the schema.
if ( index !== routeModel.get( ' versionString' ) &&
index !== schemaRoot &&
index !== ( '/' + routeModel.get( 'versionString' ).slice( 0, -1 ) )
) {
// Single items end with a regex (or the special case 'me').
if ( /(?:.*[+)]|\/me)$/.test( index ) ) {
modelRoutes.push( { index: index, route: route } );
} else {
// Collections end in a name.
collectionRoutes.push( { index: index, route: route } );
}
}
} );
/**
* Construct the models.
*
* Base the class name on the route endpoint.
*/
_.each( modelRoutes, function( modelRoute ) {
// Extract the name and any parent from the route.
var modelClassName,
routeName = wp.api.utils.extractRoutePart( modelRoute.index, 2, routeModel.get( 'versionString' ), true ),
parentName = wp.api.utils.extractRoutePart( modelRoute.index, 1, routeModel.get( 'versionString' ), false ),
routeEnd = wp.api.utils.extractRoutePart( modelRoute.index, 1, routeModel.get( 'versionString' ), true );
// Clear the parent part of the rouite if its actually the version string.
if ( parentName === routeModel.get( 'versionString' ) ) {
parentName = '';
}
// Handle the special case of the 'me' route.
if ( 'me' === routeEnd ) {
routeName = 'me';
}
// If the model has a parent in its route, add that to its class name.
if ( '' !== parentName && parentName !== routeName ) {
modelClassName = wp.api.utils.capitalizeAndCamelCaseDashes( parentName ) + wp.api.utils.capitalizeAndCamelCaseDashes( routeName );
modelClassName = mapping.models[ modelClassName ] || modelClassName;
loadingObjects.models[ modelClassName ] = wp.api.WPApiBaseModel.extend( {
// Return a constructed url based on the parent and id.
url: function() {
var url =
routeModel.get( 'apiRoot' ) +
routeModel.get( 'versionString' ) +
parentName + '/' +
( ( _.isUndefined( this.get( 'parent' ) ) || 0 === this.get( 'parent' ) ) ?
( _.isUndefined( this.get( 'parent_post' ) ) ? '' : this.get( 'parent_post' ) + '/' ) :
this.get( 'parent' ) + '/' ) +
routeName;
if ( ! _.isUndefined( this.get( 'id' ) ) ) {
url += '/' + this.get( 'id' );
}
return url;
},
// Include a reference to the original route object.
route: modelRoute,
// Include a reference to the original class name.
name: modelClassName,
// Include the array of route methods for easy reference.
methods: modelRoute.route.methods,
initialize: function( attributes, options ) {
wp.api.WPApiBaseModel.prototype.initialize.call( this, attributes, options );
/**
* Posts and pages support trashing, other types don't support a trash
* and require that you pass ?force=true to actually delete them.
*
* @todo we should be getting trashability from the Schema, not hard coding types here.
*/
if (
'Posts' !== this.name &&
'Pages' !== this.name &&
_.includes( this.methods, 'DELETE' )
) {
this.requireForceForDelete = true;
}
}
} );
} else {
// This is a model without a parent in its route
modelClassName = wp.api.utils.capitalizeAndCamelCaseDashes( routeName );
modelClassName = mapping.models[ modelClassName ] || modelClassName;
loadingObjects.models[ modelClassName ] = wp.api.WPApiBaseModel.extend( {
// Function that returns a constructed url based on the id.
url: function() {
var url = routeModel.get( 'apiRoot' ) +
routeModel.get( 'versionString' ) +
( ( 'me' === routeName ) ? 'users/me' : routeName );
if ( ! _.isUndefined( this.get( 'id' ) ) ) {
url += '/' + this.get( 'id' );
}
return url;
},
// Include a reference to the original route object.
route: modelRoute,
// Include a reference to the original class name.
name: modelClassName,
// Include the array of route methods for easy reference.
methods: modelRoute.route.methods
} );
}
// Add defaults to the new model, pulled form the endpoint.
wp.api.utils.decorateFromRoute(
modelRoute.route.endpoints,
loadingObjects.models[ modelClassName ],
routeModel.get( 'versionString' )
);
} );
/**
* Construct the collections.
*
* Base the class name on the route endpoint.
*/
_.each( collectionRoutes, function( collectionRoute ) {
// Extract the name and any parent from the route.
var collectionClassName, modelClassName,
routeName = collectionRoute.index.slice( collectionRoute.index.lastIndexOf( '/' ) + 1 ),
parentName = wp.api.utils.extractRoutePart( collectionRoute.index, 1, routeModel.get( 'versionString' ), false );
// If the collection has a parent in its route, add that to its class name.
if ( '' !== parentName && parentName !== routeName && routeModel.get( 'versionString' ) !== parentName ) {
collectionClassName = wp.api.utils.capitalizeAndCamelCaseDashes( parentName ) + wp.api.utils.capitalizeAndCamelCaseDashes( routeName );
modelClassName = mapping.models[ collectionClassName ] || collectionClassName;
collectionClassName = mapping.collections[ collectionClassName ] || collectionClassName;
loadingObjects.collections[ collectionClassName ] = wp.api.WPApiBaseCollection.extend( {
// Function that returns a constructed url passed on the parent.
url: function() {
return routeModel.get( 'apiRoot' ) + routeModel.get( 'versionString' ) +
parentName + '/' + this.parent + '/' +
routeName;
},
// Specify the model that this collection contains.
model: function( attrs, options ) {
return new loadingObjects.models[ modelClassName ]( attrs, options );
},
// Include a reference to the original class name.
name: collectionClassName,
// Include a reference to the original route object.
route: collectionRoute,
// Include the array of route methods for easy reference.
methods: collectionRoute.route.methods
} );
} else {
// This is a collection without a parent in its route.
collectionClassName = wp.api.utils.capitalizeAndCamelCaseDashes( routeName );
modelClassName = mapping.models[ collectionClassName ] || collectionClassName;
collectionClassName = mapping.collections[ collectionClassName ] || collectionClassName;
loadingObjects.collections[ collectionClassName ] = wp.api.WPApiBaseCollection.extend( {
// For the url of a root level collection, use a string.
url: function() {
return routeModel.get( 'apiRoot' ) + routeModel.get( 'versionString' ) + routeName;
},
// Specify the model that this collection contains.
model: function( attrs, options ) {
return new loadingObjects.models[ modelClassName ]( attrs, options );
},
// Include a reference to the original class name.
name: collectionClassName,
// Include a reference to the original route object.
route: collectionRoute,
// Include the array of route methods for easy reference.
methods: collectionRoute.route.methods
} );
}
// Add defaults to the new model, pulled form the endpoint.
wp.api.utils.decorateFromRoute( collectionRoute.route.endpoints, loadingObjects.collections[ collectionClassName ] );
} );
// Add mixins and helpers for each of the models.
_.each( loadingObjects.models, function( model, index ) {
loadingObjects.models[ index ] = wp.api.utils.addMixinsAndHelpers( model, index, loadingObjects );
} );
// Set the routeModel models and collections.
routeModel.set( 'models', loadingObjects.models );
routeModel.set( 'collections', loadingObjects.collections );
}
} );
wp.api.endpoints = new Backbone.Collection();
/**
* Initialize the wp-api, optionally passing the API root.
*
* @param {object} [args]
* @param {string} [args.apiRoot] The api root. Optional, defaults to wpApiSettings.root.
* @param {string} [args.versionString] The version string. Optional, defaults to wpApiSettings.root.
* @param {object} [args.schema] The schema. Optional, will be fetched from API if not provided.
*/
wp.api.init = function( args ) {
var endpoint, attributes = {}, deferred, promise;
args = args || {};
attributes.apiRoot = args.apiRoot || wpApiSettings.root || '/wp-json';
attributes.versionString = args.versionString || wpApiSettings.versionString || 'wp/v2/';
attributes.schema = args.schema || null;
if ( ! attributes.schema && attributes.apiRoot === wpApiSettings.root && attributes.versionString === wpApiSettings.versionString ) {
attributes.schema = wpApiSettings.schema;
}
if ( ! initializedDeferreds[ attributes.apiRoot + attributes.versionString ] ) {
// Look for an existing copy of this endpoint
endpoint = wp.api.endpoints.findWhere( { 'apiRoot': attributes.apiRoot, 'versionString': attributes.versionString } );
if ( ! endpoint ) {
endpoint = new Endpoint( attributes );
}
deferred = jQuery.Deferred();
promise = deferred.promise();
endpoint.schemaConstructed.done( function( resolvedEndpoint ) {
wp.api.endpoints.add( resolvedEndpoint );
// Map the default endpoints, extending any already present items (including Schema model).
wp.api.models = _.extend( wp.api.models, resolvedEndpoint.get( 'models' ) );
wp.api.collections = _.extend( wp.api.collections, resolvedEndpoint.get( 'collections' ) );
deferred.resolve( resolvedEndpoint );
} );
initializedDeferreds[ attributes.apiRoot + attributes.versionString ] = promise;
}
return initializedDeferreds[ attributes.apiRoot + attributes.versionString ];
};
/**
* Construct the default endpoints and add to an endpoints collection.
*/
// The wp.api.init function returns a promise that will resolve with the endpoint once it is ready.
wp.api.loadPromise = wp.api.init();
} )();