bootstrap-dialog.js 21 KB

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