bootstrap-dialog.js 26 KB

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