bootstrap-dialog.js 26 KB

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