]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/sound.js
internal: an alternative to default_focus
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / util / sound.js
1 dump('entering util/sound.js\n');
2
3 if (typeof util == 'undefined') util = {};
4 util.sound = function (params) {
5
6     try {
7
8         if (!params) { params = {}; }
9
10         this.sig = (new Date()).getMinutes() + ':' + (new Date()).getSeconds() + '.' + (new Date()).getMilliseconds()/1000;
11         if (params.sig) { this.sig += ' ' + params.sig; }
12
13         /* We're going to turn this guy into a singleton, at least for a given window, and look for it in xulG */
14         if (! window.xulG) { window.xulG = {}; }
15         if (window.xulG._sound && !params.reuse_queue_from_this_snd_obj) { 
16             dump('SOUND('+this.sig+'): reusing sound from ' + window.xulG._sound.origin + '('+xulG._sound.sig+') for ' + location.pathname + '\n');
17             return window.xulG._sound; 
18         } else {
19             dump('SOUND('+this.sig+'): instantiating new sound for ' + location.pathname + '\n');
20         }
21
22         /* So we can queue up sounds and put a pause between them instead of having them trample over each other */
23         /* Limitation: interval only gets set once for a singleton */
24         if (params.interval || params.reuse_queue_from_this_snd_obj) {
25             this._queue = true;
26             if (params.reuse_queue_from_this_snd_obj) {
27                 this._funcs = params.reuse_queue_from_this_snd_obj._funcs || [];
28             } else {
29                 this._funcs = [];
30             }
31             JSAN.use('util.exec');
32             this._exec = new util.exec();
33             var delay = params.interval;
34             if (!delay) { delay = _sound_delay_interval; /* define this in server/skin/custom.js */ }
35             if (!delay) { delay = 2000; }
36             var intervalId = this._exec.timer( this._funcs, delay );
37             dump('SOUND('+this.sig+'): starting timer with intervalId = ' + intervalId + '\n');
38         }
39
40         var SOUNDContractID = "@mozilla.org/sound;1";
41         var SOUNDIID        = Components.interfaces.nsISound;
42         this.SOUND          = Components.classes[SOUNDContractID].createInstance(SOUNDIID);
43         this.SOUND.init(); // not necessary, but helps avoid delays?
44
45         this.origin = location.pathname;
46
47         window.xulG._sound = this;
48         return this;
49
50     } catch(E) {
51         dump('error in util.sound constructor: ' + E + '\n');
52         return this;
53     }
54 };
55
56 util.sound.prototype = {
57
58     'xp_url_init' : function (aURL) {
59         try {
60             var URLContractID   = "@mozilla.org/network/standard-url;1";
61             var URLIID          = Components.classes[URLContractID].createInstance( );
62             var URL             = URLIID.QueryInterface(Components.interfaces.nsIURL);
63             if (aURL) {
64                 URL.spec = aURL;
65             }
66             return URL;
67         } catch(E) {
68             alert('xp_url_init(): ' + E);
69         }
70     },
71
72     'play_url' : function(url) {
73
74         if (!url) { return; /* sound of silence */ }
75
76         var obj = this;
77         try {
78             JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
79             var url2 = obj.xp_url_init( data.server + url );
80             if (typeof data.no_sound == 'undefined' || data.no_sound == false || data.no_sound == 'false') {
81
82                 if (obj._queue) {
83                     dump('SOUND('+obj.sig+'): queueing file = ' + url + '\n');
84                     obj._funcs.push( function() { 
85                         dump('SOUND('+obj.sig+'): playing file = ' + url + '\n');
86                         obj.SOUND.play( url2 ); 
87                     } );
88                 } else {
89                     dump('SOUND('+obj.sig+'): playing file = ' + url + '\n');
90                     obj.SOUND.play( url2 );
91                 }
92             }
93         } catch(E) {
94             try { if (data.no_sound == 'undefined' || data.no_sound == false || data.no_sound == 'false') obj.SOUND.beep(); } catch(F) { 
95                 dump('beep(): ' + F + '\n');
96             }
97             dump('play_url(): ' + E + '\n');
98         }
99     },
100
101     'event' : function event(evt) {
102         var key = 'AUDIO_' + arguments.callee.name + '_' + evt.textcode;
103         dump('SOUND('+this.sig+'): key = ' + key + '\n');
104         this.play_url( urls[key] );
105     },
106
107     'special' : function special(e) {
108         var key = 'AUDIO_' + arguments.callee.name + '_' + e;
109         dump('SOUND('+this.sig+'): key = ' + key + '\n');
110         this.play_url( urls[key] );
111     },
112
113     'good' : function good(e){
114         var key = 'AUDIO_' + arguments.callee.name;
115         dump('SOUND('+this.sig+'): key = ' + key + '\n');
116         this.play_url( urls[key] );
117     },
118
119     'bad' : function bad(e){
120         var key = 'AUDIO_' + arguments.callee.name;
121         dump('SOUND('+this.sig+'): key = ' + key + '\n');
122         this.play_url( urls[key] );
123     },
124
125     'horrible' : function horrible(e){
126         var key = 'AUDIO_' + arguments.callee.name;
127         dump('SOUND('+this.sig+'): key = ' + key + '\n');
128         this.play_url( urls[key] );
129     },
130
131     'circ_good' : function circ_good(e){
132         var key = 'AUDIO_' + arguments.callee.name;
133         dump('SOUND('+this.sig+'): key = ' + key + '\n');
134         this.play_url( urls[key] );
135     },
136
137     'circ_bad' : function circ_bad(e){
138         var key = 'AUDIO_' + arguments.callee.name;
139         dump('SOUND('+this.sig+'): key = ' + key + '\n');
140         this.play_url( urls[key] );
141     }
142 }
143
144 dump('exiting util/sound.js\n');