This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
forked from zuixjs/zuix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_loader.js
178 lines (158 loc) · 6.3 KB
/
api_loader.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
"use strict";
zuix.controller(function (cp) {
const resourcePath = zuix.store('config').resourcePath;
cp.create = function () {
const apiName = cp.view().attr('data-ui-api');
cp.view().html('Loading '+apiName+' API...');
// download the jsDoc data file and HTML-format it.
zuix.$.ajax({
url: resourcePath+'content/api/data/'+apiName+'.json?'+Date.now(),
success: function(json) {
cp.view().html('');
const dox = JSON.parse(json);
const apiDocs = {};
apiDocs.name = apiName;
apiDocs.constructor = null;
apiDocs.parameters = []; // constructor parameters
apiDocs.methods = [];
apiDocs.properties = [];
apiDocs.types = []; // custom object/types used in this object
apiDocs.callbacks = []; // callback functions
zuix.$.each(dox, function() {
const skipItem = (this.isPrivate || (this.ctx != null && (this.ctx.name !== apiName && this.ctx.cons !== apiName)) || this.tags == null || this.tags.length === 0);
if (skipItem) {
return true;
}
if (this.isConstructor === true) {
apiDocs.constructor = addConstructor(this);
return true;
}
const apiMember = (!this.isPrivate && this.ctx != null && (this.ctx.cons === apiName));
if (apiMember) {
apiDocs.methods.push(addMember(this));
return true;
}
const itemType = this.tags[0].type;
switch (itemType) {
case 'param':
apiDocs.parameters.push(addType(this));
break;
case 'typedef':
const type = addType(this);
if (type.name === apiName)
apiDocs.properties = type.properties;
else
apiDocs.types.push(type);
break;
case 'callback':
apiDocs.callbacks.push(addHandler(this));
break;
}
});
zuix.load('content/api/api_template', {
data: apiDocs,
markdown: cp.options().markdown,
prism: cp.options().prism,
ready: function (ctx) {
const view = zuix.$(ctx.view());
view.addClass('animated')
.addClass('fadeIn');
cp.view().append(view.get());
}
});
},
error: function() {
cp.view().html('Error loading '+apiName+' API!');
}
});
};
function addConstructor(constructor) {
const item = {};
item.name = constructor.ctx.name;
item.description = constructor.description.full || constructor.description;
item.parameters = [];
item.return = {};
zuix.$.each(constructor.tags, function (i) {
const param = getParam(this);
if (this.type === 'param') {
item.parameters.push(param);
} else if (this.type === 'example') {
item.example = this.string;
} else if (this.type === 'return') {
item.return = param;
}
});
return item;
}
function addMember(member) {
const item = {};
item.name = member.ctx.name;
item.description = member.description.full || member.description;
item.parameters = [];
item.return = [];
item.example = '';
zuix.$.each(member.tags, function () {
const param = getParam(this);
if (this.type === 'param') {
item.parameters.push(param);
} else if (this.type === 'example') {
item.example = this.string;
} else if (this.type === 'return') {
item.return.push(param);
}
});
return item;
}
function addType(typeDef) {
const item = {};
item.name = '';
item.description = typeDef.description.full || typeDef.description;
item.properties = [];
item.example = '';
zuix.$.each(typeDef.tags, function () {
if (this.type === 'property') {
const property = getParam(this);
item.properties.push(property);
} else if (this.type === 'example') {
item.example = this.string;
} else if (this.type === 'typedef') {
item.name = this.string;
if (item.name.indexOf('}') > 0)
item.name = item.name.substring(item.name.lastIndexOf('}')+1).trim();
}
});
return item;
}
function addHandler(handler) {
const item = {};
item.name = handler.name;
item.description = handler.description.full || handler.description;
item.parameters = [];
item.context = {};
item.return = {};
item.example = '';
zuix.$.each(handler.tags, function (i) {
const param = getParam(this);
if (this.type === 'callback') {
item.name = item.name || this.string;
} else if (this.type === 'param') {
item.parameters.push(param);
} else if (this.type === 'example') {
item.example = this.string;
} else if (this.type === 'this') {
item.context = this.string;
} else if (this.type === 'return') {
item.return = param;
}
});
return item;
}
function getParam(parameter) {
const param = {};
param.name = parameter.name != null ? parameter.name.replace('[','').replace(']','') : null;
param.description = parameter.description;
param.types = parameter.types;
param.optional = parameter.optional;
return param;
}
});