bootstrap-dialog.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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. this.updateButtons();
  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. updateButtons: function() {
  301. if (this.isRealized()) {
  302. if (this.getButtons().length === 0) {
  303. this.getModalFooter().hide();
  304. } else {
  305. this.getModalFooter().find('.' + this.getNamespace('footer')).html('').append(this.createFooterButtons());
  306. }
  307. }
  308. return this;
  309. },
  310. isAutodestroy: function() {
  311. return this.options.autodestroy;
  312. },
  313. setAutodestroy: function(autodestroy) {
  314. this.options.autodestroy = autodestroy;
  315. },
  316. getDefaultText: function() {
  317. return BootstrapDialog.DEFAULT_TEXTS[this.getType()];
  318. },
  319. getNamespace: function(name) {
  320. return BootstrapDialog.NAMESPACE + '-' + name;
  321. },
  322. createHeaderContent: function() {
  323. var $container = $('<div></div>');
  324. $container.addClass(this.getNamespace('header'));
  325. // title
  326. $container.append(this.createTitleContent());
  327. // Close button
  328. $container.append(this.createCloseButton());
  329. return $container;
  330. },
  331. createTitleContent: function() {
  332. var $title = $('<div></div>');
  333. $title.addClass(this.getNamespace('title'));
  334. return $title;
  335. },
  336. createCloseButton: function() {
  337. var $container = $('<div></div>');
  338. $container.addClass(this.getNamespace('close-button'));
  339. var $icon = $('<button class="close">&times;</button>');
  340. $container.append($icon);
  341. $container.on('click', {dialog: this}, function(event) {
  342. event.data.dialog.close();
  343. });
  344. return $container;
  345. },
  346. createBodyContent: function() {
  347. var $container = $('<div></div>');
  348. $container.addClass(this.getNamespace('body'));
  349. // Message
  350. $container.append(this.createMessageContent());
  351. return $container;
  352. },
  353. createMessageContent: function() {
  354. var $message = $('<div></div>');
  355. $message.addClass(this.getNamespace('message'));
  356. return $message;
  357. },
  358. createFooterContent: function() {
  359. var $container = $('<div></div>');
  360. $container.addClass(this.getNamespace('footer'));
  361. return $container;
  362. },
  363. createFooterButtons: function() {
  364. var that = this;
  365. var $container = $('<div></div>');
  366. $container.addClass(this.getNamespace('footer-buttons'));
  367. this.indexedButtons = {};
  368. $.each(this.options.buttons, function(index, button) {
  369. if (!button.id) {
  370. button.id = BootstrapDialog.newGuid();
  371. }
  372. var $button = that.createButton(button);
  373. that.indexedButtons[button.id] = $button;
  374. $container.append($button);
  375. });
  376. return $container;
  377. },
  378. createButton: function(button) {
  379. var $button = $('<button class="btn"></button>');
  380. $button.addClass(this.getButtonSize());
  381. $button.prop('id', button.id);
  382. // Icon
  383. if (typeof button.icon !== undefined && $.trim(button.icon) !== '') {
  384. $button.append(this.createButtonIcon(button.icon));
  385. }
  386. // Label
  387. if (typeof button.label !== undefined) {
  388. $button.append(button.label);
  389. }
  390. // Css class
  391. if (typeof button.cssClass !== undefined && $.trim(button.cssClass) !== '') {
  392. $button.addClass(button.cssClass);
  393. } else {
  394. $button.addClass('btn-default');
  395. }
  396. // Hotkey
  397. if (typeof button.hotkey !== undefined) {
  398. this.registeredButtonHotkeys[button.hotkey] = $button;
  399. }
  400. // Button on click
  401. $button.on('click', {dialog: this, $button: $button, button: button}, function(event) {
  402. var dialog = event.data.dialog;
  403. var $button = event.data.$button;
  404. var button = event.data.button;
  405. if (typeof button.action === 'function') {
  406. button.action.call($button, dialog);
  407. }
  408. if (button.autospin) {
  409. $button.toggleSpin(true);
  410. }
  411. });
  412. // Dynamically add extra functions to $button
  413. this.enhanceButton($button);
  414. return $button;
  415. },
  416. /**
  417. * Dynamically add extra functions to $button
  418. *
  419. * Using '$this' to reference 'this' is just for better readability.
  420. *
  421. * @param {type} $button
  422. * @returns {_L13.BootstrapDialog.prototype}
  423. */
  424. enhanceButton: function($button) {
  425. $button.dialog = this;
  426. // Enable / Disable
  427. $button.toggleEnable = function(enable) {
  428. var $this = this;
  429. $this.prop("disabled", !enable).toggleClass('disabled', !enable);
  430. return $this;
  431. };
  432. $button.enable = function() {
  433. var $this = this;
  434. $this.toggleEnable(true);
  435. return $this;
  436. };
  437. $button.disable = function() {
  438. var $this = this;
  439. $this.toggleEnable(false);
  440. return $this;
  441. };
  442. // Icon spinning, helpful for indicating ajax loading status.
  443. $button.toggleSpin = function(spin) {
  444. var $this = this;
  445. var dialog = $this.dialog;
  446. var $icon = $this.find('.' + dialog.getNamespace('button-icon'));
  447. if (spin) {
  448. $icon.hide();
  449. $button.prepend(dialog.createButtonIcon(dialog.getSpinicon()).addClass('icon-spin'));
  450. } else {
  451. $icon.show();
  452. $button.find('.icon-spin').remove();
  453. }
  454. return $this;
  455. };
  456. $button.spin = function() {
  457. var $this = this;
  458. $this.toggleSpin(true);
  459. return $this;
  460. };
  461. $button.stopSpin = function() {
  462. var $this = this;
  463. $this.toggleSpin(false);
  464. return $this;
  465. };
  466. return this;
  467. },
  468. createButtonIcon: function(icon) {
  469. var $icon = $('<span></span>');
  470. $icon.addClass(this.getNamespace('button-icon')).addClass(icon);
  471. return $icon;
  472. },
  473. /**
  474. * Invoke this only after the dialog is realized.
  475. *
  476. * @param {type} enable
  477. * @returns {undefined}
  478. */
  479. enableButtons: function(enable) {
  480. $.each(this.indexedButtons, function(id, $button) {
  481. $button.toggleEnable(enable);
  482. });
  483. return this;
  484. },
  485. /**
  486. * Invoke this only after the dialog is realized.
  487. *
  488. * @returns {undefined}
  489. */
  490. updateClosable: function() {
  491. if (this.isRealized()) {
  492. // Close button
  493. this.getModalHeader().find('.' + this.getNamespace('close-button')).toggle(this.isClosable());
  494. }
  495. return this;
  496. },
  497. /**
  498. * Set handler for modal event 'show'.
  499. * This is a setter!
  500. *
  501. * @param {type} onopen
  502. * @returns {_L9.BootstrapDialog.prototype}
  503. */
  504. onShow: function(onshow) {
  505. this.options.onshow = onshow;
  506. return this;
  507. },
  508. /**
  509. * Set handler for modal event 'hide'.
  510. * This is a setter!
  511. *
  512. * @param {type} onclose
  513. * @returns {_L9.BootstrapDialog.prototype}
  514. */
  515. onHide: function(onhide) {
  516. this.options.onhide = onhide;
  517. return this;
  518. },
  519. isRealized: function() {
  520. return this.realized;
  521. },
  522. setRealized: function(realized) {
  523. this.realized = realized;
  524. return this;
  525. },
  526. isOpened: function() {
  527. return this.opened;
  528. },
  529. setOpened: function(opened) {
  530. this.opened = opened;
  531. return this;
  532. },
  533. handleModalEvents: function() {
  534. this.getModal().on('show.bs.modal', {dialog: this}, function(event) {
  535. var dialog = event.data.dialog;
  536. typeof dialog.options.onshow === 'function' && dialog.options.onshow(dialog);
  537. dialog.showPageScrollBar(true);
  538. });
  539. this.getModal().on('hide.bs.modal', {dialog: this}, function(event) {
  540. var dialog = event.data.dialog;
  541. typeof dialog.options.onhide === 'function' && dialog.options.onhide(dialog);
  542. });
  543. this.getModal().on('hidden.bs.modal', {dialog: this}, function(event) {
  544. var dialog = event.data.dialog;
  545. dialog.isAutodestroy() && $(this).remove();
  546. dialog.showPageScrollBar(false);
  547. });
  548. // Backdrop, I did't find a way to change bs3 backdrop option after the dialog is popped up, so here's a new wheel.
  549. this.getModal().on('click', {dialog: this}, function(event) {
  550. event.target === this && event.data.dialog.isClosable() && event.data.dialog.close();
  551. });
  552. // ESC key support
  553. this.getModal().on('keyup', {dialog: this}, function(event) {
  554. event.which === 27 && event.data.dialog.isClosable() && event.data.dialog.close();
  555. });
  556. // Button hotkey
  557. this.getModal().on('keyup', {dialog: this}, function(event) {
  558. var dialog = event.data.dialog;
  559. if (typeof dialog.registeredButtonHotkeys[event.which] !== 'undefined') {
  560. var $button = $(dialog.registeredButtonHotkeys[event.which]);
  561. !$button.prop('disabled') && $button.focus().trigger('click');
  562. }
  563. });
  564. return this;
  565. },
  566. makeModalDraggable: function() {
  567. if (this.options.draggable) {
  568. this.getModalHeader().addClass(this.getNamespace('draggable')).on('mousedown', {dialog: this}, function(event) {
  569. var dialog = event.data.dialog;
  570. dialog.draggableData.isMouseDown = true;
  571. var dialogOffset = dialog.getModalContent().offset();
  572. dialog.draggableData.mouseOffset = {
  573. top: event.clientY - dialogOffset.top,
  574. left: event.clientX - dialogOffset.left
  575. };
  576. });
  577. this.getModal().on('mouseup mouseleave', {dialog: this}, function(event) {
  578. event.data.dialog.draggableData.isMouseDown = false;
  579. });
  580. $('body').on('mousemove', {dialog: this}, function(event) {
  581. var dialog = event.data.dialog;
  582. if (!dialog.draggableData.isMouseDown) {
  583. return;
  584. }
  585. dialog.getModalContent().offset({
  586. top: event.clientY - dialog.draggableData.mouseOffset.top,
  587. left: event.clientX - dialog.draggableData.mouseOffset.left
  588. });
  589. });
  590. }
  591. return this;
  592. },
  593. showPageScrollBar: function(show) {
  594. $(document.body).toggleClass('modal-open', show);
  595. },
  596. realize: function() {
  597. this.initModalStuff();
  598. this.getModal().addClass(BootstrapDialog.NAMESPACE)
  599. .addClass(this.getType())
  600. .addClass(this.getSize())
  601. .addClass(this.getCssClass());
  602. this.getModalFooter().append(this.createFooterContent());
  603. this.getModalHeader().append(this.createHeaderContent());
  604. this.getModalBody().append(this.createBodyContent());
  605. this.getModal().modal({
  606. backdrop: 'static',
  607. keyboard: false,
  608. show: false
  609. });
  610. this.makeModalDraggable();
  611. this.handleModalEvents();
  612. this.setRealized(true);
  613. this.updateButtons();
  614. this.updateTitle();
  615. this.updateMessage();
  616. this.updateClosable();
  617. return this;
  618. },
  619. open: function() {
  620. !this.isRealized() && this.realize();
  621. this.getModal().modal('show');
  622. this.setOpened(true);
  623. return this;
  624. },
  625. close: function() {
  626. this.getModal().modal('hide');
  627. if (this.isAutodestroy()) {
  628. delete BootstrapDialog.dialogs[this.getId()];
  629. }
  630. this.setOpened(false);
  631. return this;
  632. }
  633. };
  634. /**
  635. * RFC4122 version 4 compliant unique id creator.
  636. *
  637. * Added by https://github.com/tufanbarisyildirim/
  638. *
  639. * @returns {String}
  640. */
  641. BootstrapDialog.newGuid = function() {
  642. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  643. var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
  644. return v.toString(16);
  645. });
  646. };
  647. /* ================================================
  648. * For lazy people
  649. * ================================================ */
  650. /**
  651. * Shortcut function: show
  652. *
  653. * @param {type} options
  654. * @returns the created dialog instance
  655. */
  656. BootstrapDialog.show = function(options) {
  657. return new BootstrapDialog(options).open();
  658. };
  659. /**
  660. * Alert window
  661. *
  662. * @returns the created dialog instance
  663. */
  664. BootstrapDialog.alert = function() {
  665. var options = {};
  666. var defaultOptions = {
  667. type: BootstrapDialog.TYPE_PRIMARY,
  668. title: null,
  669. message: null,
  670. closable: true,
  671. buttonLabel: 'OK',
  672. callback: null
  673. };
  674. if (typeof arguments[0] === 'object' && arguments[0].constructor === {}.constructor) {
  675. options = $.extend(true, defaultOptions, arguments[0]);
  676. } else {
  677. options = $.extend(true, defaultOptions, {
  678. message: arguments[0],
  679. closable: false,
  680. buttonLabel: 'OK',
  681. callback: typeof arguments[1] !== 'undefined' ? arguments[1] : null
  682. });
  683. }
  684. return new BootstrapDialog({
  685. type: options.type,
  686. title: options.title,
  687. message: options.message,
  688. closable: options.closable,
  689. data: {
  690. callback: options.callback
  691. },
  692. onhide: function(dialog) {
  693. !dialog.getData('btnClicked') && dialog.isClosable() && typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(false);
  694. },
  695. buttons: [{
  696. label: options.buttonLabel,
  697. action: function(dialog) {
  698. dialog.setData('btnClicked', true);
  699. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
  700. dialog.close();
  701. }
  702. }]
  703. }).open();
  704. };
  705. /**
  706. * Confirm window
  707. *
  708. * @param {type} message
  709. * @param {type} callback
  710. * @returns the created dialog instance
  711. */
  712. BootstrapDialog.confirm = function(message, callback) {
  713. return new BootstrapDialog({
  714. title: 'Confirmation',
  715. message: message,
  716. closable: false,
  717. data: {
  718. 'callback': callback
  719. },
  720. buttons: [{
  721. label: 'Cancel',
  722. action: function(dialog) {
  723. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(false);
  724. dialog.close();
  725. }
  726. }, {
  727. label: 'OK',
  728. cssClass: 'btn-primary',
  729. action: function(dialog) {
  730. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
  731. dialog.close();
  732. }
  733. }]
  734. }).open();
  735. };
  736. BootstrapDialog.init = function() {
  737. // check for nodeJS
  738. var hasModule = (typeof module !== 'undefined' && module.exports);
  739. // CommonJS module is defined
  740. if (hasModule)
  741. module.exports = BootstrapDialog;
  742. else if (typeof define === "function" && define.amd)
  743. define("bootstrap-dialog", function() {
  744. return BootstrapDialog;
  745. });
  746. else
  747. window.BootstrapDialog = BootstrapDialog;
  748. };
  749. BootstrapDialog.init();
  750. })(window.jQuery);