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.

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

mercurial