UNPKG

9.08 kBJavaScriptView Raw
1/**
2 * ag-grid - Advanced Data Grid / Data Table supporting Javascript / React / AngularJS / Web Components
3 * @version v18.1.2
4 * @link http://www.ag-grid.com/
5 * @license MIT
6 */
7"use strict";
8Object.defineProperty(exports, "__esModule", { value: true });
9var events_1 = require("../events");
10var propertyKeys_1 = require("../propertyKeys");
11var utils_1 = require("../utils");
12var gridOptionsWrapper_1 = require("../gridOptionsWrapper");
13var ComponentUtil = (function () {
14 function ComponentUtil() {
15 }
16 ComponentUtil.getEventCallbacks = function () {
17 if (!ComponentUtil.EVENT_CALLBACKS) {
18 ComponentUtil.EVENT_CALLBACKS = [];
19 ComponentUtil.EVENT_CALLBACKS_NO_PREFIX = [];
20 ComponentUtil.EVENTS.forEach(function (eventName) {
21 ComponentUtil.EVENT_CALLBACKS.push(ComponentUtil.getCallbackForEvent(eventName));
22 ComponentUtil.EVENT_CALLBACKS_NO_PREFIX.push(eventName);
23 });
24 }
25 return ComponentUtil.EVENT_CALLBACKS;
26 };
27 ComponentUtil.copyAttributesToGridOptions = function (gridOptions, component, skipEventDeprecationCheck) {
28 if (skipEventDeprecationCheck === void 0) { skipEventDeprecationCheck = false; }
29 checkForDeprecated(component);
30 // create empty grid options if none were passed
31 if (typeof gridOptions !== 'object') {
32 gridOptions = {};
33 }
34 // to allow array style lookup in TypeScript, take type away from 'this' and 'gridOptions'
35 var pGridOptions = gridOptions;
36 // add in all the simple properties
37 ComponentUtil.ARRAY_PROPERTIES
38 .concat(ComponentUtil.STRING_PROPERTIES)
39 .concat(ComponentUtil.OBJECT_PROPERTIES)
40 .concat(ComponentUtil.FUNCTION_PROPERTIES)
41 .forEach(function (key) {
42 if (typeof component[key] !== 'undefined') {
43 pGridOptions[key] = component[key];
44 }
45 });
46 ComponentUtil.BOOLEAN_PROPERTIES.forEach(function (key) {
47 if (typeof component[key] !== 'undefined') {
48 pGridOptions[key] = ComponentUtil.toBoolean(component[key]);
49 }
50 });
51 ComponentUtil.NUMBER_PROPERTIES.forEach(function (key) {
52 if (typeof component[key] !== 'undefined') {
53 pGridOptions[key] = ComponentUtil.toNumber(component[key]);
54 }
55 });
56 ComponentUtil.getEventCallbacks().forEach(function (funcName) {
57 if (typeof component[funcName] !== 'undefined') {
58 pGridOptions[funcName] = component[funcName];
59 }
60 });
61 // purely for event deprecation checks (for frameworks - wouldn't apply for non-fw versions)
62 if (!skipEventDeprecationCheck) {
63 ComponentUtil.EVENT_CALLBACKS_NO_PREFIX.forEach(function (funcName) {
64 // react uses onXXX...not sure why this is diff to the other frameworks
65 var onMethodName = ComponentUtil.getCallbackForEvent(funcName);
66 if (typeof component[funcName] !== 'undefined' ||
67 typeof component[onMethodName] !== 'undefined') {
68 gridOptionsWrapper_1.GridOptionsWrapper.checkEventDeprecation(funcName);
69 }
70 });
71 }
72 return gridOptions;
73 };
74 ComponentUtil.getCallbackForEvent = function (eventName) {
75 if (!eventName || eventName.length < 2) {
76 return eventName;
77 }
78 else {
79 return 'on' + eventName[0].toUpperCase() + eventName.substr(1);
80 }
81 };
82 ComponentUtil.processOnChange = function (changes, gridOptions, api, columnApi) {
83 if (!changes) {
84 return;
85 }
86 checkForDeprecated(changes);
87 // to allow array style lookup in TypeScript, take type away from 'this' and 'gridOptions'
88 var pGridOptions = gridOptions;
89 // check if any change for the simple types, and if so, then just copy in the new value
90 ComponentUtil.ARRAY_PROPERTIES
91 .concat(ComponentUtil.OBJECT_PROPERTIES)
92 .concat(ComponentUtil.STRING_PROPERTIES)
93 .forEach(function (key) {
94 if (changes[key]) {
95 pGridOptions[key] = changes[key].currentValue;
96 }
97 });
98 ComponentUtil.BOOLEAN_PROPERTIES.forEach(function (key) {
99 if (changes[key]) {
100 pGridOptions[key] = ComponentUtil.toBoolean(changes[key].currentValue);
101 }
102 });
103 ComponentUtil.NUMBER_PROPERTIES.forEach(function (key) {
104 if (changes[key]) {
105 pGridOptions[key] = ComponentUtil.toNumber(changes[key].currentValue);
106 }
107 });
108 ComponentUtil.getEventCallbacks().forEach(function (funcName) {
109 if (changes[funcName]) {
110 pGridOptions[funcName] = changes[funcName].currentValue;
111 }
112 });
113 if (changes.showToolPanel) {
114 api.showToolPanel(ComponentUtil.toBoolean(changes.showToolPanel.currentValue));
115 }
116 if (changes.quickFilterText) {
117 api.setQuickFilter(changes.quickFilterText.currentValue);
118 }
119 if (changes.rowData) {
120 api.setRowData(changes.rowData.currentValue);
121 }
122 if (changes.pinnedTopRowData) {
123 api.setPinnedTopRowData(changes.pinnedTopRowData.currentValue);
124 }
125 if (changes.pinnedBottomRowData) {
126 api.setPinnedBottomRowData(changes.pinnedBottomRowData.currentValue);
127 }
128 if (changes.columnDefs) {
129 api.setColumnDefs(changes.columnDefs.currentValue, "gridOptionsChanged");
130 }
131 if (changes.datasource) {
132 api.setDatasource(changes.datasource.currentValue);
133 }
134 if (changes.headerHeight) {
135 api.setHeaderHeight(ComponentUtil.toNumber(changes.headerHeight.currentValue));
136 }
137 if (changes.paginationPageSize) {
138 api.paginationSetPageSize(ComponentUtil.toNumber(changes.paginationPageSize.currentValue));
139 }
140 if (changes.pivotMode) {
141 columnApi.setPivotMode(ComponentUtil.toBoolean(changes.pivotMode.currentValue));
142 }
143 if (changes.groupRemoveSingleChildren) {
144 api.setGroupRemoveSingleChildren(ComponentUtil.toBoolean(changes.groupRemoveSingleChildren.currentValue));
145 }
146 if (changes.suppressRowDrag) {
147 api.setSuppressRowDrag(ComponentUtil.toBoolean(changes.suppressRowDrag.currentValue));
148 }
149 if (changes.gridAutoHeight) {
150 api.setGridAutoHeight(ComponentUtil.toBoolean(changes.gridAutoHeight.currentValue));
151 }
152 if (changes.suppressClipboardPaste) {
153 api.setSuppressClipboardPaste(ComponentUtil.toBoolean(changes.suppressClipboardPaste.currentValue));
154 }
155 // copy changes into an event for dispatch
156 var event = {
157 type: events_1.Events.EVENT_COMPONENT_STATE_CHANGED,
158 api: gridOptions.api,
159 columnApi: gridOptions.columnApi
160 };
161 utils_1.Utils.iterateObject(changes, function (key, value) {
162 event[key] = value;
163 });
164 api.dispatchEvent(event);
165 };
166 ComponentUtil.toBoolean = function (value) {
167 if (typeof value === 'boolean') {
168 return value;
169 }
170 else if (typeof value === 'string') {
171 // for boolean, compare to empty String to allow attributes appearing with
172 // not value to be treated as 'true'
173 return value.toUpperCase() === 'TRUE' || value == '';
174 }
175 else {
176 return false;
177 }
178 };
179 ComponentUtil.toNumber = function (value) {
180 if (typeof value === 'number') {
181 return value;
182 }
183 else if (typeof value === 'string') {
184 return Number(value);
185 }
186 else {
187 return undefined;
188 }
189 };
190 // all the events are populated in here AFTER this class (at the bottom of the file).
191 ComponentUtil.EVENTS = [];
192 ComponentUtil.STRING_PROPERTIES = propertyKeys_1.PropertyKeys.STRING_PROPERTIES;
193 ComponentUtil.OBJECT_PROPERTIES = propertyKeys_1.PropertyKeys.OBJECT_PROPERTIES;
194 ComponentUtil.ARRAY_PROPERTIES = propertyKeys_1.PropertyKeys.ARRAY_PROPERTIES;
195 ComponentUtil.NUMBER_PROPERTIES = propertyKeys_1.PropertyKeys.NUMBER_PROPERTIES;
196 ComponentUtil.BOOLEAN_PROPERTIES = propertyKeys_1.PropertyKeys.BOOLEAN_PROPERTIES;
197 ComponentUtil.FUNCTION_PROPERTIES = propertyKeys_1.PropertyKeys.FUNCTION_PROPERTIES;
198 ComponentUtil.ALL_PROPERTIES = propertyKeys_1.PropertyKeys.ALL_PROPERTIES;
199 return ComponentUtil;
200}());
201exports.ComponentUtil = ComponentUtil;
202utils_1.Utils.iterateObject(events_1.Events, function (key, value) {
203 ComponentUtil.EVENTS.push(value);
204});
205function checkForDeprecated(changes) {
206 if (changes.rowDeselected || changes.onRowDeselected) {
207 console.warn('ag-grid: as of v3.4 rowDeselected no longer exists. Please check the docs.');
208 }
209}