1/** 2 * bootbox.js v3.2.0 3 * 4 * http://bootboxjs.com/license.txt 5 */ 6var bootbox = window.bootbox || (function(document, $) { 7 /*jshint scripturl:true sub:true */ 8 9 var _locale = 'en', 10 _defaultLocale = 'en', 11 _animate = true, 12 _backdrop = 'static', 13 _defaultHref = 'javascript:;', 14 _classes = '', 15 _btnClasses = {}, 16 _icons = {}, 17 /* last var should always be the public object we'll return */ 18 that = {}; 19 20 21 /** 22 * public API 23 */ 24 that.setLocale = function(locale) { 25 for (var i in _locales) { 26 if (i == locale) { 27 _locale = locale; 28 return; 29 } 30 } 31 throw new Error('Invalid locale: '+locale); 32 }; 33 34 that.addLocale = function(locale, translations) { 35 if (typeof _locales[locale] === 'undefined') { 36 _locales[locale] = {}; 37 } 38 for (var str in translations) { 39 _locales[locale][str] = translations[str]; 40 } 41 }; 42 43 that.setIcons = function(icons) { 44 _icons = icons; 45 if (typeof _icons !== 'object' || _icons === null) { 46 _icons = {}; 47 } 48 }; 49 50 that.setBtnClasses = function(btnClasses) { 51 _btnClasses = btnClasses; 52 if (typeof _btnClasses !== 'object' || _btnClasses === null) { 53 _btnClasses = {}; 54 } 55 }; 56 57 that.alert = function(/*str, label, cb*/) { 58 var str = "", 59 label = _translate('OK'), 60 cb = null; 61 62 switch (arguments.length) { 63 case 1: 64 // no callback, default button label 65 str = arguments[0]; 66 break; 67 case 2: 68 // callback *or* custom button label dependent on type 69 str = arguments[0]; 70 if (typeof arguments[1] == 'function') { 71 cb = arguments[1]; 72 } else { 73 label = arguments[1]; 74 } 75 break; 76 case 3: 77 // callback and custom button label 78 str = arguments[0]; 79 label = arguments[1]; 80 cb = arguments[2]; 81 break; 82 default: 83 throw new Error("Incorrect number of arguments: expected 1-3"); 84 } 85 86 return that.dialog(str, { 87 // only button (ok) 88 "label" : label, 89 "icon" : _icons.OK, 90 "class" : _btnClasses.OK, 91 "callback": cb 92 }, { 93 // ensure that the escape key works; either invoking the user's 94 // callback or true to just close the dialog 95 "onEscape": cb || true 96 }); 97 }; 98 99 that.confirm = function(/*str, labelCancel, labelOk, cb*/) { 100 var str = "", 101 labelCancel = _translate('CANCEL'), 102 labelOk = _translate('CONFIRM'), 103 cb = null; 104 105 switch (arguments.length) { 106 case 1: 107 str = arguments[0]; 108 break; 109 case 2: 110 str = arguments[0]; 111 if (typeof arguments[1] == 'function') { 112 cb = arguments[1]; 113 } else { 114 labelCancel = arguments[1]; 115 } 116 break; 117 case 3: 118 str = arguments[0]; 119 labelCancel = arguments[1]; 120 if (typeof arguments[2] == 'function') { 121 cb = arguments[2]; 122 } else { 123 labelOk = arguments[2]; 124 } 125 break; 126 case 4: 127 str = arguments[0]; 128 labelCancel = arguments[1]; 129 labelOk = arguments[2]; 130 cb = arguments[3]; 131 break; 132 default: 133 throw new Error("Incorrect number of arguments: expected 1-4"); 134 } 135 136 var cancelCallback = function() { 137 if (typeof cb === 'function') { 138 return cb(false); 139 } 140 }; 141 142 var confirmCallback = function() { 143 if (typeof cb === 'function') { 144 return cb(true); 145 } 146 }; 147 148 return that.dialog(str, [{ 149 // first button (cancel) 150 "label" : labelCancel, 151 "icon" : _icons.CANCEL, 152 "class" : _btnClasses.CANCEL, 153 "callback": cancelCallback 154 }, { 155 // second button (confirm) 156 "label" : labelOk, 157 "icon" : _icons.CONFIRM, 158 "class" : _btnClasses.CONFIRM, 159 "callback": confirmCallback 160 }], { 161 // escape key bindings 162 "onEscape": cancelCallback 163 }); 164 }; 165 166 that.prompt = function(/*str, labelCancel, labelOk, cb, defaultVal*/) { 167 var str = "", 168 labelCancel = _translate('CANCEL'),
169 labelOk = _translate('CONFIRM'), 170 cb = null, 171 defaultVal = ""; 172 173 switch (arguments.length) { 174 case 1: 175 str = arguments[0]; 176 break; 177 case 2: 178 str = arguments[0]; 179 if (typeof arguments[1] == 'function') { 180 cb = arguments[1]; 181 } else { 182 labelCancel = arguments[1]; 183 } 184 break; 185 case 3: 186 str = arguments[0]; 187 labelCancel = arguments[1]; 188 if (typeof arguments[2] == 'function') { 189 cb = arguments[2]; 190 } else { 191 labelOk = arguments[2]; 192 } 193 break; 194 case 4: 195 str = arguments[0]; 196 labelCancel = arguments[1]; 197 labelOk = arguments[2]; 198 cb = arguments[3]; 199 break; 200 case 5: 201 str = arguments[0]; 202 labelCancel = arguments[1]; 203 labelOk = arguments[2]; 204 cb = arguments[3]; 205 defaultVal = arguments[4]; 206 break; 207 default: 208 throw new Error("Incorrect number of arguments: expected 1-5"); 209 } 210 211 var header = str; 212 213 // let's keep a reference to the form object for later 214 var form = $("<form></form>"); 215 form.append("<input autocomplete=off type=text value='" + defaultVal + "' />"); 216 217 var cancelCallback = function() { 218 if (typeof cb === 'function') { 219 // yep, native prompts dismiss with null, whereas native 220 // confirms dismiss with false... 221 return cb(null); 222 } 223 }; 224 225 var confirmCallback = function() { 226 if (typeof cb === 'function') { 227 return cb(form.find("input[type=text]").val()); 228 } 229 }; 230 231 var div = that.dialog(form, [{ 232 // first button (cancel) 233 "label" : labelCancel, 234 "icon" : _icons.CANCEL, 235 "class" : _btnClasses.CANCEL, 236 "callback": cancelCallback 237 }, { 238 // second button (confirm) 239 "label" : labelOk, 240 "icon" : _icons.CONFIRM, 241 "class" : _btnClasses.CONFIRM, 242 "callback": confirmCallback 243 }], { 244 // prompts need a few extra options 245 "header" : header, 246 // explicitly tell dialog NOT to show the dialog... 247 "show" : false, 248 "onEscape": cancelCallback 249 }); 250 251 // ... the reason the prompt needs to be hidden is because we need 252 // to bind our own "shown" handler, after creating the modal but 253 // before any show(n) events are triggered 254 // @see https://github.com/makeusabrew/bootbox/issues/69 255 256 div.on("shown", function() { 257 form.find("input[type=text]").focus(); 258 259 // ensure that submitting the form (e.g. with the enter key) 260 // replicates the behaviour of a normal prompt() 261 form.on("submit", function(e) { 262 e.preventDefault(); 263 div.find(".btn-primary").click(); 264 }); 265 }); 266 267 div.modal("show"); 268 269 return div; 270 }; 271 272 that.dialog = function(str, handlers, options) { 273 var buttons = "", 274 callbacks = []; 275 276 if (!options) { 277 options = {}; 278 } 279 280 // check for single object and convert to array if necessary 281 if (typeof handlers === 'undefined') { 282 handlers = []; 283 } else if (typeof handlers.length == 'undefined') { 284 handlers = [handlers]; 285 } 286 287 var i = handlers.length; 288 while (i--) { 289 var label = null, 290 href = null, 291 _class = null, 292 icon = '', 293 callback = null; 294 295 if (typeof handlers[i]['label'] == 'undefined' && 296 typeof handlers[i]['class'] == 'undefined' && 297 typeof handlers[i]['callback'] == 'undefined') {
298 // if we've got nothing we expect, check for condensed format 299 300 var propCount = 0, // condensed will only match if this == 1 301 property = null; // save the last property we found 302 303 // be nicer to count the properties without this, but don't think it's possible... 304 for (var j in handlers[i]) { 305 property = j; 306 if (++propCount > 1) { 307 // forget it, too many properties 308 break; 309 } 310 } 311 312 if (propCount == 1 && typeof handlers[i][j] == 'function') { 313 // matches condensed format of label -> function 314 handlers[i]['label'] = property; 315 handlers[i]['callback'] = handlers[i][j]; 316 } 317 } 318 319 if (typeof handlers[i]['callback']== 'function') { 320 callback = handlers[i]['callback']; 321 } 322 323 if (handlers[i]['class']) { 324 _class = handlers[i]['class']; 325 } else if (i == handlers.length -1 && handlers.length <= 2) { 326 // always add a primary to the main option in a two-button dialog 327 _class = 'btn-primary'; 328 } 329 330 if (handlers[i]['label']) { 331 label = handlers[i]['label']; 332 } else { 333 label = "Option "+(i+1); 334 } 335 336 if (handlers[i]['icon']) { 337 icon = "<i class='"+handlers[i]['icon']+"'></i> "; 338 } 339 340 if (handlers[i]['href']) { 341 href = handlers[i]['href']; 342 } 343 else { 344 href = _defaultHref; 345 } 346 347 buttons = "<a data-handler='"+i+"' class='btn "+_class+"' href='" + href + "'>"+icon+""+label+"</a>" + buttons; 348 349 callbacks[i] = callback; 350 } 351 352 // @see https://github.com/makeusabrew/bootbox/issues/46#issuecomment-8235302 353 // and https://github.com/twitter/bootstrap/issues/4474 354 // for an explanation of the inline overflow: hidden 355 // @see https://github.com/twitter/bootstrap/issues/4854 356 // for an explanation of tabIndex=-1 357 358 var parts = ["<div class='bootbox modal' tabindex='-1' style='overflow:hidden;'>"]; 359 360 if (options['header']) { 361 var closeButton = ''; 362 if (typeof options['headerCloseButton'] == 'undefined' || options['headerCloseButton']) { 363 closeButton = "<a href='"+_defaultHref+"' class='close'>×</a>"; 364 } 365 366 parts.push("<div class='modal-header'>"+closeButton+"<h3>"+options['header']+"</h3></div>"); 367 } 368 369 // push an empty body into which we'll inject the proper content later 370 parts.push("<div class='modal-body'></div>"); 371 372 if (buttons) { 373 parts.push("<div class='modal-footer'>"+buttons+"</div>"); 374 } 375 376 parts.push("</div>"); 377 378 var div = $(parts.join("\n")); 379 380 // check whether we should fade in/out 381 var shouldFade = (typeof options.animate === 'undefined') ? _animate : options.animate; 382 383 if (shouldFade) { 384 div.addClass("fade"); 385 } 386 387 var optionalClasses = (typeof options.classes === 'undefined') ? _classes : options.classes; 388 if (optionalClasses) { 389 div.addClass(optionalClasses); 390 } 391 392 // now we've built up the div properly we can inject the content whether it was a string or a jQuery object 393 div.find(".modal-body").html(str); 394 395 function onCancel(source) { 396 // for now source is unused, but it will be in future 397 var hideModal = null; 398 if (typeof options.onEscape === 'function') { 399 // @see https://github.com/makeusabrew/bootbox/issues/91 400 hideModal = options.onEscape(); 401 } 402 403 if (hideModal !== false) { 404 div.modal('hide'); 405 } 406 } 407 408 // hook into the modal's keyup trigger to check for the escape key 409 div.on('keyup.dismiss.modal', function(e) { 410 // any truthy value passed to onEscape will dismiss the dialog 411 // as long as the onEscape function (if defined) doesn't prevent it 412 if (e.which === 27 && options.onEscape) { 413 onCancel('escape'); 414 } 415 }); 416 417 // handle close buttons too 418 div.on('click', 'a.close', function(e) { 419 e.preventDefault(); 420 onCancel('close'); 421 }); 422 423 // well, *if* we have a primary - give the first dom element focus 424 div.on('shown', function() { 425 div.find("a.btn-primary:first").focus(); 426 }); 427 428 div.on('hidden', function() { 429 div.remove(); 430 }); 431 432 // wire up button handlers 433 div.on('click', '.modal-footer a', function(e) { 434 435 var handler = $(this).data("handler"), 436 cb = callbacks[handler], 437 hideModal = null; 438 439 // sort of @see https://github.com/makeusabrew/bootbox/pull/68 - heavily adapted 440 // if we've got a custom href attribute, all bets are off 441 if (typeof handler !== 'undefined' && 442 typeof handlers[handler]['href'] !== 'undefined') { 443 444 return; 445 } 446 447 e.preventDefault(); 448 449 if (typeof cb === 'function') { 450 hideModal = cb(); 451 } 452 453 // the only way hideModal *will* be false is if a callback exists and 454 // returns it as a value. in those situations, don't hide the dialog 455 // @see https://github.com/makeusabrew/bootbox/pull/25 456 if (hideModal !== false) { 457 div.modal("hide"); 458 } 459 }); 460 461 // stick the modal right at the bottom of the main body out of the way 462 $("body").append(div); 463 464 div.modal({ 465 // unless explicitly overridden take whatever our default backdrop value is 466 backdrop : (typeof options.backdrop === 'undefined') ? _backdrop : options.backdrop, 467 // ignore bootstrap's keyboard options; we'll handle this ourselves (more fine-grained control) 468 keyboard : false, 469 // @ see https://github.com/makeusabrew/bootbox/issues/69 470 // we *never* want the modal to be shown before we can bind stuff to it 471 // this method can also take a 'show' option, but we'll only use that 472 // later if we need to 473 show : false 474 }); 475 476 // @see https://github.com/makeusabrew/bootbox/issues/64 477 // @see https://github.com/makeusabrew/bootbox/issues/60 478 // ...caused by... 479 // @see https://github.com/twitter/bootstrap/issues/4781 480 div.on("show", function(e) { 481 $(document).off("focusin.modal"); 482 }); 483 484 if (typeof options.show === 'undefined' || options.show === true) { 485 div.modal("show"); 486 } 487 488 return div; 489 }; 490 491 /**
492 * #modal is deprecated in v3; it can still be used but no guarantees are 493 * made - have never been truly convinced of its merit but perhaps just 494 * needs a tidyup and some TLC 495 */ 496 that.modal = function(/*str, label, options*/) { 497 var str; 498 var label; 499 var options; 500 501 var defaultOptions = { 502 "onEscape": null, 503 "keyboard": true, 504 "backdrop": _backdrop 505 }; 506 507 switch (arguments.length) { 508 case 1: 509 str = arguments[0]; 510 break; 511 case 2: 512 str = arguments[0]; 513 if (typeof arguments[1] == 'object') { 514 options = arguments[1]; 515 } else { 516 label = arguments[1]; 517 } 518 break; 519 case 3: 520 str = arguments[0]; 521 label = arguments[1]; 522 options = arguments[2]; 523 break; 524 default: 525 throw new Error("Incorrect number of arguments: expected 1-3"); 526 } 527 528 defaultOptions['header'] = label; 529 530 if (typeof options == 'object') { 531 options = $.extend(defaultOptions, options); 532 } else { 533 options = defaultOptions; 534 } 535 536 return that.dialog(str, [], options); 537 }; 538 539 540 that.hideAll = function() { 541 $(".bootbox").modal("hide"); 542 }; 543 544 that.animate = function(animate) { 545 _animate = animate; 546 }; 547 548 that.backdrop = function(backdrop) { 549 _backdrop = backdrop; 550 }; 551 552 that.classes = function(classes) { 553 _classes = classes; 554 }; 555 556 /** 557 * private API 558 */ 559 560 /** 561 * standard locales. Please add more according to ISO 639-1 standard. Multiple language variants are 562 * unlikely to be required. If this gets too large it can be split out into separate JS files. 563 */ 564 var _locales = { 565 'en' : { 566 OK : 'OK', 567 CANCEL : 'Cancel', 568 CONFIRM : 'OK' 569 }, 570 'fr' : { 571 OK : 'OK', 572 CANCEL : 'Annuler', 573 CONFIRM : 'D\'accord' 574 }, 575 'de' : { 576 OK : 'OK', 577 CANCEL : 'Abbrechen', 578 CONFIRM : 'Akzeptieren' 579 }, 580 'es' : { 581 OK : 'OK', 582 CANCEL : 'Cancelar', 583 CONFIRM : 'Aceptar' 584 }, 585 'br' : { 586 OK : 'OK', 587 CANCEL : 'Cancelar', 588 CONFIRM : 'Sim' 589 }, 590 'nl' : { 591 OK : 'OK', 592 CANCEL : 'Annuleren', 593 CONFIRM : 'Accepteren' 594 }, 595 'ru' : { 596 OK : 'OK', 597 CANCEL : 'ÐÑмена', 598 CONFIRM : 'ÐÑимениÑÑ' 599 }, 600 'it' : { 601 OK : 'OK', 602 CANCEL : 'Annulla', 603 CONFIRM : 'Conferma' 604 } 605 }; 606 607 function _translate(str, locale) { 608 // we assume if no target locale is probided then we should take it from current setting 609 if (typeof locale === 'undefined') { 610 locale = _locale; 611 } 612 if (typeof _locales[locale][str] === 'string') { 613 return _locales[locale][str]; 614 } 615 616 // if we couldn't find a lookup then try and fallback to a default translation 617 618 if (locale != _defaultLocale) { 619 return _translate(str, _defaultLocale); 620 } 621 622 // if we can't do anything then bail out with whatever string was passed in - last resort 623 return str; 624 } 625 626 return that; 627 628}(document, window.jQuery)); 629 630// @see https://github.com/makeusabrew/bootbox/issues/71 631window.bootbox = bootbox;
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.