Commit 8a6681554e9314f7d8ec4bccdb6e50f87ade56af
1 parent
a814fff2f4
Exists in
master
options prompt en object
Showing
4 changed files
with
47 additions
and
25 deletions
Show diff stats
src/js/controller.js
| ... | ... | @@ -34,17 +34,32 @@ angular.module('focaModal') |
| 34 | 34 | } |
| 35 | 35 | ]) |
| 36 | 36 | .controller('focaModalPromptController', [ |
| 37 | - '$uibModalInstance', '$scope', 'titulo', 'initValue', 'textarea', 'readonly', | |
| 38 | - function($uibModalInstance, $scope, titulo, initValue, textarea, readonly) { | |
| 39 | - $scope.readonly = readonly; | |
| 40 | - $scope.textarea = textarea; | |
| 41 | - $scope.titulo = titulo; | |
| 42 | - $scope.value = initValue; | |
| 37 | + '$uibModalInstance', '$scope', 'options', 'focaModalService', | |
| 38 | + function($uibModalInstance, $scope, options, focaModalService) { | |
| 39 | + | |
| 40 | + $scope.options = options; | |
| 43 | 41 | $scope.cancelar = function() { |
| 44 | 42 | $uibModalInstance.dismiss(); |
| 45 | 43 | }; |
| 46 | - $scope.aceptar = function() { | |
| 47 | - $uibModalInstance.close($scope.value); | |
| 44 | + $scope.aceptar = function(key) { | |
| 45 | + if (key === 13) { | |
| 46 | + if(options.email && !validateEmails($scope.options.value)) { | |
| 47 | + focaModalService.alert('Ingrese email/s válido/s'); | |
| 48 | + return; | |
| 49 | + } | |
| 50 | + | |
| 51 | + $uibModalInstance.close($scope.options.value); | |
| 52 | + } | |
| 48 | 53 | }; |
| 54 | + | |
| 55 | + function validateEmails(emails) { | |
| 56 | + var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | |
| 57 | + var arr = emails.split(','); | |
| 58 | + | |
| 59 | + arr.forEach(function(email) { | |
| 60 | + if(!re.test(String(email).trim().toLowerCase())) return false; | |
| 61 | + }); | |
| 62 | + return true; | |
| 63 | + } | |
| 49 | 64 | } |
| 50 | 65 | ]); |
src/js/controllerModal.js
src/js/service.js
| 1 | 1 | angular.module('focaModal') |
| 2 | - .factory('focaModalService', [ | |
| 2 | + .service('focaModalService', [ | |
| 3 | 3 | '$uibModal', 'API_ENDPOINT', '$http', |
| 4 | 4 | function($uibModal, API_ENDPOINT, $http) { |
| 5 | 5 | return { |
| ... | ... | @@ -71,18 +71,23 @@ angular.module('focaModal') |
| 71 | 71 | } |
| 72 | 72 | ); |
| 73 | 73 | }, |
| 74 | - prompt: function(titulo, initValue, textarea, readonly){ | |
| 75 | - textarea = textarea ? true : false; | |
| 76 | - readonly = readonly ? true : false; | |
| 74 | + prompt: function(options) { | |
| 75 | + //DEFAULT VALUES | |
| 76 | + var defaultValues = { | |
| 77 | + titulo: '', | |
| 78 | + value: '', | |
| 79 | + textarea: false, | |
| 80 | + readonly: false, | |
| 81 | + email: false | |
| 82 | + }; | |
| 83 | + options = Object.assign(defaultValues, options); | |
| 84 | + | |
| 77 | 85 | return $uibModal.open({ |
| 78 | 86 | templateUrl: 'modal-prompt.html', |
| 79 | 87 | controller: 'focaModalPromptController', |
| 80 | 88 | size: 'md', |
| 81 | 89 | resolve: { |
| 82 | - titulo: function() {return titulo;}, | |
| 83 | - initValue: function() {return initValue;}, | |
| 84 | - textarea: function() {return textarea;}, | |
| 85 | - readonly: function() {return readonly;} | |
| 90 | + options: function() {return options;}, | |
| 86 | 91 | } |
| 87 | 92 | }) |
| 88 | 93 | .result.then( |
src/views/modal-prompt.html
| 1 | 1 | <div class="modal-header"> |
| 2 | - <h4 ng-bind="titulo"></h4> | |
| 2 | + <h4 ng-bind="options.titulo"></h4> | |
| 3 | 3 | </div> |
| 4 | 4 | <div class="modal-body"> |
| 5 | 5 | <input |
| 6 | 6 | type="text" |
| 7 | 7 | class="form-control" |
| 8 | - ng-model="value" | |
| 9 | - ng-show="!textarea" | |
| 10 | - ng-readonly="readonly"> | |
| 8 | + ng-model="options.value" | |
| 9 | + ng-show="!options.textarea" | |
| 10 | + ng-readonly="options.readonly" | |
| 11 | + ng-keypress="aceptar($event.keyCode)"> | |
| 11 | 12 | |
| 12 | 13 | <textarea |
| 13 | 14 | rows="5" |
| 14 | 15 | class="form-control text-uppercase" |
| 15 | - ng-model="value" | |
| 16 | - ng-show="textarea" | |
| 17 | - ng-readonly="readonly"></textarea> | |
| 16 | + ng-model="options.value" | |
| 17 | + ng-show="options.textarea" | |
| 18 | + ng-readonly="options.readonly" | |
| 19 | + ng-keypress="aceptar($event.keyCode)"></textarea> | |
| 18 | 20 | </div> |
| 19 | 21 | <div class="modal-footer"> |
| 20 | - <button class="btn btn-primary" ng-click="aceptar()" foca-focus="true">Aceptar</button> | |
| 22 | + <button class="btn btn-primary" ng-click="aceptar(13)" foca-focus="true">Aceptar</button> | |
| 21 | 23 | <button class="btn btn-default" ng-click="cancelar()">Cancelar</button> |
| 22 | 24 | </div> |