adapter.d.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * @license
  3. * Copyright 2019 Google Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. import { SortValue } from './constants';
  24. import { MDCDataTableRowSelectionChangedEventDetail, ProgressIndicatorStyles, RowClickEventData, SortActionEventDetail } from './types';
  25. /**
  26. * Defines the shape of the adapter expected by the foundation.
  27. * Implement this adapter for your framework of choice to delegate updates to
  28. * the component in your framework of choice. See architecture documentation
  29. * for more details.
  30. * https://github.com/material-components/material-components-web/blob/master/docs/code/architecture.md
  31. */
  32. export interface MDCDataTableAdapter {
  33. /**
  34. * Adds CSS class name to root element.
  35. *
  36. * @param className CSS class name to add to root element.
  37. */
  38. addClass(className: string): void;
  39. /**
  40. * Removes CSS class name from root element.
  41. *
  42. * @param className CSS class name to add to root element.
  43. */
  44. removeClass(className: string): void;
  45. /**
  46. * Adds a class name to row element at given row index excluding header row.
  47. *
  48. * @param rowIndex Index of row element excluding header row.
  49. * @param cssClasses CSS Class string to add.
  50. */
  51. addClassAtRowIndex(rowIndex: number, cssClasses: string): void;
  52. /**
  53. * @return Row count excluding header row.
  54. */
  55. getRowCount(): number;
  56. /**
  57. * @return Array of row elements excluding header row.
  58. */
  59. getRowElements(): HTMLElement[];
  60. /**
  61. * Returns row id of row element at given row index based on `data-row-id`
  62. * attribute on row element `tr`.
  63. *
  64. * @param rowIndex Index of row element.
  65. * @return Row id of row element, returns `null` in absence of `data-row-id`
  66. * attribute on row element.
  67. */
  68. getRowIdAtIndex(rowIndex: number): string | null;
  69. /**
  70. * Returns index of row element that contains give child element. Returns -1
  71. * if element is not child of any row element.
  72. *
  73. * @param el Child element of row element.
  74. * @return Index of row element.
  75. */
  76. getRowIndexByChildElement(el: Element): number;
  77. /**
  78. * @return Selected row count.
  79. */
  80. getSelectedRowCount(): number;
  81. /**
  82. * @param rowIndex Index of row element.
  83. * @return True if row checkbox at given row index is checked.
  84. */
  85. isCheckboxAtRowIndexChecked(rowIndex: number): boolean;
  86. /**
  87. * @return True if header row checkbox is checked.
  88. */
  89. isHeaderRowCheckboxChecked(): boolean;
  90. /**
  91. * @return True if table rows are selectable.
  92. */
  93. isRowsSelectable(): boolean;
  94. /**
  95. * Notifies when row selection is changed.
  96. *
  97. * @param data Event detail data for row selection changed event.
  98. */
  99. notifyRowSelectionChanged(data: MDCDataTableRowSelectionChangedEventDetail): void;
  100. /**
  101. * Notifies when header row is checked.
  102. */
  103. notifySelectedAll(): void;
  104. /**
  105. * Notifies when header row is unchecked.
  106. */
  107. notifyUnselectedAll(): void;
  108. /**
  109. * Notifies when data row is clicked.
  110. */
  111. notifyRowClick(detail: RowClickEventData): void;
  112. /**
  113. * Initializes header row checkbox. Destroys previous header row checkbox
  114. * instance if any.
  115. * @return Can return Promise only if registering checkbox is asynchronous.
  116. */
  117. registerHeaderRowCheckbox(): Promise<void> | void;
  118. /**
  119. * Initializes all row checkboxes. Destroys previous row checkbox instances if
  120. * any. This is usually called when row checkboxes are added or removed from
  121. * table.
  122. * @return Can return Promise only if registering checkbox is asynchronous.
  123. */
  124. registerRowCheckboxes(): Promise<void> | void;
  125. /**
  126. * Removes class name from row element at give row index.
  127. *
  128. * @param rowIndex Index of row element excluding header row element.
  129. * @param cssClasses Class name string.
  130. */
  131. removeClassAtRowIndex(rowIndex: number, cssClasses: string): void;
  132. /**
  133. * Sets attribute to row element at given row index.
  134. *
  135. * @param rowIndex Index of row element excluding header row element.
  136. * @param attr Name of attribute.
  137. * @param value Value of attribute.
  138. */
  139. setAttributeAtRowIndex(rowIndex: number, attr: string, value: string): void;
  140. /**
  141. * Sets header row checkbox checked or unchecked.
  142. *
  143. * @param checked True to set header row checkbox checked.
  144. */
  145. setHeaderRowCheckboxChecked(checked: boolean): void;
  146. /**
  147. * Sets header row checkbox to indeterminate.
  148. *
  149. * @param indeterminate True to set header row checkbox indeterminate.
  150. */
  151. setHeaderRowCheckboxIndeterminate(indeterminate: boolean): void;
  152. /**
  153. * Sets row checkbox to checked or unchecked at given row index.
  154. *
  155. * @param rowIndex Index of row element excluding header row element.
  156. * @param checked True to set checked.
  157. */
  158. setRowCheckboxCheckedAtIndex(rowIndex: number, checked: boolean): void;
  159. /**
  160. * @return Total count of header cells.
  161. */
  162. getHeaderCellCount(): number;
  163. /**
  164. * @return Array of header cell elements.
  165. */
  166. getHeaderCellElements(): HTMLElement[];
  167. /**
  168. * @return Attribute value for given header cell index.
  169. */
  170. getAttributeByHeaderCellIndex(columnIndex: number, attribute: string): string | null;
  171. /**
  172. * Sets attribute of a header cell by index.
  173. */
  174. setAttributeByHeaderCellIndex(columnIndex: number, attribute: string, value: string): void;
  175. /**
  176. * Sets class name of a header cell by index.
  177. */
  178. setClassNameByHeaderCellIndex(columnIndex: number, className: string): void;
  179. /**
  180. * Removes a class name of a header cell by index.
  181. */
  182. removeClassNameByHeaderCellIndex(columnIndex: number, className: string): void;
  183. /**
  184. * Notifies when column is sorted.
  185. */
  186. notifySortAction(data: SortActionEventDetail): void;
  187. /**
  188. * @return Returns computed styles height of table container element.
  189. */
  190. getTableContainerHeight(): number;
  191. /**
  192. * @return Returns computed styles height of table's header element.
  193. */
  194. getTableHeaderHeight(): number;
  195. /**
  196. * Sets progress indicator CSS styles to position it on top of table body.
  197. */
  198. setProgressIndicatorStyles(styles: ProgressIndicatorStyles): void;
  199. /**
  200. * Sets appropriate sort status label by header cell index. Skips setting sort
  201. * status label if header cell is not sortable.
  202. *
  203. * Example status label to set for sortValue:
  204. *
  205. * - `SortValue.ASCENDING`: 'Sorted in ascending order'
  206. * - `SortValue.DESCENDING`: 'Sorted in descending order'
  207. * - `SortValue.NONE`: '' (Empty string)
  208. */
  209. setSortStatusLabelByHeaderCellIndex(columnIndex: number, sortValue: SortValue): void;
  210. }