MCPcopy
hub / github.com/videojs/video.js / on

Function on

src/js/utils/events.js:266–337  ·  view source on GitHub ↗
(elem, type, fn)

Source from the content-addressed store, hash-verified

264 * Event listener.
265 */
266export function on(elem, type, fn) {
267 if (Array.isArray(type)) {
268 return _handleMultipleEvents(on, elem, type, fn);
269 }
270
271 if (!DomData.has(elem)) {
272 DomData.set(elem, {});
273 }
274
275 const data = DomData.get(elem);
276
277 // We need a place to store all our handler data
278 if (!data.handlers) {
279 data.handlers = {};
280 }
281
282 if (!data.handlers[type]) {
283 data.handlers[type] = [];
284 }
285
286 if (!fn.guid) {
287 fn.guid = Guid.newGUID();
288 }
289
290 data.handlers[type].push(fn);
291
292 if (!data.dispatcher) {
293 data.disabled = false;
294
295 data.dispatcher = function(event, hash) {
296
297 if (data.disabled) {
298 return;
299 }
300
301 event = fixEvent(event);
302
303 const handlers = data.handlers[event.type];
304
305 if (handlers) {
306 // Copy handlers so if handlers are added/removed during the process it doesn't throw everything off.
307 const handlersCopy = handlers.slice(0);
308
309 for (let m = 0, n = handlersCopy.length; m < n; m++) {
310 if (event.isImmediatePropagationStopped()) {
311 break;
312 } else {
313 try {
314 handlersCopy[m].call(elem, event, hash);
315 } catch (e) {
316 log.error(e);
317 }
318 }
319 }
320 }
321 };
322 }
323

Callers 2

oneFunction · 0.70
anyFunction · 0.70

Calls 6

_handleMultipleEventsFunction · 0.85
fixEventFunction · 0.85
supportsPassiveFunction · 0.85
setMethod · 0.45
getMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected