|
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 /*global Windows:true */ |
|
23 |
|
24 var cordova = require('cordova'); |
|
25 |
|
26 var isAlertShowing = false; |
|
27 var alertStack = []; |
|
28 |
|
29 module.exports = { |
|
30 alert:function(win, loseX, args) { |
|
31 |
|
32 if (isAlertShowing) { |
|
33 var later = function () { |
|
34 module.exports.alert(win, loseX, args); |
|
35 }; |
|
36 alertStack.push(later); |
|
37 return; |
|
38 } |
|
39 isAlertShowing = true; |
|
40 |
|
41 var message = args[0]; |
|
42 var _title = args[1]; |
|
43 var _buttonLabel = args[2]; |
|
44 |
|
45 var md = new Windows.UI.Popups.MessageDialog(message, _title); |
|
46 md.commands.append(new Windows.UI.Popups.UICommand(_buttonLabel)); |
|
47 md.showAsync().then(function() { |
|
48 isAlertShowing = false; |
|
49 win && win(); |
|
50 |
|
51 if (alertStack.length) { |
|
52 setTimeout(alertStack.shift(), 0); |
|
53 } |
|
54 |
|
55 }); |
|
56 }, |
|
57 |
|
58 confirm:function(win, loseX, args) { |
|
59 |
|
60 if (isAlertShowing) { |
|
61 var later = function () { |
|
62 module.exports.confirm(win, loseX, args); |
|
63 }; |
|
64 alertStack.push(later); |
|
65 return; |
|
66 } |
|
67 |
|
68 isAlertShowing = true; |
|
69 |
|
70 try { |
|
71 var message = args[0]; |
|
72 var _title = args[1]; |
|
73 var buttons = args[2]; |
|
74 |
|
75 var md = new Windows.UI.Popups.MessageDialog(message, _title); |
|
76 |
|
77 buttons.forEach(function(buttonLabel) { |
|
78 md.commands.append(new Windows.UI.Popups.UICommand(buttonLabel)); |
|
79 }); |
|
80 |
|
81 md.showAsync().then(function(res) { |
|
82 isAlertShowing = false; |
|
83 var result = res ? buttons.indexOf(res.label) + 1 : 0; |
|
84 win && win(result); |
|
85 if (alertStack.length) { |
|
86 setTimeout(alertStack.shift(), 0); |
|
87 } |
|
88 |
|
89 }); |
|
90 } catch (e) { |
|
91 // set isAlertShowing flag back to false in case of exception |
|
92 isAlertShowing = false; |
|
93 if (alertStack.length) { |
|
94 setTimeout(alertStack.shift(), 0); |
|
95 } |
|
96 // rethrow exception |
|
97 throw e; |
|
98 } |
|
99 }, |
|
100 |
|
101 beep:function(winX, loseX, args) { |
|
102 |
|
103 // set a default args if it is not set |
|
104 args = args && args.length ? args : ["1"]; |
|
105 |
|
106 var snd = new Audio('ms-winsoundevent:Notification.Default'); |
|
107 var count = parseInt(args[0]) || 1; |
|
108 snd.msAudioCategory = "Alerts"; |
|
109 |
|
110 var onEvent = function () { |
|
111 if (count > 0) { |
|
112 snd.play(); |
|
113 } else { |
|
114 snd.removeEventListener("ended", onEvent); |
|
115 snd = null; |
|
116 winX && winX(); // notification.js just sends null, but this is future friendly |
|
117 } |
|
118 count--; |
|
119 }; |
|
120 snd.addEventListener("ended", onEvent); |
|
121 onEvent(); |
|
122 |
|
123 } |
|
124 }; |
|
125 |
|
126 require("cordova/exec/proxy").add("Notification",module.exports); |