-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathproject-version.js
178 lines (161 loc) · 6.06 KB
/
project-version.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
import { inject as service } from '@ember/service';
import Route from '@ember/routing/route';
import semverCompare from 'semver-compare';
import getCompactVersion from 'ember-api-docs/utils/get-compact-version';
import getFullVersion from 'ember-api-docs/utils/get-full-version';
import getLastVersion from 'ember-api-docs/utils/get-last-version';
import config from 'ember-api-docs/config/environment';
export default class ProjectVersionRoute extends Route {
@service
fastboot;
@service
headData;
@service
metaStore;
/** @type {import('@ember/routing/router-service').default} */
@service
router;
@service('project')
projectService;
titleToken(model) {
return model.version;
}
async model({ project, project_version }) {
let projectObj = await this.store.findRecord('project', project);
let projectVersion = getFullVersion(
project_version,
project,
projectObj,
this.metaStore
);
let id = `${project}-${projectVersion}`;
this.projectService.setUrlVersion(project_version);
this.projectService.setVersion(projectVersion);
return this.store.findRecord('project-version', id, {
includes: 'project',
});
}
afterModel(model) {
this.headData.modelVersion = model.version;
}
// Using redirect instead of afterModel so transition succeeds and returns 307
redirect(model, transition) {
const lookupParams = (routeName) => {
let route = transition.routeInfos.find(({ name }) => name === routeName);
return route && route.params;
};
this._gatherHeadDataFromVersion(
model,
lookupParams('project-version').project_version
);
let classParams = lookupParams('project-version.classes.class');
let moduleParams = lookupParams('project-version.modules.module');
let namespaceParams = lookupParams('project-version.namespaces.namespace');
let functionParams = lookupParams('project-version.functions.function');
let transitionVersion = this.projectService.getUrlVersion();
if (
moduleParams?.module === 'ember-data-overview' &&
semverCompare(transitionVersion, '4.7') < 0 &&
transitionVersion !== 'release' // infinite redirects happen without this line
) {
return this.router.transitionTo('project-version.index');
}
if (!classParams && !moduleParams && !namespaceParams && !functionParams) {
// if there is no class, module, or namespace specified...
let latestVersion = getLastVersion(
model.get('project.content').hasMany('projectVersions').ids()
);
let isLatestVersion =
transitionVersion === latestVersion || transitionVersion === 'release';
let shouldConvertPackages = semverCompare(model.version, '2.16') < 0;
if (!shouldConvertPackages || isLatestVersion) {
// ... and the transition version is the latest release,
// display the landing page at
// ember-data if @main declaration exists for ember-data-overview
let versionId = model.get('id');
let modules = model
.hasMany('modules')
.ids()
.map((id) => id.substring(versionId.length + 1));
if (
model.get('project.id') === 'ember-data' &&
modules.indexOf('ember-data-overview') !== -1
) {
return this.router.transitionTo(
'project-version.modules.module',
model.get('project.id'),
transitionVersion,
'ember-data-overview'
);
}
// ember / ember-cli / ember-data if no @main declaration exists for ember-data-overview
return this.router.transitionTo('project-version.index');
} else {
// else go to the version specified
let moduleRevs = this.metaStore.getEncodedModulesFromProjectRev(
model.get('id')
);
let module = this.getFirstModule(moduleRevs);
return this.router.transitionTo(
'project-version.modules.module',
model.get('project.id'),
transitionVersion,
module
);
}
}
}
_gatherHeadDataFromVersion(model, projectVersion) {
this.headData.isRelease = projectVersion === 'release';
this.headData.compactVersion = model.get('compactVersion');
this.headData.urlVersion = projectVersion;
if (!this.headData.isRelease) {
let request = this.fastboot.request;
let href = this.fastboot.isFastBoot
? `${config.APP.domain}/${request.path}`
: window.location.href;
let version = new RegExp(model.get('compactVersion'), 'g');
let canonicalUrl = href.replace(version, 'release');
this.headData.canonicalUrl = canonicalUrl;
}
}
_getEncodedNameForCurrentClass() {
// escape any reserved characters for url, like slashes
return encodeURIComponent(
this.modelFor('project-version.classes.class').get('name')
);
}
serialize(model) {
return {
project: model.get('project.id'),
project_version: model.get('compactVersion'),
};
}
// Input some version info, returns a boolean based on
// whether the user is switching versions for a 2.16 docs release or later.
// The urls for pre-2.16 classes and later packages are quite different
shouldConvertPackages(targetVer, previousVer) {
let targetVersion = getCompactVersion(targetVer.id);
let previousVersion = getCompactVersion(previousVer);
let previousComparison = semverCompare(previousVersion, '2.16');
let targetComparison = semverCompare(targetVersion, '2.16');
return (
(previousComparison < 0 && targetComparison >= 0) ||
(previousComparison >= 0 && targetComparison < 0)
);
}
/**
splits the first encoded revision string in the list and takes the string after the version (which is the encoded name), then decodes the result.
*/
getFirstModule(moduleRevs) {
let encodedModule = moduleRevs[0]
.split('-')
.reduce((result, val, index, arry) => {
if (val === this.projectService.version) {
return arry.slice(index + 1).join('-');
}
return result;
});
return decodeURIComponent(encodedModule);
}
}