Thu, 04 Jun 2015 14:50:33 +0200
Genesis of lecture sources for Droidcon Berlin 2015 in Postbahnhof.
michael@0 | 1 | /* |
michael@0 | 2 | * |
michael@0 | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
michael@0 | 4 | * or more contributor license agreements. See the NOTICE file |
michael@0 | 5 | * distributed with this work for additional information |
michael@0 | 6 | * regarding copyright ownership. The ASF licenses this file |
michael@0 | 7 | * to you under the Apache License, Version 2.0 (the |
michael@0 | 8 | * "License"); you may not use this file except in compliance |
michael@0 | 9 | * with the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, |
michael@0 | 14 | * software distributed under the License is distributed on an |
michael@0 | 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
michael@0 | 16 | * KIND, either express or implied. See the License for the |
michael@0 | 17 | * specific language governing permissions and limitations |
michael@0 | 18 | * under the License. |
michael@0 | 19 | * |
michael@0 | 20 | */ |
michael@0 | 21 | |
michael@0 | 22 | /*global Windows:true */ |
michael@0 | 23 | |
michael@0 | 24 | var cordova = require('cordova'); |
michael@0 | 25 | |
michael@0 | 26 | var isAlertShowing = false; |
michael@0 | 27 | var alertStack = []; |
michael@0 | 28 | |
michael@0 | 29 | module.exports = { |
michael@0 | 30 | alert:function(win, loseX, args) { |
michael@0 | 31 | |
michael@0 | 32 | if (isAlertShowing) { |
michael@0 | 33 | var later = function () { |
michael@0 | 34 | module.exports.alert(win, loseX, args); |
michael@0 | 35 | }; |
michael@0 | 36 | alertStack.push(later); |
michael@0 | 37 | return; |
michael@0 | 38 | } |
michael@0 | 39 | isAlertShowing = true; |
michael@0 | 40 | |
michael@0 | 41 | var message = args[0]; |
michael@0 | 42 | var _title = args[1]; |
michael@0 | 43 | var _buttonLabel = args[2]; |
michael@0 | 44 | |
michael@0 | 45 | var md = new Windows.UI.Popups.MessageDialog(message, _title); |
michael@0 | 46 | md.commands.append(new Windows.UI.Popups.UICommand(_buttonLabel)); |
michael@0 | 47 | md.showAsync().then(function() { |
michael@0 | 48 | isAlertShowing = false; |
michael@0 | 49 | win && win(); |
michael@0 | 50 | |
michael@0 | 51 | if (alertStack.length) { |
michael@0 | 52 | setTimeout(alertStack.shift(), 0); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | }); |
michael@0 | 56 | }, |
michael@0 | 57 | |
michael@0 | 58 | confirm:function(win, loseX, args) { |
michael@0 | 59 | |
michael@0 | 60 | if (isAlertShowing) { |
michael@0 | 61 | var later = function () { |
michael@0 | 62 | module.exports.confirm(win, loseX, args); |
michael@0 | 63 | }; |
michael@0 | 64 | alertStack.push(later); |
michael@0 | 65 | return; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | isAlertShowing = true; |
michael@0 | 69 | |
michael@0 | 70 | try { |
michael@0 | 71 | var message = args[0]; |
michael@0 | 72 | var _title = args[1]; |
michael@0 | 73 | var buttons = args[2]; |
michael@0 | 74 | |
michael@0 | 75 | var md = new Windows.UI.Popups.MessageDialog(message, _title); |
michael@0 | 76 | |
michael@0 | 77 | buttons.forEach(function(buttonLabel) { |
michael@0 | 78 | md.commands.append(new Windows.UI.Popups.UICommand(buttonLabel)); |
michael@0 | 79 | }); |
michael@0 | 80 | |
michael@0 | 81 | md.showAsync().then(function(res) { |
michael@0 | 82 | isAlertShowing = false; |
michael@0 | 83 | var result = res ? buttons.indexOf(res.label) + 1 : 0; |
michael@0 | 84 | win && win(result); |
michael@0 | 85 | if (alertStack.length) { |
michael@0 | 86 | setTimeout(alertStack.shift(), 0); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | }); |
michael@0 | 90 | } catch (e) { |
michael@0 | 91 | // set isAlertShowing flag back to false in case of exception |
michael@0 | 92 | isAlertShowing = false; |
michael@0 | 93 | if (alertStack.length) { |
michael@0 | 94 | setTimeout(alertStack.shift(), 0); |
michael@0 | 95 | } |
michael@0 | 96 | // rethrow exception |
michael@0 | 97 | throw e; |
michael@0 | 98 | } |
michael@0 | 99 | }, |
michael@0 | 100 | |
michael@0 | 101 | beep:function(winX, loseX, args) { |
michael@0 | 102 | |
michael@0 | 103 | // set a default args if it is not set |
michael@0 | 104 | args = args && args.length ? args : ["1"]; |
michael@0 | 105 | |
michael@0 | 106 | var snd = new Audio('ms-winsoundevent:Notification.Default'); |
michael@0 | 107 | var count = parseInt(args[0]) || 1; |
michael@0 | 108 | snd.msAudioCategory = "Alerts"; |
michael@0 | 109 | |
michael@0 | 110 | var onEvent = function () { |
michael@0 | 111 | if (count > 0) { |
michael@0 | 112 | snd.play(); |
michael@0 | 113 | } else { |
michael@0 | 114 | snd.removeEventListener("ended", onEvent); |
michael@0 | 115 | snd = null; |
michael@0 | 116 | winX && winX(); // notification.js just sends null, but this is future friendly |
michael@0 | 117 | } |
michael@0 | 118 | count--; |
michael@0 | 119 | }; |
michael@0 | 120 | snd.addEventListener("ended", onEvent); |
michael@0 | 121 | onEvent(); |
michael@0 | 122 | |
michael@0 | 123 | } |
michael@0 | 124 | }; |
michael@0 | 125 | |
michael@0 | 126 | require("cordova/exec/proxy").add("Notification",module.exports); |