1(function() { 2 'use strict'; 3 4 // Check we have sweet alert js included 5 if (angular.isUndefined(window.swal)) { 6 throw 'Please make sure Sweet Alert 2 is included! https://github.com/limonte/sweetalert2'; 7 } 8 9 angular.module('swangular', []).provider('swangular', function ProvideSwangular() { 10 return { 11 setDefaults: setDefaults, 12 $get: [ 13 '$compile', 14 '$http', 15 '$rootScope', 16 '$q', 17 '$controller', 18 '$injector', 19 '$templateCache', 20 '$timeout', 21 instantiateSwangular 22 ] 23 }; 24 25 function setDefaults(options) { 26 swal.setDefaults(options); 27 } 28 29 function instantiateSwangular($compile, $http, $rootScope, $q, $controller, $injector, $templateCache, $timeout) { 30 /* 31 * 32 * Sweet Alert functions 33 * The four functions below are just convenience methods 34 * 35 * ex. Swangular.alert("This is a warning message"); 36 * 37 */ 38 function swal_alert(message, customOptions) { 39 var options = angular.extend( 40 { 41 title: 'Alert', 42 text: message, 43 type: 'warning',
44 showCancelButton: false 45 }, 46 customOptions 47 ); 48 49 return $q.when(swal(options)); 50 } 51 52 function swal_info(message, customOptions) { 53 var options = angular.extend( 54 { 55 title: 'Info', 56 text: message, 57 type: 'info', 58 showCancelButton: false 59 }, 60 customOptions 61 ); 62 63 return $q.when(swal(options)); 64 } 65 66 function swal_success(message, customOptions) { 67 var options = angular.extend( 68 { 69 title: 'Success', 70 text: message, 71 type: 'success', 72 showCancelButton: false 73 }, 74 customOptions 75 ); 76 77 return $q.when(swal(options)); 78 } 79 80 function swal_confirm(message, customOptions) { 81 var options = angular.extend( 82 { 83 title: 'Alert', 84 text: message, 85 type: 'warning' 86 }, 87 customOptions 88 ); 89 90 return $q.when(swal(options)); 91 } 92 93 /* 94 * 95 * HTML Sweet Alert function 96 * Use this for for passing html that will be compiled to Angular 97 * You can pass an HTML string directly via 'html' option, or you can pass a link to template via 'templateHtml' option. 98 * 99 * ex. Swangular.html({title: "Please fill in!",scope: $scope,templateHtml: '/templates/template.html'}) 100 * .then(function() { console.log("Done"); }) 101 * 102 */ 103 function swal_open(options) { 104 var resolve = angular.extend({}, options.resolve); 105 106 angular.forEach(resolve, function(value, key) { 107 resolve[key] = angular.isString(value) ? $injector.get(value) : $injector.invoke(value, null, null, key); 108 }); 109 110 return $q 111 .all({ 112 html: _getHtml(options), 113 locals: $q.all(resolve) 114 }) 115 .then(function(setup) { 116 var scope = options.scope, 117 controller = options.controller, 118 controllerAsOption = options.controllerAs, 119 preConfirm = options.preConfirm; 120 121 delete options.html; 122 delete options.htmlTemplate; 123 delete options.resolve; 124 delete options.scope; 125 delete options.controller; 126 delete options.controllerAs; 127 delete options.preConfirm; 128 129 var locals = setup.locals; 130 131 options.html = setup.html; 132 133 scope = scope ? scope.$new() : $rootScope.$new(); 134 135 if (controller) { 136 var controllerAs; 137 138 if (controllerAsOption && angular.isString(controllerAsOption)) { 139 controllerAs = controllerAsOption; 140 } 141 142 var controllerInstance = $controller( 143 controller, 144 angular.extend(locals, { 145 $scope: scope 146 }), 147 false, 148 controllerAs 149 ); 150 151 if (typeof preConfirm === 'string') { 152 options.preConfirm = function() { 153 return $timeout(controllerInstance[preConfirm].bind(controllerInstance)); 154 }; 155 } 156 } 157 158 if (typeof preConfirm === 'function') { 159 options.preConfirm = function() { 160 return $timeout(preConfirm); 161 }; 162 } 163 164 var prom = $q.when(swal(options)); 165 var html = document.getElementsByClassName('swal2-modal')[0]; 166 167 // If there is a form, trigger the button on press of enter button 168 var form = html.getElementsByTagName('form'); 169 if (form[0]) { 170 form[0].onkeyup = function(event) { 171 event.stopPropagation(); 172 if (event.keyCode === 13) { 173 html.querySelector('.swal2-confirm').click(); 174 } 175 }; 176 } 177 178 $compile(html)(scope); 179 180 return prom; 181 }); 182 } 183 184 function _getHtml(options) { 185 if (options.htmlTemplate) { 186 return _getTemplate(options.htmlTemplate); 187 } else { 188 return options.html; 189 } 190 } 191 192 function _getTemplate(tmpl) { 193 return $http.get(tmpl, { cache: $templateCache }).then(function(res) { 194 return res.data || ''; 195 }); 196 } 197 198 /* 199 * 200 * Base Sweet Alert function 201 * Use this for for a direct mapping to sweetalert, 202 * by either passing an options object or by passing up to 3 string arguments 203 * 204 * ex. 1: Swangular.swal("Good job!", "You clicked the button!", "success") 205 * 206 * ex. 2: Swangular.swal({title: "Auto close alert!", text: "I will close in 2 seconds.", timer: 2000}); 207 * 208 */ 209 function swal_base(arg1, arg2, arg3) { 210 // In case of options 211 if (arg1 !== null && typeof arg1 === 'object') { 212 return $q.when(swal(arg1)); 213 } else { 214 // In case of: title, text, type 215 return $q.when(swal(arg1, arg2, arg3)); 216 } 217 } 218 219 /* 220 * SweetAlert2 Global Methods 221 */ 222 223 function close() { 224 $rootScope.$evalAsync(function() { 225 swal.close(); 226 }); 227 } 228 229 function disableButtons() { 230 $rootScope.$evalAsync(function() { 231 swal.disableButtons(); 232 }); 233 } 234 235 function enableButtons() { 236 $rootScope.$evalAsync(function() { 237 swal.enableButtons(); 238 }); 239 } 240 241 function disableConfirmButton() { 242 $rootScope.$evalAsync(function() { 243 swal.disableConfirmButton(); 244 }); 245 } 246 247 function enableConfirmButton() { 248 $rootScope.$evalAsync(function() { 249 swal.enableConfirmButton(); 250 }); 251 } 252 253 function clickConfirm() { 254 $rootScope.$evalAsync(function() { 255 swal.clickConfirm(); 256 }); 257 } 258 259 function showLoading() { 260 $rootScope.$evalAsync(function() { 261 swal.showLoading(); 262 }); 263 } 264 265 function clickCancel() { 266 $rootScope.$evalAsync(function() { 267 swal.clickCancel(); 268 }); 269 } 270 271 function hideLoading() { 272 $rootScope.$evalAsync(function() { 273 swal.hideLoading(); 274 }); 275 } 276 277 function setDefaults(options) { 278 $rootScope.$evalAsync(function() { 279 swal.setDefaults(options); 280 }); 281 } 282 283 return { 284 alert: swal_alert, 285 confirm: swal_confirm, 286 info: swal_info, 287 success: swal_success, 288 open: swal_open, 289 swal: swal_base, 290 closeModal: close, 291 close: close, 292 disableButtons: disableButtons, 293 enableButtons: enableButtons, 294 disableConfirmButton: disableConfirmButton, 295 enableConfirmButton: enableConfirmButton, 296 clickConfirm: clickConfirm, 297 clickCancel: clickCancel, 298 showLoading: showLoading, 299 enableLoading: showLoading, 300 hideLoading: hideLoading, 301 disableLoading: hideLoading, 302 setDefaults: setDefaults 303 }; 304 } 305 }); 306})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.