UNPKG

2.94 kBJavaScriptView Raw
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/* global qnx, PluginResult */
18
19function showDialog(args, dialogType, result) {
20 //Unpack and map the args
21 var msg = JSON.parse(decodeURIComponent(args[0])),
22 title = JSON.parse(decodeURIComponent(args[1])),
23 btnLabel = JSON.parse(decodeURIComponent(args[2]));
24
25 if (!Array.isArray(btnLabel)) {
26 //Converts to array for (string) and (string,string, ...) cases
27 btnLabel = btnLabel.split(",");
28 }
29
30 if (msg && typeof msg === "string") {
31 msg = msg.replace(/^"|"$/g, "").replace(/\\"/g, '"');
32 } else {
33 result.error("message is undefined");
34 return;
35 }
36
37 var messageObj = {
38 title : title,
39 htmlmessage : msg,
40 dialogType : dialogType,
41 optionalButtons : btnLabel
42 };
43
44 //TODO replace with getOverlayWebview() when available in webplatform
45 qnx.webplatform.getWebViews()[2].dialog.show(messageObj, function (data) {
46 if (typeof data === "number") {
47 //Confirm dialog call back needs to be called with one-based indexing [1,2,3 etc]
48 result.callbackOk(++data, false);
49 } else {
50 //Prompt dialog callback expects object
51 result.callbackOk({
52 buttonIndex: data.ok ? 1 : 0,
53 input1: (data.oktext) ? decodeURIComponent(data.oktext) : ""
54 }, false);
55 }
56 });
57
58 result.noResult(true);
59}
60
61module.exports = {
62 alert: function (success, fail, args, env) {
63 var result = new PluginResult(args, env);
64
65 if (Object.keys(args).length < 3) {
66 result.error("Notification action - alert arguments not found.");
67 } else {
68 showDialog(args, "CustomAsk", result);
69 }
70 },
71 confirm: function (success, fail, args, env) {
72 var result = new PluginResult(args, env);
73
74 if (Object.keys(args).length < 3) {
75 result.error("Notification action - confirm arguments not found.");
76 } else {
77 showDialog(args, "CustomAsk", result);
78 }
79 },
80 prompt: function (success, fail, args, env) {
81 var result = new PluginResult(args, env);
82
83 if (Object.keys(args).length < 3) {
84 result.error("Notification action - prompt arguments not found.");
85 } else {
86 showDialog(args, "JavaScriptPrompt", result);
87 }
88 }
89};