Touchgui/plugins/org.apache.cordova.dialogs/src/firefoxos/notification.js

changeset 0
e8ccd40d0ef6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Touchgui/plugins/org.apache.cordova.dialogs/src/firefoxos/notification.js	Thu Jun 04 14:50:33 2015 +0200
     1.3 @@ -0,0 +1,154 @@
     1.4 +/*
     1.5 + *
     1.6 + * Licensed to the Apache Software Foundation (ASF) under one
     1.7 + * or more contributor license agreements.  See the NOTICE file
     1.8 + * distributed with this work for additional information
     1.9 + * regarding copyright ownership.  The ASF licenses this file
    1.10 + * to you under the Apache License, Version 2.0 (the
    1.11 + * "License"); you may not use this file except in compliance
    1.12 + * with the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *   http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + * Unless required by applicable law or agreed to in writing,
    1.17 + * software distributed under the License is distributed on an
    1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.19 + * KIND, either express or implied.  See the License for the
    1.20 + * specific language governing permissions and limitations
    1.21 + * under the License.
    1.22 + *
    1.23 +*/
    1.24 +
    1.25 +var modulemapper = require('cordova/modulemapper');
    1.26 +
    1.27 +
    1.28 +var origOpenFunc = modulemapper.getOriginalSymbol(window, 'window.open');
    1.29 +
    1.30 +
    1.31 +function _empty() {}
    1.32 +
    1.33 +
    1.34 +function modal(message, callback, title, buttonLabels, domObjects) {
    1.35 +    var mainWindow = window;
    1.36 +    var modalWindow = origOpenFunc();
    1.37 +    var modalDocument = modalWindow.document;
    1.38 +
    1.39 +    modalDocument.write(
    1.40 +        '<html><head>' +
    1.41 +        '<link rel="stylesheet" type="text/css" href="/css/index.css" />' +
    1.42 +        '<link rel="stylesheet" type="text/css" href="/css/notification.css" />' +
    1.43 +        '</head><body></body></html>');
    1.44 +
    1.45 +    var box = modalDocument.createElement('form');
    1.46 +    box.setAttribute('role', 'dialog');
    1.47 +    // prepare and append empty section
    1.48 +    var section = modalDocument.createElement('section');
    1.49 +    box.appendChild(section);
    1.50 +    // add title
    1.51 +    var boxtitle = modalDocument.createElement('h1');
    1.52 +    boxtitle.appendChild(modalDocument.createTextNode(title));
    1.53 +    section.appendChild(boxtitle);
    1.54 +    // add message
    1.55 +    var boxMessage = modalDocument.createElement('p');
    1.56 +    boxMessage.appendChild(modalDocument.createTextNode(message));
    1.57 +    section.appendChild(boxMessage);
    1.58 +    // inject what's needed
    1.59 +    if (domObjects) {
    1.60 +        section.appendChild(domObjects);
    1.61 +    }
    1.62 +    // add buttons and assign callbackButton on click
    1.63 +    var menu = modalDocument.createElement('menu');
    1.64 +    box.appendChild(menu);
    1.65 +    for (var index = 0; index < buttonLabels.length; index++) {
    1.66 +        addButton(buttonLabels[index], index, (index === 0));
    1.67 +    }
    1.68 +    modalDocument.body.appendChild(box);
    1.69 +
    1.70 +    function addButton(label, index, recommended) {
    1.71 +        var thisButtonCallback = makeCallbackButton(index + 1);
    1.72 +        var button = modalDocument.createElement('button');
    1.73 +        button.appendChild(modalDocument.createTextNode(label));
    1.74 +        button.addEventListener('click', thisButtonCallback, false);
    1.75 +        if (recommended) {
    1.76 +          // TODO: default one listens to Enter key
    1.77 +          button.classList.add('recommend');
    1.78 +        }
    1.79 +        menu.appendChild(button);
    1.80 +    }
    1.81 +
    1.82 +    // TODO: onUnload listens to the cancel key
    1.83 +    function onUnload() {
    1.84 +        var result = 0;
    1.85 +        if (modalDocument.getElementById('prompt-input')) {
    1.86 +            result = {
    1.87 +                input1: '',
    1.88 +                buttonIndex: 0
    1.89 +            }
    1.90 +        }
    1.91 +        mainWindow.setTimeout(function() {
    1.92 +            callback(result);
    1.93 +        }, 10);
    1.94 +    };
    1.95 +    modalWindow.addEventListener('unload', onUnload, false);
    1.96 +
    1.97 +    // call callback and destroy modal
    1.98 +    function makeCallbackButton(labelIndex) {
    1.99 +        return function() {
   1.100 +          if (modalWindow) {
   1.101 +              modalWindow.removeEventListener('unload', onUnload, false);
   1.102 +              modalWindow.close();
   1.103 +          }
   1.104 +          // checking if prompt
   1.105 +          var promptInput = modalDocument.getElementById('prompt-input');
   1.106 +          var response;
   1.107 +          if (promptInput) {
   1.108 +              response = {
   1.109 +                input1: promptInput.value,
   1.110 +                buttonIndex: labelIndex
   1.111 +              };
   1.112 +          }
   1.113 +          response = response || labelIndex;
   1.114 +          callback(response);
   1.115 +        }
   1.116 +    }
   1.117 +}
   1.118 +
   1.119 +var Notification = {
   1.120 +    vibrate: function(milliseconds) {
   1.121 +        navigator.vibrate(milliseconds);
   1.122 +    },
   1.123 +    alert: function(successCallback, errorCallback, args) {
   1.124 +        var message = args[0];
   1.125 +        var title = args[1];
   1.126 +        var _buttonLabels = [args[2]];
   1.127 +        var _callback = (successCallback || _empty);
   1.128 +        modal(message, _callback, title, _buttonLabels);
   1.129 +    },
   1.130 +    confirm: function(successCallback, errorCallback, args) {
   1.131 +        var message = args[0];
   1.132 +        var title = args[1];
   1.133 +        var buttonLabels = args[2];
   1.134 +        var _callback = (successCallback || _empty);
   1.135 +        modal(message, _callback, title, buttonLabels);
   1.136 +    },
   1.137 +    prompt: function(successCallback, errorCallback, args) {
   1.138 +        var message = args[0];
   1.139 +        var title = args[1];
   1.140 +        var buttonLabels = args[2];
   1.141 +        var defaultText = args[3];
   1.142 +        var inputParagraph = document.createElement('p');
   1.143 +        inputParagraph.classList.add('input');
   1.144 +        var inputElement = document.createElement('input');
   1.145 +        inputElement.setAttribute('type', 'text');
   1.146 +        inputElement.id = 'prompt-input';
   1.147 +        if (defaultText) {
   1.148 +            inputElement.setAttribute('placeholder', defaultText);
   1.149 +        }
   1.150 +        inputParagraph.appendChild(inputElement);
   1.151 +        modal(message, successCallback, title, buttonLabels, inputParagraph);
   1.152 +    }
   1.153 +};
   1.154 +
   1.155 +
   1.156 +module.exports = Notification;
   1.157 +require('cordova/exec/proxy').add('Notification', Notification);

mercurial