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.
 
 
 
 

94 lines
3.4 KiB

  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 = createTargetFactory;
  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 = ['canDrop', 'hover', 'drop'];
  14. function createTargetFactory(spec) {
  15. Object.keys(spec).forEach(function (key) {
  16. (0, _invariant2.default)(ALLOWED_SPEC_METHODS.indexOf(key) > -1, 'Expected the drop target 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-drop-target.html', ALLOWED_SPEC_METHODS.join(', '), key);
  17. (0, _invariant2.default)(typeof spec[key] === 'function', 'Expected %s in the drop target specification to be a function. ' + 'Instead received a specification with %s: %s. ' + 'Read more: http://react-dnd.github.io/react-dnd/docs-drop-target.html', key, key, spec[key]);
  18. });
  19. var Target = function () {
  20. function Target(monitor) {
  21. _classCallCheck(this, Target);
  22. this.monitor = monitor;
  23. this.props = null;
  24. this.component = null;
  25. }
  26. _createClass(Target, [{
  27. key: 'receiveProps',
  28. value: function receiveProps(props) {
  29. this.props = props;
  30. }
  31. }, {
  32. key: 'receiveMonitor',
  33. value: function receiveMonitor(monitor) {
  34. this.monitor = monitor;
  35. }
  36. }, {
  37. key: 'receiveComponent',
  38. value: function receiveComponent(component) {
  39. this.component = component;
  40. }
  41. }, {
  42. key: 'canDrop',
  43. value: function canDrop() {
  44. if (!spec.canDrop) {
  45. return true;
  46. }
  47. return spec.canDrop(this.props, this.monitor);
  48. }
  49. }, {
  50. key: 'hover',
  51. value: function hover() {
  52. if (!spec.hover) {
  53. return;
  54. }
  55. spec.hover(this.props, this.monitor, this.component);
  56. }
  57. }, {
  58. key: 'drop',
  59. value: function drop() {
  60. if (!spec.drop) {
  61. return undefined;
  62. }
  63. var dropResult = spec.drop(this.props, this.monitor, this.component);
  64. if (process.env.NODE_ENV !== 'production') {
  65. (0, _invariant2.default)(typeof dropResult === 'undefined' || (0, _isPlainObject2.default)(dropResult), 'drop() must either return undefined, or an object that represents the drop result. ' + 'Instead received %s. ' + 'Read more: http://react-dnd.github.io/react-dnd/docs-drop-target.html', dropResult);
  66. }
  67. return dropResult;
  68. }
  69. }]);
  70. return Target;
  71. }();
  72. return function createTarget(monitor) {
  73. return new Target(monitor);
  74. };
  75. }