1 |
|
2 |
|
3 | const browser = require('./utils/browser');
|
4 | const events = require('./utils/events');
|
5 | const utils = require('./utils');
|
6 | const scope = require('./scope');
|
7 | const Interactable = require('./Interactable');
|
8 | const Interaction = require('./Interaction');
|
9 |
|
10 | const globalEvents = {};
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function interact (element, options) {
|
38 | let interactable = scope.interactables.get(element, options);
|
39 |
|
40 | if (!interactable) {
|
41 | interactable = new Interactable(element, options);
|
42 | interactable.events.global = globalEvents;
|
43 | }
|
44 |
|
45 | return interactable;
|
46 | }
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | interact.isSet = function (element, options) {
|
59 | return scope.interactables.indexOfElement(element, options && options.context) !== -1;
|
60 | };
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | interact.on = function (type, listener, options) {
|
74 | if (utils.is.string(type) && type.search(' ') !== -1) {
|
75 | type = type.trim().split(/ +/);
|
76 | }
|
77 |
|
78 | if (utils.is.array(type)) {
|
79 | for (const eventType of type) {
|
80 | interact.on(eventType, listener, options);
|
81 | }
|
82 |
|
83 | return interact;
|
84 | }
|
85 |
|
86 | if (utils.is.object(type)) {
|
87 | for (const prop in type) {
|
88 | interact.on(prop, type[prop], listener);
|
89 | }
|
90 |
|
91 | return interact;
|
92 | }
|
93 |
|
94 |
|
95 | if (utils.contains(Interactable.eventTypes, type)) {
|
96 |
|
97 | if (!globalEvents[type]) {
|
98 | globalEvents[type] = [listener];
|
99 | }
|
100 | else {
|
101 | globalEvents[type].push(listener);
|
102 | }
|
103 | }
|
104 |
|
105 | else {
|
106 | events.add(scope.document, type, listener, { options });
|
107 | }
|
108 |
|
109 | return interact;
|
110 | };
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | interact.off = function (type, listener, options) {
|
125 | if (utils.is.string(type) && type.search(' ') !== -1) {
|
126 | type = type.trim().split(/ +/);
|
127 | }
|
128 |
|
129 | if (utils.is.array(type)) {
|
130 | for (const eventType of type) {
|
131 | interact.off(eventType, listener, options);
|
132 | }
|
133 |
|
134 | return interact;
|
135 | }
|
136 |
|
137 | if (utils.is.object(type)) {
|
138 | for (const prop in type) {
|
139 | interact.off(prop, type[prop], listener);
|
140 | }
|
141 |
|
142 | return interact;
|
143 | }
|
144 |
|
145 | if (!utils.contains(Interactable.eventTypes, type)) {
|
146 | events.remove(scope.document, type, listener, options);
|
147 | }
|
148 | else {
|
149 | let index;
|
150 |
|
151 | if (type in globalEvents
|
152 | && (index = globalEvents[type].indexOf(listener)) !== -1) {
|
153 | globalEvents[type].splice(index, 1);
|
154 | }
|
155 | }
|
156 |
|
157 | return interact;
|
158 | };
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | interact.debug = function () {
|
169 | return scope;
|
170 | };
|
171 |
|
172 |
|
173 | interact.getPointerAverage = utils.pointerAverage;
|
174 | interact.getTouchBBox = utils.touchBBox;
|
175 | interact.getTouchDistance = utils.touchDistance;
|
176 | interact.getTouchAngle = utils.touchAngle;
|
177 |
|
178 | interact.getElementRect = utils.getElementRect;
|
179 | interact.getElementClientRect = utils.getElementClientRect;
|
180 | interact.matchesSelector = utils.matchesSelector;
|
181 | interact.closest = utils.closest;
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | interact.supportsTouch = function () {
|
189 | return browser.supportsTouch;
|
190 | };
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | interact.supportsPointerEvent = function () {
|
198 | return browser.supportsPointerEvent;
|
199 | };
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 | interact.stop = function (event) {
|
210 | for (let i = scope.interactions.length - 1; i >= 0; i--) {
|
211 | scope.interactions[i].stop(event);
|
212 | }
|
213 |
|
214 | return interact;
|
215 | };
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | interact.pointerMoveTolerance = function (newValue) {
|
227 | if (utils.is.number(newValue)) {
|
228 | Interaction.pointerMoveTolerance = newValue;
|
229 |
|
230 | return interact;
|
231 | }
|
232 |
|
233 | return Interaction.pointerMoveTolerance;
|
234 | };
|
235 |
|
236 | interact.addDocument = scope.addDocument;
|
237 | interact.removeDocument = scope.removeDocument;
|
238 |
|
239 | scope.interact = interact;
|
240 |
|
241 | module.exports = interact;
|