From a843647890429f5af96cb021f184584aa521e685 Mon Sep 17 00:00:00 2001 From: Pawel Andruszkiewicz Date: Wed, 21 Oct 2015 09:50:06 +0200 Subject: [PATCH] [Dialog] Implementation of Dialog plugin. [Verification] All automatic and manual tests pass. Change-Id: I522f9533c1cc7cd047ecee595df0e0ab221cba76 Signed-off-by: Pawel Andruszkiewicz --- src/cordova-api.gyp | 1 + src/dialog/cordova_dialog.gyp | 25 +++ src/dialog/cordova_dialog_api.js | 341 +++++++++++++++++++++++++++++++++ src/dialog/cordova_dialog_extension.cc | 39 ++++ src/dialog/cordova_dialog_extension.h | 36 ++++ 5 files changed, 442 insertions(+) create mode 100644 src/dialog/cordova_dialog.gyp create mode 100755 src/dialog/cordova_dialog_api.js create mode 100755 src/dialog/cordova_dialog_extension.cc create mode 100755 src/dialog/cordova_dialog_extension.h diff --git a/src/cordova-api.gyp b/src/cordova-api.gyp index bb61934..013edb0 100644 --- a/src/cordova-api.gyp +++ b/src/cordova-api.gyp @@ -10,6 +10,7 @@ 'dependencies': [ 'cordova/cordova.gyp:*', 'device/cordova_device.gyp:*', + 'dialog/cordova_dialog.gyp:*', 'file/cordova_file.gyp:*', ], }, diff --git a/src/dialog/cordova_dialog.gyp b/src/dialog/cordova_dialog.gyp new file mode 100644 index 0000000..8f13ec3 --- /dev/null +++ b/src/dialog/cordova_dialog.gyp @@ -0,0 +1,25 @@ +{ + 'includes':[ + '/usr/include/webapi-plugins/src/common/common.gypi', + ], + 'targets': [ + { + 'target_name': 'tizen_cordova_dialog', + 'type': 'loadable_module', + 'sources': [ + 'cordova_dialog_api.js', + 'cordova_dialog_extension.cc', + 'cordova_dialog_extension.h', + ], + 'include_dirs': [ + '../', + '<(SHARED_INTERMEDIATE_DIR)', + ], + 'variables': { + 'packages': [ + 'webapi-plugins', + ], + }, + }, + ], +} diff --git a/src/dialog/cordova_dialog_api.js b/src/dialog/cordova_dialog_api.js new file mode 100755 index 0000000..6491303 --- /dev/null +++ b/src/dialog/cordova_dialog_api.js @@ -0,0 +1,341 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +var _navigator = navigator || {}; +var _document = document || {}; + +var playback = (function() { + var soundElement; + var counter = 1; + + tizen.systemsetting.getProperty("NOTIFICATION_EMAIL", function(v) { + soundElement = new Audio(v); + soundElement.addEventListener('ended', function() { + if (--counter > 0) { + soundElement.play(); + } + }); + }, function(e) { + console.error('Failed to get the notification sound: ' + e); + }); + + function beep(times) { + counter = times || 1; + if (soundElement) { + soundElement.play(); + } + } + + return { + beep: beep + } +})(); + +var popup = (function () { + var boxId = 'cordova-modal-box'; + var overlayId = 'cordova-modal-overlay'; + var popupId = 'cordova-modal-popup'; + var buttonIdPrefix = 'cordova-modal-popup-button-'; + var inputId = 'cordova-modal-popup-input'; + var d = _document; + var sheet; + var box; + var overlay; + var popup; + var dismissCallback; + + function isPopupVisible() { + if (box) { + return box.style.display === 'block'; + } else { + return false; + } + } + + function showPopup() { + box.style.display = 'block'; + } + + function hidePopup() { + box.style.display = 'none'; + } + + function createCloseCallback(callback, id, prompt) { + return function() { + var text; + if (prompt) { + text = d.getElementById(inputId).value; + } + hidePopup(); + callback(id, text); + } + } + + function hasCssClass(name) { + for (var i = 0; i < d.styleSheets.length; ++i) { + for (var j = 0; j < d.styleSheets[i].rules.length; ++j) { + if (name === d.styleSheets[i].rules[j].selectorText) { + return true; + } + } + } + return false; + } + + function createStyleSheet() { + if (!sheet) { + var style = document.createElement('style'); + d.head.appendChild(style); + sheet = style.sheet; + } + } + + function initCss() { + var hasOverlay = hasCssClass('.cordova-ui-popupwindow-overlay'); + var hasPopup = hasCssClass('.cordova-ui-popupwindow'); + + if (!hasOverlay) { + createStyleSheet(); + + sheet.insertRule('.cordova-ui-popupwindow-overlay {' + + 'background: #000000;' + + 'opacity: 0.6;' + + 'position: fixed;' + + 'top: 0;' + + 'left: 0;' + + 'margin: 0;' + + 'padding: 0;' + + 'width: 100%;' + + 'height: 100%;' + + 'z-index: 1200;' + + '}'); + } + + if (!hasPopup) { + createStyleSheet(); + + sheet.insertRule('.cordova-ui-popupwindow {' + + 'position: fixed;' + + 'top: 50%;' + + 'left: 50%;' + + 'margin-right: -50%;' + + 'transform: translate(-50%, -50%);' + + '-webkit-transform: translate(-50%, -50%);' + + 'z-index: 1201 !important;' + + 'color: #f8f6ef;' + + 'background: #2a2d30;' + + 'width: 90%;' + + 'padding: 6px;' + + '}'); + sheet.insertRule('.cordova-ui-popupwindow .popup-title {' + + 'width: 100%;' + + 'height: 100%;' + + 'font-size: 1.0909090909090908rem;' + + 'background: #5093b6;' + + '}'); + sheet.insertRule('.cordova-ui-popupwindow .popup-title p {' + + 'margin: 0rem 0rem;' + + 'padding: 0.5909090909090909rem 0.5rem;' + + '}'); + sheet.insertRule('.cordova-ui-popupwindow .popup-text {' + + 'width: 100%;' + + 'color: #f9f9f9;' + + 'font-size: 1.0909090909090908rem;' + + 'background: #2a2d30;' + + '}'); + sheet.insertRule('.cordova-ui-popupwindow .popup-text p {' + + 'text-align: left;' + + 'padding: 0.4rem;' + + '}'); + sheet.insertRule('.cordova-ui-popupwindow .popup-input {' + + 'width: 100%;' + + 'padding-bottom: 0.4rem;' + + 'text-align: center;' + + '}'); + sheet.insertRule('.cordova-ui-popupwindow .popup-input input {' + + 'width: 90%;' + + 'color: #222222;' + + 'font-size: 1rem;' + + 'line-height: 1.2;' + + 'font-weight: normal;' + + 'border: 1px solid #555;' + + 'border-radius: 5px;' + + '-webkit-border-radius: 5px;' + + '}'); + sheet.insertRule('.cordova-ui-popupwindow .popup-button-bg {' + + 'font-size: 1.4545454545454546rem;' + + 'background: #2a2d30;' + + 'width: 100%;' + + 'padding-top: 0.5rem;' + + 'padding-bottom: 0.5rem;' + + 'vertical-align: middle;' + + 'text-align: right;' + + '}'); + sheet.insertRule('.cordova-ui-popupwindow .popup-button-bg input {' + + 'border: 1px solid #555;' + + 'border-radius: 5px;' + + '-webkit-border-radius: 5px;' + + 'text-align: center;' + + 'display: inline-block;' + + 'background: #444;' + + 'color: #9ab;' + + 'font-size: 0.8em;' + + 'text-decoration: none;' + + 'margin: 1px 1px 1px 3px;' + + '}'); + } + } + + function initHtml() { + var touchMoveDisable = function(e) { + e.preventDefault(); // disable the touch-move of the background + }; + + box = d.createElement('div'); + box.id = boxId; + + overlay = box.appendChild(d.createElement('div')); + overlay.id = overlayId; + overlay.className = 'cordova-ui-popupwindow-overlay'; + overlay.addEventListener('click', function() { + if (dismissCallback) { + dismissCallback(); + } + }, false); + overlay.addEventListener('touchmove', touchMoveDisable); + + popup = box.appendChild(d.createElement('div')); + popup.id = popupId; + popup.className = 'cordova-ui-popupwindow'; + popup.addEventListener('touchmove', touchMoveDisable); + + hidePopup(); + + d.getElementsByTagName('body')[0].appendChild(box); + } + + function init() { + if (!box) { + initCss(); + initHtml(); + } + } + + function show(options) { + if (isPopupVisible()) { + console.warn('Popup is already shown, ignoring.'); + return; + } + + init(); + + options = options || {}; + + // prepare contents of the popup + var html = ''; + html += ''; + html += ''; + if (options.prompt) { + html += ''; + } + var buttons = options.buttons || ['Button']; + html += ''; + + popup.innerHTML = html; + + // link callbacks + var callback = options.callback || function(){}; + dismissCallback = createCloseCallback(callback, 0, options.prompt); + + for (var i = 0; i < buttons.length; ++i) { + var e = d.getElementById(buttonIdPrefix + (i + 1)); + e.addEventListener('click', createCloseCallback(callback, i + 1, options.prompt), false); + } + + // show popup + showPopup(); + } + + return { + show: show + }; +})(); + +var notification = { + alert: function(message, alertCallback, title, buttonName) { + title = title || 'Alert'; + buttonName = buttonName || 'OK'; + popup.show({ + message: message, + callback: alertCallback, + title: title, + buttons: [buttonName] + }); + }, + confirm: function(message, confirmCallback, title, buttonLabels) { + title = title || 'Confirm'; + buttonLabels = buttonLabels || ['OK', 'Cancel']; + if (!Array.isArray(buttonLabels)) { + if (typeof buttonLabels === 'string') { + buttonLabels = buttonLabels.split(','); // deprecated way of specifying the labels + } else { + buttonLabels = [buttonLabels]; + } + } + popup.show({ + message: message, + callback: confirmCallback, + title: title, + buttons: buttonLabels + }); + }, + prompt: function(message, promptCallback, title, buttonLabels, defaultText) { + title = title || 'Prompt'; + buttonLabels = buttonLabels || ['OK', 'Cancel']; + if (!Array.isArray(buttonLabels)) { + buttonLabels = [buttonLabels]; + } + defaultText = defaultText || ''; + popup.show({ + message: message, + callback: function(idx, text) { + promptCallback({buttonIndex: idx, input1: text}); + }, + title: title, + buttons: buttonLabels, + prompt: true, + promptDefaultText: defaultText + }); + }, + beep: function(times) { + playback.beep(times); + } +}; + +Object.freeze(notification); +Object.defineProperty(_navigator, 'notification', { + configurable: false, + enumerable: true, + writable: false, + value: notification +}); + +console.log('Loaded cordova.dialog API'); diff --git a/src/dialog/cordova_dialog_extension.cc b/src/dialog/cordova_dialog_extension.cc new file mode 100755 index 0000000..a375d3a --- /dev/null +++ b/src/dialog/cordova_dialog_extension.cc @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dialog/cordova_dialog_extension.h" + +// This will be generated from cordova_dialog_api.js +extern const char kSource_cordova_dialog_api[]; + +common::Extension* CreateExtension() { + return new extension::cordova::dialog::CordovaDialogExtension(); +} + +namespace extension { +namespace cordova { +namespace dialog { + +CordovaDialogExtension::CordovaDialogExtension() { + SetExtensionName("tizen.cordova.dialog"); + SetJavaScriptAPI(kSource_cordova_dialog_api); +} + +CordovaDialogExtension::~CordovaDialogExtension() {} + +} // dialog +} // cordova +} // extension diff --git a/src/dialog/cordova_dialog_extension.h b/src/dialog/cordova_dialog_extension.h new file mode 100755 index 0000000..613653b --- /dev/null +++ b/src/dialog/cordova_dialog_extension.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DIALOG_CORDOVA_DIALOG_EXTENSION_H_ +#define DIALOG_CORDOVA_DIALOG_EXTENSION_H_ + +#include + +namespace extension { +namespace cordova { +namespace dialog { + +class CordovaDialogExtension : public common::Extension { + public: + CordovaDialogExtension(); + virtual ~CordovaDialogExtension(); +}; + +} // dialog +} // cordova +} // extension + +#endif // DIALOG_CORDOVA_DIALOG_EXTENSION_H_ -- 2.7.4