<script src="[% ctx.media_prefix %]/js/ui/default/staff/services/startup.js"></script>
<script src="[% ctx.media_prefix %]/js/ui/default/staff/services/hatch.js"></script>
<script src="[% ctx.media_prefix %]/js/ui/default/staff/services/print.js"></script>
+<script src="[% ctx.media_prefix %]/js/ui/default/staff/services/audio.js"></script>
<script src="[% ctx.media_prefix %]/js/ui/default/staff/services/coresvc.js"></script>
<script src="[% ctx.media_prefix %]/js/ui/default/staff/services/user.js"></script>
<script src="[% ctx.media_prefix %]/js/ui/default/staff/services/navbar.js"></script>
--- /dev/null
+/**
+ * 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;
+}]);
+
.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,
print : egPrint,
startup : egStartup,
strings : egStrings,
+ audio : egAudio,
date : egDate
};