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.

100 lines
4.0 KiB

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