-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathproject-version.js
More file actions
186 lines (157 loc) · 5.3 KB
/
Copy pathproject-version.js
File metadata and controls
186 lines (157 loc) · 5.3 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
import { action } from '@ember/object';
import { service } from '@ember/service';
import Controller from '@ember/controller';
import values from 'lodash.values';
import groupBy from 'lodash.groupby';
import semverCompare from 'semver-compare';
import getCompactVersion from '../utils/get-compact-version';
export default class ProjectVersionController extends Controller {
@service
filterData;
@service
metaStore;
@service
project;
@service
prerender;
@service router;
@service('project') projectService;
get showPrivateClasses() {
return this.filterData.showPrivateClasses;
}
get classesIDs() {
return this.getRelationshipIDs('classes');
}
get publicClassesIDs() {
return this.getRelationshipIDs('public-classes');
}
get namespaceIDs() {
return this.getRelationshipIDs('namespaces');
}
get publicNamespaceIDs() {
return this.getRelationshipIDs('public-namespaces');
}
get moduleIDs() {
return this.getModuleRelationships(this.model.id, 'modules');
}
get publicModuleIDs() {
return this.getModuleRelationships(this.model.id, 'public-modules');
}
getModuleRelationships(versionId, moduleType) {
let relations = this.getRelations(moduleType);
// filter overviews out. If other projects add their overview we should filter those too.
return relations
.map((id) => id.substring(versionId.length + 1))
.filter((id) => id !== 'ember-data-overview');
}
getRelations(relationship) {
return this.model.hasMany(relationship).ids().sort();
}
getRelationshipIDs(relationship) {
const projectId = this.model.project.id;
const classes = this.model.hasMany(relationship);
const sorted = Array.from(classes.ids()).sort();
//ids come in as ember-2.16.0-@ember/object/promise-proxy-mixin or ember-4.12.0-alpha.23-@ember/object/promise-proxy-mixin
//so we remove the project name and version (including pre-release) to get the class name
return sorted.map((id) => {
// Remove project name prefix
const withoutProject = id.replace(`${projectId}-`, '');
// Remove version (e.g., 2.16.0 or 4.12.0-alpha.23) and the following dash
return withoutProject.replace(/^\d+\.\d+\.\d+(?:-[a-z]+\.\d+)?-/i, '');
});
}
get shownClassesIDs() {
return this.showPrivateClasses ? this.classesIDs : this.publicClassesIDs;
}
get shownModuleIDs() {
return this.showPrivateClasses ? this.moduleIDs : this.publicModuleIDs;
}
get shownNamespaceIDs() {
return this.showPrivateClasses
? this.namespaceIDs
: this.publicNamespaceIDs;
}
get projectVersions() {
const id = this.model.belongsTo('project').id();
const projectVersions = this.metaStore.availableProjectVersions[id];
let versions = projectVersions.sort((a, b) => semverCompare(b, a));
versions = versions.map((version) => {
const compactVersion = getCompactVersion(version);
return { id: version, compactVersion };
});
let groupedVersions = groupBy(
versions,
(version) => version.compactVersion,
);
return values(groupedVersions).map((groupedVersion) => groupedVersion[0]);
}
get urlVersion() {
return this.projectService.getUrlVersion();
}
get selectedProjectVersion() {
return this.projectVersions.filter((pV) => pV.id === this.model.version)[0];
}
get activeProject() {
return this.model.project.id;
}
@action
togglePrivateClasses() {
this.filterData.togglePrivateClasses();
}
@action
updateProject(project, ver /*, component */) {
const currentURL = this.router.currentURL;
this.router.transitionTo(
findEndingRoute({
project,
targetVersion: ver.id,
currentVersion: this.projectService.version,
currentUrlVersion: this.projectService.getUrlVersion(),
currentURL,
currentAnchor: window.location.hash,
}),
);
}
}
export function findEndingRoute({
project,
targetVersion,
currentVersion,
currentUrlVersion,
currentURL,
currentAnchor,
}) {
let projectVersionID = getCompactVersion(targetVersion);
// if the user is navigating to/from api versions Ember >= 2.16 or Ember Data >= 4.0, take them
// to the home page instead of trying to translate the url
if (shouldGoToVersionIndex(project, targetVersion, currentVersion)) {
return `/${project}/${projectVersionID}`;
} else {
return `${currentURL.replace(currentUrlVersion, projectVersionID)}${currentAnchor}`;
}
}
function shouldGoToVersionIndex(project, targetVersion, currentVersion) {
let boundaryVersion;
if (project === 'ember') {
boundaryVersion = '2.16';
} else if (project === 'ember-data') {
boundaryVersion = '4.0';
}
return isCrossingVersionBoundary(
targetVersion,
currentVersion,
boundaryVersion,
);
}
// Input some version info, returns a boolean based on
// whether the user is switching versions for a release or later.
function isCrossingVersionBoundary(targetVer, previousVer, boundaryVersion) {
let targetVersion = getCompactVersion(targetVer);
let previousVersion = getCompactVersion(previousVer);
let previousComparison = semverCompare(previousVersion, boundaryVersion);
let targetComparison = semverCompare(targetVersion, boundaryVersion);
return (
(previousComparison < 0 && targetComparison >= 0) ||
(previousComparison >= 0 && targetComparison < 0)
);
}