|
| 1 | +/* |
| 2 | +* Copyright 2013 Research In Motion Limited. |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +function showDialog(args, dialogType, result) { |
| 18 | + //Unpack and map the args |
| 19 | + var msg = JSON.parse(decodeURIComponent(args[0])), |
| 20 | + title = JSON.parse(decodeURIComponent(args[1])), |
| 21 | + btnLabel = JSON.parse(decodeURIComponent(args[2])); |
| 22 | + |
| 23 | + if (!Array.isArray(btnLabel)) { |
| 24 | + //Converts to array for (string) and (string,string, ...) cases |
| 25 | + btnLabel = btnLabel.split(","); |
| 26 | + } |
| 27 | + |
| 28 | + if (msg && typeof msg === "string") { |
| 29 | + msg = msg.replace(/^"|"$/g, "").replace(/\\"/g, '"').replace(/\\\\/g, '\\'); |
| 30 | + } else { |
| 31 | + result.error("message is undefined"); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + var messageObj = { |
| 36 | + title : title, |
| 37 | + htmlmessage : msg, |
| 38 | + dialogType : dialogType, |
| 39 | + optionalButtons : btnLabel |
| 40 | + }; |
| 41 | + |
| 42 | + //TODO replace with getOverlayWebview() when available in webplatform |
| 43 | + qnx.webplatform.getWebViews()[2].dialog.show(messageObj, function (data) { |
| 44 | + if (typeof data === "number") { |
| 45 | + //Confirm dialog call back needs to be called with one-based indexing [1,2,3 etc] |
| 46 | + result.callbackOk(++data, false); |
| 47 | + } else { |
| 48 | + //Prompt dialog callback expects object |
| 49 | + result.callbackOk({ |
| 50 | + buttonIndex: data.ok ? 1 : 0, |
| 51 | + input1: (data.oktext) ? decodeURIComponent(data.oktext) : "" |
| 52 | + }, false); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + result.noResult(true); |
| 57 | +} |
| 58 | + |
| 59 | +module.exports = { |
| 60 | + alert: function (success, fail, args, env) { |
| 61 | + var result = new PluginResult(args, env); |
| 62 | + |
| 63 | + if (Object.keys(args).length < 3) { |
| 64 | + result.error("Notification action - alert arguments not found."); |
| 65 | + } else { |
| 66 | + showDialog(args, "CustomAsk", result); |
| 67 | + } |
| 68 | + }, |
| 69 | + confirm: function (success, fail, args, env) { |
| 70 | + var result = new PluginResult(args, env); |
| 71 | + |
| 72 | + if (Object.keys(args).length < 3) { |
| 73 | + result.error("Notification action - confirm arguments not found."); |
| 74 | + } else { |
| 75 | + showDialog(args, "CustomAsk", result); |
| 76 | + } |
| 77 | + }, |
| 78 | + prompt: function (success, fail, args, env) { |
| 79 | + var result = new PluginResult(args, env); |
| 80 | + |
| 81 | + if (Object.keys(args).length < 3) { |
| 82 | + result.error("Notification action - prompt arguments not found."); |
| 83 | + } else { |
| 84 | + showDialog(args, "JavaScriptPrompt", result); |
| 85 | + } |
| 86 | + }, |
| 87 | + beep: function (success, fail, args, env) { |
| 88 | + var result = new PluginResult(args, env); |
| 89 | + result.error("Beep not supported"); |
| 90 | + } |
| 91 | +}; |
0 commit comments