click-track
Version:
JavaScript utility for click track events.
2 lines (1 loc) • 11.6 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).ClickTrack=e()}(this,function(){"use strict";var d="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function t(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}function e(t,e){return t(e={exports:{}},e.exports),e.exports}var c=e(function(t,e){Object.defineProperty(e,"__esModule",{value:!0});var i=(n.prototype.stopPropagation=function(){this.propagationStopped=!0},n);function n(t){this.unsub=t,this.propagationStopped=!1}e.EventManagement=i});t(c);c.EventManagement;var h=e(function(t,e){Object.defineProperty(e,"__esModule",{value:!0});var i=(n.prototype.execute=function(t,e,i){if(!this.isOnce||!this.isExecuted){this.isExecuted=!0;var n=this.handler;t?setTimeout(function(){n.apply(e,i)},1):n.apply(e,i)}},n);function n(t,e){this.handler=t,this.isOnce=e,this.isExecuted=!1}e.Subscription=i});t(h);h.Subscription;var i=e(function(t,e){var a=d&&d.__spreadArrays||function(){for(var t=0,e=0,i=arguments.length;e<i;e++)t+=arguments[e].length;var n=Array(t),s=0;for(e=0;e<i;e++)for(var r=arguments[e],o=0,u=r.length;o<u;o++,s++)n[s]=r[o];return n};Object.defineProperty(e,"__esModule",{value:!0});var i=(Object.defineProperty(n.prototype,"count",{get:function(){return this._subscriptions.length},enumerable:!0,configurable:!0}),n.prototype.subscribe=function(t){var e=this;return t&&this._subscriptions.push(new h.Subscription(t,!1)),function(){e.unsubscribe(t)}},n.prototype.sub=function(t){return this.subscribe(t)},n.prototype.one=function(t){var e=this;return t&&this._subscriptions.push(new h.Subscription(t,!0)),function(){e.unsubscribe(t)}},n.prototype.has=function(e){return!!e&&this._subscriptions.some(function(t){return t.handler==e})},n.prototype.unsubscribe=function(t){if(t)for(var e=0;e<this._subscriptions.length;e++)if(this._subscriptions[e].handler==t){this._subscriptions.splice(e,1);break}},n.prototype.unsub=function(t){this.unsubscribe(t)},n.prototype._dispatch=function(n,s,r){for(var o=this,t=function(t){var e=new c.EventManagement(function(){return o.unsub(t.handler)}),i=Array.prototype.slice.call(r);if(i.push(e),t.execute(n,s,i),u.cleanup(t),!n&&e.propagationStopped)return"break"},u=this,e=0,i=a(this._subscriptions);e<i.length&&"break"!==t(i[e]);e++);},n.prototype.cleanup=function(t){if(t.isOnce&&t.isExecuted){var e=this._subscriptions.indexOf(t);-1<e&&this._subscriptions.splice(e,1)}},n.prototype.asEvent=function(){return this._wrap},n.prototype.clear=function(){this._subscriptions.splice(0,this._subscriptions.length)},n);function n(){this._wrap=new o(this),this._subscriptions=new Array}e.DispatcherBase=i;var s=(r.prototype.get=function(t){var e=this._events[t];return e||(e=this.createDispatcher(),this._events[t]=e)},r.prototype.remove=function(t){delete this._events[t]},r);function r(){this._events={}}e.EventListBase=s;var o=(Object.defineProperty(u.prototype,"count",{get:function(){return this._count()},enumerable:!0,configurable:!0}),u.prototype.subscribe=function(t){return this._subscribe(t)},u.prototype.sub=function(t){return this.subscribe(t)},u.prototype.unsubscribe=function(t){this._unsubscribe(t)},u.prototype.unsub=function(t){this.unsubscribe(t)},u.prototype.one=function(t){return this._one(t)},u.prototype.has=function(t){return this._has(t)},u.prototype.clear=function(){this._clear()},u);function u(e){this._subscribe=function(t){return e.subscribe(t)},this._unsubscribe=function(t){return e.unsubscribe(t)},this._one=function(t){return e.one(t)},this._has=function(t){return e.has(t)},this._clear=function(){return e.clear()},this._count=function(){return e.count}}e.DispatcherWrapper=o});t(i);i.DispatcherBase,i.EventListBase,i.DispatcherWrapper;var l=e(function(t,e){Object.defineProperty(e,"__esModule",{value:!0}),e.DispatcherBase=i.DispatcherBase,e.DispatcherWrapper=i.DispatcherWrapper,e.EventListBase=i.EventListBase,e.Subscription=h.Subscription});t(l);l.DispatcherBase,l.DispatcherWrapper,l.EventListBase,l.Subscription;var n=e(function(t,e){var n,i=d&&d.__extends||(n=function(t,e){return(n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])})(t,e)},function(t,e){function i(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(i.prototype=e.prototype,new i)});Object.defineProperty(e,"__esModule",{value:!0});var s,r=(s=l.DispatcherBase,i(o,s),o.prototype.dispatch=function(t,e){this._dispatch(!1,this,arguments)},o.prototype.dispatchAsync=function(t,e){this._dispatch(!0,this,arguments)},o.prototype.asEvent=function(){return s.prototype.asEvent.call(this)},o);function o(){return s.call(this)||this}e.EventDispatcher=r;var u=(a.prototype.get=function(t){if(this._events[t])return this._events[t];var e=this.createDispatcher();return this._events[t]=e},a.prototype.remove=function(t){delete this._events[t]},a.prototype.createDispatcher=function(){return new r},a);function a(){this._events={}}e.NonUniformEventList=u;var c,h=(c=l.EventListBase,i(p,c),p.prototype.createDispatcher=function(){return new r},p);function p(){return c.call(this)||this}e.EventList=h;var f=(Object.defineProperty(v.prototype,"events",{get:function(){return this._events},enumerable:!0,configurable:!0}),v.prototype.subscribe=function(t,e){this._events.get(t).subscribe(e)},v.prototype.sub=function(t,e){this.subscribe(t,e)},v.prototype.unsubscribe=function(t,e){this._events.get(t).unsubscribe(e)},v.prototype.unsub=function(t,e){this.unsubscribe(t,e)},v.prototype.one=function(t,e){this._events.get(t).one(e)},v.prototype.has=function(t,e){return this._events.get(t).has(e)},v);function v(){this._events=new h}e.EventHandlingBase=f});t(n);n.EventDispatcher,n.NonUniformEventList,n.EventList,n.EventHandlingBase;var s=e(function(t,e){Object.defineProperty(e,"__esModule",{value:!0}),e.EventDispatcher=n.EventDispatcher,e.EventHandlingBase=n.EventHandlingBase,e.EventList=n.EventList,e.NonUniformEventList=n.NonUniformEventList});t(s);var r=s.EventDispatcher,o=(s.EventHandlingBase,s.EventList,s.NonUniformEventList);var u=(a.prototype.start=function(){this.startTimeMarker=Date.now(),this.animationFrameId&&cancelAnimationFrame(this.animationFrameId),this.animationFrameId=requestAnimationFrame(this.updateTime.bind(this))},a.prototype.stop=function(){this.animationFrameId&&cancelAnimationFrame(this.animationFrameId)},a.prototype.updateTime=function(){this.animationFrameId=void 0;var t=(Date.now()-this.startTimeMarker)/1e3;if(t!==this.position){this.position=t,this.position>this.length&&(this.loop?this.position=this.position%this.length:this.position=this.length);try{this.onTick.dispatch(this,t)}finally{this.animationFrameId=requestAnimationFrame(this.updateTime.bind(this))}}},a.prototype.onUpdate=function(t){this.onTick.subscribe(t)},a.prototype.offUpdate=function(t){this.onTick.unsubscribe(t)},a.prototype.deconstruct=function(){this.onTick.clear(),this.animationFrameId&&cancelAnimationFrame(this.animationFrameId)},a);function a(t,e,i){void 0===t&&(t=!1),void 0===e&&(e=1/0),void 0===i&&(i=!0),this.autostart=t,this.length=e,this.loop=i,this.startTimeMarker=Date.now(),this.position=0,this.animationFrameId=void 0,this.onTick=new r,t&&this.start()}var p=(f.prototype.updateTime=function(){this.animationFrameId=void 0;var t=this.getTimeFn();try{t!==this.position&&(this.position=t,this.onTick.dispatch(this,t))}finally{this.animationFrameId=requestAnimationFrame(this.updateTime.bind(this))}},f.prototype.onUpdate=function(t){this.onTick.subscribe(t)},f.prototype.offUpdate=function(t){this.onTick.unsubscribe(t)},f.prototype.deconstruct=function(){this.onTick.clear(),this.animationFrameId&&cancelAnimationFrame(this.animationFrameId)},f);function f(t){this.getTimeFn=t,this.position=0,this.onTick=new r,this.animationFrameId=void 0,this.animationFrameId=requestAnimationFrame(this.updateTime.bind(this))}function v(t){if(this.tempo=60,this.tempoBPS=1,this.offset=0,this.length=1/0,this.hasOwnTimer=!1,this.cues=[],this.cueData=[],this.currentBeat=-1,this.previousBeat=-1,this.currentCue=-1,this.previousCue=-1,this.events=new o,this.dragSamples=new Array,this.fixDrag=!1,void(this.dragOffset=0)!==t.tempo){if(0===t.tempo||t.tempo<0)throw new Error("Invalid tempo ("+t.tempo+"), must be greater than 0.");this.tempo=t.tempo,this.tempoBPS=this.tempo/60}if(void 0!==t.offset&&(this.offset=t.offset),t.fixDrag&&(this.fixDrag=!0,this.dragSamples=new Array(10).fill(0)),void 0!==t.cues){var e=function(t){var i=[],n=[];return t.forEach(function(t,e){"number"!=typeof t?(i[e]=t[0],n[e]=t[1]):i[e]=t}),[i,n]}(t.cues),i=e[0],n=e[1];this.cues=i,this.cueData=n}if(function(t){return"object"==typeof t&&"function"==typeof t.onUpdate&&"function"==typeof t.offUpdate&&"function"==typeof t.deconstruct}(t.timerSource))this.timer=t.timerSource,this.length=1/0;else if(void 0===t.timerSource)this.timer=new u(t.autostart,t.length,t.loop),this.hasOwnTimer=!0,void 0!==t.length&&(this.length=t.length);else{if("function"!=typeof t.timerSource)throw new Error("Constructing ClickTrack: Unknown value type for timerSource option.");this.timer=new p(t.timerSource),this.hasOwnTimer=!0}this.timer.onUpdate(this.setTime.bind(this))}return v.prototype.on=function(t,e){this.events.get(t).subscribe(e)},v.prototype.once=function(i,n){var s=this;this.events.get(i).subscribe(function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];s.events.get(i).unsubscribe(n),n.apply(s,t)})},v.prototype.off=function(t,e){this.events.get(t).unsubscribe(e)},v.prototype.start=function(){if(!(this.timer instanceof u))throw new Error("Click must be master to support start()");this.timer.start()},v.prototype.deconstruct=function(){this.timer.offUpdate(this.setTime.bind(this)),this.hasOwnTimer&&this.timer.deconstruct(),delete this.cues,delete this.cueData},v.prototype.setTime=function(t,e){var i=e-this.offset;this.previousBeat=this.currentBeat,this.currentBeat=i*this.tempoBPS,this.events.get("beat").count&&this.tickClickEvents(this.previousBeat,this.currentBeat),this.cues&&(this.events.get("cue").count||this.events.get("firstCue").count||this.events.get("lastCue").count)&&this.tickCueEvents(this.previousBeat,this.currentBeat)},Object.defineProperty(v.prototype,"beat",{get:function(){return this.currentBeat},enumerable:!0,configurable:!0}),v.prototype.tickCueEvents=function(t,e){var i,n=t<e?this.previousCue:0;for(i=Math.max(0,n);this.cues[i]<e+this.dragOffset&&i<this.cues.length;i++);if(i-=1,this.previousCue=this.currentCue,this.currentCue=i,-1!==this.currentCue&&this.currentCue!==this.previousCue&&!(this.currentCue<this.previousCue)){for(var s=this.previousCue+1;s<=this.currentCue;s++){var r=this.cueData[s]||null,o=this.cues[s]/this.tempoBPS,u=e-this.cues[s];this.fixDrag&&(this.dragSamples.unshift(u),this.dragSamples.pop());var a={time:o,beat:this.cues[s],data:r,cue:s,drag:u};this.events.get("cue").dispatchAsync(this,a),0===s&&this.events.get("firstCue").dispatchAsync(this,a),s===this.cues.length-1&&this.events.get("lastCue").dispatchAsync(this,a)}this.fixDrag&&(this.dragOffset=Math.min(1,Math.max(-1,this.dragSamples.reduce(function(t,e){return t+e},0)/this.dragSamples.length/2)))}},v.prototype.tickClickEvents=function(t,e){if(!(e<t)&&t!==e){var i=Math.max(0,Math.ceil(t)),n=Math.floor(e);if(!(n<i)){for(var s=[],r=i;r<=n;r++){var o=r/this.tempoBPS;s.push({time:o,beat:r,drag:e-r})}for(r=0;r<s.length;r++)this.events.get("beat").dispatchAsync(this,s[r])}}},v});