/* global angular */ /** * Created by intelWorx on 19/11/2015. */ (function() { 'use strict'; /** * Main module, your application should depend on this * @module {mdWavesurfer} */ let app = angular.module('mdWavesurfer', ['ngMaterial']); /** * @ngdoc service * @name $mdWavesurferUtils * * @description * * Utility service for this directive, exposes method: * - getLength(url), which returns a promise for the length of the audio specified by URL * * ```js * app.directive('myFancyDirective', function(mdWavesurferUtils) { * return { * restrict: 'e', * link: function(scope, el, attrs) { * mdWavesurferUtils(attrs.url) * .then(function(l){ * scope.length = l; * }, function(){ * someErrorhandler() * }) * ; * } * }; * }); * ``` */ app.factory('mdWavesurferUtils', [ '$q', '$document', '$timeout', function($q, $document, $timeout) { return { getLength: function(object) { let deferred = $q.defer(); let estimateLength = function(url) { let audio = $document[0].createElement('audio'); audio.src = url; audio.addEventListener( 'loadeddata', function listener() { deferred.resolve(this.duration); audio.removeEventListener( 'loadeddata', listener ); audio.src = 'data:audio/mpeg,0'; //destroy loading. } ); audio.addEventListener('error', function(e) { deferred.resolve(e.target.error); }); }; if (typeof object === 'string') { //this is a URL estimateLength(object); } else { $timeout(function() { deferred.reject( new DOMError( 'NotSupportedError', 'Specified argument is not supported' ) ); }); } return deferred.promise; } }; } ]); /** * @ngdoc filter * @name mdWavesurferTimeFormat * * Simple filter to convert value in seconds to MM:SS format * * @param Number duration in seconds */ app.filter('mdWavesurferTimeFormat', function() { return function(input) { if (!input) { return '00:00'; } const minutes = Math.floor(input / 60); const seconds = Math.floor(input) % 60; return ( (minutes < 10 ? '0' : '') + minutes + ':' + (seconds < 10 ? '0' : '') + seconds ); }; }); app.controller('mdWavesurferAudioController', [ '$attrs', '$element', function(attributes, $element) { let audio = this; audio.tracks = []; audio.selectedIndex = audio.selectedIndex || 0; audio.currentTrack = null; //adds to an audio track audio.addTrack = function(trackScope) { if (audio.tracks.indexOf(trackScope) < 0) { audio.tracks.push(trackScope); } if (!audio.currentTrack) { audio.currentTrack = audio.tracks[audio.selectedIndex]; } }; //remove audio track audio.removeTrack = function(trackScope) { const idx = audio.tracks.indexOf(trackScope); if (idx >= 0) { audio.tracks.splice(idx, 1); } }; audio.playerProperties = {}; let nKey; for (let attr in attributes) { if (attr.match(/^player/)) { nKey = attr.replace(/^player([A-Z])/, function(m, $1) { return $1.toLowerCase(); }); audio.playerProperties[nKey] = attributes[attr]; } } let getPlayer = function() { return $element .find('md-wavesurfer-player') .controller('mdWavesurferPlayer'); }; let setAutoPlay = function(forcePlay) { let controller = getPlayer(); if ( controller && (forcePlay || controller.surfer.isPlaying()) ) { controller.autoPlay = true; } }; audio.setTrack = function(idx, forcePlay) { if (audio.tracks.length > idx) { if (audio.selectedIndex === idx) { let ctrl = getPlayer(); ctrl.surfer.playPause(); } else { setAutoPlay(forcePlay); audio.currentTrack = audio.tracks[idx]; audio.selectedIndex = idx; } } }; audio.extraButtons = [ { icon: 'zmdi zmdi-skip-previous', title: 'Previous', action: function($event) { if (audio.selectedIndex > 0) { audio.setTrack(audio.selectedIndex - 1); } }, class: '' }, { icon: 'zmdi zmdi-skip-next', title: 'Next', action: function($event) { if (audio.selectedIndex < audio.tracks.length - 1) { audio.setTrack(audio.selectedIndex + 1); } }, class: '' } ]; } ]); /** * @ngdoc directive * @name md-wavesurfer-audio * * Directive for playing a set of audio files. This directive is analogous to `