This repository has been archived by the owner on Jan 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
api-loader.js
391 lines (318 loc) · 13.7 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
(function () {
var blank_iframe = '/index-blank.html';
var example_jquery = 'http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js'; // latest from google
var re_opt = /options/i;
if (!window.jquerydocs) window.jquerydocs = {};
if (!window.xmldoc) window.xmldoc = null;
window.loadDocs = function(data) {
$(document).trigger('api-loading');
if (!xmldoc && typeof data != "undefined") {
jquerydocs = data;
attachFind(jquerydocs);
$(document).trigger('api-load-success');
$(document).trigger('api-load-complete');
} else {
// parser
$.ajax({
url: xmldoc || 'jquery-docs.xml', // generated from jquery source: /tools/wikiapi2xml/createjQueryXMLDocs.py
dataType: 'xml',
success: parse,
error: function () {
$(document).trigger('api-load-error');
},
complete: function () {
$(document).trigger('api-load-complete');
}
});
}
};
function parse(xml) {
var docinfo = $('docs', xml);
var guid = 0; // TODO upgrade guid to a combo of fn name and params - like Jorn's browser
jquerydocs.version = docinfo.attr('version');
jquerydocs.timestamp = docinfo.attr('timestamp');
jquerydocs.startdoc = docinfo.attr('startdoc');
var letters = []; // holder before sorting and inserting
jquerydocs.letters = [];
jquerydocs.data = {};
jquerydocs.searchNames = [];
jquerydocs.categories = [];
// loop through all types collecting data
$('cat', xml).each(function (i) {
var catName = this.getAttribute('value');
var category = {};
category.name = catName;
category.subcategories = [];
$('subcat', this).each(function (i) {
var subcatName = this.getAttribute('value');
category.subcategories.push(subcatName);
$('function,property,selector', this).each(function () {
var data = {};
guid++;
// some function names have spaces around them - so trim
var name = this.getAttribute('name').replace( /^\s+|\s+$/g, '');
var searchName = name.toLowerCase().replace(/^jquery\./, '');
letters.push(name.toLowerCase().substr(0,1));
name = name.replace(/^jquery\./i, '$.');
jquerydocs.searchNames.push(searchName + guid);
data['id'] = guid;
data['searchname'] = searchName;
data['name'] = name;
data['type'] = this.nodeName.toLowerCase();
data['category'] = this.getAttribute('cat');
data['subcategory'] = subcatName;
data['return'] = escapeHTML(this.getAttribute('return'));
data['added'] = $('added', this).text();
data['sample'] = $('> sample', this).text();
data['desc'] = $('> desc', this).text();
data['longdesc'] = deWikify($('> longdesc', this).text());
// silly hack because of conversion issue from wiki to text (the .ready function
// has HTML in the description), but also includes HTML that should be printed,
// in particular the body tag :-(
data.longdesc = data.longdesc.replace(/<body>/, '<body>');
// some descs are in HTML format, some aren't
if (!(/<p>/).test(data.longdesc)) {
data.longdesc = '<p>' + data.longdesc.split(/\n\n/).join('</p><p>') + '</p>';
}
// strip our empty p tag if there was no description
if (data.longdesc == '<p></p>') {
data.longdesc = '';
}
/** params - we'll also search for Options to decide whether we need to parse */
var readOptions = false;
data.params = [];
$('params', this).each(function (i) {
var type = escapeHTML(this.getAttribute('type'));
var name = this.getAttribute('name');
var opt = this.getAttribute('optional') || "";
var desc = $('desc', this).text();
if (re_opt.test(type)) {
readOptions = true;
}
data.params.push({
optional : (/true/i).test(opt), // bool
name : name,
type : type,
desc : desc
});
});
if (readOptions) {
data.options = [];
$('option', this).each(function () {
var option = {};
option['name'] = this.getAttribute('name');
option['default'] = this.getAttribute('default') || '';
option['type'] = escapeHTML(this.getAttribute('type'));
option['desc'] = deWikify($('desc', this).text());
data.options.push(option);
});
}
data.examples = [];
/** examples */
$('example', this).each(function (i) {
var iframe = '', exampleId = '';
var example = {};
example['code'] = $('code', this).text();
example['htmlCode'] = escapeHTML(example.code);
example['desc'] = deWikify(escapeHTML($('desc', this).text()));
example['css'] = $('css', this).text() || '';
example['inhead'] = $('inhead', this).text() || '';
example['html'] = $('html', this).text() || '';
exampleId = guid + 'iframeExample' + i;
example['exampleId'] = exampleId;
if (example.html) {
iframe = '<iframe id="' + exampleId + '" class="example" src="' + blank_iframe + '"></iframe>';
// we're storing the example iframe source to insert in to
// the iframe only once it's inserted in to the DOM.
example['runCode'] = iframeTemplate().replace(/%([a-z]*)%/ig, function (m, l) {
return example[l] || "";
});
} else {
example.runCode = '';
}
data.examples.push(example);
});
jquerydocs.data[searchName + data.id] = data;
});
});
jquerydocs.categories.push(category); // FIXME should I warn if this exists?
});
jquerydocs.letters = unique($.map(letters.sort(), function (i) {
return i.substr(0,1);
}));
// attachFind(jquerydocs);
$(document).trigger('api-load-success');
}
// helpers
function attachFind(o) {
o.find = function (s, by) {
var found = [],
tmp = {},
tmpNames = [],
lettersLK = {},
letters = [],
catsLK = {},
cats = [],
catPointer = 0,
subLK = {},
sub = [],
data = {};
var i = 0;
s = s.toLowerCase();
by = (by || 'searchname').toLowerCase();
if (by == 'name') by = 'searchname'; // search without the $.
for (i = 0; i < jquerydocs.searchNames.length; i++) {
if (jquerydocs.data[jquerydocs.searchNames[i]][by] && jquerydocs.data[jquerydocs.searchNames[i]][by].toLowerCase().indexOf(s) == 0) {
data = tmp[jquerydocs.searchNames[i]] = jquerydocs.data[jquerydocs.searchNames[i]];
tmpNames.push(jquerydocs.searchNames[i]);
if (!lettersLK[jquerydocs.searchNames[i].substr(0, 1)]) {
lettersLK[jquerydocs.searchNames[i].substr(0, 1)] = true;
letters.push(jquerydocs.searchNames[i].substr(0, 1));
}
if (typeof catsLK[data.category] == 'undefined') {
catsLK[data.category] = catPointer;
cats.push({ name : data.category, subcategories : [] });
catPointer++;
}
if (!subLK[data.subcategory]) {
subLK[data.subcategory] = true;
cats[catsLK[data.category]].subcategories.push(data.subcategory);
}
}
}
tmpNames = tmpNames.sort().reverse(); // never sure if this is faster with the reverse
i = tmpNames.length;
while (i--) {
found.push(tmp[tmpNames[i]]);
}
// this is kind of noddy, but returns the same object as we queried - which is cool!
found.letters = letters;
found.categories = cats;
found.data = tmp;
found.searchNames = tmpNames;
attachFind(found);
return found;
};
}
function fieldMap() {
return {
}
}
function unique(a) {
var ret = [], done = {};
try {
for ( var i = 0, length = a.length; i < length; i++ ) {
var id = a[ i ] ;
if ( !done[ id ] ) {
done[ id ] = true;
ret.push( a[ i ] );
}
}
} catch( e ) {
ret = a;
}
return ret;
}
function iframeTemplate() {
// array so that we maintain some formatting
return [
'<!' + 'DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"',
' "http://www.w3.org/TR/html4/loose.dtd">',
'<' + 'html>',
'<' + 'head>',
'<base href="http://docs.jquery.com" />',
'<' + 'script src="' + example_jquery + '"><' + '/script>',
'%inhead%',
'<' + 'script>',
'$(document).ready(function(){', '%code%', ' });',
'<' + '/script>',
'<' + 'style>',
'%css%',
'<' + '/style>',
'<' + '/head>',
'<' + 'body>',
'%html%',
'<' + '/body>',
'<' + '/html>'
].join("\n");
}
/** public utility functions */
window.escapeHTML = function (s) {
// converts null to string
return (s+"").replace(/[<>]/g, function (m) {
if (m == '<') return '<';
else if (m == '>') return '>';
});
};
window.cleanSelector = function(s) {
return (s+"").replace(/[\$\.]/g, function (m) {
// handle escaping characters that break the selector engine
if (m == '$') {
return '\\$';
} else if (m == '.') {
return '\\.';
}
});
};
window.linkifyTypes = function(type) {
// cheeky way to avoid doing a massive if (m == x || m == y || m == etc) - we just do an .indexOf()
var nodocs = '|jQuery|XMLHttpRequest|Plugins|Validator|Validation|undefined|or|Any|DOM|Map|top|left|lt|gt|\(s\)||'; // note we purposely include an empty match
return type ? $.map(type.replace(/DOMElement/g, 'DOM Element').split(/, /), function (n) {
// match words and linkify, then italic to the optionals
return n.replace(/boolean/, 'Boolean').replace(/\b[a-z]*\b/gi, function (m, l) {
// special case
if (m == 'Elements') {
return '<a href="http://docs.jquery.com/Types#Element">Element</a>s';
// no specific documentation for these types
} else if (nodocs.indexOf('|' + m + '|') !== -1) {
return m;
} else {
return '<a href="http://docs.jquery.com/Types#' + m + '">' + m + '</a>';
}
});
}).join(', ') : "";
};
window.deWikify = function (s) {
return (""+s).replace(/'''.*?'''/g, function (m) {
return '<strong>' + m.replace(/'''/g, '') + '</strong>';
}).replace(/''.*?''/g, function (m) {
return '<em>' + m.replace(/''/g, '') + '</em>';
}).replace(/\[http.*?\]/, function (m) {
var p = m.replace(/^\[/, '').replace(/\]$/, '').split(/ /);
return '<a href="' + p[0] + '">' + (p.length == 2 ? p[1] : p[0]) + '</a>';
}).replace(/(((^|\n)(\*|[0-9]+.).*)+)/g, function (m) {
var type = 'ol';
// strip leading new line
m = m.replace( /^\s+|\s+$/g, "" );
if (m.match(/^\*/)) type = 'ul';
return '<' + type + '><li>' + m.replace(/\*?/g, '').split(/\n/).join("</li><li>") + '</li></' + type + '>';
});
};
window.runExample = function(data) {
if (!data.examples || data.examples.length == 0) return;
var i, win, example;
for (i = 0; i < data.examples.length; i++) {
example = data.examples[i];
win = $('#' + cleanSelector(example.exampleId)).get(0);
if (win) {
win = win.contentDocument || win.contentWindow.document;
// from docs.jquery.com
win.write(example.runCode.replace("$(document).ready(function(){", "window.onload = (function(){try{")
.replace(/}\);\s*<\/sc/, "}catch(e){}});</sc")
.replace("</head>", "<style>html,body{border:0; margin:0; padding:0;}</style></head>")
);
win.close();
}
}
};
window.fixLinks = function (context) {
// since the source comes from the wiki, we need to adjust some of the links
$('a', context).each(function () {
var href = this.getAttribute('href');
if (href && !href.match(/http/) && !href.match(/^#/) && this.className != 'fnName') {
this.host = 'docs.jquery.com';
this.pathname = this.pathname.replace(window.location.pathname, '');
}
});
};
})();