|
| 1 | +// The export defaults go on gridOptions.defaultCsvExportParams, not on the button's call, |
| 2 | +// so a gadget calling api.exportDataAsCsv() gets them too. Params passed to that call still |
| 3 | +// override them (gadget code is trusted); the author's defaultCsvExportParams from Lua is |
| 4 | +// not, and cannot switch the formula guard off. |
| 5 | + |
| 6 | +const { setClass, normalizeButtonOption } = require( './toolbar.js' ); |
| 7 | + |
| 8 | +// AG Grid's list of leading characters a spreadsheet runs as a formula, plus line feed |
| 9 | +// (OWASP). Prefixed with an apostrophe, not OWASP's tab: tab is itself on AG Grid's list. |
| 10 | +const FORMULA_START = /^[=+\-@\t\r\n]/; |
| 11 | + |
| 12 | +const UNSAFE_FILE_CHARS = /[\\/:*?"<>|]/g; |
| 13 | + |
| 14 | +// Unquoted, a newline or separator inside a cell starts a new, unchecked cell; prepended |
| 15 | +// and appended content is written out verbatim. |
| 16 | +const UNSAFE_AUTHOR_KEYS = [ 'suppressQuotes', 'prependContent', 'appendContent' ]; |
| 17 | + |
| 18 | +/** |
| 19 | + * @param {*} value |
| 20 | + * @return {*} |
| 21 | + */ |
| 22 | +function neutralize( value ) { |
| 23 | + return typeof value === 'string' && FORMULA_START.test( value ) ? `'${ value }` : value; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * @param {Object} params AG Grid ProcessCellForExportParams. |
| 28 | + * @return {*} |
| 29 | + */ |
| 30 | +function processCell( params ) { |
| 31 | + const value = params.value; |
| 32 | + if ( params.column.getColDef().useValueFormatterForExport === false ) { |
| 33 | + return neutralize( value ); |
| 34 | + } |
| 35 | + const text = params.formatValue( value ); |
| 36 | + // AG Grid infers a formatter for a plain column that only stringifies (-5 → "-5", |
| 37 | + // ['a','b'] → "a,b"): keep such a number a number and join such an array. A column |
| 38 | + // type's own formatter is used as it is. |
| 39 | + if ( String( text ) === String( value ) ) { |
| 40 | + if ( typeof value === 'number' ) { |
| 41 | + return value; |
| 42 | + } |
| 43 | + if ( Array.isArray( value ) ) { |
| 44 | + return neutralize( value.join( ', ' ) ); |
| 45 | + } |
| 46 | + } |
| 47 | + return neutralize( text ); |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * The export defaults for a grid on the given page. |
| 52 | + * |
| 53 | + * @param {string|null} pageTitle wgTitle; names the downloaded file. |
| 54 | + * @param {*} [authorParams] The author's defaultCsvExportParams, from the placeholder JSON. |
| 55 | + * @return {Object} CsvExportParams for gridOptions.defaultCsvExportParams. |
| 56 | + */ |
| 57 | +function defaultParams( pageTitle, authorParams ) { |
| 58 | + const params = {}; |
| 59 | + if ( pageTitle ) { |
| 60 | + // AG Grid appends the extension only to a name with no dot in it. |
| 61 | + params.fileName = `${ pageTitle.replace( UNSAFE_FILE_CHARS, '_' ) }.csv`; |
| 62 | + } |
| 63 | + if ( authorParams && typeof authorParams === 'object' && !Array.isArray( authorParams ) ) { |
| 64 | + Object.assign( params, authorParams ); |
| 65 | + UNSAFE_AUTHOR_KEYS.forEach( ( key ) => delete params[ key ] ); |
| 66 | + } |
| 67 | + // Last: from Lua, the author's callbacks can only be non-functions, which would switch |
| 68 | + // the guard off or break the export. |
| 69 | + return Object.assign( params, { |
| 70 | + processCellCallback: processCell, |
| 71 | + // The locations AG Grid's own export reads header names with. |
| 72 | + processHeaderCallback: ( p ) => neutralize( |
| 73 | + p.api.getDisplayNameForColumn( p.column, 'csv' ) |
| 74 | + ), |
| 75 | + processGroupHeaderCallback: ( p ) => neutralize( |
| 76 | + p.api.getDisplayNameForColumnGroup( p.columnGroup, 'header' ) |
| 77 | + ) |
| 78 | + } ); |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Build the export button. |
| 83 | + * |
| 84 | + * @param {Object} api The AG Grid GridApi. |
| 85 | + * @param {Object} config Normalized config from normalizeButtonOption(). |
| 86 | + * @return {HTMLElement} The toolbar item. |
| 87 | + */ |
| 88 | +function buildItem( api, config ) { |
| 89 | + const item = setClass( document.createElement( 'div' ), |
| 90 | + 'ag-toolbar-item ag-toolbar-button-wrapper ext-aggrid-toolbar__item' ); |
| 91 | + const button = setClass( document.createElement( 'button' ), |
| 92 | + 'ag-toolbar-button ext-aggrid-toolbar__button' ); |
| 93 | + button.type = 'button'; |
| 94 | + const label = config.label || mw.msg( 'aggrid-csvexport-label' ); |
| 95 | + button.setAttribute( 'aria-label', label ); |
| 96 | + button.setAttribute( 'title', label ); |
| 97 | + const icon = setClass( document.createElement( 'span' ), 'ag-icon ag-icon-csv' ); |
| 98 | + icon.setAttribute( 'aria-hidden', 'true' ); |
| 99 | + button.appendChild( icon ); |
| 100 | + // No params: they would override the author's defaultCsvExportParams. |
| 101 | + button.addEventListener( 'click', () => api.exportDataAsCsv() ); |
| 102 | + item.appendChild( button ); |
| 103 | + return item; |
| 104 | +} |
| 105 | + |
| 106 | +module.exports = { normalize: normalizeButtonOption, defaultParams, buildItem }; |
0 commit comments