jquery.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. export default function jQueryWrapper(Handsontable) {
  2. var jQuery = typeof window === 'undefined' ? false : window.jQuery;
  3. if (!jQuery) {
  4. return;
  5. }
  6. jQuery.fn.handsontable = function (action) {
  7. var $this = this.first(); // Use only first element from list
  8. var instance = $this.data('handsontable');
  9. // Init case
  10. if (typeof action !== 'string') {
  11. var 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. var args = [];
  23. var output = void 0;
  24. if (arguments.length > 1) {
  25. for (var 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. };