bootstrap-dialog.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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.$modaFooter;
  156. },
  157. setModalFooter: function($modaFooter) {
  158. this.$modaFooter = $modaFooter;
  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. return this;
  217. },
  218. getMessage: function() {
  219. return this.options.message;
  220. },
  221. setMessage: function(message) {
  222. this.options.message = message;
  223. return this;
  224. },
  225. isClosable: function() {
  226. return this.options.closable;
  227. },
  228. setClosable: function(closable) {
  229. this.options.closable = closable;
  230. this.updateClosable();
  231. return this;
  232. },
  233. getSpinicon: function() {
  234. return this.options.spinicon;
  235. },
  236. setSpinicon: function(spinicon) {
  237. this.options.spinicon = spinicon;
  238. return this;
  239. },
  240. addButton: function(button) {
  241. this.options.buttons.push(button);
  242. return this;
  243. },
  244. addButtons: function(buttons) {
  245. var that = this;
  246. $.each(buttons, function(index, button) {
  247. that.addButton(button);
  248. });
  249. return this;
  250. },
  251. getButtons: function() {
  252. return this.options.buttons;
  253. },
  254. setButtons: function(buttons) {
  255. this.options.buttons = buttons;
  256. return this;
  257. },
  258. /**
  259. * If there is id provided for a button option, it will be in dialog.indexedButtons list.
  260. *
  261. * In that case you can use dialog.getButton(id) to find the button.
  262. *
  263. * @param {type} id
  264. * @returns {undefined}
  265. */
  266. getButton: function(id) {
  267. if (typeof this.indexedButtons[id] !== 'undefined') {
  268. return this.indexedButtons[id];
  269. }
  270. return null;
  271. },
  272. getButtonSize: function() {
  273. if (typeof BootstrapDialog.BUTTON_SIZES[this.getSize()] !== 'undefined') {
  274. return BootstrapDialog.BUTTON_SIZES[this.getSize()];
  275. }
  276. return '';
  277. },
  278. isAutodestroy: function() {
  279. return this.options.autodestroy;
  280. },
  281. setAutodestroy: function(autodestroy) {
  282. this.options.autodestroy = autodestroy;
  283. },
  284. getDefaultText: function() {
  285. return BootstrapDialog.DEFAULT_TEXTS[this.getType()];
  286. },
  287. getNamespace: function(name) {
  288. return BootstrapDialog.NAMESPACE + '-' + name;
  289. },
  290. createHeaderContent: function() {
  291. var $container = $('<div></div>');
  292. $container.addClass(this.getNamespace('header'));
  293. // title
  294. $container.append(this.createTitleContent());
  295. // Close button
  296. $container.append(this.createCloseButton());
  297. return $container;
  298. },
  299. createTitleContent: function() {
  300. var $title = $('<div></div>');
  301. $title.addClass(this.getNamespace('title'));
  302. $title.append(this.getTitle() !== null ? this.createDynamicContent(this.getTitle()) : this.getDefaultText());
  303. return $title;
  304. },
  305. createCloseButton: function() {
  306. var $container = $('<div></div>');
  307. $container.addClass(this.getNamespace('close-button'));
  308. var $icon = $('<button class="close">×</button>');
  309. $container.append($icon);
  310. $container.on('click', {dialog: this}, function(event) {
  311. event.data.dialog.close();
  312. });
  313. return $container;
  314. },
  315. createBodyContent: function() {
  316. var $container = $('<div></div>');
  317. $container.addClass(this.getNamespace('body'));
  318. // Message
  319. $container.append(this.createMessageContent());
  320. return $container;
  321. },
  322. createMessageContent: function() {
  323. var $message = $('<div></div>');
  324. $message.addClass(this.getNamespace('message'));
  325. $message.append(this.createDynamicContent(this.getMessage()));
  326. return $message;
  327. },
  328. createFooterContent: function() {
  329. var $container = $('<div></div>');
  330. $container.addClass(this.getNamespace('footer'));
  331. // Buttons
  332. $container.append(this.createFooterButtons());
  333. return $container;
  334. },
  335. createFooterButtons: function() {
  336. var that = this;
  337. var $container = $('<div></div>');
  338. $container.addClass(this.getNamespace('footer-buttons'));
  339. this.indexedButtons = {};
  340. $.each(this.options.buttons, function(index, button) {
  341. var $button = that.createButton(button);
  342. if (typeof button.id !== 'undefined') {
  343. that.indexedButtons[button.id] = $button;
  344. }
  345. $container.append($button);
  346. });
  347. return $container;
  348. },
  349. createButton: function(button) {
  350. var $button = $('<button class="btn"></button>');
  351. $button.addClass(this.getButtonSize());
  352. // Icon
  353. if (typeof button.icon !== undefined && $.trim(button.icon) !== '') {
  354. $button.append(this.createButtonIcon(button.icon));
  355. }
  356. // Label
  357. if (typeof button.label !== undefined) {
  358. $button.append(button.label);
  359. }
  360. // Css class
  361. if (typeof button.cssClass !== undefined && $.trim(button.cssClass) !== '') {
  362. $button.addClass(button.cssClass);
  363. } else {
  364. $button.addClass('btn-default');
  365. }
  366. // Button on click
  367. $button.on('click', {dialog: this, button: button}, function(event) {
  368. var dialog = event.data.dialog;
  369. var button = event.data.button;
  370. if (typeof button.action === 'function') {
  371. button.action.call(this, dialog);
  372. }
  373. if (button.autospin) {
  374. var $button = $(this);
  375. $button.find('.' + dialog.getNamespace('button-icon')).remove();
  376. $button.prepend(dialog.createButtonIcon(dialog.getSpinicon()).addClass('icon-spin'));
  377. }
  378. });
  379. return $button;
  380. },
  381. createButtonIcon: function(icon) {
  382. var $icon = $('<span></span>');
  383. $icon.addClass(this.getNamespace('button-icon')).addClass(icon);
  384. return $icon;
  385. },
  386. /**
  387. * Invoke this only after the dialog is realized.
  388. *
  389. * @param {type} enable
  390. * @returns {undefined}
  391. */
  392. enableButtons: function(enable) {
  393. var $buttons = this.getModalFooter().find('.btn');
  394. $buttons.prop("disabled", !enable).toggleClass('disabled', !enable);
  395. return this;
  396. },
  397. /**
  398. * Invoke this only after the dialog is realized.
  399. *
  400. * @param {type} enable
  401. * @returns {undefined}
  402. */
  403. updateClosable: function() {
  404. if (this.isRealized()) {
  405. // Backdrop, I did't find a way to change bs3 backdrop option after the dialog is popped up, so here's a new wheel.
  406. var $theBigMask = this.getModal();
  407. $theBigMask.off('click').on('click', {dialog: this}, function(event) {
  408. event.target === this && event.data.dialog.isClosable() && event.data.dialog.close();
  409. });
  410. // Close button
  411. this.getModalHeader().find('.' + this.getNamespace('close-button')).toggle(this.isClosable());
  412. // ESC key support
  413. $theBigMask.off('keyup').on('keyup', {dialog: this}, function(event) {
  414. event.which === 27 && event.data.dialog.isClosable() && event.data.dialog.close();
  415. });
  416. }
  417. return this;
  418. },
  419. /**
  420. * Set handler for modal event 'show'.
  421. * This is a setter!
  422. *
  423. * @param {type} onopen
  424. * @returns {_L9.BootstrapDialog.prototype}
  425. */
  426. onShow: function(onshow) {
  427. this.options.onshow = onshow;
  428. return this;
  429. },
  430. /**
  431. * Set handler for modal event 'hide'.
  432. * This is a setter!
  433. *
  434. * @param {type} onclose
  435. * @returns {_L9.BootstrapDialog.prototype}
  436. */
  437. onHide: function(onhide) {
  438. this.options.onhide = onhide;
  439. return this;
  440. },
  441. isRealized: function() {
  442. return this.realized;
  443. },
  444. setRealized: function(realized) {
  445. this.realized = realized;
  446. return this;
  447. },
  448. isOpened: function() {
  449. return this.opened;
  450. },
  451. setOpened: function(opened) {
  452. this.opened = opened;
  453. return this;
  454. },
  455. handleModalEvents: function() {
  456. this.getModal().on('show.bs.modal', {dialog: this}, function(event) {
  457. var dialog = event.data.dialog;
  458. typeof dialog.options.onshow === 'function' && dialog.options.onshow(dialog);
  459. dialog.showPageScrollBar(true);
  460. });
  461. this.getModal().on('hide.bs.modal', {dialog: this}, function(event) {
  462. var dialog = event.data.dialog;
  463. typeof dialog.options.onhide === 'function' && dialog.options.onhide(dialog);
  464. });
  465. this.getModal().on('hidden.bs.modal', {dialog: this}, function(event) {
  466. var dialog = event.data.dialog;
  467. dialog.isAutodestroy() && $(this).remove();
  468. dialog.showPageScrollBar(false);
  469. });
  470. return this;
  471. },
  472. showPageScrollBar: function(show) {
  473. $(document.body).toggleClass('modal-open', show);
  474. },
  475. realize: function() {
  476. this.initModalStuff();
  477. this.getModal().addClass(BootstrapDialog.NAMESPACE)
  478. .addClass(this.getType())
  479. .addClass(this.getSize())
  480. .addClass(this.getCssClass());
  481. this.getModalHeader().append(this.createHeaderContent());
  482. this.getModalBody().append(this.createBodyContent());
  483. this.getModalFooter().append(this.createFooterContent());
  484. this.getModal().modal({
  485. backdrop: 'static',
  486. keyboard: false,
  487. show: false
  488. });
  489. this.handleModalEvents();
  490. this.setRealized(true);
  491. return this;
  492. },
  493. open: function() {
  494. !this.isRealized() && this.realize();
  495. this.updateClosable();
  496. this.getModal().modal('show');
  497. this.setOpened(true);
  498. return this;
  499. },
  500. close: function() {
  501. this.getModal().modal('hide');
  502. if (this.isAutodestroy()) {
  503. delete BootstrapDialog.dialogs[this.getId()];
  504. }
  505. this.setOpened(false);
  506. return this;
  507. }
  508. };
  509. /**
  510. * RFC4122 version 4 compliant unique id creator.
  511. *
  512. * Added by https://github.com/tufanbarisyildirim/
  513. *
  514. * @returns {String}
  515. */
  516. BootstrapDialog.newGuid = function() {
  517. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  518. var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
  519. return v.toString(16);
  520. });
  521. };
  522. /* ================================================
  523. * For lazy people
  524. * ================================================ */
  525. /**
  526. * Shortcut function: show
  527. *
  528. * @param {type} options
  529. * @returns the created dialog instance
  530. */
  531. BootstrapDialog.show = function(options) {
  532. return new BootstrapDialog(options).open();
  533. };
  534. /**
  535. * Alert window
  536. *
  537. * @param {type} message
  538. * @param {type} callback
  539. * @returns the created dialog instance
  540. */
  541. BootstrapDialog.alert = function(message, callback) {
  542. return new BootstrapDialog({
  543. message: message,
  544. data: {
  545. 'callback': callback
  546. },
  547. closable: false,
  548. buttons: [{
  549. label: 'OK',
  550. action: function(dialog) {
  551. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
  552. dialog.close();
  553. }
  554. }]
  555. }).open();
  556. };
  557. /**
  558. * Confirm window
  559. *
  560. * @param {type} message
  561. * @param {type} callback
  562. * @returns the created dialog instance
  563. */
  564. BootstrapDialog.confirm = function(message, callback) {
  565. return new BootstrapDialog({
  566. title: 'Confirmation',
  567. message: message,
  568. closable: false,
  569. data: {
  570. 'callback': callback
  571. },
  572. buttons: [{
  573. label: 'Cancel',
  574. action: function(dialog) {
  575. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(false);
  576. dialog.close();
  577. }
  578. }, {
  579. label: 'OK',
  580. cssClass: 'btn-primary',
  581. action: function(dialog) {
  582. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
  583. dialog.close();
  584. }
  585. }]
  586. }).open();
  587. };
  588. }(window.jQuery);