09bcd3faac55796f2373c1bd3ce160d1dba583a2dca2aa364bd95f41532a7ad78f2d30023b8706b2c0b8c1bdc0a02bff594874d7d0c1832e322357d5adac77 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. const registeredEditorClasses = new WeakMap();
  18. const {
  19. register,
  20. getItem,
  21. hasItem,
  22. getNames,
  23. getValues,
  24. } = staticRegister('editors');
  25. _register('base', BaseEditor);
  26. _register('autocomplete', AutocompleteEditor);
  27. _register('checkbox', CheckboxEditor);
  28. _register('date', DateEditor);
  29. _register('dropdown', DropdownEditor);
  30. _register('handsontable', HandsontableEditor);
  31. _register('mobile', MobileTextEditor);
  32. _register('numeric', NumericEditor);
  33. _register('password', PasswordEditor);
  34. _register('select', SelectEditor);
  35. _register('text', TextEditor);
  36. export function RegisteredEditor(editorClass) {
  37. let instances = {};
  38. const Clazz = editorClass;
  39. this.getConstructor = function() {
  40. return editorClass;
  41. };
  42. this.getInstance = function(hotInstance) {
  43. if (!(hotInstance.guid in instances)) {
  44. instances[hotInstance.guid] = new Clazz(hotInstance);
  45. }
  46. return instances[hotInstance.guid];
  47. };
  48. Hooks.getSingleton().add('afterDestroy', function() {
  49. instances = {};
  50. });
  51. }
  52. /**
  53. * Returns instance (singleton) of editor class.
  54. *
  55. * @param {String} name Name of an editor under which it has been stored.
  56. * @param {Object} hotInstance Instance of Handsontable.
  57. * @returns {Function} Returns instance of editor.
  58. */
  59. export function _getEditorInstance(name, hotInstance) {
  60. let editor;
  61. if (typeof name === 'function') {
  62. if (!(registeredEditorClasses.get(name))) {
  63. _register(null, name);
  64. }
  65. editor = registeredEditorClasses.get(name);
  66. } else if (typeof name === 'string') {
  67. editor = getItem(name);
  68. } else {
  69. throw Error('Only strings and functions can be passed as "editor" parameter');
  70. }
  71. if (!editor) {
  72. throw Error(`No editor registered under name "${name}"`);
  73. }
  74. return editor.getInstance(hotInstance);
  75. }
  76. /**
  77. * Retrieve editor class.
  78. *
  79. * @param {String} name Editor identification.
  80. * @returns {Function} Returns editor class.
  81. */
  82. function _getItem(name) {
  83. if (!hasItem(name)) {
  84. throw Error(`No registered editor found under "${name}" name`);
  85. }
  86. return getItem(name).getConstructor();
  87. }
  88. /**
  89. * Register editor class under specified name.
  90. *
  91. * @param {String} name Editor identification.
  92. * @param {Function} editorClass Editor class.
  93. */
  94. function _register(name, editorClass) {
  95. const editorWrapper = new RegisteredEditor(editorClass);
  96. if (typeof name === 'string') {
  97. register(name, editorWrapper);
  98. }
  99. registeredEditorClasses.set(editorClass, editorWrapper);
  100. }
  101. export {
  102. _register as registerEditor,
  103. _getItem as getEditor,
  104. _getEditorInstance as getEditorInstance,
  105. hasItem as hasEditor,
  106. getNames as getRegisteredEditorNames,
  107. getValues as getRegisteredEditors,
  108. };