93668997881238e621edd6fcbe79b973ffd000056c78aef153c8e5ee708bd359e3f7b54b40ae80f9e8aed67a38952684a00251a47e6cbe4a17149dc68d3828 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. export default function jQueryWrapper(Handsontable) {
  2. const jQuery = typeof window === 'undefined' ? false : window.jQuery;
  3. if (!jQuery) {
  4. return;
  5. }
  6. jQuery.fn.handsontable = function(action) {
  7. const $this = this.first(); // Use only first element from list
  8. let instance = $this.data('handsontable');
  9. // Init case
  10. if (typeof action !== 'string') {
  11. const userSettings = action || {};
  12. if (instance) {
  13. instance.updateSettings(userSettings);
  14. } else {
  15. instance = new Handsontable.Core($this[0], userSettings);
  16. $this.data('handsontable', instance);
  17. instance.init();
  18. }
  19. return $this;
  20. }
  21. // Action case
  22. const args = [];
  23. let output;
  24. if (arguments.length > 1) {
  25. for (let i = 1, ilen = arguments.length; i < ilen; i++) {
  26. args.push(arguments[i]);
  27. }
  28. }
  29. if (instance) {
  30. if (typeof instance[action] !== 'undefined') {
  31. output = instance[action].apply(instance, args);
  32. if (action === 'destroy') {
  33. $this.removeData();
  34. }
  35. } else {
  36. throw new Error('Handsontable do not provide action: ' + action);
  37. }
  38. }
  39. return output;
  40. };
  41. };