* Loads new data to Handsontable. * * @private * @param {Array} data Array of arrays or array of objects containing data. * @param {Function} setDataMapFunction Function that updates the datamap instance. * @param {Function} callbackFunction Function that takes care of updating Handsontable to
( data: unknown[], setDataMapFunction: (dataMap: DataMap) => void, callbackFunction: (dataMap: DataMap) => void, config: ReplaceDataConfig )
| 83 | * @fires Hooks#afterChange |
| 84 | */ |
| 85 | function replaceData( |
| 86 | data: unknown[], |
| 87 | setDataMapFunction: (dataMap: DataMap) => void, |
| 88 | callbackFunction: (dataMap: DataMap) => void, |
| 89 | config: ReplaceDataConfig |
| 90 | ) { |
| 91 | const { |
| 92 | hotInstance, |
| 93 | dataMap, |
| 94 | dataSource, |
| 95 | internalSource, |
| 96 | source, |
| 97 | metaManager, |
| 98 | firstRun |
| 99 | } = config; |
| 100 | const capitalizedInternalSource = toUpperCaseFirst(internalSource); |
| 101 | const tableMeta = hotInstance.getSettings(); |
| 102 | |
| 103 | if (Array.isArray(tableMeta.dataSchema)) { |
| 104 | hotInstance.dataType = 'array'; |
| 105 | } else if (isFunction(tableMeta.dataSchema)) { |
| 106 | hotInstance.dataType = 'function'; |
| 107 | } else { |
| 108 | hotInstance.dataType = 'object'; |
| 109 | } |
| 110 | |
| 111 | if (dataMap) { |
| 112 | dataMap.destroy(); |
| 113 | } |
| 114 | |
| 115 | data = hotInstance.runHooks(`before${capitalizedInternalSource}`, data, firstRun, source) as unknown[]; |
| 116 | |
| 117 | const newDataMap = new DataMap(hotInstance, data as (Record<string, unknown> | unknown[])[], metaManager); |
| 118 | |
| 119 | // We need to apply the new dataMap immediately, because of some asynchronous logic in the |
| 120 | // `autoRowSize`/`autoColumnSize` plugins. |
| 121 | setDataMapFunction(newDataMap); |
| 122 | |
| 123 | if (typeof data === 'object' && data !== null) { |
| 124 | if (!((data as unknown[]).push && (data as unknown[]).splice)) { // check if data is array. Must use duck-type check so Backbone Collections also pass it |
| 125 | // when data is not an array, attempt to make a single-row array of it |
| 126 | // eslint-disable-next-line no-param-reassign |
| 127 | data = [data]; |
| 128 | } |
| 129 | |
| 130 | } else if (data === null) { |
| 131 | // eslint-disable-next-line no-param-reassign |
| 132 | data = buildNullData(hotInstance, newDataMap, tableMeta); |
| 133 | |
| 134 | } else { |
| 135 | throwWithCause(`${internalSource} only accepts array of objects or array of arrays (${typeof data} given)`); |
| 136 | } |
| 137 | |
| 138 | if (Array.isArray((data as unknown[])[0])) { |
| 139 | hotInstance.dataType = 'array'; |
| 140 | } |
| 141 | |
| 142 | tableMeta.data = data as unknown[][] | object[]; |
no test coverage detected
searching dependent graphs…