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