bootstrap-dialog.js 18 KB

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