draggable-rails 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. checksums.yaml +4 -4
  2. data/lib/draggable/rails/version.rb +1 -1
  3. data/vendor/assets/javascripts/behaviour/collidable.js +99 -0
  4. data/vendor/assets/javascripts/behaviour/snappable.js +107 -0
  5. data/vendor/assets/javascripts/core/accessibility.js +58 -0
  6. data/vendor/assets/javascripts/core/mirror.js +129 -0
  7. data/vendor/assets/javascripts/draggable.js +487 -0
  8. data/vendor/assets/javascripts/droppable.js +168 -0
  9. data/vendor/assets/javascripts/events/abstract-event.js +45 -0
  10. data/vendor/assets/javascripts/events/collidable-event.js +25 -0
  11. data/vendor/assets/javascripts/events/drag-event.js +91 -0
  12. data/vendor/assets/javascripts/events/draggable-event.js +17 -0
  13. data/vendor/assets/javascripts/events/droppable-event.js +21 -0
  14. data/vendor/assets/javascripts/events/mirror-event.js +43 -0
  15. data/vendor/assets/javascripts/events/sensor-event.js +47 -0
  16. data/vendor/assets/javascripts/events/snappable-event.js +15 -0
  17. data/vendor/assets/javascripts/events/sortable-event.js +35 -0
  18. data/vendor/assets/javascripts/events/swappable-event.js +23 -0
  19. data/vendor/assets/javascripts/index.js +18 -0
  20. data/vendor/assets/javascripts/sensors/drag-sensor.js +158 -0
  21. data/vendor/assets/javascripts/sensors/force-touch-sensor.js +166 -0
  22. data/vendor/assets/javascripts/sensors/mouse-sensor.js +110 -0
  23. data/vendor/assets/javascripts/sensors/sensor.js +23 -0
  24. data/vendor/assets/javascripts/sensors/touch-sensor.js +145 -0
  25. data/vendor/assets/javascripts/sortable.js +161 -0
  26. data/vendor/assets/javascripts/swappable.js +104 -0
  27. data/vendor/assets/javascripts/utils.js +52 -0
  28. metadata +26 -1
@@ -0,0 +1,15 @@
1
+ import AbstractEvent from './abstract-event';
2
+
3
+ export class SnapEvent extends AbstractEvent {
4
+ get dragEvent() {
5
+ return this.data.dragEvent;
6
+ }
7
+ }
8
+
9
+ export class SnapInEvent extends SnapEvent {
10
+ static type = 'snap:in';
11
+ }
12
+
13
+ export class SnapOutEvent extends SnapEvent {
14
+ static type = 'snap:out';
15
+ }
@@ -0,0 +1,35 @@
1
+ import AbstractEvent from './abstract-event';
2
+
3
+ export class SortableEvent extends AbstractEvent {
4
+ get dragEvent() {
5
+ return this.data.dragEvent;
6
+ }
7
+ }
8
+
9
+ export class SortableStartEvent extends SortableEvent {
10
+ static type = 'sortable:start';
11
+
12
+ get startIndex() {
13
+ return this.data.startIndex;
14
+ }
15
+ }
16
+
17
+ export class SortableSortedEvent extends SortableEvent {
18
+ static type = 'sortable:sorted';
19
+
20
+ get moves() {
21
+ return this.data.moves;
22
+ }
23
+ }
24
+
25
+ export class SortableStopEvent extends SortableEvent {
26
+ static type = 'sortable:stop';
27
+
28
+ get oldIndex() {
29
+ return this.data.oldIndex;
30
+ }
31
+
32
+ get newIndex() {
33
+ return this.data.newIndex;
34
+ }
35
+ }
@@ -0,0 +1,23 @@
1
+ import AbstractEvent from './abstract-event';
2
+
3
+ export class SwappableEvent extends AbstractEvent {
4
+ get dragEvent() {
5
+ return this.data.dragEvent;
6
+ }
7
+ }
8
+
9
+ export class SwappableStartEvent extends SwappableEvent {
10
+ static type = 'swappable:start';
11
+ }
12
+
13
+ export class SwappableSwappedEvent extends SwappableEvent {
14
+ static type = 'swappable:swapped';
15
+
16
+ get swappedElement() {
17
+ return this.data.swappedElement;
18
+ }
19
+ }
20
+
21
+ export class SwappableStopEvent extends SwappableEvent {
22
+ static type = 'swappable:stop';
23
+ }
@@ -0,0 +1,18 @@
1
+ import Draggable from './draggable';
2
+ import Sortable from './sortable';
3
+ import Swappable from './swappable';
4
+ import Droppable from './droppable';
5
+ import AbstractEvent from './events/abstract-event';
6
+
7
+ export {Draggable};
8
+ export {Sortable};
9
+ export {Swappable};
10
+ export {Droppable};
11
+ export {AbstractEvent};
12
+
13
+ export function createEventClass(options) {
14
+ function EventConstructor() { return null; }
15
+ EventConstructor.prototype = AbstractEvent.prototype;
16
+ createEventClass.type = options.type;
17
+ return createEventClass;
18
+ }
@@ -0,0 +1,158 @@
1
+ import Sensor from './sensor';
2
+ import {closest} from './../utils';
3
+
4
+ import {
5
+ DragStartSensorEvent,
6
+ DragMoveSensorEvent,
7
+ DragStopSensorEvent,
8
+ } from './../events/sensor-event';
9
+
10
+ export default class DragSensor extends Sensor {
11
+ constructor(containers = [], options = {}) {
12
+ super(containers, options);
13
+
14
+ this.dragging = false;
15
+ this.currentContainer = null;
16
+
17
+ this._onMouseDown = this._onMouseDown.bind(this);
18
+ this._onMouseUp = this._onMouseUp.bind(this);
19
+ this._onDragStart = this._onDragStart.bind(this);
20
+ this._onDragOver = this._onDragOver.bind(this);
21
+ this._onDragEnd = this._onDragEnd.bind(this);
22
+ this._onDragDrop = this._onDragDrop.bind(this);
23
+ }
24
+
25
+ attach() {
26
+ for (const container of this.containers) {
27
+ container.addEventListener('mousedown', this._onMouseDown, true);
28
+ container.addEventListener('dragstart', this._onDragStart, false);
29
+ container.addEventListener('dragover', this._onDragOver, false);
30
+ container.addEventListener('dragend', this._onDragEnd, false);
31
+ container.addEventListener('drop', this._onDragDrop, false);
32
+ }
33
+
34
+ document.addEventListener('mouseup', this._onMouseUp, true);
35
+ }
36
+
37
+ detach() {
38
+ for (const container of this.containers) {
39
+ container.removeEventListener('mousedown', this._onMouseDown, true);
40
+ container.removeEventListener('dragstart', this._onDragStart, false);
41
+ container.removeEventListener('dragover', this._onDragOver, false);
42
+ container.removeEventListener('dragend', this._onDragEnd, false);
43
+ container.removeEventListener('drop', this._onDragDrop, false);
44
+ }
45
+
46
+ document.removeEventListener('mouseup', this._onMouseUp, true);
47
+ }
48
+
49
+ // private
50
+
51
+ _onDragStart(event) {
52
+ // Need for firefox. "text" key is needed for IE
53
+ event.dataTransfer.setData('text', '');
54
+ event.dataTransfer.effectAllowed = this.options.type;
55
+
56
+ const target = document.elementFromPoint(event.clientX, event.clientY);
57
+ this.currentContainer = event.currentTarget;
58
+
59
+ const dragStartEvent = new DragStartSensorEvent({
60
+ clientX: event.clientX,
61
+ clientY: event.clientY,
62
+ target,
63
+ container: this.currentContainer,
64
+ originalEvent: event,
65
+ });
66
+
67
+ this.trigger(this.currentContainer, dragStartEvent);
68
+
69
+ if (dragStartEvent.canceled()) {
70
+ this.dragging = false;
71
+ // prevent drag event if fired event has been prevented
72
+ event.preventDefault();
73
+ } else {
74
+ this.dragging = true;
75
+ }
76
+ }
77
+
78
+ _onDragOver(event) {
79
+ if (!this.dragging) {
80
+ return;
81
+ }
82
+
83
+ const target = document.elementFromPoint(event.clientX, event.clientY);
84
+ const container = event.currentTarget;
85
+
86
+ const dragMoveEvent = new DragMoveSensorEvent({
87
+ clientX: event.clientX,
88
+ clientY: event.clientY,
89
+ target,
90
+ container: this.currentContainer,
91
+ overContainer: container,
92
+ originalEvent: event,
93
+ });
94
+
95
+ this.trigger(container, dragMoveEvent);
96
+
97
+ // event.preventDefault();
98
+ // event.dataTransfer.dropEffect = 'copy';
99
+
100
+ if (!dragMoveEvent.canceled()) {
101
+ event.preventDefault();
102
+ // event.dataTransfer.dropEffect = this.options.type;
103
+ }
104
+ }
105
+
106
+ _onDragEnd(event) {
107
+ if (!this.dragging) {
108
+ return;
109
+ }
110
+
111
+ // prevent click on drop if draggable contains a clickable element
112
+ event.preventDefault();
113
+
114
+ const container = event.currentTarget;
115
+
116
+ const dragStopEvent = new DragStopSensorEvent({
117
+ clientX: event.clientX,
118
+ clientY: event.clientY,
119
+ originalEvent: event,
120
+ container,
121
+ });
122
+
123
+ this.trigger(container, dragStopEvent);
124
+
125
+ this.dragging = false;
126
+ }
127
+
128
+ _onDragDrop(event) { // eslint-disable-line class-methods-use-this
129
+ event.preventDefault();
130
+ }
131
+
132
+ _onMouseDown(event) {
133
+ // Firefox bug for inputs within draggables https://bugzilla.mozilla.org/show_bug.cgi?id=739071
134
+ if ((event.target && (event.target.form || event.target.contenteditable))) {
135
+ return;
136
+ }
137
+
138
+ const target = closest(event.target, this.options.draggable);
139
+
140
+ if (target) {
141
+ clearTimeout(this.mouseDownTimeout);
142
+
143
+ this.mouseDownTimeout = setTimeout(() => {
144
+ target.draggable = true;
145
+ }, this.options.delay);
146
+ }
147
+ }
148
+
149
+ _onMouseUp(event) {
150
+ clearTimeout(this.mouseDownTimeout);
151
+
152
+ const target = closest(event.target, this.options.draggable);
153
+
154
+ if (target) {
155
+ target.draggable = false;
156
+ }
157
+ }
158
+ }
@@ -0,0 +1,166 @@
1
+ import Sensor from './sensor';
2
+
3
+ import {
4
+ DragStartSensorEvent,
5
+ DragMoveSensorEvent,
6
+ DragStopSensorEvent,
7
+ DragPressureSensorEvent,
8
+ } from './../events/sensor-event';
9
+
10
+ export default class ForceTouchSensor extends Sensor {
11
+ constructor(containers = [], options = {}) {
12
+ super(containers, options);
13
+
14
+ this.dragging = false;
15
+ this.mightDrag = false;
16
+ this.currentContainer = null;
17
+
18
+ this._onMouseForceWillBegin = this._onMouseForceWillBegin.bind(this);
19
+ this._onMouseForceDown = this._onMouseForceDown.bind(this);
20
+ this._onMouseDown = this._onMouseDown.bind(this);
21
+ this._onMouseForceChange = this._onMouseForceChange.bind(this);
22
+ this._onMouseMove = this._onMouseMove.bind(this);
23
+ this._onMouseUp = this._onMouseUp.bind(this);
24
+ }
25
+
26
+ attach() {
27
+ for (const container of this.containers) {
28
+ container.addEventListener('webkitmouseforcewillbegin', this._onMouseForceWillBegin, false);
29
+ container.addEventListener('webkitmouseforcedown', this._onMouseForceDown, false);
30
+ container.addEventListener('mousedown', this._onMouseDown, true);
31
+ container.addEventListener('webkitmouseforcechanged', this._onMouseForceChange, false);
32
+ }
33
+
34
+ document.addEventListener('mousemove', this._onMouseMove);
35
+ document.addEventListener('mouseup', this._onMouseUp);
36
+ }
37
+
38
+ detach() {
39
+ for (const container of this.containers) {
40
+ container.removeEventListener('webkitmouseforcewillbegin', this._onMouseForceWillBegin, false);
41
+ container.removeEventListener('webkitmouseforcedown', this._onMouseForceDown, false);
42
+ container.removeEventListener('mousedown', this._onMouseDown, true);
43
+ container.removeEventListener('webkitmouseforcechanged', this._onMouseForceChange, false);
44
+ }
45
+
46
+ document.removeEventListener('mousemove', this._onMouseMove);
47
+ document.removeEventListener('mouseup', this._onMouseUp);
48
+ }
49
+
50
+ _onMouseForceWillBegin(event) {
51
+ event.preventDefault();
52
+ this.mightDrag = true;
53
+ }
54
+
55
+ _onMouseForceDown(event) {
56
+ if (this.dragging) {
57
+ return;
58
+ }
59
+
60
+ const target = document.elementFromPoint(event.clientX, event.clientY);
61
+ const container = event.currentTarget;
62
+
63
+ const dragStartEvent = new DragStartSensorEvent({
64
+ clientX: event.clientX,
65
+ clientY: event.clientY,
66
+ target,
67
+ container,
68
+ originalEvent: event,
69
+ });
70
+
71
+ this.trigger(container, dragStartEvent);
72
+
73
+ this.currentContainer = container;
74
+ this.dragging = !dragStartEvent.canceled();
75
+ this.mightDrag = false;
76
+ }
77
+
78
+ _onMouseUp(event) {
79
+ if (!this.dragging) {
80
+ return;
81
+ }
82
+
83
+ const dragStopEvent = new DragStopSensorEvent({
84
+ clientX: event.clientX,
85
+ clientY: event.clientY,
86
+ target: null,
87
+ container: this.currentContainer,
88
+ originalEvent: event,
89
+ });
90
+
91
+ this.trigger(this.currentContainer, dragStopEvent);
92
+
93
+ this.currentContainer = null;
94
+ this.dragging = false;
95
+ this.mightDrag = false;
96
+ }
97
+
98
+ _onMouseDown(event) {
99
+ if (!this.mightDrag) {
100
+ return;
101
+ }
102
+
103
+ // Need workaround for real click
104
+ // Cancel potential drag events
105
+ event.stopPropagation();
106
+ event.stopImmediatePropagation();
107
+ event.preventDefault();
108
+ }
109
+
110
+ _onMouseMove(event) {
111
+ if (!this.dragging) {
112
+ return;
113
+ }
114
+
115
+ const target = document.elementFromPoint(event.clientX, event.clientY);
116
+
117
+ const dragMoveEvent = new DragMoveSensorEvent({
118
+ clientX: event.clientX,
119
+ clientY: event.clientY,
120
+ target,
121
+ container: this.currentContainer,
122
+ originalEvent: event,
123
+ });
124
+
125
+ this.trigger(this.currentContainer, dragMoveEvent);
126
+ }
127
+
128
+ _onMouseForceChange(event) {
129
+ if (this.dragging) {
130
+ return;
131
+ }
132
+
133
+ const target = event.target;
134
+ const container = event.currentTarget;
135
+
136
+ const dragPressureEvent = new DragPressureSensorEvent({
137
+ pressure: event.webkitForce,
138
+ clientX: event.clientX,
139
+ clientY: event.clientY,
140
+ target,
141
+ container,
142
+ originalEvent: event,
143
+ });
144
+
145
+ this.trigger(container, dragPressureEvent);
146
+ }
147
+
148
+ _onMouseForceGlobalChange(event) {
149
+ if (!this.dragging) {
150
+ return;
151
+ }
152
+
153
+ const target = event.target;
154
+
155
+ const dragPressureEvent = new DragPressureSensorEvent({
156
+ pressure: event.webkitForce,
157
+ clientX: event.clientX,
158
+ clientY: event.clientY,
159
+ target,
160
+ container: this.currentContainer,
161
+ originalEvent: event,
162
+ });
163
+
164
+ this.trigger(this.currentContainer, dragPressureEvent);
165
+ }
166
+ }
@@ -0,0 +1,110 @@
1
+ import Sensor from './sensor';
2
+
3
+ import {
4
+ DragStartSensorEvent,
5
+ DragMoveSensorEvent,
6
+ DragStopSensorEvent,
7
+ } from './../events/sensor-event';
8
+
9
+ export default class MouseSensor extends Sensor {
10
+ constructor(containers = [], options = {}) {
11
+ super(containers, options);
12
+
13
+ this.dragging = false;
14
+ this.mouseDown = false;
15
+ this.currentContainer = null;
16
+
17
+ this._onMouseDown = this._onMouseDown.bind(this);
18
+ this._onMouseMove = this._onMouseMove.bind(this);
19
+ this._onMouseUp = this._onMouseUp.bind(this);
20
+ }
21
+
22
+ attach() {
23
+ for (const container of this.containers) {
24
+ container.addEventListener('mousedown', this._onMouseDown, true);
25
+ }
26
+
27
+ document.addEventListener('mousemove', this._onMouseMove);
28
+ document.addEventListener('mouseup', this._onMouseUp);
29
+ }
30
+
31
+ detach() {
32
+ for (const container of this.containers) {
33
+ container.removeEventListener('mousedown', this._onMouseDown, true);
34
+ }
35
+
36
+ document.removeEventListener('mousemove', this._onMouseMove);
37
+ document.removeEventListener('mouseup', this._onMouseUp);
38
+ }
39
+
40
+ _onMouseDown(event) {
41
+ if (event.button === 2) {
42
+ return;
43
+ }
44
+
45
+ this.mouseDown = true;
46
+ const target = document.elementFromPoint(event.clientX, event.clientY);
47
+ const container = event.currentTarget;
48
+
49
+ clearTimeout(this.mouseDownTimeout);
50
+ this.mouseDownTimeout = setTimeout(() => {
51
+ if (!this.mouseDown) {
52
+ return;
53
+ }
54
+
55
+ const dragStartEvent = new DragStartSensorEvent({
56
+ clientX: event.clientX,
57
+ clientY: event.clientY,
58
+ target,
59
+ container,
60
+ originalEvent: event,
61
+ });
62
+
63
+ this.trigger(container, dragStartEvent);
64
+
65
+ this.currentContainer = container;
66
+ this.dragging = !dragStartEvent.canceled();
67
+ }, this.options.delay);
68
+ }
69
+
70
+ _onMouseMove(event) {
71
+ if (!this.dragging) {
72
+ return;
73
+ }
74
+
75
+ const target = document.elementFromPoint(event.clientX, event.clientY);
76
+
77
+ const dragMoveEvent = new DragMoveSensorEvent({
78
+ clientX: event.clientX,
79
+ clientY: event.clientY,
80
+ target,
81
+ container: this.currentContainer,
82
+ originalEvent: event,
83
+ });
84
+
85
+ this.trigger(this.currentContainer, dragMoveEvent);
86
+ }
87
+
88
+ _onMouseUp(event) {
89
+ this.mouseDown = false;
90
+
91
+ if (!this.dragging) {
92
+ return;
93
+ }
94
+
95
+ const target = document.elementFromPoint(event.clientX, event.clientY);
96
+
97
+ const dragStopEvent = new DragStopSensorEvent({
98
+ clientX: event.clientX,
99
+ clientY: event.clientY,
100
+ target,
101
+ container: this.currentContainer,
102
+ originalEvent: event,
103
+ });
104
+
105
+ this.trigger(this.currentContainer, dragStopEvent);
106
+
107
+ this.currentContainer = null;
108
+ this.dragging = false;
109
+ }
110
+ }