From f53a40a3071d0a98e02b89ed92ed253a32f24d96 Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Tue, 30 Aug 2016 13:31:37 -0300 Subject: [PATCH 1/2] New modal service --- public/views/modals/amazon-card-details.html | 6 +- src/js/controllers/amazon.js | 13 +- .../controllers/modals/amazonCardDetails.js | 6 +- src/js/services/modalService.js | 153 ++++++++++++++++++ 4 files changed, 163 insertions(+), 15 deletions(-) create mode 100644 src/js/services/modalService.js diff --git a/public/views/modals/amazon-card-details.html b/public/views/modals/amazon-card-details.html index 528933a6c..00432c63c 100644 --- a/public/views/modals/amazon-card-details.html +++ b/public/views/modals/amazon-card-details.html @@ -1,13 +1,13 @@ - +

Details

- +
Amazon.com Gift Card diff --git a/src/js/controllers/amazon.js b/src/js/controllers/amazon.js index f36d984bf..2f02ea6ee 100644 --- a/src/js/controllers/amazon.js +++ b/src/js/controllers/amazon.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('copayApp.controllers').controller('amazonController', - function($scope, $timeout, $ionicModal, $log, lodash, bwcError, amazonService, platformInfo, nodeWebkit) { + function($scope, $timeout, $log, lodash, bwcError, amazonService, platformInfo, nodeWebkit, modalService) { if (platformInfo.isCordova && StatusBar.isVisible) { StatusBar.backgroundColorByHexString("#4B6178"); @@ -82,12 +82,11 @@ angular.module('copayApp.controllers').controller('amazonController', var self = this; $scope.card = card; - $ionicModal.fromTemplateUrl('views/modals/amazon-card-details.html', { - scope: $scope - }).then(function(modal) { - $scope.amazonCardDetailsModal = modal; - $scope.amazonCardDetailsModal.show(); - }); + modalService + .popup('views/modals/amazon-card-details.html', $scope) + .then(function(modal) { + modal.show(); + }); $scope.$on('UpdateAmazonList', function(event) { self.init(); diff --git a/src/js/controllers/modals/amazonCardDetails.js b/src/js/controllers/modals/amazonCardDetails.js index 432d32a0d..a93df11ac 100644 --- a/src/js/controllers/modals/amazonCardDetails.js +++ b/src/js/controllers/modals/amazonCardDetails.js @@ -22,7 +22,7 @@ angular.module('copayApp.controllers').controller('amazonCardDetailsController', remove: true }, function(err) { $scope.$emit('UpdateAmazonList'); - $scope.cancel(); + $scope.closeModal(); }); }; @@ -59,8 +59,4 @@ angular.module('copayApp.controllers').controller('amazonCardDetailsController', }); }; - $scope.cancel = function() { - $scope.amazonCardDetailsModal.hide(); - }; - }); diff --git a/src/js/services/modalService.js b/src/js/services/modalService.js new file mode 100644 index 000000000..ddbc49698 --- /dev/null +++ b/src/js/services/modalService.js @@ -0,0 +1,153 @@ +'use strict'; + +angular.module('copayApp.services').service('modalService', function($rootScope, $log, $ionicModal, $ionicPopup, platformInfo) { + var isCordova = platformInfo.isCordova; + + var _modalIonic = function(tpl, scope) { + var promise; + scope = scope || $rootScope.$new(); + + promise = $ionicModal.fromTemplateUrl(tpl, { + scope: scope + }).then(function(modal) { + scope.modal = modal; + return modal; + }); + + scope.openModal = function() { + scope.modal.show(); + }; + + scope.closeModal = function() { + scope.modal.hide(); + }; + + scope.$on('$destroy', function() { + scope.modal.remove(); + }); + + return promise; + }; + + var _ionicAlert = function(title, message, cb) { + if (!cb) cb = function() {}; + var promise = $ionicPopup.alert({ + title: title, + template: message + }).then(cb); + + return promise; + }; + + var _ionicConfirm = function(title, message, cb) { + $ionicPopup.confirm({ + title: title, + template: message + }).then(function(res) { + return cb(res); + }); + }; + + var _ionicPrompt = function(title, message, opts, cb) { + opts = opts || {}; + $ionicPopup.prompt({ + title: title, + template: message, + inputType: opts.inputType || 'password', + inputPlaceholder: opts.inputPlaceholder || 'Your password' + }).then(function(res) { + return cb(res) + }); + }; + + var _cordovaAlert = function(title, message, cb) { + if (!cb) cb = function() {}; + navigator.notification.alert(message, cb, title); + }; + + var _cordovaConfirm = function(title, message, cb) { + var onConfirm = function (buttonIndex) { + if (buttonIndex == 1) return cb(true); + else return cb(false); + } + navigator.notification.confirm(message, onConfirm, title); + }; + + var _cordovaPrompt = function(title, message, cb) { + var onPrompt = function (results) { + if (results.buttonIndex) return cb(results.input1); + else return cb(); + } + navigator.notification.prompt(message, onPrompt, title); + }; + + /** + * Show a simple alert popup + * + * @param {String} Title + * @param {String} Message + * @param {Callback} Function + * @returns {Promise} + */ + + this.showAlert = function(title, msg, cb) { + var message = msg.message ? msg.message : msg; + $log.warn(title + ": " + message); + + if (isCordova) + _cordovaAlert(title, message, cb); + else + _ionicAlert(title, message, cb); + }; + + /** + * Show a simple confirm popup + * + * @param {String} Title + * @param {String} Message + * @param {Callback} Function + * @returns {Callback} OK: true, Cancel: false + */ + + this.showConfirm = function(title, message, cb) { + $log.warn(title + ": " + message); + + if (isCordova) + _cordovaConfirm(title, message, cb); + else + _ionicConfirm(title, message, cb); + }; + + /** + * Show a simple prompt popup + * + * @param {String} Title + * @param {String} Message + * @param {Object} Object{ inputType, inputPlaceholder } + * @param {Callback} Function + * @returns {Callback} Return the value of the input if user presses OK + */ + + this.showPrompt = function(title, message, opts, cb) { + $log.warn(title + ": " + message); + + if (isCordova) + _cordovaPrompt(title, message, cb); + else + _ionicPrompt(title, message, opts, cb); + }; + + /** + * Show a modal popup + * + * @param {String} TemplateURL + * @param {Object} Scope + * @returns {Promise} + */ + + this.showModal = function(tpl, scope) { + return _modalIonic(tpl, scope); + }; + + +}); From 5e33b19b0e923e32888e3cbaf3ae256c54265021 Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Tue, 30 Aug 2016 14:56:36 -0300 Subject: [PATCH 2/2] Rename functions --- public/views/modals/amazon-card-details.html | 6 +-- src/js/controllers/amazon.js | 13 ++--- .../controllers/modals/amazonCardDetails.js | 6 ++- .../{modalService.js => popupService.js} | 54 ++++--------------- 4 files changed, 24 insertions(+), 55 deletions(-) rename src/js/services/{modalService.js => popupService.js} (70%) diff --git a/public/views/modals/amazon-card-details.html b/public/views/modals/amazon-card-details.html index 00432c63c..528933a6c 100644 --- a/public/views/modals/amazon-card-details.html +++ b/public/views/modals/amazon-card-details.html @@ -1,13 +1,13 @@ - +

Details

- +
Amazon.com Gift Card diff --git a/src/js/controllers/amazon.js b/src/js/controllers/amazon.js index 2f02ea6ee..f36d984bf 100644 --- a/src/js/controllers/amazon.js +++ b/src/js/controllers/amazon.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('copayApp.controllers').controller('amazonController', - function($scope, $timeout, $log, lodash, bwcError, amazonService, platformInfo, nodeWebkit, modalService) { + function($scope, $timeout, $ionicModal, $log, lodash, bwcError, amazonService, platformInfo, nodeWebkit) { if (platformInfo.isCordova && StatusBar.isVisible) { StatusBar.backgroundColorByHexString("#4B6178"); @@ -82,11 +82,12 @@ angular.module('copayApp.controllers').controller('amazonController', var self = this; $scope.card = card; - modalService - .popup('views/modals/amazon-card-details.html', $scope) - .then(function(modal) { - modal.show(); - }); + $ionicModal.fromTemplateUrl('views/modals/amazon-card-details.html', { + scope: $scope + }).then(function(modal) { + $scope.amazonCardDetailsModal = modal; + $scope.amazonCardDetailsModal.show(); + }); $scope.$on('UpdateAmazonList', function(event) { self.init(); diff --git a/src/js/controllers/modals/amazonCardDetails.js b/src/js/controllers/modals/amazonCardDetails.js index a93df11ac..432d32a0d 100644 --- a/src/js/controllers/modals/amazonCardDetails.js +++ b/src/js/controllers/modals/amazonCardDetails.js @@ -22,7 +22,7 @@ angular.module('copayApp.controllers').controller('amazonCardDetailsController', remove: true }, function(err) { $scope.$emit('UpdateAmazonList'); - $scope.closeModal(); + $scope.cancel(); }); }; @@ -59,4 +59,8 @@ angular.module('copayApp.controllers').controller('amazonCardDetailsController', }); }; + $scope.cancel = function() { + $scope.amazonCardDetailsModal.hide(); + }; + }); diff --git a/src/js/services/modalService.js b/src/js/services/popupService.js similarity index 70% rename from src/js/services/modalService.js rename to src/js/services/popupService.js index ddbc49698..0cddb00a9 100644 --- a/src/js/services/modalService.js +++ b/src/js/services/popupService.js @@ -1,42 +1,17 @@ 'use strict'; -angular.module('copayApp.services').service('modalService', function($rootScope, $log, $ionicModal, $ionicPopup, platformInfo) { +angular.module('copayApp.services').service('popupService', function($log, $ionicPopup, platformInfo) { + var isCordova = platformInfo.isCordova; - var _modalIonic = function(tpl, scope) { - var promise; - scope = scope || $rootScope.$new(); - - promise = $ionicModal.fromTemplateUrl(tpl, { - scope: scope - }).then(function(modal) { - scope.modal = modal; - return modal; - }); - - scope.openModal = function() { - scope.modal.show(); - }; - - scope.closeModal = function() { - scope.modal.hide(); - }; - - scope.$on('$destroy', function() { - scope.modal.remove(); - }); - - return promise; - }; + /*************** Ionic ****************/ var _ionicAlert = function(title, message, cb) { if (!cb) cb = function() {}; - var promise = $ionicPopup.alert({ + $ionicPopup.alert({ title: title, template: message }).then(cb); - - return promise; }; var _ionicConfirm = function(title, message, cb) { @@ -60,6 +35,8 @@ angular.module('copayApp.services').service('modalService', function($rootScope, }); }; + /*************** Cordova ****************/ + var _cordovaAlert = function(title, message, cb) { if (!cb) cb = function() {}; navigator.notification.alert(message, cb, title); @@ -75,7 +52,7 @@ angular.module('copayApp.services').service('modalService', function($rootScope, var _cordovaPrompt = function(title, message, cb) { var onPrompt = function (results) { - if (results.buttonIndex) return cb(results.input1); + if (results.buttonIndex == 1) return cb(results.input1); else return cb(); } navigator.notification.prompt(message, onPrompt, title); @@ -86,8 +63,7 @@ angular.module('copayApp.services').service('modalService', function($rootScope, * * @param {String} Title * @param {String} Message - * @param {Callback} Function - * @returns {Promise} + * @param {Callback} Function (optional) */ this.showAlert = function(title, msg, cb) { @@ -123,7 +99,7 @@ angular.module('copayApp.services').service('modalService', function($rootScope, * * @param {String} Title * @param {String} Message - * @param {Object} Object{ inputType, inputPlaceholder } + * @param {Object} Object{ inputType, inputPlaceholder } (optional) * @param {Callback} Function * @returns {Callback} Return the value of the input if user presses OK */ @@ -137,17 +113,5 @@ angular.module('copayApp.services').service('modalService', function($rootScope, _ionicPrompt(title, message, opts, cb); }; - /** - * Show a modal popup - * - * @param {String} TemplateURL - * @param {Object} Scope - * @returns {Promise} - */ - - this.showModal = function(tpl, scope) { - return _modalIonic(tpl, scope); - }; - });