bootstrap-dialog.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 contentType = typeof rawContent;
  134. if (contentType === 'function') {
  135. return rawContent.call(rawContent, this);
  136. }
  137. return rawContent;
  138. },
  139. setData: function(key, value) {
  140. this.options.data[key] = value;
  141. return this;
  142. },
  143. getData: function(key) {
  144. return this.options.data[key];
  145. },
  146. getType: function() {
  147. return this.options.type;
  148. },
  149. setType: function(type) {
  150. this.options.type = type;
  151. return this;
  152. },
  153. getSize: function() {
  154. return this.options.size;
  155. },
  156. setSize: function(size) {
  157. this.options.size = size;
  158. return this;
  159. },
  160. getTitle: function() {
  161. return this.options.title;
  162. },
  163. setTitle: function(title) {
  164. this.options.title = title;
  165. return this;
  166. },
  167. getMessage: function() {
  168. return this.options.message;
  169. },
  170. setMessage: function(message) {
  171. this.options.message = message;
  172. return this;
  173. },
  174. isClosable: function() {
  175. return this.options.closable;
  176. },
  177. setClosable: function(closable) {
  178. this.options.closable = closable;
  179. this.updateClosable();
  180. return this;
  181. },
  182. getSpinicon: function() {
  183. return this.options.spinicon;
  184. },
  185. setSpinicon: function(spinicon) {
  186. this.options.spinicon = spinicon;
  187. return this;
  188. },
  189. addButton: function(button) {
  190. this.options.buttons.push(button);
  191. return this;
  192. },
  193. addButtons: function(buttons) {
  194. var that = this;
  195. $.each(buttons, function(index, button) {
  196. that.addButton(button);
  197. });
  198. return this;
  199. },
  200. getButtons: function() {
  201. return this.options.buttons;
  202. },
  203. setButtons: function(buttons) {
  204. this.options.buttons = buttons;
  205. return this;
  206. },
  207. /**
  208. * If there is id provided for a button option, it will be in dialog.indexedButtons list.
  209. *
  210. * In that case you can use dialog.getButton(id) to find the button.
  211. *
  212. * @param {type} id
  213. * @returns {undefined}
  214. */
  215. getButton: function(id) {
  216. if (typeof this.indexedButtons[id] !== 'undefined') {
  217. return this.indexedButtons[id];
  218. }
  219. return null;
  220. },
  221. getButtonSize: function() {
  222. if (typeof BootstrapDialog.BUTTON_SIZES[this.getSize()] !== 'undefined') {
  223. return BootstrapDialog.BUTTON_SIZES[this.getSize()];
  224. }
  225. return '';
  226. },
  227. isAutodestroy: function() {
  228. return this.options.autodestroy;
  229. },
  230. setAutodestroy: function(autodestroy) {
  231. this.options.autodestroy = autodestroy;
  232. },
  233. getDefaultText: function() {
  234. return BootstrapDialog.DEFAULT_TEXTS[this.getType()];
  235. },
  236. getNamespace: function(name) {
  237. return BootstrapDialog.NAMESPACE + '-' + name;
  238. },
  239. createHeaderContent: function() {
  240. var $container = $('<div></div>');
  241. $container.addClass(this.getNamespace('header'));
  242. // title
  243. $container.append(this.createTitleContent());
  244. // Close button
  245. $container.append(this.createCloseButton());
  246. return $container;
  247. },
  248. createTitleContent: function() {
  249. var $title = $('<div></div>');
  250. $title.addClass(this.getNamespace('title'));
  251. $title.append(this.getTitle() !== null ? this.createDynamicContent(this.getTitle()) : this.getDefaultText());
  252. return $title;
  253. },
  254. createCloseButton: function() {
  255. var $container = $('<div></div>');
  256. $container.addClass(this.getNamespace('close-button'));
  257. var $icon = $('<button class="close">×</button>');
  258. $container.append($icon);
  259. $container.on('click', {dialog: this}, function(event) {
  260. event.data.dialog.close();
  261. });
  262. return $container;
  263. },
  264. createBodyContent: function() {
  265. var $container = $('<div></div>');
  266. $container.addClass(this.getNamespace('body'));
  267. // Message
  268. $container.append(this.createMessageContent());
  269. return $container;
  270. },
  271. createMessageContent: function() {
  272. var $message = $('<div></div>');
  273. $message.addClass(this.getNamespace('message'));
  274. $message.append(this.createDynamicContent(this.getMessage()));
  275. return $message;
  276. },
  277. createFooterContent: function() {
  278. var $container = $('<div></div>');
  279. $container.addClass(this.getNamespace('footer'));
  280. // Buttons
  281. $container.append(this.createFooterButtons());
  282. return $container;
  283. },
  284. createFooterButtons: function() {
  285. var that = this;
  286. var $container = $('<div></div>');
  287. $container.addClass(this.getNamespace('footer-buttons'));
  288. this.indexedButtons = {};
  289. $.each(this.options.buttons, function(index, button) {
  290. var $button = that.createButton(button);
  291. if (typeof button.id !== 'undefined') {
  292. that.indexedButtons[button.id] = $button;
  293. }
  294. $container.append($button);
  295. });
  296. return $container;
  297. },
  298. createButton: function(button) {
  299. var $button = $('<button class="btn"></button>');
  300. $button.addClass(this.getButtonSize());
  301. // Icon
  302. if (typeof button.icon !== undefined && $.trim(button.icon) !== '') {
  303. $button.append(this.createButtonIcon(button.icon));
  304. }
  305. // Label
  306. if (typeof button.label !== undefined) {
  307. $button.append(button.label);
  308. }
  309. // Css class
  310. if (typeof button.cssClass !== undefined && $.trim(button.cssClass) !== '') {
  311. $button.addClass(button.cssClass);
  312. } else {
  313. $button.addClass('btn-default');
  314. }
  315. // Button on click
  316. $button.on('click', {dialog: this, button: button}, function(event) {
  317. var dialog = event.data.dialog;
  318. var button = event.data.button;
  319. if (typeof button.action === 'function') {
  320. button.action.call(this, dialog);
  321. }
  322. if (button.autospin) {
  323. var $button = $(this);
  324. $button.find('.' + dialog.getNamespace('button-icon')).remove();
  325. $button.prepend(dialog.createButtonIcon(dialog.getSpinicon()).addClass('icon-spin'));
  326. }
  327. });
  328. return $button;
  329. },
  330. createButtonIcon: function(icon) {
  331. var $icon = $('<span></span>');
  332. $icon.addClass(this.getNamespace('button-icon')).addClass(icon);
  333. return $icon;
  334. },
  335. /**
  336. * Invoke this only after the dialog is realized.
  337. *
  338. * @param {type} enable
  339. * @returns {undefined}
  340. */
  341. enableButtons: function(enable) {
  342. var $buttons = this.getModalFooter().find('.btn');
  343. $buttons.prop("disabled", !enable).toggleClass('disabled', !enable);
  344. return this;
  345. },
  346. /**
  347. * Invoke this only after the dialog is realized.
  348. *
  349. * @param {type} enable
  350. * @returns {undefined}
  351. */
  352. updateClosable: function() {
  353. if (this.isRealized()) {
  354. // Backdrop, I did't find a way to change bs3 backdrop option after the dialog is poped up, so here's a new wheel.
  355. var $theBigMask = this.getModal();
  356. $theBigMask.off('click').on('click', {dialog: this}, function(event) {
  357. event.target === this && event.data.dialog.isClosable() && event.data.dialog.close();
  358. });
  359. // Close button
  360. this.getModalHeader().find('.' + this.getNamespace('close-button')).toggle(this.isClosable());
  361. // ESC key support
  362. $theBigMask.off('keyup').on('keyup', {dialog: this}, function(event) {
  363. event.which === 27 && event.data.dialog.isClosable() && event.data.dialog.close();
  364. });
  365. }
  366. return this;
  367. },
  368. /**
  369. * Set handler for modal event 'show'.
  370. * This is a setter!
  371. *
  372. * @param {type} onopen
  373. * @returns {_L9.BootstrapDialog.prototype}
  374. */
  375. onShow: function(onshow) {
  376. this.options.onshow = onshow;
  377. return this;
  378. },
  379. /**
  380. * Set handler for modal event 'hide'.
  381. * This is a setter!
  382. *
  383. * @param {type} onclose
  384. * @returns {_L9.BootstrapDialog.prototype}
  385. */
  386. onHide: function(onhide) {
  387. this.options.onhide = onhide;
  388. return this;
  389. },
  390. isRealized: function() {
  391. return this.realized;
  392. },
  393. setRealized: function(realized) {
  394. this.realized = realized;
  395. return this;
  396. },
  397. handleModalEvents: function() {
  398. this.getModal().on('show.bs.modal', {dialog: this}, function(event) {
  399. var dialog = event.data.dialog;
  400. typeof dialog.options.onshow === 'function' && dialog.options.onshow(dialog);
  401. });
  402. this.getModal().on('hide.bs.modal', {dialog: this}, function(event) {
  403. var dialog = event.data.dialog;
  404. typeof dialog.options.onhide === 'function' && dialog.options.onhide(dialog);
  405. });
  406. this.getModal().on('hidden.bs.modal', {dialog: this}, function(event) {
  407. var dialog = event.data.dialog;
  408. dialog.isAutodestroy() && $(this).remove();
  409. });
  410. return this;
  411. },
  412. realize: function() {
  413. this.initModalStuff();
  414. this.getModal().addClass(BootstrapDialog.NAMESPACE)
  415. .addClass(this.getType())
  416. .addClass(this.getSize());
  417. this.getModalHeader().append(this.createHeaderContent());
  418. this.getModalBody().append(this.createBodyContent());
  419. this.getModalFooter().append(this.createFooterContent());
  420. this.getModal().modal({
  421. backdrop: 'static',
  422. keyboard: false,
  423. show: false
  424. });
  425. this.handleModalEvents();
  426. this.setRealized(true);
  427. return this;
  428. },
  429. open: function() {
  430. !this.isRealized() && this.realize();
  431. this.updateClosable();
  432. this.getModal().modal('show');
  433. return this;
  434. },
  435. close: function() {
  436. this.getModal().modal('hide');
  437. return this;
  438. }
  439. };
  440. /* ================================================
  441. * For lazy people
  442. * ================================================ */
  443. /**
  444. * Shortcut function: show
  445. *
  446. * @param {type} options
  447. * @returns {undefined}
  448. */
  449. BootstrapDialog.show = function(options) {
  450. new BootstrapDialog(options).open();
  451. };
  452. /**
  453. * Alert window
  454. *
  455. * @param {type} message
  456. * @param {type} callback
  457. * @returns {undefined}
  458. */
  459. BootstrapDialog.alert = function(message, callback) {
  460. new BootstrapDialog({
  461. message: message,
  462. data: {
  463. 'callback': callback
  464. },
  465. closable: false,
  466. buttons: [{
  467. label: 'OK',
  468. action: function(dialog) {
  469. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
  470. dialog.close();
  471. }
  472. }]
  473. }).open();
  474. };
  475. /**
  476. * Confirm window
  477. *
  478. * @param {type} message
  479. * @param {type} callback
  480. * @returns {undefined}
  481. */
  482. BootstrapDialog.confirm = function(message, callback) {
  483. new BootstrapDialog({
  484. title: 'Confirmation',
  485. message: message,
  486. closable: false,
  487. data: {
  488. 'callback': callback
  489. },
  490. buttons: [{
  491. label: 'Cancel',
  492. action: function(dialog) {
  493. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(false);
  494. dialog.close();
  495. }
  496. }, {
  497. label: 'OK',
  498. cssClass: 'btn-primary',
  499. action: function(dialog) {
  500. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
  501. dialog.close();
  502. }
  503. }]
  504. }).open();
  505. };
  506. }(window.jQuery);