index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Utility to register editors and common namespace for keeping reference to all editor classes
  3. */
  4. import staticRegister from './../utils/staticRegister';
  5. import Hooks from './../pluginHooks';
  6. import BaseEditor from './_baseEditor';
  7. import AutocompleteEditor from './autocompleteEditor';
  8. import CheckboxEditor from './checkboxEditor';
  9. import DateEditor from './dateEditor';
  10. import DropdownEditor from './dropdownEditor';
  11. import HandsontableEditor from './handsontableEditor';
  12. import MobileTextEditor from './mobileTextEditor';
  13. import NumericEditor from './numericEditor';
  14. import PasswordEditor from './passwordEditor';
  15. import SelectEditor from './selectEditor';
  16. import TextEditor from './textEditor';
  17. var registeredEditorClasses = new WeakMap();
  18. var _staticRegister = staticRegister('editors'),
  19. register = _staticRegister.register,
  20. getItem = _staticRegister.getItem,
  21. hasItem = _staticRegister.hasItem,
  22. getNames = _staticRegister.getNames,
  23. getValues = _staticRegister.getValues;
  24. _register('base', BaseEditor);
  25. _register('autocomplete', AutocompleteEditor);
  26. _register('checkbox', CheckboxEditor);
  27. _register('date', DateEditor);
  28. _register('dropdown', DropdownEditor);
  29. _register('handsontable', HandsontableEditor);
  30. _register('mobile', MobileTextEditor);
  31. _register('numeric', NumericEditor);
  32. _register('password', PasswordEditor);
  33. _register('select', SelectEditor);
  34. _register('text', TextEditor);
  35. export function RegisteredEditor(editorClass) {
  36. var instances = {};
  37. var Clazz = editorClass;
  38. this.getConstructor = function () {
  39. return editorClass;
  40. };
  41. this.getInstance = function (hotInstance) {
  42. if (!(hotInstance.guid in instances)) {
  43. instances[hotInstance.guid] = new Clazz(hotInstance);
  44. }
  45. return instances[hotInstance.guid];
  46. };
  47. Hooks.getSingleton().add('afterDestroy', function () {
  48. instances = {};
  49. });
  50. }
  51. /**
  52. * Returns instance (singleton) of editor class.
  53. *
  54. * @param {String} name Name of an editor under which it has been stored.
  55. * @param {Object} hotInstance Instance of Handsontable.
  56. * @returns {Function} Returns instance of editor.
  57. */
  58. export function _getEditorInstance(name, hotInstance) {
  59. var editor = void 0;
  60. if (typeof name === 'function') {
  61. if (!registeredEditorClasses.get(name)) {
  62. _register(null, name);
  63. }
  64. editor = registeredEditorClasses.get(name);
  65. } else if (typeof name === 'string') {
  66. editor = getItem(name);
  67. } else {
  68. throw Error('Only strings and functions can be passed as "editor" parameter');
  69. }
  70. if (!editor) {
  71. throw Error('No editor registered under name "' + name + '"');
  72. }
  73. return editor.getInstance(hotInstance);
  74. }
  75. /**
  76. * Retrieve editor class.
  77. *
  78. * @param {String} name Editor identification.
  79. * @returns {Function} Returns editor class.
  80. */
  81. function _getItem(name) {
  82. if (!hasItem(name)) {
  83. throw Error('No registered editor found under "' + name + '" name');
  84. }
  85. return getItem(name).getConstructor();
  86. }
  87. /**
  88. * Register editor class under specified name.
  89. *
  90. * @param {String} name Editor identification.
  91. * @param {Function} editorClass Editor class.
  92. */
  93. function _register(name, editorClass) {
  94. var editorWrapper = new RegisteredEditor(editorClass);
  95. if (typeof name === 'string') {
  96. register(name, editorWrapper);
  97. }
  98. registeredEditorClasses.set(editorClass, editorWrapper);
  99. }
  100. export { _register as registerEditor, _getItem as getEditor, _getEditorInstance as getEditorInstance, hasItem as hasEditor, getNames as getRegisteredEditorNames, getValues as getRegisteredEditors };