bootstrap-dialog.js 28 KB

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