Touchgui/plugins/org.apache.cordova.dialogs/src/firefoxos/notification.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 var modulemapper = require('cordova/modulemapper');
    25 var origOpenFunc = modulemapper.getOriginalSymbol(window, 'window.open');
    28 function _empty() {}
    31 function modal(message, callback, title, buttonLabels, domObjects) {
    32     var mainWindow = window;
    33     var modalWindow = origOpenFunc();
    34     var modalDocument = modalWindow.document;
    36     modalDocument.write(
    37         '<html><head>' +
    38         '<link rel="stylesheet" type="text/css" href="/css/index.css" />' +
    39         '<link rel="stylesheet" type="text/css" href="/css/notification.css" />' +
    40         '</head><body></body></html>');
    42     var box = modalDocument.createElement('form');
    43     box.setAttribute('role', 'dialog');
    44     // prepare and append empty section
    45     var section = modalDocument.createElement('section');
    46     box.appendChild(section);
    47     // add title
    48     var boxtitle = modalDocument.createElement('h1');
    49     boxtitle.appendChild(modalDocument.createTextNode(title));
    50     section.appendChild(boxtitle);
    51     // add message
    52     var boxMessage = modalDocument.createElement('p');
    53     boxMessage.appendChild(modalDocument.createTextNode(message));
    54     section.appendChild(boxMessage);
    55     // inject what's needed
    56     if (domObjects) {
    57         section.appendChild(domObjects);
    58     }
    59     // add buttons and assign callbackButton on click
    60     var menu = modalDocument.createElement('menu');
    61     box.appendChild(menu);
    62     for (var index = 0; index < buttonLabels.length; index++) {
    63         addButton(buttonLabels[index], index, (index === 0));
    64     }
    65     modalDocument.body.appendChild(box);
    67     function addButton(label, index, recommended) {
    68         var thisButtonCallback = makeCallbackButton(index + 1);
    69         var button = modalDocument.createElement('button');
    70         button.appendChild(modalDocument.createTextNode(label));
    71         button.addEventListener('click', thisButtonCallback, false);
    72         if (recommended) {
    73           // TODO: default one listens to Enter key
    74           button.classList.add('recommend');
    75         }
    76         menu.appendChild(button);
    77     }
    79     // TODO: onUnload listens to the cancel key
    80     function onUnload() {
    81         var result = 0;
    82         if (modalDocument.getElementById('prompt-input')) {
    83             result = {
    84                 input1: '',
    85                 buttonIndex: 0
    86             }
    87         }
    88         mainWindow.setTimeout(function() {
    89             callback(result);
    90         }, 10);
    91     };
    92     modalWindow.addEventListener('unload', onUnload, false);
    94     // call callback and destroy modal
    95     function makeCallbackButton(labelIndex) {
    96         return function() {
    97           if (modalWindow) {
    98               modalWindow.removeEventListener('unload', onUnload, false);
    99               modalWindow.close();
   100           }
   101           // checking if prompt
   102           var promptInput = modalDocument.getElementById('prompt-input');
   103           var response;
   104           if (promptInput) {
   105               response = {
   106                 input1: promptInput.value,
   107                 buttonIndex: labelIndex
   108               };
   109           }
   110           response = response || labelIndex;
   111           callback(response);
   112         }
   113     }
   114 }
   116 var Notification = {
   117     vibrate: function(milliseconds) {
   118         navigator.vibrate(milliseconds);
   119     },
   120     alert: function(successCallback, errorCallback, args) {
   121         var message = args[0];
   122         var title = args[1];
   123         var _buttonLabels = [args[2]];
   124         var _callback = (successCallback || _empty);
   125         modal(message, _callback, title, _buttonLabels);
   126     },
   127     confirm: function(successCallback, errorCallback, args) {
   128         var message = args[0];
   129         var title = args[1];
   130         var buttonLabels = args[2];
   131         var _callback = (successCallback || _empty);
   132         modal(message, _callback, title, buttonLabels);
   133     },
   134     prompt: function(successCallback, errorCallback, args) {
   135         var message = args[0];
   136         var title = args[1];
   137         var buttonLabels = args[2];
   138         var defaultText = args[3];
   139         var inputParagraph = document.createElement('p');
   140         inputParagraph.classList.add('input');
   141         var inputElement = document.createElement('input');
   142         inputElement.setAttribute('type', 'text');
   143         inputElement.id = 'prompt-input';
   144         if (defaultText) {
   145             inputElement.setAttribute('placeholder', defaultText);
   146         }
   147         inputParagraph.appendChild(inputElement);
   148         modal(message, successCallback, title, buttonLabels, inputParagraph);
   149     }
   150 };
   153 module.exports = Notification;
   154 require('cordova/exec/proxy').add('Notification', Notification);

mercurial