|
| 1 | +import staticRegister from './../utils/staticRegister'; |
| 2 | +import {hasEditor, registerEditor} from './../editors'; |
| 3 | +import {hasRenderer, registerRenderer} from './../renderers'; |
| 4 | +import {hasValidator, registerValidator} from './../validators'; |
| 5 | + |
| 6 | +import autocompleteCellType from './autocompleteType'; |
| 7 | +import checkboxCellType from './checkboxType'; |
| 8 | +import dateCellType from './dateType'; |
| 9 | +import dropdownCellType from './dropdownType'; |
| 10 | +import handsontableCellType from './handsontableType'; |
| 11 | +import numericCellType from './numericType'; |
| 12 | +import passwordCellType from './passwordType'; |
| 13 | +import textCellType from './textType'; |
| 14 | +import timeCellType from './timeType'; |
| 15 | + |
| 16 | +const { |
| 17 | + register, |
| 18 | + getItem, |
| 19 | + hasItem, |
| 20 | + getNames, |
| 21 | + getValues, |
| 22 | +} = staticRegister('cellTypes'); |
| 23 | + |
| 24 | +_register('autocomplete', autocompleteCellType); |
| 25 | +_register('checkbox', checkboxCellType); |
| 26 | +_register('date', dateCellType); |
| 27 | +_register('dropdown', dropdownCellType); |
| 28 | +_register('handsontable', handsontableCellType); |
| 29 | +_register('numeric', numericCellType); |
| 30 | +_register('password', passwordCellType); |
| 31 | +_register('text', textCellType); |
| 32 | +_register('time', timeCellType); |
| 33 | + |
| 34 | +/** |
| 35 | + * Retrieve cell type object. |
| 36 | + * |
| 37 | + * @param {String} name Cell type identification. |
| 38 | + * @returns {Object} Returns cell type object. |
| 39 | + */ |
| 40 | +function _getItem(name) { |
| 41 | + if (!hasItem(name)) { |
| 42 | + throw Error(`You declared cell type "${name}" as a string that is not mapped to a known object. |
| 43 | + Cell type must be an object or a string mapped to an object registered by "Handsontable.cellTypes.registerCellType" method`); |
| 44 | + } |
| 45 | + |
| 46 | + return getItem(name); |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Register cell type under specified name. |
| 51 | + * |
| 52 | + * @param {String} name Cell type identification. |
| 53 | + * @param {Object} type An object with contains keys (eq: `editor`, `renderer`, `validator`) which describes specified behaviour of the cell. |
| 54 | + */ |
| 55 | +function _register(name, type) { |
| 56 | + const {editor, renderer, validator} = type; |
| 57 | + |
| 58 | + if (editor) { |
| 59 | + registerEditor(name, editor); |
| 60 | + } |
| 61 | + if (renderer) { |
| 62 | + registerRenderer(name, renderer); |
| 63 | + } |
| 64 | + if (validator) { |
| 65 | + registerValidator(name, validator); |
| 66 | + } |
| 67 | + |
| 68 | + register(name, type); |
| 69 | +} |
| 70 | + |
| 71 | +export { |
| 72 | + _register as registerCellType, |
| 73 | + _getItem as getCellType, |
| 74 | + hasItem as hasCellType, |
| 75 | + getNames as getRegisteredCellTypeNames, |
| 76 | + getValues as getRegisteredCellTypes, |
| 77 | +}; |
0 commit comments