bootstrap-dialog.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. if (this.isClosable()) {
  246. $container.append(this.createCloseButton());
  247. }
  248. return $container;
  249. },
  250. createTitleContent: function() {
  251. var $title = $('<div></div>');
  252. $title.addClass(this.getNamespace('title'));
  253. $title.append(this.getTitle() !== null ? this.createDynamicContent(this.getTitle()) : this.getDefaultText());
  254. return $title;
  255. },
  256. createCloseButton: function() {
  257. var $container = $('<div></div>');
  258. $container.addClass(this.getNamespace('close-button'));
  259. var $icon = $('<button class="close">×</button>');
  260. $container.append($icon);
  261. $container.on('click', {dialog: this}, function(event) {
  262. event.data.dialog.close();
  263. });
  264. return $container;
  265. },
  266. createBodyContent: function() {
  267. var $container = $('<div></div>');
  268. $container.addClass(this.getNamespace('body'));
  269. // Message
  270. $container.append(this.createMessageContent());
  271. return $container;
  272. },
  273. createMessageContent: function() {
  274. var $message = $('<div></div>');
  275. $message.addClass(this.getNamespace('message'));
  276. $message.append(this.createDynamicContent(this.getMessage()));
  277. return $message;
  278. },
  279. createFooterContent: function() {
  280. var $container = $('<div></div>');
  281. $container.addClass(this.getNamespace('footer'));
  282. // Buttons
  283. $container.append(this.createFooterButtons());
  284. return $container;
  285. },
  286. createFooterButtons: function() {
  287. var that = this;
  288. var $container = $('<div></div>');
  289. $container.addClass(this.getNamespace('footer-buttons'));
  290. this.indexedButtons = {};
  291. $.each(this.options.buttons, function(index, button) {
  292. var $button = that.createButton(button);
  293. if (typeof button.id !== 'undefined') {
  294. that.indexedButtons[button.id] = $button;
  295. }
  296. $container.append($button);
  297. });
  298. return $container;
  299. },
  300. createButton: function(button) {
  301. var $button = $('<button class="btn"></button>');
  302. $button.addClass(this.getButtonSize());
  303. // Icon
  304. if (typeof button.icon !== undefined && $.trim(button.icon) !== '') {
  305. $button.append(this.createButtonIcon(button.icon));
  306. }
  307. // Label
  308. if (typeof button.label !== undefined) {
  309. $button.append(button.label);
  310. }
  311. // Css class
  312. if (typeof button.cssClass !== undefined && $.trim(button.cssClass) !== '') {
  313. $button.addClass(button.cssClass);
  314. } else {
  315. $button.addClass('btn-default');
  316. }
  317. // Button on click
  318. $button.on('click', {dialog: this, button: button}, function(event) {
  319. var dialog = event.data.dialog;
  320. var button = event.data.button;
  321. if (typeof button.action === 'function') {
  322. button.action.call(this, dialog);
  323. }
  324. if (button.autospin) {
  325. var $button = $(this);
  326. $button.find('.' + dialog.getNamespace('button-icon')).remove();
  327. $button.prepend(dialog.createButtonIcon(dialog.getSpinicon()).addClass('icon-spin'));
  328. }
  329. });
  330. return $button;
  331. },
  332. createButtonIcon: function(icon) {
  333. var $icon = $('<span></span>');
  334. $icon.addClass(this.getNamespace('button-icon')).addClass(icon);
  335. return $icon;
  336. },
  337. /**
  338. * Invoke this only after the dialog is realized.
  339. *
  340. * @param {type} enable
  341. * @returns {undefined}
  342. */
  343. enableButtons: function(enable) {
  344. var $buttons = this.getModalFooter().find('.btn');
  345. $buttons.prop("disabled", !enable).toggleClass('disabled', !enable);
  346. return this;
  347. },
  348. /**
  349. * Invoke this only after the dialog is realized.
  350. *
  351. * @param {type} enable
  352. * @returns {undefined}
  353. */
  354. updateClosable: function() {
  355. if (this.isRealized()) {
  356. // Backdrop, I did't find a way to change bs3 backdrop option after the dialog is poped up, so here's a new wheel.
  357. var $theBigMask = this.getModal();
  358. $theBigMask.off('click').on('click', {dialog: this}, function(event) {
  359. event.target === this && event.data.dialog.isClosable() && event.data.dialog.close();
  360. });
  361. // Close button
  362. this.getModalHeader().find('.' + this.getNamespace('close-button')).toggle(this.isClosable());
  363. // ESC key support
  364. $theBigMask.off('keyup').on('keyup', {dialog: this}, function(event) {
  365. event.which === 27 && event.data.dialog.isClosable() && event.data.dialog.close();
  366. });
  367. }
  368. return this;
  369. },
  370. /**
  371. * Set handler for modal event 'show'.
  372. * This is a setter!
  373. *
  374. * @param {type} onopen
  375. * @returns {_L9.BootstrapDialog.prototype}
  376. */
  377. onShow: function(onshow) {
  378. this.options.onshow = onshow;
  379. return this;
  380. },
  381. /**
  382. * Set handler for modal event 'hide'.
  383. * This is a setter!
  384. *
  385. * @param {type} onclose
  386. * @returns {_L9.BootstrapDialog.prototype}
  387. */
  388. onHide: function(onhide) {
  389. this.options.onhide = onhide;
  390. return this;
  391. },
  392. isRealized: function() {
  393. return this.realized;
  394. },
  395. setRealized: function(realized) {
  396. this.realized = realized;
  397. return this;
  398. },
  399. handleModalEvents: function() {
  400. this.getModal().on('show.bs.modal', {dialog: this}, function(event) {
  401. var dialog = event.data.dialog;
  402. typeof dialog.options.onshow === 'function' && dialog.options.onshow(dialog);
  403. });
  404. this.getModal().on('hide.bs.modal', {dialog: this}, function(event) {
  405. var dialog = event.data.dialog;
  406. typeof dialog.options.onhide === 'function' && dialog.options.onhide(dialog);
  407. });
  408. this.getModal().on('hidden.bs.modal', {dialog: this}, function(event) {
  409. var dialog = event.data.dialog;
  410. dialog.isAutodestroy() && $(this).remove();
  411. });
  412. return this;
  413. },
  414. realize: function() {
  415. this.initModalStuff();
  416. this.getModal().addClass(BootstrapDialog.NAMESPACE)
  417. .addClass(this.getType())
  418. .addClass(this.getSize());
  419. this.getModalHeader().append(this.createHeaderContent());
  420. this.getModalBody().append(this.createBodyContent());
  421. this.getModalFooter().append(this.createFooterContent());
  422. this.getModal().modal({
  423. backdrop: 'static',
  424. keyboard: false,
  425. show: false
  426. });
  427. this.handleModalEvents();
  428. this.setRealized(true);
  429. return this;
  430. },
  431. open: function() {
  432. !this.isRealized() && this.realize();
  433. this.updateClosable();
  434. this.getModal().modal('show');
  435. return this;
  436. },
  437. close: function() {
  438. this.getModal().modal('hide');
  439. return this;
  440. }
  441. };
  442. /* ================================================
  443. * For lazy people
  444. * ================================================ */
  445. /**
  446. * Shortcut function: show
  447. *
  448. * @param {type} options
  449. * @returns {undefined}
  450. */
  451. BootstrapDialog.show = function(options) {
  452. new BootstrapDialog(options).open();
  453. };
  454. /**
  455. * Alert window
  456. *
  457. * @param {type} message
  458. * @param {type} callback
  459. * @returns {undefined}
  460. */
  461. BootstrapDialog.alert = function(message, callback) {
  462. new BootstrapDialog({
  463. message: message,
  464. data: {
  465. 'callback': callback
  466. },
  467. closable: false,
  468. buttons: [{
  469. label: 'OK',
  470. action: function(dialog) {
  471. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
  472. dialog.close();
  473. }
  474. }]
  475. }).open();
  476. };
  477. /**
  478. * Confirm window
  479. *
  480. * @param {type} message
  481. * @param {type} callback
  482. * @returns {undefined}
  483. */
  484. BootstrapDialog.confirm = function(message, callback) {
  485. new BootstrapDialog({
  486. title: 'Confirmation',
  487. message: message,
  488. closable: false,
  489. data: {
  490. 'callback': callback
  491. },
  492. buttons: [{
  493. label: 'Cancel',
  494. action: function(dialog) {
  495. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(false);
  496. dialog.close();
  497. }
  498. }, {
  499. label: 'OK',
  500. cssClass: 'btn-primary',
  501. action: function(dialog) {
  502. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
  503. dialog.close();
  504. }
  505. }]
  506. }).open();
  507. };
  508. }(window.jQuery);