]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/audio.js
LP#1526185 Disable second toast on permfail
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / services / audio.js
1 /**
2  * Core Service - egAudio
3  *
4  * Plays audio files by key name.  Each sound uses a dot-path to indicate 
5  * the sound.  
6  *
7  * For example:
8  * sound => 'warning.checkout.no_item'
9  * URLs are tested in the following order until a valid audio file is found
10  * or no other paths are left to check.
11  *
12  * /audio/notifications/warning/checkout/not_found.wav
13  * /audio/notifications/warning/checkout.wav
14  * /audio/notifications/warning.wav
15  *
16  * TODO: move audio file base path settings to the template 
17  * for configurability?
18  *
19  * Files are only played when sounds are configured to play via 
20  * workstation settings.
21  */
22
23 angular.module('egCoreMod')
24
25 .factory('egAudio', ['$q','egHatch', function($q, egHatch) {
26
27     var service = {
28         url_cache : {}, // map key names to audio file URLs
29         base_url : '/audio/notifications/'
30     };
31
32     /** 
33      * Play the sound found at the requested string path.  'path' is a 
34      * key name which maps to an audio file URL.
35      */
36     service.play = function(path) {
37         if (!path) return;
38         service.play_url(path, path);
39     }
40
41     service.play_url = function(path, orig_path) {
42
43         egHatch.getItem('eg.audio.disable').then(function(audio_disabled) {
44             if (!audio_disabled) {
45         
46                 var url = service.url_cache[path] || 
47                     service.base_url + path.replace(/\./g, '/') + '.wav';
48
49                 var player = new Audio(url);
50
51                 player.onloadeddata = function() {
52                     service.url_cache[orig_path] = url;
53                     player.play();
54                 };
55
56                 if (service.url_cache[path]) {
57                     // when serving from the cache, avoid secondary URL lookups.
58                     return;
59                 }
60
61                 player.onerror = function() {
62                     // Unable to play path at the requested URL.
63             
64                     if (!path.match(/\./)) {
65                         // all fall-through options have been exhausted.
66                         // No path to play.
67                         console.warn(
68                             "No suitable URL found for path '" + orig_path + "'");
69                         return;
70                     }
71
72                     // Fall through to the next (more generic) option
73                     path = path.replace(/\.[^\.]+$/, '');
74                     service.play_url(path, orig_path);
75                 }
76             }
77         });
78     }
79
80     return service;
81 }]);
82