PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/android_ble/www/lib/SoundJS/docs/assets/js/api-list.js

https://gitlab.com/bafang/cordova_controller
JavaScript | 251 lines | 188 code | 51 blank | 12 comment | 24 complexity | e23f348ed4e8c6780f744756f8ee8531 MD5 | raw file
  1. YUI.add('api-list', function (Y) {
  2. var Lang = Y.Lang,
  3. YArray = Y.Array,
  4. APIList = Y.namespace('APIList'),
  5. classesNode = Y.one('#api-classes'),
  6. inputNode = Y.one('#api-filter'),
  7. modulesNode = Y.one('#api-modules'),
  8. tabviewNode = Y.one('#api-tabview'),
  9. tabs = APIList.tabs = {},
  10. filter = APIList.filter = new Y.APIFilter({
  11. inputNode : inputNode,
  12. maxResults: 1000,
  13. on: {
  14. results: onFilterResults
  15. }
  16. }),
  17. search = APIList.search = new Y.APISearch({
  18. inputNode : inputNode,
  19. maxResults: 100,
  20. on: {
  21. clear : onSearchClear,
  22. results: onSearchResults
  23. }
  24. }),
  25. tabview = APIList.tabview = new Y.TabView({
  26. srcNode : tabviewNode,
  27. panelNode: '#api-tabview-panel',
  28. render : true,
  29. on: {
  30. selectionChange: onTabSelectionChange
  31. }
  32. }),
  33. focusManager = APIList.focusManager = tabviewNode.plug(Y.Plugin.NodeFocusManager, {
  34. circular : true,
  35. descendants: '#api-filter, .yui3-tab-panel-selected .api-list-item a, .yui3-tab-panel-selected .result a',
  36. keys : {next: 'down:40', previous: 'down:38'}
  37. }).focusManager,
  38. LIST_ITEM_TEMPLATE =
  39. '<li class="api-list-item {typeSingular}">' +
  40. '<a href="{rootPath}{typePlural}/{name}.html">{displayName}</a>' +
  41. '</li>';
  42. // -- Init ---------------------------------------------------------------------
  43. // Duckpunch FocusManager's key event handling to prevent it from handling key
  44. // events when a modifier is pressed.
  45. Y.before(function (e, activeDescendant) {
  46. if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) {
  47. return new Y.Do.Prevent();
  48. }
  49. }, focusManager, '_focusPrevious', focusManager);
  50. Y.before(function (e, activeDescendant) {
  51. if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) {
  52. return new Y.Do.Prevent();
  53. }
  54. }, focusManager, '_focusNext', focusManager);
  55. // Create a mapping of tabs in the tabview so we can refer to them easily later.
  56. tabview.each(function (tab, index) {
  57. var name = tab.get('label').toLowerCase();
  58. tabs[name] = {
  59. index: index,
  60. name : name,
  61. tab : tab
  62. };
  63. });
  64. // Switch tabs on Ctrl/Cmd-Left/Right arrows.
  65. tabviewNode.on('key', onTabSwitchKey, 'down:37,39');
  66. // Focus the filter input when the `/` key is pressed.
  67. Y.one(Y.config.doc).on('key', onSearchKey, 'down:83');
  68. // Keep the Focus Manager up to date.
  69. inputNode.on('focus', function () {
  70. focusManager.set('activeDescendant', inputNode);
  71. });
  72. // Update all tabview links to resolved URLs.
  73. tabview.get('panelNode').all('a').each(function (link) {
  74. link.setAttribute('href', link.get('href'));
  75. });
  76. // -- Private Functions --------------------------------------------------------
  77. function getFilterResultNode() {
  78. return filter.get('queryType') === 'classes' ? classesNode : modulesNode;
  79. }
  80. // -- Event Handlers -----------------------------------------------------------
  81. function onFilterResults(e) {
  82. var frag = Y.one(Y.config.doc.createDocumentFragment()),
  83. resultNode = getFilterResultNode(),
  84. typePlural = filter.get('queryType'),
  85. typeSingular = typePlural === 'classes' ? 'class' : 'module';
  86. if (e.results.length) {
  87. YArray.each(e.results, function (result) {
  88. frag.append(Lang.sub(LIST_ITEM_TEMPLATE, {
  89. rootPath : APIList.rootPath,
  90. displayName : filter.getDisplayName(result.highlighted),
  91. name : result.text,
  92. typePlural : typePlural,
  93. typeSingular: typeSingular
  94. }));
  95. });
  96. } else {
  97. frag.append(
  98. '<li class="message">' +
  99. 'No ' + typePlural + ' found.' +
  100. '</li>'
  101. );
  102. }
  103. resultNode.empty(true);
  104. resultNode.append(frag);
  105. focusManager.refresh();
  106. }
  107. function onSearchClear(e) {
  108. focusManager.refresh();
  109. }
  110. function onSearchKey(e) {
  111. var target = e.target;
  112. if (target.test('input,select,textarea')
  113. || target.get('isContentEditable')) {
  114. return;
  115. }
  116. e.preventDefault();
  117. inputNode.focus();
  118. focusManager.refresh();
  119. }
  120. function onSearchResults(e) {
  121. var frag = Y.one(Y.config.doc.createDocumentFragment());
  122. if (e.results.length) {
  123. YArray.each(e.results, function (result) {
  124. frag.append(result.display);
  125. });
  126. } else {
  127. frag.append(
  128. '<li class="message">' +
  129. 'No results found. Maybe you\'ll have better luck with a ' +
  130. 'different query?' +
  131. '</li>'
  132. );
  133. }
  134. focusManager.refresh();
  135. }
  136. function onTabSelectionChange(e) {
  137. var tab = e.newVal,
  138. name = tab.get('label').toLowerCase();
  139. tabs.selected = {
  140. index: tab.get('index'),
  141. name : name,
  142. tab : tab
  143. };
  144. switch (name) {
  145. case 'classes': // fallthru
  146. case 'modules':
  147. filter.setAttrs({
  148. minQueryLength: 0,
  149. queryType : name
  150. });
  151. search.set('minQueryLength', -1);
  152. // Only send a request if this isn't the initially-selected tab.
  153. if (e.prevVal) {
  154. filter.sendRequest(filter.get('value'));
  155. }
  156. break;
  157. case 'everything':
  158. filter.set('minQueryLength', -1);
  159. search.set('minQueryLength', 1);
  160. if (search.get('value')) {
  161. search.sendRequest(search.get('value'));
  162. } else {
  163. inputNode.focus();
  164. }
  165. break;
  166. default:
  167. // WTF? We shouldn't be here!
  168. filter.set('minQueryLength', -1);
  169. search.set('minQueryLength', -1);
  170. }
  171. if (focusManager) {
  172. setTimeout(function () {
  173. focusManager.refresh();
  174. }, 1);
  175. }
  176. }
  177. function onTabSwitchKey(e) {
  178. var currentTabIndex = tabs.selected.index;
  179. if (!(e.ctrlKey || e.metaKey)) {
  180. return;
  181. }
  182. e.preventDefault();
  183. switch (e.keyCode) {
  184. case 37: // left arrow
  185. if (currentTabIndex > 0) {
  186. tabview.selectChild(currentTabIndex - 1);
  187. inputNode.focus();
  188. }
  189. break;
  190. case 39: // right arrow
  191. if (currentTabIndex < (Y.Object.size(tabs) - 2)) {
  192. tabview.selectChild(currentTabIndex + 1);
  193. inputNode.focus();
  194. }
  195. break;
  196. }
  197. }
  198. }, '3.4.0', {requires: [
  199. 'api-filter', 'api-search', 'event-key', 'node-focusmanager', 'tabview'
  200. ]});