@shortfuse/materialdesignweb 0.0.9 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (392) hide show
  1. package/.browserslistrc +3 -0
  2. package/.eslintrc.json +146 -27
  3. package/.stylelintrc.json +598 -2
  4. package/.vscode/launch.json +20 -5
  5. package/.vscode/settings.json +3 -0
  6. package/.vscode/tasks.json +19 -10
  7. package/CHANGELOG.md +24 -0
  8. package/README.md +84 -2
  9. package/adapters/datatable/column.js +203 -0
  10. package/adapters/datatable/index.js +972 -0
  11. package/adapters/dom/index.js +601 -0
  12. package/adapters/list/index.js +69 -0
  13. package/adapters/search/index.js +521 -0
  14. package/components/appbar/_spec.scss +225 -0
  15. package/components/appbar/_theme.scss +0 -0
  16. package/components/appbar/index.scss +2 -0
  17. package/components/banner/_spec.scss +118 -0
  18. package/components/banner/_theme.scss +0 -0
  19. package/components/banner/index.scss +2 -0
  20. package/components/bottomnav/README.md +85 -0
  21. package/components/bottomnav/_spec.scss +157 -0
  22. package/components/bottomnav/_theme.scss +0 -0
  23. package/components/bottomnav/index.js +122 -0
  24. package/components/bottomnav/index.scss +2 -0
  25. package/components/bottomnav/item.js +89 -0
  26. package/components/{core/button → button}/README.md +16 -22
  27. package/components/button/_spec.scss +161 -0
  28. package/components/button/_theme.scss +65 -0
  29. package/components/button/index.eta +32 -0
  30. package/components/button/index.js +43 -0
  31. package/components/button/index.pug +18 -0
  32. package/components/button/index.scss +2 -0
  33. package/components/card/_spec.scss +249 -0
  34. package/components/card/_theme.scss +0 -0
  35. package/components/card/index.scss +2 -0
  36. package/components/chip/_spec.scss +134 -0
  37. package/components/chip/_theme.scss +177 -0
  38. package/components/chip/index.js +21 -0
  39. package/components/chip/index.scss +2 -0
  40. package/components/chip/item.js +20 -0
  41. package/components/datatable/_spec.scss +288 -0
  42. package/components/datatable/_theme.scss +154 -0
  43. package/components/datatable/cell.js +45 -0
  44. package/components/datatable/columnheader.js +47 -0
  45. package/components/datatable/index.js +388 -0
  46. package/components/datatable/index.scss +2 -0
  47. package/components/datatable/row.js +49 -0
  48. package/components/datatable/rowheader.js +18 -0
  49. package/components/dialog/_spec.scss +213 -0
  50. package/components/dialog/_theme.scss +0 -0
  51. package/components/dialog/index.js +627 -0
  52. package/components/dialog/index.scss +2 -0
  53. package/components/divider/_spec.scss +13 -0
  54. package/components/divider/_theme.scss +0 -0
  55. package/components/divider/index.scss +2 -0
  56. package/components/elevation/_spec.scss +9 -0
  57. package/components/elevation/_theme.scss +0 -0
  58. package/components/elevation/index.scss +2 -0
  59. package/components/fab/_spec.scss +222 -0
  60. package/components/fab/_theme.scss +0 -0
  61. package/components/fab/index.js +103 -0
  62. package/components/fab/index.scss +2 -0
  63. package/components/grid/_spec.scss +312 -0
  64. package/components/grid/_theme.scss +0 -0
  65. package/components/grid/index.scss +2 -0
  66. package/components/layout/_mixins.scss +33 -0
  67. package/components/layout/_spec.scss +1012 -0
  68. package/components/layout/_theme.scss +44 -0
  69. package/components/layout/index.js +464 -0
  70. package/components/layout/index.scss +2 -0
  71. package/components/list/_spec.scss +397 -0
  72. package/components/list/_theme.scss +111 -0
  73. package/components/list/content.js +110 -0
  74. package/components/list/index.js +260 -0
  75. package/components/list/index.scss +2 -0
  76. package/components/list/item.js +170 -0
  77. package/components/list/secondary.js +46 -0
  78. package/components/menu/_spec.scss +362 -0
  79. package/components/menu/_theme.scss +0 -0
  80. package/components/menu/index.js +721 -0
  81. package/components/menu/index.scss +2 -0
  82. package/components/menu/item.js +239 -0
  83. package/components/{core/progress/style.scss → progress/_spec.scss} +36 -25
  84. package/components/progress/_theme.scss +0 -0
  85. package/components/progress/index.js +36 -0
  86. package/components/progress/index.scss +2 -0
  87. package/components/selection/_spec.scss +386 -0
  88. package/components/selection/_theme.scss +166 -0
  89. package/components/selection/index.eta +60 -0
  90. package/components/selection/index.js +76 -0
  91. package/components/selection/index.pug +30 -0
  92. package/components/selection/index.scss +2 -0
  93. package/components/selection/input.js +56 -0
  94. package/components/selection/radiogroup.js +47 -0
  95. package/components/slider/_spec.scss +64 -0
  96. package/components/slider/_theme.scss +0 -0
  97. package/components/slider/index.scss +2 -0
  98. package/components/snackbar/_spec.scss +195 -0
  99. package/components/snackbar/_theme.scss +0 -0
  100. package/components/snackbar/index.js +344 -0
  101. package/components/snackbar/index.scss +2 -0
  102. package/components/tab/_spec.scss +235 -0
  103. package/components/tab/_theme.scss +0 -0
  104. package/components/tab/content.js +205 -0
  105. package/components/tab/index.js +260 -0
  106. package/components/tab/index.scss +2 -0
  107. package/components/tab/item.js +89 -0
  108. package/components/tab/list.js +210 -0
  109. package/components/tab/panel.js +54 -0
  110. package/components/template/_theme.scss +27 -0
  111. package/components/{core/textfield → textfield}/README.md +70 -50
  112. package/components/textfield/_mixins.scss +52 -0
  113. package/components/textfield/_spec.scss +809 -0
  114. package/components/textfield/_theme.scss +299 -0
  115. package/components/textfield/index.eta +74 -0
  116. package/components/textfield/index.js +168 -0
  117. package/components/textfield/index.pug +30 -0
  118. package/components/textfield/index.scss +2 -0
  119. package/components/tooltip/_spec.scss +188 -0
  120. package/components/tooltip/_theme.scss +0 -0
  121. package/components/tooltip/index.scss +2 -0
  122. package/components/type/_spec.scss +224 -0
  123. package/components/type/_theme.scss +0 -0
  124. package/components/type/index.scss +2 -0
  125. package/core/_breakpoint.scss +189 -0
  126. package/core/_elevation.scss +38 -0
  127. package/core/_length.scss +9 -0
  128. package/core/_motion.scss +31 -0
  129. package/core/_platform.scss +34 -0
  130. package/core/_type.scss +127 -0
  131. package/core/aria/attributes.js +141 -0
  132. package/core/aria/button.js +50 -0
  133. package/core/aria/keyboard.js +93 -0
  134. package/core/aria/rovingtabindex.js +178 -0
  135. package/core/aria/tab.js +60 -0
  136. package/core/color/_spec.scss +0 -0
  137. package/core/color/_theme.scss +390 -0
  138. package/core/color/index.scss +2 -0
  139. package/core/document/index.js +39 -0
  140. package/core/dom.js +271 -0
  141. package/core/overlay/_spec.scss +31 -0
  142. package/core/overlay/_theme.scss +171 -0
  143. package/core/overlay/index.js +108 -0
  144. package/core/overlay/index.scss +2 -0
  145. package/core/ripple/_spec.scss +197 -0
  146. package/core/ripple/_theme.scss +40 -0
  147. package/core/ripple/index.js +294 -0
  148. package/core/ripple/index.scss +2 -0
  149. package/core/theme/_config.scss +2 -0
  150. package/core/theme/_mixins.scss +172 -0
  151. package/{components/theming/palettes.scss → core/theme/_palettes.scss} +173 -150
  152. package/core/theme/_variables.scss +24 -0
  153. package/core/theme/index.js +50 -0
  154. package/core/throttler.js +42 -0
  155. package/core/transition/index.js +468 -0
  156. package/docs/_flex.scss +22 -0
  157. package/docs/_menuoptions.js +183 -0
  158. package/docs/_mixins.pug +155 -0
  159. package/docs/_partials/_androidnavbar.eta +5 -0
  160. package/docs/_partials/_androidstatusbar.eta +13 -0
  161. package/docs/_partials/_appbar.eta +29 -0
  162. package/docs/_partials/_buttontest.eta +31 -0
  163. package/docs/_partials/_header.eta +149 -0
  164. package/docs/_partials/_navlistitem.eta +16 -0
  165. package/docs/_partials/_target.eta +1 -0
  166. package/docs/_sample-utils.js +93 -0
  167. package/docs/_storage.js +33 -0
  168. package/docs/docs.scss +295 -0
  169. package/docs/index.eta +16 -0
  170. package/docs/index.js +0 -0
  171. package/docs/pages/appbar.eta +114 -0
  172. package/docs/pages/appbar.js +0 -0
  173. package/docs/pages/appbar.pug +78 -0
  174. package/docs/pages/bottomnav.eta +188 -0
  175. package/docs/pages/bottomnav.js +115 -0
  176. package/docs/pages/bottomnav.pug +137 -0
  177. package/docs/pages/button.eta +124 -0
  178. package/docs/pages/button.js +224 -0
  179. package/docs/pages/button.pug +121 -0
  180. package/docs/pages/card.eta +90 -0
  181. package/docs/pages/card.js +177 -0
  182. package/docs/pages/card.pug +74 -0
  183. package/docs/pages/chip.eta +122 -0
  184. package/docs/pages/chip.js +82 -0
  185. package/docs/pages/chip.pug +91 -0
  186. package/docs/pages/color.eta +143 -0
  187. package/docs/pages/color.js +262 -0
  188. package/docs/pages/color.pug +121 -0
  189. package/docs/pages/datatable.eta +323 -0
  190. package/docs/pages/datatable.js +164 -0
  191. package/docs/pages/datatable.pug +283 -0
  192. package/docs/pages/dialog.eta +186 -0
  193. package/docs/pages/dialog.js +177 -0
  194. package/docs/pages/dialog.pug +132 -0
  195. package/docs/pages/dom.eta +26 -0
  196. package/docs/pages/dom.js +143 -0
  197. package/docs/pages/dom.pug +22 -0
  198. package/docs/pages/elevation.eta +35 -0
  199. package/docs/pages/elevation.js +0 -0
  200. package/docs/pages/elevation.pug +25 -0
  201. package/docs/pages/fab.eta +99 -0
  202. package/docs/pages/fab.js +44 -0
  203. package/docs/pages/fab.pug +66 -0
  204. package/docs/pages/grid.eta +135 -0
  205. package/docs/pages/grid.js +128 -0
  206. package/docs/pages/grid.pug +95 -0
  207. package/docs/pages/layout.eta +8 -0
  208. package/docs/pages/layout.js +0 -0
  209. package/docs/pages/layout.pug +7 -0
  210. package/docs/pages/list.eta +465 -0
  211. package/docs/pages/list.js +9 -0
  212. package/docs/pages/list.pug +326 -0
  213. package/docs/pages/menu.eta +276 -0
  214. package/docs/pages/menu.js +217 -0
  215. package/docs/pages/menu.pug +205 -0
  216. package/docs/pages/overlay.eta +69 -0
  217. package/docs/pages/overlay.js +4 -0
  218. package/docs/pages/overlay.pug +55 -0
  219. package/docs/pages/progress.eta +23 -0
  220. package/docs/pages/progress.js +12 -0
  221. package/docs/pages/progress.pug +16 -0
  222. package/docs/pages/ripple.eta +27 -0
  223. package/docs/pages/ripple.js +4 -0
  224. package/docs/pages/ripple.pug +21 -0
  225. package/docs/pages/search.eta +246 -0
  226. package/docs/pages/search.js +243 -0
  227. package/docs/pages/search.pug +165 -0
  228. package/docs/pages/selection.eta +111 -0
  229. package/docs/pages/selection.js +13 -0
  230. package/docs/pages/selection.pug +74 -0
  231. package/docs/pages/slider.eta +23 -0
  232. package/docs/pages/slider.js +0 -0
  233. package/docs/pages/slider.pug +17 -0
  234. package/docs/pages/snackbar.eta +83 -0
  235. package/docs/pages/snackbar.js +158 -0
  236. package/docs/pages/snackbar.pug +60 -0
  237. package/docs/pages/tab.eta +421 -0
  238. package/docs/pages/tab.js +151 -0
  239. package/docs/pages/tab.pug +304 -0
  240. package/docs/pages/textfield.eta +486 -0
  241. package/docs/pages/textfield.js +254 -0
  242. package/docs/pages/textfield.pug +360 -0
  243. package/docs/pages/tooltip.eta +94 -0
  244. package/docs/pages/tooltip.js +0 -0
  245. package/docs/pages/tooltip.pug +78 -0
  246. package/docs/pages/transition.eta +117 -0
  247. package/docs/pages/transition.js +54 -0
  248. package/docs/pages/transition.pug +76 -0
  249. package/docs/pages/type.eta +31 -0
  250. package/docs/pages/type.js +0 -0
  251. package/docs/pages/type.pug +29 -0
  252. package/docs/postrender.js +39 -0
  253. package/docs/prerender.js +16 -0
  254. package/docs/pwa/_dialogs.eta +143 -0
  255. package/docs/pwa/_dialogs.pug +96 -0
  256. package/docs/pwa/_menus.eta +16 -0
  257. package/docs/pwa/_menus.pug +11 -0
  258. package/docs/pwa/pwa-prerender.js +3 -0
  259. package/docs/pwa/pwa.eta +480 -0
  260. package/docs/pwa/pwa.js +306 -0
  261. package/docs/pwa/pwa.pug +325 -0
  262. package/docs/pwa/pwa.scss +26 -0
  263. package/docs/spec.scss +26 -0
  264. package/docs/themes/_component-themes.scss +26 -0
  265. package/docs/themes/theme-colored-fallbacks.scss +17 -0
  266. package/docs/themes/theme-colored.scss +17 -0
  267. package/docs/themes/theme-default-fallbacks.scss +17 -0
  268. package/docs/themes/theme-default.scss +17 -0
  269. package/jsconfig.json +4 -2
  270. package/package.json +43 -28
  271. package/scripts/deploy-docs.sh +9 -0
  272. package/templates/index.eta +2 -0
  273. package/templates/index.pug +3 -0
  274. package/utils/function.js +3 -0
  275. package/webpack.config.cjs +257 -0
  276. package/components/_index.scss +0 -4
  277. package/components/all-components.scss +0 -14
  278. package/components/common/functions.scss +0 -173
  279. package/components/common/mixins.scss +0 -107
  280. package/components/common/motion.scss +0 -36
  281. package/components/common/ripple.scss +0 -92
  282. package/components/common/variables.scss +0 -48
  283. package/components/complex/dialog/style.scss +0 -159
  284. package/components/complex/dialog/theming.scss +0 -29
  285. package/components/complex/navdrawer/style.scss +0 -477
  286. package/components/complex/navdrawer/theming.scss +0 -58
  287. package/components/complex/search/index.js +0 -492
  288. package/components/core/bottomnav/README.md +0 -85
  289. package/components/core/bottomnav/index.js +0 -140
  290. package/components/core/bottomnav/style.scss +0 -173
  291. package/components/core/bottomnav/theming.scss +0 -42
  292. package/components/core/button/index.js +0 -52
  293. package/components/core/button/style.scss +0 -283
  294. package/components/core/button/theming.scss +0 -131
  295. package/components/core/list/index.js +0 -94
  296. package/components/core/list/style.scss +0 -269
  297. package/components/core/list/theming.scss +0 -74
  298. package/components/core/menu/index.js +0 -127
  299. package/components/core/menu/style.scss +0 -239
  300. package/components/core/menu/theming.scss +0 -55
  301. package/components/core/progress/index.js +0 -33
  302. package/components/core/selection/style.scss +0 -249
  303. package/components/core/selection/theming.scss +0 -49
  304. package/components/core/switch/style.scss +0 -3
  305. package/components/core/tab/index.js +0 -174
  306. package/components/core/tab/style.scss +0 -202
  307. package/components/core/tab/theming.scss +0 -43
  308. package/components/core/textfield/index.js +0 -169
  309. package/components/core/textfield/style.scss +0 -672
  310. package/components/core/textfield/theming.scss +0 -262
  311. package/components/core/toolbar/style.scss +0 -109
  312. package/components/core/toolbar/theming.scss +0 -28
  313. package/components/core/tooltip/style.scss +0 -127
  314. package/components/core/type/style.scss +0 -133
  315. package/components/core/type/theming.scss +0 -25
  316. package/components/index.js +0 -24
  317. package/components/template/theming.scss +0 -31
  318. package/components/theming/theming.scss +0 -504
  319. package/docs/bottomnav.html +0 -171
  320. package/docs/bottomnav.min.js +0 -383
  321. package/docs/button.html +0 -322
  322. package/docs/button.min.js +0 -251
  323. package/docs/components.min.css +0 -1
  324. package/docs/components.min.js +0 -83
  325. package/docs/dialog.html +0 -103
  326. package/docs/dialog.min.js +0 -160
  327. package/docs/docs.min.css +0 -1
  328. package/docs/docs.min.js +0 -83
  329. package/docs/index.html +0 -55
  330. package/docs/index.min.js +0 -83
  331. package/docs/list.html +0 -442
  332. package/docs/list.min.js +0 -312
  333. package/docs/menu.html +0 -185
  334. package/docs/menu.min.js +0 -370
  335. package/docs/navdrawer.html +0 -199
  336. package/docs/navdrawer.min.js +0 -244
  337. package/docs/progress.html +0 -75
  338. package/docs/progress.min.js +0 -162
  339. package/docs/search.html +0 -230
  340. package/docs/search.min.js +0 -1202
  341. package/docs/selection.html +0 -188
  342. package/docs/selection.min.js +0 -160
  343. package/docs/src/complex/dialog.js +0 -3
  344. package/docs/src/complex/dialog.pug +0 -44
  345. package/docs/src/complex/navdrawer.js +0 -82
  346. package/docs/src/complex/navdrawer.pug +0 -109
  347. package/docs/src/complex/search.js +0 -207
  348. package/docs/src/complex/search.pug +0 -143
  349. package/docs/src/components.scss +0 -1
  350. package/docs/src/core/bottomnav.js +0 -22
  351. package/docs/src/core/bottomnav.pug +0 -93
  352. package/docs/src/core/button.js +0 -16
  353. package/docs/src/core/button.pug +0 -73
  354. package/docs/src/core/list.js +0 -21
  355. package/docs/src/core/list.pug +0 -246
  356. package/docs/src/core/menu.js +0 -33
  357. package/docs/src/core/menu.pug +0 -108
  358. package/docs/src/core/progress.js +0 -11
  359. package/docs/src/core/progress.pug +0 -17
  360. package/docs/src/core/selection.js +0 -4
  361. package/docs/src/core/selection.pug +0 -92
  362. package/docs/src/core/tab.js +0 -21
  363. package/docs/src/core/tab.pug +0 -180
  364. package/docs/src/core/textfield.js +0 -15
  365. package/docs/src/core/textfield.pug +0 -274
  366. package/docs/src/core/toolbar.js +0 -4
  367. package/docs/src/core/toolbar.pug +0 -79
  368. package/docs/src/core/tooltip.js +0 -4
  369. package/docs/src/core/tooltip.pug +0 -76
  370. package/docs/src/core/type.js +0 -4
  371. package/docs/src/core/type.pug +0 -36
  372. package/docs/src/docs.scss +0 -200
  373. package/docs/src/index.pug +0 -5
  374. package/docs/src/mixins.pug +0 -72
  375. package/docs/src/targetHandler.js +0 -50
  376. package/docs/src/theming.ie11.scss +0 -35
  377. package/docs/src/theming.scss +0 -36
  378. package/docs/tab.html +0 -301
  379. package/docs/tab.min.js +0 -397
  380. package/docs/textfield.html +0 -476
  381. package/docs/textfield.min.js +0 -381
  382. package/docs/theming.ie11.min.css +0 -1
  383. package/docs/theming.ie11.min.js +0 -83
  384. package/docs/theming.min.css +0 -1
  385. package/docs/theming.min.js +0 -83
  386. package/docs/toolbar.html +0 -213
  387. package/docs/toolbar.min.js +0 -160
  388. package/docs/tooltip.html +0 -138
  389. package/docs/tooltip.min.js +0 -160
  390. package/docs/type.html +0 -94
  391. package/docs/type.min.js +0 -160
  392. package/webpack.config.js +0 -176
@@ -0,0 +1,521 @@
1
+ // https://www.w3.org/TR/wai-aria-practices/#combobox
2
+
3
+ import * as ListContent from '../../components/list/content.js';
4
+ import * as List from '../../components/list/index.js';
5
+ import * as TextField from '../../components/textfield/index.js';
6
+ import { getTextNode, iterateArrayLike, iterateSomeOfArrayLike } from '../../core/dom.js';
7
+ import { noop } from '../../utils/function.js';
8
+
9
+ /**
10
+ * @param {string} input
11
+ * @param {string} content
12
+ * @return {boolean}
13
+ */
14
+ function containsTextFilter(input, content) {
15
+ return content.trim().toLocaleLowerCase()
16
+ .indexOf(input.trim().toLocaleLowerCase()) !== -1;
17
+ }
18
+
19
+ /**
20
+ * @param {string} input
21
+ * @param {string} content
22
+ * @return {boolean}
23
+ */
24
+ function startsWithTextFilter(input, content) {
25
+ return content.trim().toLocaleLowerCase()
26
+ .indexOf(input.trim().toLocaleLowerCase()) === 0;
27
+ }
28
+
29
+ /**
30
+ * @param {Element} item
31
+ * @return {string}
32
+ */
33
+ function defaultItemTextParser(item) {
34
+ if (item.hasAttribute('data-mdw-search-text')) {
35
+ return item.getAttribute('data-mdw-search-text');
36
+ }
37
+ let textElement;
38
+ [
39
+ 'mdw-list__text-line',
40
+ 'mdw-list__text',
41
+ 'mdw-list__content',
42
+ ].some((className) => {
43
+ textElement = item.getElementsByClassName(className)[0];
44
+ return textElement != null;
45
+ });
46
+
47
+ const textNode = textElement ? getTextNode(textElement) : getTextNode(item);
48
+ if (textNode) {
49
+ return textNode.textContent || '';
50
+ }
51
+ return '';
52
+ }
53
+
54
+ /**
55
+ * @param {Element} list
56
+ * @param {boolean} [backwards=false]
57
+ * @return {Element} sibling
58
+ */
59
+ function selectSibling(list, backwards) {
60
+ const current = list.querySelector('[role="option"][aria-selected="true"]');
61
+ const items = list.querySelectorAll('[role="option"]:not([aria-hidden="true"]):not([disabled])');
62
+ let sibling;
63
+ if (current && current.getAttribute('aria-hidden') !== 'true') {
64
+ iterateSomeOfArrayLike(items, (item, index) => {
65
+ if (item !== current) {
66
+ return false;
67
+ }
68
+ if (backwards) {
69
+ sibling = items[index - 1];
70
+ } else {
71
+ sibling = items[index + 1];
72
+ }
73
+ return true;
74
+ });
75
+ } else if (backwards) {
76
+ sibling = items[items.length - 1];
77
+ } else {
78
+ sibling = items[0];
79
+ }
80
+ if (sibling && sibling !== current) {
81
+ if (current) {
82
+ current.setAttribute('aria-selected', 'false');
83
+ }
84
+ sibling.setAttribute('aria-selected', 'true');
85
+ return sibling;
86
+ }
87
+ return null;
88
+ }
89
+
90
+ /**
91
+ * @param {Element} listItem
92
+ * @return {void}
93
+ */
94
+ function scrollItemIntoView(listItem) {
95
+ /**
96
+ * @param {Element} el
97
+ * @return {number}
98
+ */
99
+ function getElementVisibility(el) {
100
+ let rect = el.getBoundingClientRect();
101
+ const { top, height } = rect;
102
+ let next = el.parentElement;
103
+ do {
104
+ rect = next.getBoundingClientRect();
105
+ if ((top + height) > rect.bottom) {
106
+ // bottom clipped
107
+ return 1;
108
+ }
109
+ if (top < rect.top) {
110
+ // top clipped
111
+ return -1;
112
+ }
113
+ next = next.parentElement;
114
+ } while (next !== document.body);
115
+ if (top < 0) {
116
+ return -1;
117
+ }
118
+ if ((top + height) > document.documentElement.clientHeight) {
119
+ return 1;
120
+ }
121
+ return 0;
122
+ }
123
+ const visibility = getElementVisibility(listItem);
124
+ if (visibility < 0) {
125
+ listItem.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'center' });
126
+ } else if (visibility > 0) {
127
+ listItem.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'center' });
128
+ }
129
+ }
130
+
131
+ /**
132
+ * @callback SearchAdapterTextFilter
133
+ * @param {string} input
134
+ * @param {string} content
135
+ * @return {boolean} passes
136
+ */
137
+
138
+ /**
139
+ * @template T
140
+ * @typedef SearchAdapterOptions
141
+ * @prop {Element} textfield
142
+ * @prop {Element} list
143
+ * @prop {('contains'|'startsWith'|SearchAdapterTextFilter)} [textFilter='contains']
144
+ * @prop {function(Element):string} [itemTextParser]
145
+ * @prop {boolean} [dropdown=false]
146
+ * @prop {boolean} [filterItems=true]
147
+ * @prop {('replace'|'append'|'none')} [suggestionMethod='replace']
148
+ * @prop {function(string, function(T[]=), function(Error=)=):void} [performSearch]
149
+ * @prop {function(T[], function(), function(Error=)):void} [updateList]
150
+ * @prop {boolean} [searchOnFocus=true]
151
+ * @prop {number} [debounce=null] Debounce time in milliseconds
152
+ */
153
+
154
+ /** @template T */
155
+ export default class SearchAdapter {
156
+ /** @param {SearchAdapterOptions<T>} options */
157
+ constructor(options) {
158
+ this.textfield = options.textfield;
159
+ this.list = options.list;
160
+ this.list.setAttribute('role', 'listbox');
161
+ List.attach(this.list);
162
+ if (typeof options.textFilter === 'function') {
163
+ this.filter = options.textFilter;
164
+ } else if (options.textFilter === 'startsWith') {
165
+ this.filter = startsWithTextFilter;
166
+ } else {
167
+ this.filter = containsTextFilter;
168
+ }
169
+ this.itemTextParser = defaultItemTextParser || options.itemTextParser;
170
+ /** @type {HTMLInputElement} */
171
+ const input = (this.textfield.getElementsByClassName('mdw-textfield__input')[0]);
172
+ this.input = input;
173
+ this.list.addEventListener(ListContent.ACTIVATE_EVENT, (event) => {
174
+ /** @type {HTMLElement} */
175
+ const item = (event.target);
176
+ this.onItemSelected(item);
177
+ const inputValue = this.input.value || '';
178
+ this.suggestedInput = inputValue;
179
+ this.input.setSelectionRange(inputValue.length, inputValue.length);
180
+ this.input.focus();
181
+ this.hideDropDown();
182
+ });
183
+ this.input.addEventListener('keydown', (event) => {
184
+ this.onTextFieldKeydownEvent(event);
185
+ });
186
+ this.input.addEventListener('input', (event) => {
187
+ if (this.handleInputEvent) {
188
+ this.handleInputEvent(event);
189
+ }
190
+ });
191
+ this.input.addEventListener('blur', (event) => {
192
+ if (this.handleBlurEvent) {
193
+ this.handleBlurEvent(event);
194
+ }
195
+ });
196
+ this.input.addEventListener('focus', (event) => {
197
+ if (this.searchOnFocus) {
198
+ this.handleInputEvent(event);
199
+ }
200
+ });
201
+
202
+ this.dropdown = options.dropdown;
203
+ if (options.filterItems !== false) {
204
+ this.filterItems = true;
205
+ }
206
+ if (options.searchOnFocus !== false) {
207
+ this.searchOnFocus = true;
208
+ }
209
+ this.debounce = options.debounce;
210
+ this.suggestionMethod = options.suggestionMethod || 'replace';
211
+ this.currentSearchTerm = this.input.value || '';
212
+ /** @type {string} */
213
+ this.suggestedInput = null;
214
+ /** @type {string} */
215
+ this.previousValue = null;
216
+ this.performSearch = options.performSearch || ((value, resolve = noop) => resolve());
217
+ this.updateList = options.updateList || ((results, resolve = noop) => resolve());
218
+ }
219
+
220
+ /**
221
+ * Default input handler
222
+ * @param {Event} [event]
223
+ * @return {void}
224
+ */
225
+ handleInputEvent(event) { // eslint-disable-line @typescript-eslint/no-unused-vars
226
+ if (document.activeElement !== this.input) {
227
+ return;
228
+ }
229
+ const inputValue = this.input.value || '';
230
+ if (inputValue === this.suggestedInput) {
231
+ return;
232
+ }
233
+ this.showDropDown();
234
+ if (inputValue === this.previousValue) {
235
+ return;
236
+ }
237
+ this.previousValue = inputValue;
238
+ this.currentSearchTerm = inputValue;
239
+
240
+ /**
241
+ * @param {Error} error
242
+ * @return {void}
243
+ */
244
+ function onErrorCallback(error) {
245
+ if (error.message === 'debounced') {
246
+ return;
247
+ }
248
+ if (error.message === 'expired') {
249
+ return;
250
+ }
251
+ throw error;
252
+ }
253
+
254
+ // IE does not support ES6 Promises
255
+ // Using resolve/reject callbacks instead.
256
+ // Syntactically ugly, but removes need for a polyfill.
257
+ this.performDebounce(inputValue,
258
+ () => this.checkExpired(inputValue,
259
+ () => this.performSearch(inputValue,
260
+ (searchResults) => this.checkExpired(inputValue,
261
+ () => this.updateList(searchResults,
262
+ () => this.filterListItems(),
263
+ onErrorCallback),
264
+ onErrorCallback),
265
+ onErrorCallback),
266
+ onErrorCallback),
267
+ onErrorCallback);
268
+
269
+ /**
270
+ * Promise.resolve()
271
+ * .then(() => this.performDebounce(inputValue))
272
+ * .then(() => this.checkExpired(inputValue))
273
+ * .then(() => this.performSearch(inputValue))
274
+ * .then((searchResults) => {
275
+ * results = searchResults;
276
+ * })
277
+ * .then(() => this.checkExpired(inputValue))
278
+ * .then(() => this.updateList(results))
279
+ * .then(() => this.filterListItems())
280
+ * .catch((error) => {
281
+ * if (error.message === 'debounced') {
282
+ * return;
283
+ * }
284
+ * if (error.message === 'expired') {
285
+ * return;
286
+ * }
287
+ * throw error;
288
+ * });
289
+ */
290
+ }
291
+
292
+ /**
293
+ * @param {string} inputValue
294
+ * @param {Function} resolve
295
+ * @param {function(Error):any} reject
296
+ * @return {void}
297
+ */
298
+ checkExpired(inputValue, resolve, reject) {
299
+ if (inputValue === this.currentSearchTerm) {
300
+ resolve();
301
+ } else {
302
+ reject(new Error('expired'));
303
+ }
304
+ }
305
+
306
+ /**
307
+ * @param {string} searchTerm
308
+ * @param {Function} resolve
309
+ * @param {function(Error):any} reject
310
+ * @return {void}
311
+ */
312
+ performDebounce(searchTerm, resolve, reject) {
313
+ if (!this.debounce) {
314
+ resolve();
315
+ return;
316
+ }
317
+ setTimeout(() => {
318
+ if (searchTerm !== this.currentSearchTerm) {
319
+ reject(new Error('debounced'));
320
+ return;
321
+ }
322
+ resolve();
323
+ }, this.debounce);
324
+ }
325
+
326
+ /** @return {boolean} handled */
327
+ showDropDown() {
328
+ if (!this.dropdown) {
329
+ return false;
330
+ }
331
+ const dropDownElement = this.textfield.querySelector('.mdw-textfield__dropdown');
332
+ let changed = false;
333
+ if (dropDownElement.hasAttribute('mdw-hide')) {
334
+ dropDownElement.removeAttribute('mdw-hide');
335
+ changed = true;
336
+ }
337
+ if (!dropDownElement.hasAttribute('mdw-show')) {
338
+ dropDownElement.setAttribute('mdw-show', '');
339
+ changed = true;
340
+ }
341
+ return changed;
342
+ }
343
+
344
+ /** @return {boolean} handled */
345
+ isDropDownShown() {
346
+ if (!this.dropdown) {
347
+ return true;
348
+ }
349
+ const dropDownElement = this.textfield.querySelector('.mdw-textfield__dropdown');
350
+ if (dropDownElement.hasAttribute('mdw-hide')) {
351
+ return false;
352
+ }
353
+ if (this.input === document.activeElement) {
354
+ return true;
355
+ }
356
+ if (dropDownElement.hasAttribute('mdw-show')) {
357
+ return true;
358
+ }
359
+ return false;
360
+ }
361
+
362
+ /** @return {boolean} handled */
363
+ hideDropDown() {
364
+ if (!this.dropdown) {
365
+ return true;
366
+ }
367
+ const dropDownElement = this.textfield.querySelector('.mdw-textfield__dropdown');
368
+ if (!dropDownElement.hasAttribute('mdw-hide')) {
369
+ dropDownElement.setAttribute('mdw-hide', '');
370
+ return true;
371
+ }
372
+ return false;
373
+ }
374
+
375
+ /**
376
+ * @param {Event|FocusEvent} event
377
+ * @return {void}
378
+ */
379
+ handleBlurEvent(event) { // eslint-disable-line @typescript-eslint/no-unused-vars
380
+ if (this.dropdown) {
381
+ const dropDownElement = this.textfield.querySelector('.mdw-textfield__dropdown');
382
+ if (dropDownElement.hasAttribute('mdw-show')) {
383
+ dropDownElement.removeAttribute('mdw-show');
384
+ }
385
+ }
386
+ }
387
+
388
+ /**
389
+ * @param {Element} item
390
+ * @return {void}
391
+ */
392
+ onItemSelected(item) {
393
+ if (this.suggestionMethod === 'none') {
394
+ return;
395
+ }
396
+ let suggestion = this.itemTextParser(item);
397
+ if (suggestion) {
398
+ suggestion = suggestion.trim();
399
+ }
400
+ if (!suggestion) {
401
+ return;
402
+ }
403
+ this.suggestedInput = suggestion;
404
+ if (this.suggestionMethod === 'replace') {
405
+ TextField.setValue(this.textfield, suggestion);
406
+ } else if (this.suggestionMethod === 'append') {
407
+ const selectionStart = (this.previousValue || '').length;
408
+ const selectionEnd = suggestion.length;
409
+ TextField.setValue(this.textfield, suggestion);
410
+ this.input.setSelectionRange(selectionStart, selectionEnd);
411
+ }
412
+ }
413
+
414
+ /**
415
+ * @param {SearchAdapterTextFilter} [fnFilter]
416
+ * @return {void}
417
+ */
418
+ filterListItems(fnFilter) {
419
+ if (!this.filterItems) {
420
+ return;
421
+ }
422
+ const input = this.input.value;
423
+ const current = this.list.querySelector('[role="option"][aria-selected="true"]');
424
+ const items = this.list.querySelectorAll('[role="option"]');
425
+ let hasItem = false;
426
+ iterateArrayLike(items, (item) => {
427
+ const content = this.itemTextParser(item);
428
+ const fn = fnFilter || this.filter;
429
+ if (fn(input, content)) {
430
+ hasItem = true;
431
+ item.setAttribute('aria-hidden', 'false');
432
+ } else {
433
+ item.setAttribute('aria-hidden', 'true');
434
+ }
435
+ });
436
+ if (current && current.getAttribute('aria-hidden') === 'true') {
437
+ const newSelection = selectSibling(this.list);
438
+ if (newSelection) {
439
+ this.onItemSelected(newSelection);
440
+ }
441
+ }
442
+ if (!hasItem) {
443
+ this.hideDropDown();
444
+ }
445
+ }
446
+
447
+ /**
448
+ * @param {KeyboardEvent} event
449
+ * @return {void}
450
+ */
451
+ onTextFieldKeydownEvent(event) {
452
+ if (event.defaultPrevented) {
453
+ return;
454
+ }
455
+ if (event.ctrlKey || event.altKey) {
456
+ return;
457
+ }
458
+ switch (event.key) {
459
+ case 'ArrowUp':
460
+ case 'Up': {
461
+ if (this.isDropDownShown()) {
462
+ const sibling = selectSibling(this.list, true);
463
+ if (sibling) {
464
+ scrollItemIntoView(sibling);
465
+ this.onItemSelected(sibling);
466
+ }
467
+ }
468
+ event.stopPropagation();
469
+ event.preventDefault();
470
+ break;
471
+ }
472
+ case 'ArrowDown':
473
+ case 'Down': {
474
+ if (this.isDropDownShown()) {
475
+ const sibling = selectSibling(this.list, false);
476
+ if (sibling) {
477
+ scrollItemIntoView(sibling);
478
+ this.onItemSelected(sibling);
479
+ }
480
+ }
481
+ event.stopPropagation();
482
+ event.preventDefault();
483
+ break;
484
+ }
485
+ case 'Esc':
486
+ case 'Escape': {
487
+ if (this.hideDropDown()) {
488
+ this.suggestedInput = this.previousValue;
489
+ TextField.setValue(this.textfield, this.previousValue);
490
+ event.stopPropagation();
491
+ event.preventDefault();
492
+ }
493
+ break;
494
+ }
495
+ case 'Enter': {
496
+ /** @type {HTMLElement} */
497
+ const current = this.list.querySelector('[role="option"][aria-selected="true"]');
498
+ if (current) {
499
+ if (this.hideDropDown()) {
500
+ current.click();
501
+ event.stopPropagation();
502
+ event.preventDefault();
503
+ }
504
+ }
505
+ break;
506
+ }
507
+ case 'Tab': {
508
+ /** @type {HTMLElement} */
509
+ const current = this.list.querySelector('[role="option"][aria-selected="true"]');
510
+ if (current) {
511
+ if (this.hideDropDown()) {
512
+ current.click();
513
+ event.stopPropagation();
514
+ }
515
+ }
516
+ break;
517
+ }
518
+ default:
519
+ }
520
+ }
521
+ }