Licitator 1.0
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.

91 lines
3.5 KiB

5 years ago
  1. /**
  2. * ItemHold plugin
  3. *
  4. * @copyright Rafal Pospiech <https://neuronet.io>
  5. * @author Rafal Pospiech <neuronet.io@gmail.com>
  6. * @package gantt-schedule-timeline-calendar
  7. * @license AGPL-3.0 (https://github.com/neuronetio/gantt-schedule-timeline-calendar/blob/master/LICENSE)
  8. * @link https://github.com/neuronetio/gantt-schedule-timeline-calendar
  9. */
  10. function ItemHold(options = {}) {
  11. let api;
  12. const defaultOptions = {
  13. time: 1000,
  14. movementThreshold: 2,
  15. action(element, data) { }
  16. };
  17. options = Object.assign(Object.assign({}, defaultOptions), options);
  18. const holding = {};
  19. const pointer = { x: 0, y: 0 };
  20. function onPointerDown(item, element, event) {
  21. if (typeof holding[item.id] === 'undefined') {
  22. const normalized = api.normalizePointerEvent(event);
  23. holding[item.id] = { x: normalized.x, y: normalized.y };
  24. event.stopPropagation();
  25. event.preventDefault();
  26. setTimeout(() => {
  27. if (typeof holding[item.id] !== 'undefined') {
  28. let exec = true;
  29. const xMovement = Math.abs(holding[item.id].x - pointer.x);
  30. const yMovement = Math.abs(holding[item.id].y - pointer.y);
  31. if (xMovement > options.movementThreshold) {
  32. exec = false;
  33. }
  34. if (yMovement > options.movementThreshold) {
  35. exec = false;
  36. }
  37. delete holding[item.id];
  38. if (exec) {
  39. options.action(element, item);
  40. }
  41. }
  42. }, options.time);
  43. }
  44. }
  45. function onPointerUp(itemId) {
  46. if (typeof holding[itemId] !== 'undefined') {
  47. delete holding[itemId];
  48. }
  49. }
  50. function action(element, data) {
  51. function elementPointerDown(event) {
  52. onPointerDown(data.item, element, event);
  53. }
  54. element.addEventListener('mousedown', elementPointerDown);
  55. element.addEventListener('touchstart', elementPointerDown);
  56. function pointerUp() {
  57. onPointerUp(data.item.id);
  58. }
  59. document.addEventListener('mouseup', pointerUp);
  60. document.addEventListener('touchend', pointerUp);
  61. function onPointerMove(event) {
  62. const normalized = api.normalizePointerEvent(event);
  63. pointer.x = normalized.x;
  64. pointer.y = normalized.y;
  65. }
  66. document.addEventListener('mousemove', onPointerMove);
  67. document.addEventListener('touchmove', onPointerMove);
  68. return {
  69. update(element, changedData) {
  70. data = changedData;
  71. },
  72. destroy(element, data) {
  73. document.removeEventListener('mouseup', onPointerUp);
  74. document.removeEventListener('mousemove', onPointerMove);
  75. element.removeEventListener('mousedown', elementPointerDown);
  76. document.removeEventListener('touchend', onPointerUp);
  77. document.removeEventListener('touchmove', onPointerMove);
  78. element.removeEventListener('touchstart', elementPointerDown);
  79. }
  80. };
  81. }
  82. return function initialize(vido) {
  83. api = vido.api;
  84. vido.state.update('config.actions.chart-timeline-items-row-item', actions => {
  85. actions.push(action);
  86. return actions;
  87. });
  88. };
  89. }
  90. export default ItemHold;