You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

createSourceFactory.js 3.8 KiB

3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  6. exports.default = createSourceFactory;
  7. var _invariant = require('invariant');
  8. var _invariant2 = _interopRequireDefault(_invariant);
  9. var _isPlainObject = require('lodash/isPlainObject');
  10. var _isPlainObject2 = _interopRequireDefault(_isPlainObject);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  13. var ALLOWED_SPEC_METHODS = ['canDrag', 'beginDrag', 'isDragging', 'endDrag'];
  14. var REQUIRED_SPEC_METHODS = ['beginDrag'];
  15. function createSourceFactory(spec) {
  16. Object.keys(spec).forEach(function (key) {
  17. (0, _invariant2.default)(ALLOWED_SPEC_METHODS.indexOf(key) > -1, 'Expected the drag source specification to only have ' + 'some of the following keys: %s. ' + 'Instead received a specification with an unexpected "%s" key. ' + 'Read more: http://react-dnd.github.io/react-dnd/docs-drag-source.html', ALLOWED_SPEC_METHODS.join(', '), key);
  18. (0, _invariant2.default)(typeof spec[key] === 'function', 'Expected %s in the drag source specification to be a function. ' + 'Instead received a specification with %s: %s. ' + 'Read more: http://react-dnd.github.io/react-dnd/docs-drag-source.html', key, key, spec[key]);
  19. });
  20. REQUIRED_SPEC_METHODS.forEach(function (key) {
  21. (0, _invariant2.default)(typeof spec[key] === 'function', 'Expected %s in the drag source specification to be a function. ' + 'Instead received a specification with %s: %s. ' + 'Read more: http://react-dnd.github.io/react-dnd/docs-drag-source.html', key, key, spec[key]);
  22. });
  23. var Source = function () {
  24. function Source(monitor) {
  25. _classCallCheck(this, Source);
  26. this.monitor = monitor;
  27. this.props = null;
  28. this.component = null;
  29. }
  30. _createClass(Source, [{
  31. key: 'receiveProps',
  32. value: function receiveProps(props) {
  33. this.props = props;
  34. }
  35. }, {
  36. key: 'receiveComponent',
  37. value: function receiveComponent(component) {
  38. this.component = component;
  39. }
  40. }, {
  41. key: 'canDrag',
  42. value: function canDrag() {
  43. if (!spec.canDrag) {
  44. return true;
  45. }
  46. return spec.canDrag(this.props, this.monitor);
  47. }
  48. }, {
  49. key: 'isDragging',
  50. value: function isDragging(globalMonitor, sourceId) {
  51. if (!spec.isDragging) {
  52. return sourceId === globalMonitor.getSourceId();
  53. }
  54. return spec.isDragging(this.props, this.monitor);
  55. }
  56. }, {
  57. key: 'beginDrag',
  58. value: function beginDrag() {
  59. var item = spec.beginDrag(this.props, this.monitor, this.component);
  60. if (process.env.NODE_ENV !== 'production') {
  61. (0, _invariant2.default)((0, _isPlainObject2.default)(item), 'beginDrag() must return a plain object that represents the dragged item. ' + 'Instead received %s. ' + 'Read more: http://react-dnd.github.io/react-dnd/docs-drag-source.html', item);
  62. }
  63. return item;
  64. }
  65. }, {
  66. key: 'endDrag',
  67. value: function endDrag() {
  68. if (!spec.endDrag) {
  69. return;
  70. }
  71. spec.endDrag(this.props, this.monitor, this.component);
  72. }
  73. }]);
  74. return Source;
  75. }();
  76. return function createSource(monitor) {
  77. return new Source(monitor);
  78. };
  79. }