Touchgui/plugins/org.apache.cordova.dialogs/src/windows8/NotificationProxy.js

Thu, 04 Jun 2015 14:50:33 +0200

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 04 Jun 2015 14:50:33 +0200
changeset 0
e8ccd40d0ef6
permissions
-rw-r--r--

Genesis of lecture sources for Droidcon Berlin 2015 in Postbahnhof.

     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 */
    22 /*global Windows:true */
    24 var cordova = require('cordova');
    26 var isAlertShowing = false;
    27 var alertStack = [];
    29 module.exports = {
    30     alert:function(win, loseX, args) {
    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;
    41         var message = args[0];
    42         var _title = args[1];
    43         var _buttonLabel = args[2];
    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();
    51             if (alertStack.length) {
    52                 setTimeout(alertStack.shift(), 0);
    53             }
    55         });
    56     },
    58     confirm:function(win, loseX, args) {
    60         if (isAlertShowing) {
    61             var later = function () {
    62                 module.exports.confirm(win, loseX, args);
    63             };
    64             alertStack.push(later);
    65             return;
    66         }
    68         isAlertShowing = true;
    70         try {
    71             var message = args[0];
    72             var _title = args[1];
    73             var buttons = args[2];
    75             var md = new Windows.UI.Popups.MessageDialog(message, _title);
    77             buttons.forEach(function(buttonLabel) {
    78                 md.commands.append(new Windows.UI.Popups.UICommand(buttonLabel));
    79             });
    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                 }
    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     },
   101     beep:function(winX, loseX, args) {
   103         // set a default args if it is not set
   104         args = args && args.length ? args : ["1"];
   106         var snd = new Audio('ms-winsoundevent:Notification.Default');
   107         var count = parseInt(args[0]) || 1;
   108         snd.msAudioCategory = "Alerts";
   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();
   123     }
   124 };
   126 require("cordova/exec/proxy").add("Notification",module.exports);

mercurial