bootstrap-dialog.js 27 KB

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