bootstrap-dialog.js 20 KB

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