notification.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // Platform: browser
  22. window.navigator.notification = window.navigator.notification || {};
  23. module.exports.alert = window.navigator.notification.alert = function(message, callback) {
  24. // `notification.alert` executes asynchronously
  25. setTimeout(function() {
  26. window.alert(message);
  27. if (callback) {
  28. callback();
  29. }
  30. }, 0);
  31. };
  32. module.exports.confirm = window.navigator.notification.confirm = function(message, callback) {
  33. // `notification.confirm` executes asynchronously
  34. setTimeout(function() {
  35. var result = window.confirm(message);
  36. if (callback) {
  37. if (result) {
  38. callback(1); // OK
  39. }
  40. else {
  41. callback(2); // Cancel
  42. }
  43. }
  44. }, 0);
  45. };
  46. module.exports.prompt = window.navigator.notification.prompt = function(message, callback, title, buttonLabels, defaultText) {
  47. // `notification.prompt` executes asynchronously
  48. setTimeout(function() {
  49. var result = window.prompt(message, defaultText || '');
  50. if (callback) {
  51. if (result === null) {
  52. callback({ buttonIndex: 2, input1: '' }); // Cancel
  53. }
  54. else {
  55. callback({ buttonIndex: 1, input1: result }); // OK
  56. }
  57. }
  58. }, 0);
  59. };
  60. var audioContext = (function() {
  61. // Determine if the Audio API is supported by this browser
  62. var AudioApi = window.AudioContext;
  63. if (!AudioApi) {
  64. AudioApi = window.webkitAudioContext;
  65. }
  66. if (AudioApi) {
  67. // The Audio API is supported, so create a singleton instance of the AudioContext
  68. return new AudioApi();
  69. }
  70. return undefined;
  71. }());
  72. module.exports.beep = window.navigator.notification.beep = function(times) {
  73. if (times > 0) {
  74. var BEEP_DURATION = 700;
  75. var BEEP_INTERVAL = 300;
  76. if (audioContext) {
  77. // Start a beep, using the Audio API
  78. var osc = audioContext.createOscillator();
  79. osc.type = 0; // sounds like a "beep"
  80. osc.connect(audioContext.destination);
  81. osc.start(0);
  82. setTimeout(function() {
  83. // Stop the beep after the BEEP_DURATION
  84. osc.stop(0);
  85. if (--times > 0) {
  86. // Beep again, after a pause
  87. setTimeout(function() {
  88. navigator.notification.beep(times);
  89. }, BEEP_INTERVAL);
  90. }
  91. }, BEEP_DURATION);
  92. }
  93. else if (typeof(console) !== 'undefined' && typeof(console.log) === 'function') {
  94. // Audio API isn't supported, so just write `beep` to the console
  95. for (var i = 0; i < times; i++) {
  96. console.log('Beep!');
  97. }
  98. }
  99. }
  100. };