bootstrap-dialog.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /* ================================================
  2. * Make use of Twitter Bootstrap's modal more monkey-friendly.
  3. *
  4. * For Bootstrap 3.
  5. *
  6. * javanoob@hotmail.com
  7. *
  8. * https://github.com/nakupanda/bootstrap3-dialog
  9. *
  10. * Licensed under The MIT License.
  11. * ================================================ */
  12. var BootstrapDialog = null;
  13. !function($) {
  14. "use strict";
  15. BootstrapDialog = function(options) {
  16. this.defaultOptions = {
  17. id: BootstrapDialog.newGuid(),
  18. type: BootstrapDialog.TYPE_PRIMARY,
  19. size: BootstrapDialog.SIZE_NORMAL,
  20. cssClass: '',
  21. title: null,
  22. message: null,
  23. buttons: [],
  24. closable: true,
  25. spinicon: BootstrapDialog.ICON_SPINNER,
  26. data: {},
  27. onshow: null,
  28. onhide: null,
  29. autodestroy: true,
  30. draggable: false
  31. };
  32. this.indexedButtons = {};
  33. this.registeredButtonHotkeys = {};
  34. this.draggableData = {
  35. isMouseDown: false,
  36. mouseOffset: {}
  37. };
  38. this.realized = false;
  39. this.opened = false;
  40. this.initOptions(options);
  41. this.holdThisInstance();
  42. };
  43. /**
  44. * Some constants.
  45. */
  46. BootstrapDialog.NAMESPACE = 'bootstrap-dialog';
  47. BootstrapDialog.TYPE_DEFAULT = 'type-default';
  48. BootstrapDialog.TYPE_INFO = 'type-info';
  49. BootstrapDialog.TYPE_PRIMARY = 'type-primary';
  50. BootstrapDialog.TYPE_SUCCESS = 'type-success';
  51. BootstrapDialog.TYPE_WARNING = 'type-warning';
  52. BootstrapDialog.TYPE_DANGER = 'type-danger';
  53. BootstrapDialog.DEFAULT_TEXTS = {};
  54. BootstrapDialog.DEFAULT_TEXTS[BootstrapDialog.TYPE_DEFAULT] = 'Information';
  55. BootstrapDialog.DEFAULT_TEXTS[BootstrapDialog.TYPE_INFO] = 'Information';
  56. BootstrapDialog.DEFAULT_TEXTS[BootstrapDialog.TYPE_PRIMARY] = 'Information';
  57. BootstrapDialog.DEFAULT_TEXTS[BootstrapDialog.TYPE_SUCCESS] = 'Success';
  58. BootstrapDialog.DEFAULT_TEXTS[BootstrapDialog.TYPE_WARNING] = 'Warning';
  59. BootstrapDialog.DEFAULT_TEXTS[BootstrapDialog.TYPE_DANGER] = 'Danger';
  60. BootstrapDialog.SIZE_NORMAL = 'size-normal';
  61. BootstrapDialog.SIZE_LARGE = 'size-large';
  62. BootstrapDialog.BUTTON_SIZES = {};
  63. BootstrapDialog.BUTTON_SIZES[BootstrapDialog.SIZE_NORMAL] = '';
  64. BootstrapDialog.BUTTON_SIZES[BootstrapDialog.SIZE_LARGE] = 'btn-lg';
  65. BootstrapDialog.ICON_SPINNER = 'glyphicon glyphicon-asterisk';
  66. /**
  67. * Open / Close all created dialogs all at once.
  68. */
  69. BootstrapDialog.dialogs = {};
  70. BootstrapDialog.openAll = function() {
  71. $.each(BootstrapDialog.dialogs, function(id, dialogInstance) {
  72. dialogInstance.open();
  73. });
  74. };
  75. BootstrapDialog.closeAll = function() {
  76. $.each(BootstrapDialog.dialogs, function(id, dialogInstance) {
  77. dialogInstance.close();
  78. });
  79. };
  80. BootstrapDialog.prototype = {
  81. constructor: BootstrapDialog,
  82. initOptions: function(options) {
  83. this.options = $.extend(true, this.defaultOptions, options);
  84. return this;
  85. },
  86. holdThisInstance: function() {
  87. BootstrapDialog.dialogs[this.getId()] = this;
  88. return this;
  89. },
  90. initModalStuff: function() {
  91. this.setModal(this.createModal())
  92. .setModalDialog(this.createModalDialog())
  93. .setModalContent(this.createModalContent())
  94. .setModalHeader(this.createModalHeader())
  95. .setModalBody(this.createModalBody())
  96. .setModalFooter(this.createModalFooter());
  97. this.getModal().append(this.getModalDialog());
  98. this.getModalDialog().append(this.getModalContent());
  99. this.getModalContent()
  100. .append(this.getModalHeader())
  101. .append(this.getModalBody())
  102. .append(this.getModalFooter());
  103. return this;
  104. },
  105. createModal: function() {
  106. var $modal = $('<div class="modal fade" tabindex="-1"></div>');
  107. $modal.prop('id', this.getId());
  108. return $modal;
  109. },
  110. getModal: function() {
  111. return this.$modal;
  112. },
  113. setModal: function($modal) {
  114. this.$modal = $modal;
  115. return this;
  116. },
  117. createModalDialog: function() {
  118. return $('<div class="modal-dialog"></div>');
  119. },
  120. getModalDialog: function() {
  121. return this.$modalDialog;
  122. },
  123. setModalDialog: function($modalDialog) {
  124. this.$modalDialog = $modalDialog;
  125. return this;
  126. },
  127. createModalContent: function() {
  128. return $('<div class="modal-content"></div>');
  129. },
  130. getModalContent: function() {
  131. return this.$modalContent;
  132. },
  133. setModalContent: function($modalContent) {
  134. this.$modalContent = $modalContent;
  135. return this;
  136. },
  137. createModalHeader: function() {
  138. return $('<div class="modal-header"></div>');
  139. },
  140. getModalHeader: function() {
  141. return this.$modalHeader;
  142. },
  143. setModalHeader: function($modalHeader) {
  144. this.$modalHeader = $modalHeader;
  145. return this;
  146. },
  147. createModalBody: function() {
  148. return $('<div class="modal-body"></div>');
  149. },
  150. getModalBody: function() {
  151. return this.$modalBody;
  152. },
  153. setModalBody: function($modalBody) {
  154. this.$modalBody = $modalBody;
  155. return this;
  156. },
  157. createModalFooter: function() {
  158. return $('<div class="modal-footer"></div>');
  159. },
  160. getModalFooter: function() {
  161. return this.$modalFooter;
  162. },
  163. setModalFooter: function($modalFooter) {
  164. this.$modalFooter = $modalFooter;
  165. return this;
  166. },
  167. createDynamicContent: function(rawContent) {
  168. var content = null;
  169. if (typeof rawContent === 'function') {
  170. content = rawContent.call(rawContent, this);
  171. } else {
  172. content = rawContent;
  173. }
  174. if (typeof content === 'string') {
  175. content = this.formatStringContent(content);
  176. }
  177. return content;
  178. },
  179. formatStringContent: function(content) {
  180. return content.replace(/\r\n/g, '<br />').replace(/[\r\n]/g, '<br />');
  181. },
  182. setData: function(key, value) {
  183. this.options.data[key] = value;
  184. return this;
  185. },
  186. getData: function(key) {
  187. return this.options.data[key];
  188. },
  189. setId: function(id) {
  190. this.options.id = id;
  191. return this;
  192. },
  193. getId: function() {
  194. return this.options.id;
  195. },
  196. getType: function() {
  197. return this.options.type;
  198. },
  199. setType: function(type) {
  200. this.options.type = type;
  201. return this;
  202. },
  203. getSize: function() {
  204. return this.options.size;
  205. },
  206. setSize: function(size) {
  207. this.options.size = size;
  208. return this;
  209. },
  210. getCssClass: function() {
  211. return this.options.cssClass;
  212. },
  213. setCssClass: function(cssClass) {
  214. this.options.cssClass = cssClass;
  215. return this;
  216. },
  217. getTitle: function() {
  218. return this.options.title;
  219. },
  220. setTitle: function(title) {
  221. this.options.title = title;
  222. this.updateTitle();
  223. return this;
  224. },
  225. updateTitle: function() {
  226. if (this.isRealized()) {
  227. var title = this.getTitle() !== null ? this.createDynamicContent(this.getTitle()) : this.getDefaultText();
  228. this.getModalHeader().find('.' + this.getNamespace('title')).html('').append(title);
  229. }
  230. return this;
  231. },
  232. getMessage: function() {
  233. return this.options.message;
  234. },
  235. setMessage: function(message) {
  236. this.options.message = message;
  237. this.updateMessage();
  238. return this;
  239. },
  240. updateMessage: function() {
  241. if (this.isRealized()) {
  242. var message = this.createDynamicContent(this.getMessage());
  243. this.getModalBody().find('.' + this.getNamespace('message')).html('').append(message);
  244. }
  245. return this;
  246. },
  247. isClosable: function() {
  248. return this.options.closable;
  249. },
  250. setClosable: function(closable) {
  251. this.options.closable = closable;
  252. this.updateClosable();
  253. return this;
  254. },
  255. getSpinicon: function() {
  256. return this.options.spinicon;
  257. },
  258. setSpinicon: function(spinicon) {
  259. this.options.spinicon = spinicon;
  260. return this;
  261. },
  262. addButton: function(button) {
  263. this.options.buttons.push(button);
  264. return this;
  265. },
  266. addButtons: function(buttons) {
  267. var that = this;
  268. $.each(buttons, function(index, button) {
  269. that.addButton(button);
  270. });
  271. return this;
  272. },
  273. getButtons: function() {
  274. return this.options.buttons;
  275. },
  276. setButtons: function(buttons) {
  277. this.options.buttons = buttons;
  278. return this;
  279. },
  280. /**
  281. * If there is id provided for a button option, it will be in dialog.indexedButtons list.
  282. *
  283. * In that case you can use dialog.getButton(id) to find the button.
  284. *
  285. * @param {type} id
  286. * @returns {undefined}
  287. */
  288. getButton: function(id) {
  289. if (typeof this.indexedButtons[id] !== 'undefined') {
  290. return this.indexedButtons[id];
  291. }
  292. return null;
  293. },
  294. getButtonSize: function() {
  295. if (typeof BootstrapDialog.BUTTON_SIZES[this.getSize()] !== 'undefined') {
  296. return BootstrapDialog.BUTTON_SIZES[this.getSize()];
  297. }
  298. return '';
  299. },
  300. isAutodestroy: function() {
  301. return this.options.autodestroy;
  302. },
  303. setAutodestroy: function(autodestroy) {
  304. this.options.autodestroy = autodestroy;
  305. },
  306. getDefaultText: function() {
  307. return BootstrapDialog.DEFAULT_TEXTS[this.getType()];
  308. },
  309. getNamespace: function(name) {
  310. return BootstrapDialog.NAMESPACE + '-' + name;
  311. },
  312. createHeaderContent: function() {
  313. var $container = $('<div></div>');
  314. $container.addClass(this.getNamespace('header'));
  315. // title
  316. $container.append(this.createTitleContent());
  317. // Close button
  318. $container.append(this.createCloseButton());
  319. return $container;
  320. },
  321. createTitleContent: function() {
  322. var $title = $('<div></div>');
  323. $title.addClass(this.getNamespace('title'));
  324. return $title;
  325. },
  326. createCloseButton: function() {
  327. var $container = $('<div></div>');
  328. $container.addClass(this.getNamespace('close-button'));
  329. var $icon = $('<button class="close">×</button>');
  330. $container.append($icon);
  331. $container.on('click', {dialog: this}, function(event) {
  332. event.data.dialog.close();
  333. });
  334. return $container;
  335. },
  336. createBodyContent: function() {
  337. var $container = $('<div></div>');
  338. $container.addClass(this.getNamespace('body'));
  339. // Message
  340. $container.append(this.createMessageContent());
  341. return $container;
  342. },
  343. createMessageContent: function() {
  344. var $message = $('<div></div>');
  345. $message.addClass(this.getNamespace('message'));
  346. return $message;
  347. },
  348. createFooterContent: function() {
  349. var $container = $('<div></div>');
  350. $container.addClass(this.getNamespace('footer'));
  351. // Buttons
  352. $container.append(this.createFooterButtons());
  353. return $container;
  354. },
  355. createFooterButtons: function() {
  356. var that = this;
  357. var $container = $('<div></div>');
  358. $container.addClass(this.getNamespace('footer-buttons'));
  359. this.indexedButtons = {};
  360. $.each(this.options.buttons, function(index, button) {
  361. if (!button.id) {
  362. button.id = BootstrapDialog.newGuid();
  363. }
  364. var $button = that.createButton(button);
  365. that.indexedButtons[button.id] = $button;
  366. $container.append($button);
  367. });
  368. return $container;
  369. },
  370. createButton: function(button) {
  371. var $button = $('<button class="btn"></button>');
  372. $button.addClass(this.getButtonSize());
  373. $button.prop('id', button.id);
  374. // Icon
  375. if (typeof button.icon !== undefined && $.trim(button.icon) !== '') {
  376. $button.append(this.createButtonIcon(button.icon));
  377. }
  378. // Label
  379. if (typeof button.label !== undefined) {
  380. $button.append(button.label);
  381. }
  382. // Css class
  383. if (typeof button.cssClass !== undefined && $.trim(button.cssClass) !== '') {
  384. $button.addClass(button.cssClass);
  385. } else {
  386. $button.addClass('btn-default');
  387. }
  388. // Hotkey
  389. if (typeof button.hotkey !== undefined) {
  390. this.registeredButtonHotkeys[button.hotkey] = $button;
  391. }
  392. // Button on click
  393. $button.on('click', {dialog: this, $button: $button, button: button}, function(event) {
  394. var dialog = event.data.dialog;
  395. var $button = event.data.$button;
  396. var button = event.data.button;
  397. if (typeof button.action === 'function') {
  398. button.action.call($button, dialog);
  399. }
  400. if (button.autospin) {
  401. $button.toggleSpin(true);
  402. }
  403. });
  404. // Dynamically add extra functions to $button
  405. this.enhanceButton($button);
  406. return $button;
  407. },
  408. /**
  409. * Dynamically add extra functions to $button
  410. *
  411. * Using '$this' to reference 'this' is just for better readability.
  412. *
  413. * @param {type} $button
  414. * @returns {_L13.BootstrapDialog.prototype}
  415. */
  416. enhanceButton: function($button) {
  417. $button.dialog = this;
  418. // Enable / Disable
  419. $button.toggleEnable = function(enable) {
  420. var $this = this;
  421. $this.prop("disabled", !enable).toggleClass('disabled', !enable);
  422. return $this;
  423. };
  424. $button.enable = function() {
  425. var $this = this;
  426. $this.toggleEnable(true);
  427. return $this;
  428. };
  429. $button.disable = function() {
  430. var $this = this;
  431. $this.toggleEnable(false);
  432. return $this;
  433. };
  434. // Icon spinning, helpful for indicating ajax loading status.
  435. $button.toggleSpin = function(spin) {
  436. var $this = this;
  437. var dialog = $this.dialog;
  438. var $icon = $this.find('.' + dialog.getNamespace('button-icon'));
  439. if (spin) {
  440. $icon.hide();
  441. $button.prepend(dialog.createButtonIcon(dialog.getSpinicon()).addClass('icon-spin'));
  442. } else {
  443. $icon.show();
  444. $button.find('.icon-spin').remove();
  445. }
  446. return $this;
  447. };
  448. $button.spin = function() {
  449. var $this = this;
  450. $this.toggleSpin(true);
  451. return $this;
  452. };
  453. $button.stopSpin = function() {
  454. var $this = this;
  455. $this.toggleSpin(false);
  456. return $this;
  457. };
  458. return this;
  459. },
  460. createButtonIcon: function(icon) {
  461. var $icon = $('<span></span>');
  462. $icon.addClass(this.getNamespace('button-icon')).addClass(icon);
  463. return $icon;
  464. },
  465. /**
  466. * Invoke this only after the dialog is realized.
  467. *
  468. * @param {type} enable
  469. * @returns {undefined}
  470. */
  471. enableButtons: function(enable) {
  472. $.each(this.indexedButtons, function(id, $button) {
  473. $button.toggleEnable(enable);
  474. });
  475. return this;
  476. },
  477. /**
  478. * Invoke this only after the dialog is realized.
  479. *
  480. * @returns {undefined}
  481. */
  482. updateClosable: function() {
  483. if (this.isRealized()) {
  484. // Close button
  485. this.getModalHeader().find('.' + this.getNamespace('close-button')).toggle(this.isClosable());
  486. }
  487. return this;
  488. },
  489. /**
  490. * Set handler for modal event 'show'.
  491. * This is a setter!
  492. *
  493. * @param {type} onopen
  494. * @returns {_L9.BootstrapDialog.prototype}
  495. */
  496. onShow: function(onshow) {
  497. this.options.onshow = onshow;
  498. return this;
  499. },
  500. /**
  501. * Set handler for modal event 'hide'.
  502. * This is a setter!
  503. *
  504. * @param {type} onclose
  505. * @returns {_L9.BootstrapDialog.prototype}
  506. */
  507. onHide: function(onhide) {
  508. this.options.onhide = onhide;
  509. return this;
  510. },
  511. isRealized: function() {
  512. return this.realized;
  513. },
  514. setRealized: function(realized) {
  515. this.realized = realized;
  516. return this;
  517. },
  518. isOpened: function() {
  519. return this.opened;
  520. },
  521. setOpened: function(opened) {
  522. this.opened = opened;
  523. return this;
  524. },
  525. handleModalEvents: function() {
  526. this.getModal().on('show.bs.modal', {dialog: this}, function(event) {
  527. var dialog = event.data.dialog;
  528. typeof dialog.options.onshow === 'function' && dialog.options.onshow(dialog);
  529. dialog.showPageScrollBar(true);
  530. });
  531. this.getModal().on('hide.bs.modal', {dialog: this}, function(event) {
  532. var dialog = event.data.dialog;
  533. typeof dialog.options.onhide === 'function' && dialog.options.onhide(dialog);
  534. });
  535. this.getModal().on('hidden.bs.modal', {dialog: this}, function(event) {
  536. var dialog = event.data.dialog;
  537. dialog.isAutodestroy() && $(this).remove();
  538. dialog.showPageScrollBar(false);
  539. });
  540. // Backdrop, I did't find a way to change bs3 backdrop option after the dialog is popped up, so here's a new wheel.
  541. this.getModal().on('click', {dialog: this}, function(event) {
  542. event.target === this && event.data.dialog.isClosable() && event.data.dialog.close();
  543. });
  544. // ESC key support
  545. this.getModal().on('keyup', {dialog: this}, function(event) {
  546. event.which === 27 && event.data.dialog.isClosable() && event.data.dialog.close();
  547. });
  548. // Button hotkey
  549. this.getModal().on('keyup', {dialog: this}, function(event) {
  550. var dialog = event.data.dialog;
  551. if (typeof dialog.registeredButtonHotkeys[event.which] !== 'undefined') {
  552. var $button = $(dialog.registeredButtonHotkeys[event.which]);
  553. !$button.prop('disabled') && $button.focus().trigger('click');
  554. }
  555. });
  556. return this;
  557. },
  558. makeModalDraggable: function() {
  559. if (this.options.draggable) {
  560. this.getModalHeader().addClass(this.getNamespace('draggable')).on('mousedown', {dialog: this}, function(event) {
  561. var dialog = event.data.dialog;
  562. dialog.draggableData.isMouseDown = true;
  563. var dialogOffset = dialog.getModalContent().offset();
  564. dialog.draggableData.mouseOffset = {
  565. top: event.clientY - dialogOffset.top,
  566. left: event.clientX - dialogOffset.left
  567. };
  568. });
  569. this.getModal().on('mouseup mouseleave', {dialog: this}, function(event) {
  570. event.data.dialog.draggableData.isMouseDown = false;
  571. });
  572. $('body').on('mousemove', {dialog: this}, function(event) {
  573. var dialog = event.data.dialog;
  574. if (!dialog.draggableData.isMouseDown) {
  575. return;
  576. }
  577. dialog.getModalContent().offset({
  578. top: event.clientY - dialog.draggableData.mouseOffset.top,
  579. left: event.clientX - dialog.draggableData.mouseOffset.left
  580. });
  581. });
  582. }
  583. return this;
  584. },
  585. showPageScrollBar: function(show) {
  586. $(document.body).toggleClass('modal-open', show);
  587. },
  588. realize: function() {
  589. this.initModalStuff();
  590. this.getModal().addClass(BootstrapDialog.NAMESPACE)
  591. .addClass(this.getType())
  592. .addClass(this.getSize())
  593. .addClass(this.getCssClass());
  594. this.getModalFooter().append(this.createFooterContent());
  595. this.getModalHeader().append(this.createHeaderContent());
  596. this.getModalBody().append(this.createBodyContent());
  597. this.getModal().modal({
  598. backdrop: 'static',
  599. keyboard: false,
  600. show: false
  601. });
  602. this.makeModalDraggable();
  603. this.handleModalEvents();
  604. this.setRealized(true);
  605. this.updateTitle();
  606. this.updateMessage();
  607. this.updateClosable();
  608. return this;
  609. },
  610. open: function() {
  611. !this.isRealized() && this.realize();
  612. this.getModal().modal('show');
  613. this.setOpened(true);
  614. return this;
  615. },
  616. close: function() {
  617. this.getModal().modal('hide');
  618. if (this.isAutodestroy()) {
  619. delete BootstrapDialog.dialogs[this.getId()];
  620. }
  621. this.setOpened(false);
  622. return this;
  623. }
  624. };
  625. /**
  626. * RFC4122 version 4 compliant unique id creator.
  627. *
  628. * Added by https://github.com/tufanbarisyildirim/
  629. *
  630. * @returns {String}
  631. */
  632. BootstrapDialog.newGuid = function() {
  633. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  634. var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
  635. return v.toString(16);
  636. });
  637. };
  638. /* ================================================
  639. * For lazy people
  640. * ================================================ */
  641. /**
  642. * Shortcut function: show
  643. *
  644. * @param {type} options
  645. * @returns the created dialog instance
  646. */
  647. BootstrapDialog.show = function(options) {
  648. return new BootstrapDialog(options).open();
  649. };
  650. /**
  651. * Alert window
  652. *
  653. * @param {type} message
  654. * @param {type} callback
  655. * @returns the created dialog instance
  656. */
  657. BootstrapDialog.alert = function(message, callback) {
  658. return new BootstrapDialog({
  659. message: message,
  660. data: {
  661. 'callback': callback
  662. },
  663. closable: false,
  664. buttons: [{
  665. label: 'OK',
  666. action: function(dialog) {
  667. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
  668. dialog.close();
  669. }
  670. }]
  671. }).open();
  672. };
  673. /**
  674. * Confirm window
  675. *
  676. * @param {type} message
  677. * @param {type} callback
  678. * @returns the created dialog instance
  679. */
  680. BootstrapDialog.confirm = function(message, callback) {
  681. return new BootstrapDialog({
  682. title: 'Confirmation',
  683. message: message,
  684. closable: false,
  685. data: {
  686. 'callback': callback
  687. },
  688. buttons: [{
  689. label: 'Cancel',
  690. action: function(dialog) {
  691. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(false);
  692. dialog.close();
  693. }
  694. }, {
  695. label: 'OK',
  696. cssClass: 'btn-primary',
  697. action: function(dialog) {
  698. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
  699. dialog.close();
  700. }
  701. }]
  702. }).open();
  703. };
  704. }(window.jQuery);