bootstrap-dialog.js 23 KB

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