bootstrap-dialog.js 29 KB

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