libraries/EaselJS_docs/assets/js/apidocs.js (view raw)
1YUI().use(
2 'yuidoc-meta',
3 'api-list', 'history-hash', 'node-screen', 'node-style', 'pjax',
4function (Y) {
5
6var win = Y.config.win,
7 localStorage = win.localStorage,
8
9 bdNode = Y.one('#bd'),
10
11 pjax,
12 defaultRoute,
13
14 classTabView,
15 selectedTab;
16
17// Kill pjax functionality unless serving over HTTP.
18if (!Y.getLocation().protocol.match(/^https?\:/)) {
19 Y.Router.html5 = false;
20}
21
22// Create the default route with middleware which enables syntax highlighting
23// on the loaded content.
24defaultRoute = Y.Pjax.defaultRoute.concat(function (req, res, next) {
25 prettyPrint();
26 bdNode.removeClass('loading');
27
28 next();
29});
30
31pjax = new Y.Pjax({
32 container : '#docs-main',
33 contentSelector: '#docs-main > .content',
34 linkSelector : '#bd a',
35 titleSelector : '#xhr-title',
36
37 navigateOnHash: true,
38 root : '/',
39 routes : [
40 // -- / ----------------------------------------------------------------
41 {
42 path : '/(index.html)?',
43 callbacks: defaultRoute
44 },
45
46 // -- /classes/* -------------------------------------------------------
47 {
48 path : '/classes/:class.html*',
49 callbacks: [defaultRoute, 'handleClasses']
50 },
51
52 // -- /files/* ---------------------------------------------------------
53 {
54 path : '/files/*file',
55 callbacks: [defaultRoute, 'handleFiles']
56 },
57
58 // -- /modules/* -------------------------------------------------------
59 {
60 path : '/modules/:module.html*',
61 callbacks: defaultRoute
62 }
63 ]
64});
65
66// -- Utility Functions --------------------------------------------------------
67
68pjax.checkVisibility = function (tab) {
69 tab || (tab = selectedTab);
70
71 if (!tab) { return; }
72
73 var panelNode = tab.get('panelNode'),
74 visibleItems;
75
76 // If no items are visible in the tab panel due to the current visibility
77 // settings, display a message to that effect.
78 visibleItems = panelNode.all('.item,.index-item').some(function (itemNode) {
79 if (itemNode.getComputedStyle('display') !== 'none') {
80 return true;
81 }
82 });
83
84 panelNode.all('.no-visible-items').remove();
85
86 if (!visibleItems) {
87 if (Y.one('#index .index-item')) {
88 panelNode.append(
89 '<div class="no-visible-items">' +
90 '<p>' +
91 'Some items are not shown due to the current visibility ' +
92 'settings. Use the checkboxes at the upper right of this ' +
93 'page to change the visibility settings.' +
94 '</p>' +
95 '</div>'
96 );
97 } else {
98 panelNode.append(
99 '<div class="no-visible-items">' +
100 '<p>' +
101 'This class doesn\'t provide any methods, properties, ' +
102 'attributes, or events.' +
103 '</p>' +
104 '</div>'
105 );
106 }
107 }
108
109 // Hide index sections without any visible items.
110 Y.all('.index-section').each(function (section) {
111 var items = 0,
112 visibleItems = 0;
113
114 section.all('.index-item').each(function (itemNode) {
115 items += 1;
116
117 if (itemNode.getComputedStyle('display') !== 'none') {
118 visibleItems += 1;
119 }
120 });
121
122 section.toggleClass('hidden', !visibleItems);
123 section.toggleClass('no-columns', visibleItems < 4);
124 });
125};
126
127pjax.initClassTabView = function () {
128 if (!Y.all('#classdocs .api-class-tab').size()) {
129 return;
130 }
131
132 if (classTabView) {
133 classTabView.destroy();
134 selectedTab = null;
135 }
136
137 classTabView = new Y.TabView({
138 srcNode: '#classdocs',
139
140 on: {
141 selectionChange: pjax.onTabSelectionChange
142 }
143 });
144
145 pjax.updateTabState();
146 classTabView.render();
147};
148
149pjax.initLineNumbers = function () {
150 var hash = win.location.hash.substring(1),
151 container = pjax.get('container'),
152 hasLines, node;
153
154 // Add ids for each line number in the file source view.
155 container.all('.linenums>li').each(function (lineNode, index) {
156 lineNode.set('id', 'l' + (index + 1));
157 lineNode.addClass('file-line');
158 hasLines = true;
159 });
160
161 // Scroll to the desired line.
162 if (hasLines && /^l\d+$/.test(hash)) {
163 if ((node = container.getById(hash))) {
164 win.scroll(0, node.getY());
165 }
166 }
167};
168
169pjax.initRoot = function () {
170 var terminators = /^(?:classes|files|modules)$/,
171 parts = pjax._getPathRoot().split('/'),
172 root = [],
173 i, len, part;
174
175 for (i = 0, len = parts.length; i < len; i += 1) {
176 part = parts[i];
177
178 if (part.match(terminators)) {
179 // Makes sure the path will end with a "/".
180 root.push('');
181 break;
182 }
183
184 root.push(part);
185 }
186
187 pjax.set('root', root.join('/'));
188};
189
190pjax.updateTabState = function (src) {
191 var hash = win.location.hash.substring(1),
192 defaultTab, node, tab, tabPanel;
193
194 function scrollToNode() {
195 if (node.hasClass('protected')) {
196 Y.one('#api-show-protected').set('checked', true);
197 pjax.updateVisibility();
198 }
199
200 if (node.hasClass('private')) {
201 Y.one('#api-show-private').set('checked', true);
202 pjax.updateVisibility();
203 }
204
205 setTimeout(function () {
206 // For some reason, unless we re-get the node instance here,
207 // getY() always returns 0.
208 var node = Y.one('#classdocs').getById(hash);
209 win.scrollTo(0, node.getY() - 70);
210 }, 1);
211 }
212
213 if (!classTabView) {
214 return;
215 }
216
217 if (src === 'hashchange' && !hash) {
218 defaultTab = 'index';
219 } else {
220 if (localStorage) {
221 defaultTab = localStorage.getItem('tab_' + pjax.getPath()) ||
222 'index';
223 } else {
224 defaultTab = 'index';
225 }
226 }
227
228 if (hash && (node = Y.one('#classdocs').getById(hash))) {
229 if ((tabPanel = node.ancestor('.api-class-tabpanel', true))) {
230 if ((tab = Y.one('#classdocs .api-class-tab.' + tabPanel.get('id')))) {
231 if (classTabView.get('rendered')) {
232 Y.Widget.getByNode(tab).set('selected', 1);
233 } else {
234 tab.addClass('yui3-tab-selected');
235 }
236 }
237 }
238
239 // Scroll to the desired element if this is a hash URL.
240 if (node) {
241 if (classTabView.get('rendered')) {
242 scrollToNode();
243 } else {
244 classTabView.once('renderedChange', scrollToNode);
245 }
246 }
247 } else {
248 tab = Y.one('#classdocs .api-class-tab.' + defaultTab);
249
250 // When the `defaultTab` node isn't found, `localStorage` is stale.
251 if (!tab && defaultTab !== 'index') {
252 tab = Y.one('#classdocs .api-class-tab.index');
253 }
254
255 if (classTabView.get('rendered')) {
256 Y.Widget.getByNode(tab).set('selected', 1);
257 } else {
258 tab.addClass('yui3-tab-selected');
259 }
260 }
261};
262
263pjax.updateVisibility = function () {
264 var container = pjax.get('container');
265
266 container.toggleClass('hide-inherited',
267 !Y.one('#api-show-inherited').get('checked'));
268
269 container.toggleClass('show-deprecated',
270 Y.one('#api-show-deprecated').get('checked'));
271
272 container.toggleClass('show-protected',
273 Y.one('#api-show-protected').get('checked'));
274
275 container.toggleClass('show-private',
276 Y.one('#api-show-private').get('checked'));
277
278 pjax.checkVisibility();
279};
280
281// -- Route Handlers -----------------------------------------------------------
282
283pjax.handleClasses = function (req, res, next) {
284 var status = res.ioResponse.status;
285
286 // Handles success and local filesystem XHRs.
287 if (res.ioResponse.readyState === 4 && (!status || (status >= 200 && status < 300))) {
288 pjax.initClassTabView();
289 }
290
291 next();
292};
293
294pjax.handleFiles = function (req, res, next) {
295 var status = res.ioResponse.status;
296
297 // Handles success and local filesystem XHRs.
298 if (res.ioResponse.readyState === 4 && (!status || (status >= 200 && status < 300))) {
299 pjax.initLineNumbers();
300 }
301
302 next();
303};
304
305// -- Event Handlers -----------------------------------------------------------
306
307pjax.onNavigate = function (e) {
308 var hash = e.hash,
309 originTarget = e.originEvent && e.originEvent.target,
310 tab;
311
312 if (hash) {
313 tab = originTarget && originTarget.ancestor('.yui3-tab', true);
314
315 if (hash === win.location.hash) {
316 pjax.updateTabState('hashchange');
317 } else if (!tab) {
318 win.location.hash = hash;
319 }
320
321 e.preventDefault();
322 return;
323 }
324
325 // Only scroll to the top of the page when the URL doesn't have a hash.
326 this.set('scrollToTop', !e.url.match(/#.+$/));
327
328 bdNode.addClass('loading');
329};
330
331pjax.onOptionClick = function (e) {
332 pjax.updateVisibility();
333};
334
335pjax.onTabSelectionChange = function (e) {
336 var tab = e.newVal,
337 tabId = tab.get('contentBox').getAttribute('href').substring(1);
338
339 selectedTab = tab;
340
341 // If switching from a previous tab (i.e., this is not the default tab),
342 // replace the history entry with a hash URL that will cause this tab to
343 // be selected if the user navigates away and then returns using the back
344 // or forward buttons.
345 if (e.prevVal && localStorage) {
346 localStorage.setItem('tab_' + pjax.getPath(), tabId);
347 }
348
349 pjax.checkVisibility(tab);
350};
351
352// -- Init ---------------------------------------------------------------------
353
354pjax.on('navigate', pjax.onNavigate);
355
356pjax.initRoot();
357pjax.upgrade();
358pjax.initClassTabView();
359pjax.initLineNumbers();
360pjax.updateVisibility();
361
362Y.APIList.rootPath = pjax.get('root');
363
364Y.one('#api-options').delegate('click', pjax.onOptionClick, 'input');
365
366Y.on('hashchange', function (e) {
367 pjax.updateTabState('hashchange');
368}, win);
369
370});