1 | import {GridOptions} from '../entities/gridOptions';
|
2 | import {GridApi} from '../gridApi';
|
3 | import {ComponentStateChangedEvent, Events} from '../events';
|
4 | import {PropertyKeys} from '../propertyKeys';
|
5 | import {Utils as _} from '../utils';
|
6 | import {ColumnApi} from '../columnController/columnApi';
|
7 | import {GridOptionsWrapper} from "../gridOptionsWrapper";
|
8 |
|
9 | export class ComponentUtil {
|
10 |
|
11 | public static EVENTS: string[] = [];
|
12 |
|
13 |
|
14 | private static EVENT_CALLBACKS: string[];
|
15 |
|
16 |
|
17 | private static EVENT_CALLBACKS_NO_PREFIX: string[];
|
18 |
|
19 | public static STRING_PROPERTIES = PropertyKeys.STRING_PROPERTIES;
|
20 |
|
21 | public static OBJECT_PROPERTIES = PropertyKeys.OBJECT_PROPERTIES;
|
22 |
|
23 | public static ARRAY_PROPERTIES = PropertyKeys.ARRAY_PROPERTIES;
|
24 |
|
25 | public static NUMBER_PROPERTIES = PropertyKeys.NUMBER_PROPERTIES;
|
26 |
|
27 | public static BOOLEAN_PROPERTIES = PropertyKeys.BOOLEAN_PROPERTIES;
|
28 |
|
29 | public static FUNCTION_PROPERTIES = PropertyKeys.FUNCTION_PROPERTIES;
|
30 |
|
31 | public static ALL_PROPERTIES = PropertyKeys.ALL_PROPERTIES;
|
32 |
|
33 | public static getEventCallbacks(): string[] {
|
34 | if (!ComponentUtil.EVENT_CALLBACKS) {
|
35 | ComponentUtil.EVENT_CALLBACKS = [];
|
36 | ComponentUtil.EVENT_CALLBACKS_NO_PREFIX = [];
|
37 |
|
38 | ComponentUtil.EVENTS.forEach((eventName: string) => {
|
39 | ComponentUtil.EVENT_CALLBACKS.push(ComponentUtil.getCallbackForEvent(eventName));
|
40 | ComponentUtil.EVENT_CALLBACKS_NO_PREFIX.push(eventName);
|
41 | });
|
42 | }
|
43 | return ComponentUtil.EVENT_CALLBACKS;
|
44 | }
|
45 |
|
46 | public static copyAttributesToGridOptions(gridOptions: GridOptions, component: any, skipEventDeprecationCheck: boolean = false): GridOptions {
|
47 | checkForDeprecated(component);
|
48 |
|
49 | if (typeof gridOptions !== 'object') {
|
50 | gridOptions = <GridOptions>{};
|
51 | }
|
52 |
|
53 | let pGridOptions = <any>gridOptions;
|
54 |
|
55 | ComponentUtil.ARRAY_PROPERTIES
|
56 | .concat(ComponentUtil.STRING_PROPERTIES)
|
57 | .concat(ComponentUtil.OBJECT_PROPERTIES)
|
58 | .concat(ComponentUtil.FUNCTION_PROPERTIES)
|
59 | .forEach(key => {
|
60 | if (typeof component[key] !== 'undefined') {
|
61 | pGridOptions[key] = component[key];
|
62 | }
|
63 | });
|
64 | ComponentUtil.BOOLEAN_PROPERTIES.forEach(key => {
|
65 | if (typeof component[key] !== 'undefined') {
|
66 | pGridOptions[key] = ComponentUtil.toBoolean(component[key]);
|
67 | }
|
68 | });
|
69 | ComponentUtil.NUMBER_PROPERTIES.forEach(key => {
|
70 | if (typeof component[key] !== 'undefined') {
|
71 | pGridOptions[key] = ComponentUtil.toNumber(component[key]);
|
72 | }
|
73 | });
|
74 | ComponentUtil.getEventCallbacks().forEach(funcName => {
|
75 | if (typeof component[funcName] !== 'undefined') {
|
76 | pGridOptions[funcName] = component[funcName];
|
77 | }
|
78 | });
|
79 |
|
80 |
|
81 | if (!skipEventDeprecationCheck) {
|
82 | ComponentUtil.EVENT_CALLBACKS_NO_PREFIX.forEach(funcName => {
|
83 |
|
84 | const onMethodName = ComponentUtil.getCallbackForEvent(funcName);
|
85 |
|
86 | if (typeof component[funcName] !== 'undefined' ||
|
87 | typeof component[onMethodName] !== 'undefined') {
|
88 | GridOptionsWrapper.checkEventDeprecation(funcName);
|
89 | }
|
90 | });
|
91 | }
|
92 |
|
93 | return gridOptions;
|
94 | }
|
95 |
|
96 | public static getCallbackForEvent(eventName: string): string {
|
97 | if (!eventName || eventName.length < 2) {
|
98 | return eventName;
|
99 | } else {
|
100 | return 'on' + eventName[0].toUpperCase() + eventName.substr(1);
|
101 | }
|
102 | }
|
103 |
|
104 | public static processOnChange(changes: any, gridOptions: GridOptions, api: GridApi, columnApi: ColumnApi): void {
|
105 | if (!changes) {
|
106 | return;
|
107 | }
|
108 |
|
109 | checkForDeprecated(changes);
|
110 |
|
111 |
|
112 | let pGridOptions = <any>gridOptions;
|
113 |
|
114 |
|
115 | ComponentUtil.ARRAY_PROPERTIES
|
116 | .concat(ComponentUtil.OBJECT_PROPERTIES)
|
117 | .concat(ComponentUtil.STRING_PROPERTIES)
|
118 | .forEach(key => {
|
119 | if (changes[key]) {
|
120 | pGridOptions[key] = changes[key].currentValue;
|
121 | }
|
122 | });
|
123 | ComponentUtil.BOOLEAN_PROPERTIES.forEach(key => {
|
124 | if (changes[key]) {
|
125 | pGridOptions[key] = ComponentUtil.toBoolean(changes[key].currentValue);
|
126 | }
|
127 | });
|
128 | ComponentUtil.NUMBER_PROPERTIES.forEach(key => {
|
129 | if (changes[key]) {
|
130 | pGridOptions[key] = ComponentUtil.toNumber(changes[key].currentValue);
|
131 | }
|
132 | });
|
133 | ComponentUtil.getEventCallbacks().forEach(funcName => {
|
134 | if (changes[funcName]) {
|
135 | pGridOptions[funcName] = changes[funcName].currentValue;
|
136 | }
|
137 | });
|
138 |
|
139 | if (changes.showToolPanel) {
|
140 | api.showToolPanel(ComponentUtil.toBoolean(changes.showToolPanel.currentValue));
|
141 | }
|
142 |
|
143 | if (changes.quickFilterText) {
|
144 | api.setQuickFilter(changes.quickFilterText.currentValue);
|
145 | }
|
146 |
|
147 | if (changes.rowData) {
|
148 | api.setRowData(changes.rowData.currentValue);
|
149 | }
|
150 |
|
151 | if (changes.pinnedTopRowData) {
|
152 | api.setPinnedTopRowData(changes.pinnedTopRowData.currentValue);
|
153 | }
|
154 |
|
155 | if (changes.pinnedBottomRowData) {
|
156 | api.setPinnedBottomRowData(changes.pinnedBottomRowData.currentValue);
|
157 | }
|
158 |
|
159 | if (changes.columnDefs) {
|
160 | api.setColumnDefs(changes.columnDefs.currentValue, "gridOptionsChanged");
|
161 | }
|
162 |
|
163 | if (changes.datasource) {
|
164 | api.setDatasource(changes.datasource.currentValue);
|
165 | }
|
166 |
|
167 | if (changes.headerHeight) {
|
168 | api.setHeaderHeight(ComponentUtil.toNumber(changes.headerHeight.currentValue));
|
169 | }
|
170 |
|
171 | if (changes.paginationPageSize) {
|
172 | api.paginationSetPageSize(ComponentUtil.toNumber(changes.paginationPageSize.currentValue));
|
173 | }
|
174 |
|
175 | if (changes.pivotMode) {
|
176 | columnApi.setPivotMode(ComponentUtil.toBoolean(changes.pivotMode.currentValue));
|
177 | }
|
178 |
|
179 | if (changes.groupRemoveSingleChildren) {
|
180 | api.setGroupRemoveSingleChildren(ComponentUtil.toBoolean(changes.groupRemoveSingleChildren.currentValue));
|
181 | }
|
182 |
|
183 | if (changes.suppressRowDrag) {
|
184 | api.setSuppressRowDrag(ComponentUtil.toBoolean(changes.suppressRowDrag.currentValue));
|
185 | }
|
186 |
|
187 | if (changes.gridAutoHeight) {
|
188 | api.setGridAutoHeight(ComponentUtil.toBoolean(changes.gridAutoHeight.currentValue));
|
189 | }
|
190 |
|
191 | if (changes.suppressClipboardPaste) {
|
192 | api.setSuppressClipboardPaste(ComponentUtil.toBoolean(changes.suppressClipboardPaste.currentValue));
|
193 | }
|
194 |
|
195 |
|
196 | let event: ComponentStateChangedEvent = {
|
197 | type: Events.EVENT_COMPONENT_STATE_CHANGED,
|
198 | api: gridOptions.api,
|
199 | columnApi: gridOptions.columnApi
|
200 | };
|
201 | _.iterateObject(changes, (key: string, value: any) => {
|
202 | (<any>event)[key] = value;
|
203 | });
|
204 |
|
205 | api.dispatchEvent(event);
|
206 | }
|
207 |
|
208 | public static toBoolean(value: any): boolean {
|
209 | if (typeof value === 'boolean') {
|
210 | return value;
|
211 | } else if (typeof value === 'string') {
|
212 |
|
213 |
|
214 | return value.toUpperCase() === 'TRUE' || value == '';
|
215 | } else {
|
216 | return false;
|
217 | }
|
218 | }
|
219 |
|
220 | public static toNumber(value: any): number {
|
221 | if (typeof value === 'number') {
|
222 | return value;
|
223 | } else if (typeof value === 'string') {
|
224 | return Number(value);
|
225 | } else {
|
226 | return undefined;
|
227 | }
|
228 | }
|
229 | }
|
230 |
|
231 | _.iterateObject<any>(Events, function (key, value) {
|
232 | ComponentUtil.EVENTS.push(value);
|
233 | });
|
234 |
|
235 | function checkForDeprecated(changes: any): void {
|
236 | if (changes.rowDeselected || changes.onRowDeselected) {
|
237 | console.warn('ag-grid: as of v3.4 rowDeselected no longer exists. Please check the docs.');
|
238 | }
|
239 | } |
\ | No newline at end of file |