UNPKG

3.65 kBJavaScriptView Raw
1/*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20*/
21
22// Platform: browser
23window.navigator.notification = window.navigator.notification || {};
24
25module.exports.alert = window.navigator.notification.alert = function(message, callback) {
26 // `notification.alert` executes asynchronously
27 setTimeout(function() {
28 window.alert(message);
29 if (callback) {
30 callback();
31 }
32 }, 0);
33};
34
35
36module.exports.confirm = window.navigator.notification.confirm = function(message, callback) {
37 // `notification.confirm` executes asynchronously
38 setTimeout(function() {
39 var result = window.confirm(message);
40 if (callback) {
41 if (result) {
42 callback(1); // OK
43 }
44 else {
45 callback(2); // Cancel
46 }
47 }
48 }, 0);
49};
50
51
52module.exports.prompt = window.navigator.notification.prompt = function(message, callback, title, buttonLabels, defaultText) {
53 // `notification.prompt` executes asynchronously
54 setTimeout(function() {
55 var result = window.prompt(message, defaultText || '');
56 if (callback) {
57 if (result === null) {
58 callback({ buttonIndex: 2, input1: '' }); // Cancel
59 }
60 else {
61 callback({ buttonIndex: 1, input1: result }); // OK
62 }
63 }
64 }, 0);
65};
66
67
68var audioContext = (function() {
69 // Determine if the Audio API is supported by this browser
70 var AudioApi = window.AudioContext;
71 if (!AudioApi) {
72 AudioApi = window.webkitAudioContext;
73 }
74
75 if (AudioApi) {
76 // The Audio API is supported, so create a singleton instance of the AudioContext
77 return new AudioApi();
78 }
79
80 return undefined;
81}());
82
83module.exports.beep = window.navigator.notification.beep = function(times) {
84 if (times > 0) {
85 var BEEP_DURATION = 700;
86 var BEEP_INTERVAL = 300;
87
88 if (audioContext) {
89 // Start a beep, using the Audio API
90 var osc = audioContext.createOscillator();
91 osc.type = 0; // sounds like a "beep"
92 osc.connect(audioContext.destination);
93 osc.start(0);
94
95 setTimeout(function() {
96 // Stop the beep after the BEEP_DURATION
97 osc.stop(0);
98
99 if (--times > 0) {
100 // Beep again, after a pause
101 setTimeout(function() {
102 navigator.notification.beep(times);
103 }, BEEP_INTERVAL);
104 }
105
106 }, BEEP_DURATION);
107 }
108 else if (typeof(console) !== 'undefined' && typeof(console.log) === 'function') {
109 // Audio API isn't supported, so just write `beep` to the console
110 for (var i = 0; i < times; i++) {
111 console.log('Beep!');
112 }
113 }
114 }
115};