-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathapplication.js
More file actions
99 lines (85 loc) · 2.85 KB
/
application.js
File metadata and controls
99 lines (85 loc) · 2.85 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
import { service } from '@ember/service';
import JSONAPIAdapter from '@ember-data/adapter/json-api';
import { pluralize } from 'ember-inflector';
import { isBlank } from '@ember/utils';
export default class Application extends JSONAPIAdapter {
currentProject = '';
currentProjectVersion = '';
@service
metaStore;
@service('project')
projectService;
ids = null;
shouldReloadRecord(store, { modelName, id }) {
if (modelName === 'project') {
this.currentProject = id;
} else if (modelName === 'project-version') {
this.currentProjectVersion = id;
}
return; // return undefined so auto determinated
}
shouldBackgroundReloadAll() {
return false;
}
shouldBackgroundReloadRecord() {
return false;
}
constructor() {
super(...arguments);
this.ids = {};
}
async findRecord(store, { modelName }, id) {
let url;
let projectName = this.currentProject;
if (['namespace', 'class', 'module'].indexOf(modelName) > -1) {
let [version] =
id
.replace(`${projectName}-`, '')
.match(/^(\d+\.\d+\.\d+(?:-[a-z]+\.\d+)?)/i) || [];
let revId = this.metaStore.getRevId(projectName, version, modelName, id);
let modelNameToUse = modelName;
// To account for namespaces that are also classes but not defined properly in yuidocs
if (isBlank(revId) && modelNameToUse === 'class') {
revId = this.metaStore.getRevId(projectName, version, 'namespace', id);
modelNameToUse = 'namespace';
}
if (typeof revId !== 'undefined') {
let encodedRevId = encodeURIComponent(revId);
url = `json-docs/${projectName}/${version}/${pluralize(
modelNameToUse,
)}/${encodedRevId}`;
} else {
throw new Error('Documentation item not found');
}
} else if (modelName === 'missing') {
let version = this.projectService.version;
let revId = this.metaStore.getRevId(projectName, version, modelName, id);
url = `json-docs/${projectName}/${version}/${pluralize(
modelName,
)}/${revId}`;
} else if (modelName === 'project') {
this.currentProject = id;
url = `rev-index/${id}`;
} else if (modelName === 'project-version') {
this.currentProjectVersion = id;
url = `rev-index/${id}`;
} else {
throw new Error('Unexpected model lookup');
}
const base = window.location.origin;
url = `${base}/${url}.json`;
try {
let response = await fetch(url);
if (!response.ok) {
throw new Error(
`Network response was not ok: ${response.status} ${response.statusText}`,
);
}
let json = await response.json();
return json;
} catch (error) {
console.error(`Failed to fetch or parse JSON from ${url}:`, error);
throw new Error(`Failed to load data for ${url}: ${error.message}`);
}
}
}