bootstrap-dialog.js 27 KB

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