michael@0: /* michael@0: * Copyright 2013 Research In Motion Limited. michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * 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, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: function showDialog(args, dialogType, result) { michael@0: //Unpack and map the args michael@0: var msg = JSON.parse(decodeURIComponent(args[0])), michael@0: title = JSON.parse(decodeURIComponent(args[1])), michael@0: btnLabel = JSON.parse(decodeURIComponent(args[2])); michael@0: michael@0: if (!Array.isArray(btnLabel)) { michael@0: //Converts to array for (string) and (string,string, ...) cases michael@0: btnLabel = btnLabel.split(","); michael@0: } michael@0: michael@0: if (msg && typeof msg === "string") { michael@0: msg = msg.replace(/^"|"$/g, "").replace(/\\"/g, '"'); michael@0: } else { michael@0: result.error("message is undefined"); michael@0: return; michael@0: } michael@0: michael@0: var messageObj = { michael@0: title : title, michael@0: htmlmessage : msg, michael@0: dialogType : dialogType, michael@0: optionalButtons : btnLabel michael@0: }; michael@0: michael@0: //TODO replace with getOverlayWebview() when available in webplatform michael@0: qnx.webplatform.getWebViews()[2].dialog.show(messageObj, function (data) { michael@0: if (typeof data === "number") { michael@0: //Confirm dialog call back needs to be called with one-based indexing [1,2,3 etc] michael@0: result.callbackOk(++data, false); michael@0: } else { michael@0: //Prompt dialog callback expects object michael@0: result.callbackOk({ michael@0: buttonIndex: data.ok ? 1 : 0, michael@0: input1: (data.oktext) ? decodeURIComponent(data.oktext) : "" michael@0: }, false); michael@0: } michael@0: }); michael@0: michael@0: result.noResult(true); michael@0: } michael@0: michael@0: module.exports = { michael@0: alert: function (success, fail, args, env) { michael@0: var result = new PluginResult(args, env); michael@0: michael@0: if (Object.keys(args).length < 3) { michael@0: result.error("Notification action - alert arguments not found."); michael@0: } else { michael@0: showDialog(args, "CustomAsk", result); michael@0: } michael@0: }, michael@0: confirm: function (success, fail, args, env) { michael@0: var result = new PluginResult(args, env); michael@0: michael@0: if (Object.keys(args).length < 3) { michael@0: result.error("Notification action - confirm arguments not found."); michael@0: } else { michael@0: showDialog(args, "CustomAsk", result); michael@0: } michael@0: }, michael@0: prompt: function (success, fail, args, env) { michael@0: var result = new PluginResult(args, env); michael@0: michael@0: if (Object.keys(args).length < 3) { michael@0: result.error("Notification action - prompt arguments not found."); michael@0: } else { michael@0: showDialog(args, "JavaScriptPrompt", result); michael@0: } michael@0: } michael@0: };