From: Bill Erickson Date: Sun, 15 May 2016 16:51:24 +0000 (-0400) Subject: webstaff: egAudio HTML5 audio service X-Git-Tag: sprint4-merge-nov22~172 X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=d02c84fce8a3e99ce7be5357277b5958d8b04183;p=working%2FEvergreen.git webstaff: egAudio HTML5 audio service egCore.audio.play('audio.event.dot.path'); Service to look up audio URL's by key name. Supports fall-thru behavior where 'foo.bar.baz' will fall-thru to 'foo.bar' and 'foo' depending on whether an audio file is avaialable. Signed-off-by: Bill Erickson --- diff --git a/Open-ILS/src/templates/staff/base_js.tt2 b/Open-ILS/src/templates/staff/base_js.tt2 index 980ec5b4a5..c1b721bb17 100644 --- a/Open-ILS/src/templates/staff/base_js.tt2 +++ b/Open-ILS/src/templates/staff/base_js.tt2 @@ -33,6 +33,7 @@ + diff --git a/Open-ILS/web/audio/notifications/error.wav b/Open-ILS/web/audio/notifications/error.wav new file mode 100644 index 0000000000..41e23e0794 Binary files /dev/null and b/Open-ILS/web/audio/notifications/error.wav differ diff --git a/Open-ILS/web/audio/notifications/info.wav b/Open-ILS/web/audio/notifications/info.wav new file mode 100644 index 0000000000..6740d3fe4e Binary files /dev/null and b/Open-ILS/web/audio/notifications/info.wav differ diff --git a/Open-ILS/web/audio/notifications/success.wav b/Open-ILS/web/audio/notifications/success.wav new file mode 100644 index 0000000000..eb83e16393 Binary files /dev/null and b/Open-ILS/web/audio/notifications/success.wav differ diff --git a/Open-ILS/web/audio/notifications/warning.wav b/Open-ILS/web/audio/notifications/warning.wav new file mode 100644 index 0000000000..76c4ecfb45 Binary files /dev/null and b/Open-ILS/web/audio/notifications/warning.wav differ diff --git a/Open-ILS/web/js/ui/default/staff/Gruntfile.js b/Open-ILS/web/js/ui/default/staff/Gruntfile.js index 6588ca818e..910d7e146f 100644 --- a/Open-ILS/web/js/ui/default/staff/Gruntfile.js +++ b/Open-ILS/web/js/ui/default/staff/Gruntfile.js @@ -141,6 +141,7 @@ module.exports = function(grunt) { 'services/startup.js', 'services/hatch.js', 'services/print.js', + 'services/audio.js', 'services/coresvc.js', 'services/navbar.js', 'services/statusbar.js', diff --git a/Open-ILS/web/js/ui/default/staff/services/audio.js b/Open-ILS/web/js/ui/default/staff/services/audio.js new file mode 100644 index 0000000000..1b88978e12 --- /dev/null +++ b/Open-ILS/web/js/ui/default/staff/services/audio.js @@ -0,0 +1,78 @@ +/** + * Core Service - egAudio + * + * Plays audio files by key name. Each sound uses a dot-path to indicate + * the sound. + * + * For example: + * sound => 'warning.checkout.no_item' + * URLs are tested in the following order until a valid audio file is found + * or no other paths are left to check. + * + * /audio/notifications/warning/checkout/not_found.wav + * /audio/notifications/warning/checkout.wav + * /audio/notifications/warning.wav + * + * TODO: move audio file base path settings to the template + * for configurability? + * + * Files are only played when sounds are configured to play via + * workstation settings. + */ + +angular.module('egCoreMod') + +.factory('egAudio', ['$q','egHatch', function($q, egHatch) { + + var service = { + url_cache : {}, // map key names to audio file URLs + base_url : '/audio/notifications/' + }; + + /** + * Play the sound found at the requested string path. 'path' is a + * key name which maps to an audio file URL. + */ + service.play = function(path) { + if (!path) return; + service.play_url(path, path); + } + + service.play_url = function(path, orig_path) { + + var url = service.url_cache[path] || + service.base_url + path.replace(/\./g, '/') + '.wav'; + + var player = new Audio(url); + + player.onloadeddata = function() { + console.debug('Playing audio URL: ' + url); + service.url_cache[orig_path] = url; + player.play(); + }; + + if (service.url_cache[path]) { + // when serving from the cache, avoid secondary URL lookups. + return; + } + + player.onerror = function() { + // Unable to play path at the requested URL. + + if (!path.match(/\./)) { + // all fall-through options have been exhausted. + // No path to play. + console.warn( + "No suitable URL found for path '" + orig_path + "'"); + return; + } + + // Fall through to the next (more generic) option + path = path.replace(/\.[^\.]+$/, ''); + service.play_url(path, orig_path); + } + } + + return service; +}]); + diff --git a/Open-ILS/web/js/ui/default/staff/services/coresvc.js b/Open-ILS/web/js/ui/default/staff/services/coresvc.js index 57b836194b..f75477047a 100644 --- a/Open-ILS/web/js/ui/default/staff/services/coresvc.js +++ b/Open-ILS/web/js/ui/default/staff/services/coresvc.js @@ -9,9 +9,11 @@ angular.module('egCoreMod') .factory('egCore', ['egIDL','egNet','egEnv','egOrg','egPCRUD','egEvent','egAuth', - 'egPerm','egHatch','egPrint','egStartup','egStrings','egDate', + 'egPerm','egHatch','egPrint','egStartup','egStrings','egAudio', + 'egDate', function(egIDL , egNet , egEnv , egOrg , egPCRUD , egEvent , egAuth , - egPerm , egHatch , egPrint , egStartup , egStrings , egDate) { + egPerm , egHatch , egPrint , egStartup , egStrings , egAudio , + egDate) { return { idl : egIDL, @@ -26,6 +28,7 @@ function(egIDL , egNet , egEnv , egOrg , egPCRUD , egEvent , egAuth , print : egPrint, startup : egStartup, strings : egStrings, + audio : egAudio, date : egDate }; diff --git a/Open-ILS/web/js/ui/default/staff/test/karma.conf.js b/Open-ILS/web/js/ui/default/staff/test/karma.conf.js index 06014df7b7..760fc3b8f2 100644 --- a/Open-ILS/web/js/ui/default/staff/test/karma.conf.js +++ b/Open-ILS/web/js/ui/default/staff/test/karma.conf.js @@ -34,14 +34,14 @@ module.exports = function(config){ 'services/org.js', 'services/hatch.js', 'services/print.js', + 'services/audio.js', 'services/coresvc.js', 'services/user.js', 'services/startup.js', 'services/ui.js', 'services/statusbar.js', 'services/grid.js', - 'services/navbar.js', - 'services/date.js', + 'services/navbar.js', 'services/date.js', // load app scripts 'app.js', 'circ/**/*.js',