bootstrap-dialog.js 20 KB

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