Skip to content
This repository was archived by the owner on Jan 16, 2020. It is now read-only.

Commit d53ca08

Browse files
wycatsjeresig
authored andcommitted
Bringining in the current code.
0 parents  commit d53ca08

12 files changed

+915
-0
lines changed

api-docs-1.2.6.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api-docs-1.3.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api-loader.js

Lines changed: 391 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,391 @@
1+
(function () {
2+
3+
var blank_iframe = '/index-blank.html';
4+
var example_jquery = 'http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js'; // latest from google
5+
6+
var re_opt = /options/i;
7+
8+
if (!window.jquerydocs) window.jquerydocs = {};
9+
if (!window.xmldoc) window.xmldoc = null;
10+
11+
window.loadDocs = function(data) {
12+
$(document).trigger('api-loading');
13+
14+
if (!xmldoc && typeof data != "undefined") {
15+
jquerydocs = data;
16+
attachFind(jquerydocs);
17+
$(document).trigger('api-load-success');
18+
$(document).trigger('api-load-complete');
19+
} else {
20+
// parser
21+
$.ajax({
22+
url: xmldoc || 'jquery-docs.xml', // generated from jquery source: /tools/wikiapi2xml/createjQueryXMLDocs.py
23+
dataType: 'xml',
24+
success: parse,
25+
error: function () {
26+
$(document).trigger('api-load-error');
27+
},
28+
complete: function () {
29+
$(document).trigger('api-load-complete');
30+
}
31+
});
32+
}
33+
};
34+
35+
function parse(xml) {
36+
var docinfo = $('docs', xml);
37+
var guid = 0; // TODO upgrade guid to a combo of fn name and params - like Jorn's browser
38+
39+
jquerydocs.version = docinfo.attr('version');
40+
jquerydocs.timestamp = docinfo.attr('timestamp');
41+
jquerydocs.startdoc = docinfo.attr('startdoc');
42+
43+
var letters = []; // holder before sorting and inserting
44+
jquerydocs.letters = [];
45+
46+
jquerydocs.data = {};
47+
jquerydocs.searchNames = [];
48+
jquerydocs.categories = [];
49+
50+
// loop through all types collecting data
51+
$('cat', xml).each(function (i) {
52+
var catName = this.getAttribute('value');
53+
var category = {};
54+
category.name = catName;
55+
category.subcategories = [];
56+
57+
$('subcat', this).each(function (i) {
58+
var subcatName = this.getAttribute('value');
59+
category.subcategories.push(subcatName);
60+
61+
$('function,property,selector', this).each(function () {
62+
var data = {};
63+
guid++;
64+
65+
// some function names have spaces around them - so trim
66+
var name = this.getAttribute('name').replace( /^\s+|\s+$/g, '');
67+
68+
var searchName = name.toLowerCase().replace(/^jquery\./, '');
69+
letters.push(name.toLowerCase().substr(0,1));
70+
71+
name = name.replace(/^jquery\./i, '$.');
72+
73+
jquerydocs.searchNames.push(searchName + guid);
74+
75+
data['id'] = guid;
76+
data['searchname'] = searchName;
77+
data['name'] = name;
78+
data['type'] = this.nodeName.toLowerCase();
79+
data['category'] = this.getAttribute('cat');
80+
data['subcategory'] = subcatName;
81+
data['return'] = escapeHTML(this.getAttribute('return'));
82+
data['added'] = $('added', this).text();
83+
data['sample'] = $('> sample', this).text();
84+
data['desc'] = $('> desc', this).text();
85+
data['longdesc'] = deWikify($('> longdesc', this).text());
86+
87+
// silly hack because of conversion issue from wiki to text (the .ready function
88+
// has HTML in the description), but also includes HTML that should be printed,
89+
// in particular the body tag :-(
90+
data.longdesc = data.longdesc.replace(/<body>/, '&lt;body&gt;');
91+
92+
// some descs are in HTML format, some aren't
93+
if (!(/<p>/).test(data.longdesc)) {
94+
data.longdesc = '<p>' + data.longdesc.split(/\n\n/).join('</p><p>') + '</p>';
95+
}
96+
97+
// strip our empty p tag if there was no description
98+
if (data.longdesc == '<p></p>') {
99+
data.longdesc = '';
100+
}
101+
102+
/** params - we'll also search for Options to decide whether we need to parse */
103+
var readOptions = false;
104+
data.params = [];
105+
$('params', this).each(function (i) {
106+
var type = escapeHTML(this.getAttribute('type'));
107+
var name = this.getAttribute('name');
108+
var opt = this.getAttribute('optional') || "";
109+
var desc = $('desc', this).text();
110+
111+
if (re_opt.test(type)) {
112+
readOptions = true;
113+
}
114+
115+
data.params.push({
116+
optional : (/true/i).test(opt), // bool
117+
name : name,
118+
type : type,
119+
desc : desc
120+
});
121+
});
122+
123+
if (readOptions) {
124+
data.options = [];
125+
$('option', this).each(function () {
126+
var option = {};
127+
option['name'] = this.getAttribute('name');
128+
option['default'] = this.getAttribute('default') || '';
129+
option['type'] = escapeHTML(this.getAttribute('type'));
130+
option['desc'] = deWikify($('desc', this).text());
131+
132+
data.options.push(option);
133+
});
134+
}
135+
136+
data.examples = [];
137+
/** examples */
138+
$('example', this).each(function (i) {
139+
var iframe = '', exampleId = '';
140+
var example = {};
141+
142+
example['code'] = $('code', this).text();
143+
example['htmlCode'] = escapeHTML(example.code);
144+
example['desc'] = deWikify(escapeHTML($('desc', this).text()));
145+
example['css'] = $('css', this).text() || '';
146+
example['inhead'] = $('inhead', this).text() || '';
147+
example['html'] = $('html', this).text() || '';
148+
149+
exampleId = guid + 'iframeExample' + i;
150+
example['exampleId'] = exampleId;
151+
152+
if (example.html) {
153+
154+
iframe = '<iframe id="' + exampleId + '" class="example" src="' + blank_iframe + '"></iframe>';
155+
156+
// we're storing the example iframe source to insert in to
157+
// the iframe only once it's inserted in to the DOM.
158+
example['runCode'] = iframeTemplate().replace(/%([a-z]*)%/ig, function (m, l) {
159+
return example[l] || "";
160+
});
161+
} else {
162+
example.runCode = '';
163+
}
164+
165+
data.examples.push(example);
166+
});
167+
168+
jquerydocs.data[searchName + data.id] = data;
169+
});
170+
});
171+
172+
jquerydocs.categories.push(category); // FIXME should I warn if this exists?
173+
});
174+
175+
jquerydocs.letters = unique($.map(letters.sort(), function (i) {
176+
return i.substr(0,1);
177+
}));
178+
179+
// attachFind(jquerydocs);
180+
181+
$(document).trigger('api-load-success');
182+
}
183+
184+
// helpers
185+
186+
function attachFind(o) {
187+
o.find = function (s, by) {
188+
var found = [],
189+
tmp = {},
190+
tmpNames = [],
191+
lettersLK = {},
192+
letters = [],
193+
catsLK = {},
194+
cats = [],
195+
catPointer = 0,
196+
subLK = {},
197+
sub = [],
198+
data = {};
199+
200+
var i = 0;
201+
s = s.toLowerCase();
202+
by = (by || 'searchname').toLowerCase();
203+
204+
if (by == 'name') by = 'searchname'; // search without the $.
205+
206+
for (i = 0; i < jquerydocs.searchNames.length; i++) {
207+
if (jquerydocs.data[jquerydocs.searchNames[i]][by] && jquerydocs.data[jquerydocs.searchNames[i]][by].toLowerCase().indexOf(s) == 0) {
208+
data = tmp[jquerydocs.searchNames[i]] = jquerydocs.data[jquerydocs.searchNames[i]];
209+
tmpNames.push(jquerydocs.searchNames[i]);
210+
211+
if (!lettersLK[jquerydocs.searchNames[i].substr(0, 1)]) {
212+
lettersLK[jquerydocs.searchNames[i].substr(0, 1)] = true;
213+
letters.push(jquerydocs.searchNames[i].substr(0, 1));
214+
}
215+
216+
if (typeof catsLK[data.category] == 'undefined') {
217+
catsLK[data.category] = catPointer;
218+
cats.push({ name : data.category, subcategories : [] });
219+
catPointer++;
220+
}
221+
222+
if (!subLK[data.subcategory]) {
223+
subLK[data.subcategory] = true;
224+
225+
cats[catsLK[data.category]].subcategories.push(data.subcategory);
226+
}
227+
}
228+
}
229+
230+
tmpNames = tmpNames.sort().reverse(); // never sure if this is faster with the reverse
231+
i = tmpNames.length;
232+
while (i--) {
233+
found.push(tmp[tmpNames[i]]);
234+
}
235+
236+
// this is kind of noddy, but returns the same object as we queried - which is cool!
237+
found.letters = letters;
238+
found.categories = cats;
239+
found.data = tmp;
240+
found.searchNames = tmpNames;
241+
attachFind(found);
242+
243+
return found;
244+
};
245+
}
246+
247+
function fieldMap() {
248+
return {
249+
250+
}
251+
}
252+
253+
function unique(a) {
254+
var ret = [], done = {};
255+
256+
try {
257+
for ( var i = 0, length = a.length; i < length; i++ ) {
258+
var id = a[ i ] ;
259+
260+
if ( !done[ id ] ) {
261+
done[ id ] = true;
262+
ret.push( a[ i ] );
263+
}
264+
}
265+
266+
} catch( e ) {
267+
ret = a;
268+
}
269+
270+
return ret;
271+
}
272+
273+
function iframeTemplate() {
274+
// array so that we maintain some formatting
275+
return [
276+
'<!' + 'DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"',
277+
' "http://www.w3.org/TR/html4/loose.dtd">',
278+
'<' + 'html>',
279+
'<' + 'head>',
280+
'<base href="http://docs.jquery.com" />',
281+
'<' + 'script src="' + example_jquery + '"><' + '/script>',
282+
'%inhead%',
283+
'<' + 'script>',
284+
'$(document).ready(function(){', '%code%', ' });',
285+
'<' + '/script>',
286+
'<' + 'style>',
287+
'%css%',
288+
'<' + '/style>',
289+
'<' + '/head>',
290+
'<' + 'body>',
291+
'%html%',
292+
'<' + '/body>',
293+
'<' + '/html>'
294+
].join("\n");
295+
}
296+
297+
298+
299+
/** public utility functions */
300+
301+
window.escapeHTML = function (s) {
302+
// converts null to string
303+
return (s+"").replace(/[<>]/g, function (m) {
304+
if (m == '<') return '&lt;';
305+
else if (m == '>') return '&gt;';
306+
});
307+
};
308+
309+
window.cleanSelector = function(s) {
310+
return (s+"").replace(/[\$\.]/g, function (m) {
311+
// handle escaping characters that break the selector engine
312+
if (m == '$') {
313+
return '\\$';
314+
} else if (m == '.') {
315+
return '\\.';
316+
}
317+
});
318+
};
319+
320+
window.linkifyTypes = function(type) {
321+
// cheeky way to avoid doing a massive if (m == x || m == y || m == etc) - we just do an .indexOf()
322+
var nodocs = '|jQuery|XMLHttpRequest|Plugins|Validator|Validation|undefined|or|Any|DOM|Map|top|left|lt|gt|\(s\)||'; // note we purposely include an empty match
323+
324+
return type ? $.map(type.replace(/DOMElement/g, 'DOM Element').split(/, /), function (n) {
325+
// match words and linkify, then italic to the optionals
326+
return n.replace(/boolean/, 'Boolean').replace(/\b[a-z]*\b/gi, function (m, l) {
327+
// special case
328+
if (m == 'Elements') {
329+
return '<a href="http://docs.jquery.com/Types#Element">Element</a>s';
330+
// no specific documentation for these types
331+
} else if (nodocs.indexOf('|' + m + '|') !== -1) {
332+
return m;
333+
} else {
334+
return '<a href="http://docs.jquery.com/Types#' + m + '">' + m + '</a>';
335+
}
336+
});
337+
}).join(', ') : "";
338+
};
339+
340+
window.deWikify = function (s) {
341+
return (""+s).replace(/'''.*?'''/g, function (m) {
342+
return '<strong>' + m.replace(/'''/g, '') + '</strong>';
343+
}).replace(/''.*?''/g, function (m) {
344+
return '<em>' + m.replace(/''/g, '') + '</em>';
345+
}).replace(/\[http.*?\]/, function (m) {
346+
var p = m.replace(/^\[/, '').replace(/\]$/, '').split(/ /);
347+
return '<a href="' + p[0] + '">' + (p.length == 2 ? p[1] : p[0]) + '</a>';
348+
}).replace(/(((^|\n)(\*|[0-9]+.).*)+)/g, function (m) {
349+
var type = 'ol';
350+
// strip leading new line
351+
m = m.replace( /^\s+|\s+$/g, "" );
352+
if (m.match(/^\*/)) type = 'ul';
353+
return '<' + type + '><li>' + m.replace(/\*?/g, '').split(/\n/).join("</li><li>") + '</li></' + type + '>';
354+
});
355+
};
356+
357+
window.runExample = function(data) {
358+
if (!data.examples || data.examples.length == 0) return;
359+
360+
var i, win, example;
361+
362+
for (i = 0; i < data.examples.length; i++) {
363+
example = data.examples[i];
364+
365+
win = $('#' + cleanSelector(example.exampleId)).get(0);
366+
if (win) {
367+
win = win.contentDocument || win.contentWindow.document;
368+
369+
// from docs.jquery.com
370+
win.write(example.runCode.replace("$(document).ready(function(){", "window.onload = (function(){try{")
371+
.replace(/}\);\s*<\/sc/, "}catch(e){}});</sc")
372+
.replace("</head>", "<style>html,body{border:0; margin:0; padding:0;}</style></head>")
373+
);
374+
375+
win.close();
376+
}
377+
}
378+
};
379+
380+
window.fixLinks = function (context) {
381+
// since the source comes from the wiki, we need to adjust some of the links
382+
$('a', context).each(function () {
383+
var href = this.getAttribute('href');
384+
if (href && !href.match(/http/) && !href.match(/^#/) && this.className != 'fnName') {
385+
this.host = 'docs.jquery.com';
386+
this.pathname = this.pathname.replace(window.location.pathname, '');
387+
}
388+
});
389+
};
390+
391+
})();

images/arrow-back_16.gif

562 Bytes
Loading

images/arrow-forward_16.gif

562 Bytes
Loading

images/blue-button.png

894 Bytes
Loading

images/grey-button.png

817 Bytes
Loading

images/header.png

646 Bytes
Loading

images/scroll_left.png

1.08 KB
Loading

images/scroll_right.png

1.05 KB
Loading

0 commit comments

Comments
 (0)