bootstrap-dialog.js 24 KB

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