michael@0: /* michael@0: * michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * michael@0: */ michael@0: michael@0: var modulemapper = require('cordova/modulemapper'); michael@0: michael@0: michael@0: var origOpenFunc = modulemapper.getOriginalSymbol(window, 'window.open'); michael@0: michael@0: michael@0: function _empty() {} michael@0: michael@0: michael@0: function modal(message, callback, title, buttonLabels, domObjects) { michael@0: var mainWindow = window; michael@0: var modalWindow = origOpenFunc(); michael@0: var modalDocument = modalWindow.document; michael@0: michael@0: modalDocument.write( michael@0: '
' + michael@0: '' + michael@0: '' + michael@0: ''); michael@0: michael@0: var box = modalDocument.createElement('form'); michael@0: box.setAttribute('role', 'dialog'); michael@0: // prepare and append empty section michael@0: var section = modalDocument.createElement('section'); michael@0: box.appendChild(section); michael@0: // add title michael@0: var boxtitle = modalDocument.createElement('h1'); michael@0: boxtitle.appendChild(modalDocument.createTextNode(title)); michael@0: section.appendChild(boxtitle); michael@0: // add message michael@0: var boxMessage = modalDocument.createElement('p'); michael@0: boxMessage.appendChild(modalDocument.createTextNode(message)); michael@0: section.appendChild(boxMessage); michael@0: // inject what's needed michael@0: if (domObjects) { michael@0: section.appendChild(domObjects); michael@0: } michael@0: // add buttons and assign callbackButton on click michael@0: var menu = modalDocument.createElement('menu'); michael@0: box.appendChild(menu); michael@0: for (var index = 0; index < buttonLabels.length; index++) { michael@0: addButton(buttonLabels[index], index, (index === 0)); michael@0: } michael@0: modalDocument.body.appendChild(box); michael@0: michael@0: function addButton(label, index, recommended) { michael@0: var thisButtonCallback = makeCallbackButton(index + 1); michael@0: var button = modalDocument.createElement('button'); michael@0: button.appendChild(modalDocument.createTextNode(label)); michael@0: button.addEventListener('click', thisButtonCallback, false); michael@0: if (recommended) { michael@0: // TODO: default one listens to Enter key michael@0: button.classList.add('recommend'); michael@0: } michael@0: menu.appendChild(button); michael@0: } michael@0: michael@0: // TODO: onUnload listens to the cancel key michael@0: function onUnload() { michael@0: var result = 0; michael@0: if (modalDocument.getElementById('prompt-input')) { michael@0: result = { michael@0: input1: '', michael@0: buttonIndex: 0 michael@0: } michael@0: } michael@0: mainWindow.setTimeout(function() { michael@0: callback(result); michael@0: }, 10); michael@0: }; michael@0: modalWindow.addEventListener('unload', onUnload, false); michael@0: michael@0: // call callback and destroy modal michael@0: function makeCallbackButton(labelIndex) { michael@0: return function() { michael@0: if (modalWindow) { michael@0: modalWindow.removeEventListener('unload', onUnload, false); michael@0: modalWindow.close(); michael@0: } michael@0: // checking if prompt michael@0: var promptInput = modalDocument.getElementById('prompt-input'); michael@0: var response; michael@0: if (promptInput) { michael@0: response = { michael@0: input1: promptInput.value, michael@0: buttonIndex: labelIndex michael@0: }; michael@0: } michael@0: response = response || labelIndex; michael@0: callback(response); michael@0: } michael@0: } michael@0: } michael@0: michael@0: var Notification = { michael@0: vibrate: function(milliseconds) { michael@0: navigator.vibrate(milliseconds); michael@0: }, michael@0: alert: function(successCallback, errorCallback, args) { michael@0: var message = args[0]; michael@0: var title = args[1]; michael@0: var _buttonLabels = [args[2]]; michael@0: var _callback = (successCallback || _empty); michael@0: modal(message, _callback, title, _buttonLabels); michael@0: }, michael@0: confirm: function(successCallback, errorCallback, args) { michael@0: var message = args[0]; michael@0: var title = args[1]; michael@0: var buttonLabels = args[2]; michael@0: var _callback = (successCallback || _empty); michael@0: modal(message, _callback, title, buttonLabels); michael@0: }, michael@0: prompt: function(successCallback, errorCallback, args) { michael@0: var message = args[0]; michael@0: var title = args[1]; michael@0: var buttonLabels = args[2]; michael@0: var defaultText = args[3]; michael@0: var inputParagraph = document.createElement('p'); michael@0: inputParagraph.classList.add('input'); michael@0: var inputElement = document.createElement('input'); michael@0: inputElement.setAttribute('type', 'text'); michael@0: inputElement.id = 'prompt-input'; michael@0: if (defaultText) { michael@0: inputElement.setAttribute('placeholder', defaultText); michael@0: } michael@0: inputParagraph.appendChild(inputElement); michael@0: modal(message, successCallback, title, buttonLabels, inputParagraph); michael@0: } michael@0: }; michael@0: michael@0: michael@0: module.exports = Notification; michael@0: require('cordova/exec/proxy').add('Notification', Notification);