-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathclass.js
124 lines (108 loc) · 3.22 KB
/
class.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
import { inject as service } from '@ember/service';
import { resolve, all } from 'rsvp';
import Route from '@ember/routing/route';
import { set } from '@ember/object';
import ScrollTracker from 'ember-api-docs/mixins/scroll-tracker';
import { pluralize } from 'ember-inflector';
import getFullVersion from 'ember-api-docs/utils/get-full-version';
import createExcerpt from 'ember-api-docs/utils/create-excerpt';
export default class ClassRoute extends Route.extend(ScrollTracker) {
/** @type {import('@ember/routing/router-service').default} */
@service
router;
@service
headData;
@service
metaStore;
titleToken(model) {
return model.name;
}
async model(params) {
const { project, project_version: compactVersion } =
this.paramsFor('project-version');
let projectObj = await this.store.findRecord('project', project);
let projectVersion = getFullVersion(
compactVersion,
project,
projectObj,
this.metaStore
);
const klass = params['class'];
return this.find('class', `${project}-${projectVersion}-${klass}`);
}
afterModel(model) {
this.headData.modelName = model.name;
}
find(typeName, param) {
return this.store.find(typeName, param).catch((e1) => {
if (typeName != 'namespace') {
console.warn(
e1,
'fetching by class or module failed, retrying as namespace'
);
return this.store.find('namespace', param).catch((e2) => {
console.error(e2);
return resolve({
isError: true,
status: 404,
});
});
}
console.error(e1);
return resolve({
isError: true,
status: 404,
});
});
}
redirect(model, transition) {
const lookupParams = (routeName) => {
let route = transition.routeInfos.find(({ name }) => name === routeName);
return route ? route.params : {};
};
let {
to: { queryParams },
} = transition;
if (queryParams.anchor && queryParams.type) {
let type = queryParams.type;
this.router.transitionTo(
`project-version.classes.class.${pluralize(type)}.${type}`,
lookupParams('project-version').project,
lookupParams('project-version').project_version,
lookupParams('project-version.classes.class').class,
queryParams.anchor
);
}
if (model.isError) {
let error = new Error(
'Error retrieving model in routes/project-version/classes/class'
);
error.status = 404;
throw error;
}
}
afterModel(klass) {
if (!klass.isError) {
let description = klass.get('ogDescription') || klass.get('description');
if (description) {
set(this, 'headData.description', createExcerpt(description));
}
const relationships = klass.constructor.relationshipNames;
const promises = Object.keys(relationships).reduce(
(memo, relationshipType) => {
const relationshipPromises = relationships[relationshipType].map(
(name) => klass.get(name)
);
return memo.concat(relationshipPromises);
},
[]
);
return all(promises);
}
}
serialize(model) {
return {
class: model.name,
};
}
}