bootstrap-dialog.js 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /* global define */
  2. /* ================================================
  3. * Make use of Bootstrap's modal more monkey-friendly.
  4. *
  5. * For Bootstrap 3.
  6. *
  7. * javanoob@hotmail.com
  8. *
  9. * https://github.com/nakupanda/bootstrap3-dialog
  10. *
  11. * Licensed under The MIT License.
  12. * ================================================ */
  13. (function(root, factory) {
  14. "use strict";
  15. // CommonJS module is defined
  16. if (typeof module !== 'undefined' && module.exports) {
  17. module.exports = factory(require('jquery')(root));
  18. }
  19. // AMD module is defined
  20. else if (typeof define === "function" && define.amd) {
  21. define("bootstrap-dialog", ["jquery"], function($) {
  22. return factory($);
  23. });
  24. } else {
  25. // planted over the root!
  26. root.BootstrapDialog = factory(root.jQuery);
  27. }
  28. }(this, function($) {
  29. "use strict";
  30. var BootstrapDialog = function(options) {
  31. this.defaultOptions = $.extend(true, {
  32. id: BootstrapDialog.newGuid(),
  33. buttons: [],
  34. data: {},
  35. onshow: null,
  36. onshown: null,
  37. onhide: null,
  38. onhidden: null
  39. }, BootstrapDialog.defaultOptions);
  40. this.indexedButtons = {};
  41. this.registeredButtonHotkeys = {};
  42. this.draggableData = {
  43. isMouseDown: false,
  44. mouseOffset: {}
  45. };
  46. this.realized = false;
  47. this.opened = false;
  48. this.initOptions(options);
  49. this.holdThisInstance();
  50. };
  51. /**
  52. * Some constants.
  53. */
  54. BootstrapDialog.NAMESPACE = 'bootstrap-dialog';
  55. BootstrapDialog.TYPE_DEFAULT = 'type-default';
  56. BootstrapDialog.TYPE_INFO = 'type-info';
  57. BootstrapDialog.TYPE_PRIMARY = 'type-primary';
  58. BootstrapDialog.TYPE_SUCCESS = 'type-success';
  59. BootstrapDialog.TYPE_WARNING = 'type-warning';
  60. BootstrapDialog.TYPE_DANGER = 'type-danger';
  61. BootstrapDialog.DEFAULT_TEXTS = {};
  62. BootstrapDialog.DEFAULT_TEXTS[BootstrapDialog.TYPE_DEFAULT] = 'Information';
  63. BootstrapDialog.DEFAULT_TEXTS[BootstrapDialog.TYPE_INFO] = 'Information';
  64. BootstrapDialog.DEFAULT_TEXTS[BootstrapDialog.TYPE_PRIMARY] = 'Information';
  65. BootstrapDialog.DEFAULT_TEXTS[BootstrapDialog.TYPE_SUCCESS] = 'Success';
  66. BootstrapDialog.DEFAULT_TEXTS[BootstrapDialog.TYPE_WARNING] = 'Warning';
  67. BootstrapDialog.DEFAULT_TEXTS[BootstrapDialog.TYPE_DANGER] = 'Danger';
  68. BootstrapDialog.DEFAULT_TEXTS['OK'] = 'OK';
  69. BootstrapDialog.DEFAULT_TEXTS['CANCEL'] = 'Cancel';
  70. BootstrapDialog.SIZE_NORMAL = 'size-normal';
  71. BootstrapDialog.SIZE_WIDE = 'size-wide'; // size-wide is equal to modal-lg
  72. BootstrapDialog.SIZE_LARGE = 'size-large';
  73. BootstrapDialog.BUTTON_SIZES = {};
  74. BootstrapDialog.BUTTON_SIZES[BootstrapDialog.SIZE_NORMAL] = '';
  75. BootstrapDialog.BUTTON_SIZES[BootstrapDialog.SIZE_WIDE] = '';
  76. BootstrapDialog.BUTTON_SIZES[BootstrapDialog.SIZE_LARGE] = 'btn-lg';
  77. BootstrapDialog.ICON_SPINNER = 'glyphicon glyphicon-asterisk';
  78. // Will be removed in later version, after Bootstrap Modal >= 3.3.0, updating z-index is unnecessary.
  79. BootstrapDialog.ZINDEX_BACKDROP = 1040;
  80. BootstrapDialog.ZINDEX_MODAL = 1050;
  81. /**
  82. * Default options.
  83. */
  84. BootstrapDialog.defaultOptions = {
  85. type: BootstrapDialog.TYPE_PRIMARY,
  86. size: BootstrapDialog.SIZE_NORMAL,
  87. cssClass: '',
  88. title: null,
  89. message: null,
  90. nl2br: true,
  91. closable: true,
  92. closeByBackdrop: true,
  93. closeByKeyboard: true,
  94. spinicon: BootstrapDialog.ICON_SPINNER,
  95. autodestroy: true,
  96. draggable: false,
  97. animate: true,
  98. description: ''
  99. };
  100. /**
  101. * Config default options.
  102. */
  103. BootstrapDialog.configDefaultOptions = function(options) {
  104. BootstrapDialog.defaultOptions = $.extend(true, BootstrapDialog.defaultOptions, options);
  105. };
  106. /**
  107. * Open / Close all created dialogs all at once.
  108. */
  109. BootstrapDialog.dialogs = {};
  110. BootstrapDialog.openAll = function() {
  111. $.each(BootstrapDialog.dialogs, function(id, dialogInstance) {
  112. dialogInstance.open();
  113. });
  114. };
  115. BootstrapDialog.closeAll = function() {
  116. $.each(BootstrapDialog.dialogs, function(id, dialogInstance) {
  117. dialogInstance.close();
  118. });
  119. };
  120. /**
  121. * Move focus to next visible dialog.
  122. */
  123. BootstrapDialog.moveFocus = function() {
  124. var lastDialogInstance = null;
  125. $.each(BootstrapDialog.dialogs, function(id, dialogInstance) {
  126. lastDialogInstance = dialogInstance;
  127. });
  128. if (lastDialogInstance !== null && lastDialogInstance.isRealized()) {
  129. lastDialogInstance.getModal().focus();
  130. }
  131. };
  132. /**
  133. * Determin if current version of Bootstrap Modal is greater than 3.3.0
  134. *
  135. * @returns boolean
  136. */
  137. BootstrapDialog.isModernModal = function() {
  138. var modal = $.fn.modal.Constructor;
  139. return typeof $.fn.modal.Constructor.VERSION !== 'undefined' && /3\.3\.\d+/.test($.fn.modal.Constructor.VERSION);
  140. };
  141. BootstrapDialog.prototype = {
  142. constructor: BootstrapDialog,
  143. initOptions: function(options) {
  144. this.options = $.extend(true, this.defaultOptions, options);
  145. return this;
  146. },
  147. holdThisInstance: function() {
  148. BootstrapDialog.dialogs[this.getId()] = this;
  149. return this;
  150. },
  151. initModalStuff: function() {
  152. this.setModal(this.createModal())
  153. .setModalDialog(this.createModalDialog())
  154. .setModalContent(this.createModalContent())
  155. .setModalHeader(this.createModalHeader())
  156. .setModalBody(this.createModalBody())
  157. .setModalFooter(this.createModalFooter());
  158. this.getModal().append(this.getModalDialog());
  159. this.getModalDialog().append(this.getModalContent());
  160. this.getModalContent()
  161. .append(this.getModalHeader())
  162. .append(this.getModalBody())
  163. .append(this.getModalFooter());
  164. return this;
  165. },
  166. createModal: function() {
  167. var $modal = $('<div class="modal" tabindex="-1" role="dialog" aria-hidden="true"></div>');
  168. $modal.prop('id', this.getId()).attr('aria-labelledby', this.getId() + '_title');
  169. return $modal;
  170. },
  171. getModal: function() {
  172. return this.$modal;
  173. },
  174. setModal: function($modal) {
  175. this.$modal = $modal;
  176. return this;
  177. },
  178. createModalDialog: function() {
  179. return $('<div class="modal-dialog"></div>');
  180. },
  181. getModalDialog: function() {
  182. return this.$modalDialog;
  183. },
  184. setModalDialog: function($modalDialog) {
  185. this.$modalDialog = $modalDialog;
  186. return this;
  187. },
  188. createModalContent: function() {
  189. return $('<div class="modal-content"></div>');
  190. },
  191. getModalContent: function() {
  192. return this.$modalContent;
  193. },
  194. setModalContent: function($modalContent) {
  195. this.$modalContent = $modalContent;
  196. return this;
  197. },
  198. createModalHeader: function() {
  199. return $('<div class="modal-header"></div>');
  200. },
  201. getModalHeader: function() {
  202. return this.$modalHeader;
  203. },
  204. setModalHeader: function($modalHeader) {
  205. this.$modalHeader = $modalHeader;
  206. return this;
  207. },
  208. createModalBody: function() {
  209. return $('<div class="modal-body"></div>');
  210. },
  211. getModalBody: function() {
  212. return this.$modalBody;
  213. },
  214. setModalBody: function($modalBody) {
  215. this.$modalBody = $modalBody;
  216. return this;
  217. },
  218. createModalFooter: function() {
  219. return $('<div class="modal-footer"></div>');
  220. },
  221. getModalFooter: function() {
  222. return this.$modalFooter;
  223. },
  224. setModalFooter: function($modalFooter) {
  225. this.$modalFooter = $modalFooter;
  226. return this;
  227. },
  228. createDynamicContent: function(rawContent) {
  229. var content = null;
  230. if (typeof rawContent === 'function') {
  231. content = rawContent.call(rawContent, this);
  232. } else {
  233. content = rawContent;
  234. }
  235. if (typeof content === 'string') {
  236. content = this.formatStringContent(content);
  237. }
  238. return content;
  239. },
  240. formatStringContent: function(content) {
  241. if (this.options.nl2br) {
  242. return content.replace(/\r\n/g, '<br />').replace(/[\r\n]/g, '<br />');
  243. }
  244. return content;
  245. },
  246. setData: function(key, value) {
  247. this.options.data[key] = value;
  248. return this;
  249. },
  250. getData: function(key) {
  251. return this.options.data[key];
  252. },
  253. setId: function(id) {
  254. this.options.id = id;
  255. return this;
  256. },
  257. getId: function() {
  258. return this.options.id;
  259. },
  260. getType: function() {
  261. return this.options.type;
  262. },
  263. setType: function(type) {
  264. this.options.type = type;
  265. this.updateType();
  266. return this;
  267. },
  268. updateType: function() {
  269. if (this.isRealized()) {
  270. var types = [BootstrapDialog.TYPE_DEFAULT,
  271. BootstrapDialog.TYPE_INFO,
  272. BootstrapDialog.TYPE_PRIMARY,
  273. BootstrapDialog.TYPE_SUCCESS,
  274. BootstrapDialog.TYPE_WARNING,
  275. BootstrapDialog.TYPE_DANGER];
  276. this.getModal().removeClass(types.join(' ')).addClass(this.getType());
  277. }
  278. return this;
  279. },
  280. getSize: function() {
  281. return this.options.size;
  282. },
  283. setSize: function(size) {
  284. this.options.size = size;
  285. this.updateSize();
  286. return this;
  287. },
  288. updateSize: function() {
  289. if (this.isRealized()) {
  290. var dialog = this;
  291. // Dialog size
  292. this.getModal().removeClass(BootstrapDialog.SIZE_NORMAL)
  293. .removeClass(BootstrapDialog.SIZE_WIDE)
  294. .removeClass(BootstrapDialog.SIZE_LARGE);
  295. this.getModal().addClass(this.getSize());
  296. // Wider dialog.
  297. this.getModalDialog().removeClass('modal-lg');
  298. if (this.getSize() === BootstrapDialog.SIZE_WIDE) {
  299. this.getModalDialog().addClass('modal-lg');
  300. }
  301. // Button size
  302. $.each(this.options.buttons, function(index, button) {
  303. var $button = dialog.getButton(button.id);
  304. var buttonSizes = ['btn-lg', 'btn-sm', 'btn-xs'];
  305. var sizeClassSpecified = false;
  306. if (typeof button['cssClass'] === 'string') {
  307. var btnClasses = button['cssClass'].split(' ');
  308. $.each(btnClasses, function(index, btnClass) {
  309. if ($.inArray(btnClass, buttonSizes) !== -1) {
  310. sizeClassSpecified = true;
  311. }
  312. });
  313. }
  314. if (!sizeClassSpecified) {
  315. $button.removeClass(buttonSizes.join(' '));
  316. $button.addClass(dialog.getButtonSize());
  317. }
  318. });
  319. }
  320. return this;
  321. },
  322. getCssClass: function() {
  323. return this.options.cssClass;
  324. },
  325. setCssClass: function(cssClass) {
  326. this.options.cssClass = cssClass;
  327. return this;
  328. },
  329. getTitle: function() {
  330. return this.options.title;
  331. },
  332. setTitle: function(title) {
  333. this.options.title = title;
  334. this.updateTitle();
  335. return this;
  336. },
  337. updateTitle: function() {
  338. if (this.isRealized()) {
  339. var title = this.getTitle() !== null ? this.createDynamicContent(this.getTitle()) : this.getDefaultText();
  340. this.getModalHeader().find('.' + this.getNamespace('title')).html('').append(title).prop('id', this.getId() + '_title');
  341. }
  342. return this;
  343. },
  344. getMessage: function() {
  345. return this.options.message;
  346. },
  347. setMessage: function(message) {
  348. this.options.message = message;
  349. this.updateMessage();
  350. return this;
  351. },
  352. updateMessage: function() {
  353. if (this.isRealized()) {
  354. var message = this.createDynamicContent(this.getMessage());
  355. this.getModalBody().find('.' + this.getNamespace('message')).html('').append(message);
  356. }
  357. return this;
  358. },
  359. isClosable: function() {
  360. return this.options.closable;
  361. },
  362. setClosable: function(closable) {
  363. this.options.closable = closable;
  364. this.updateClosable();
  365. return this;
  366. },
  367. setCloseByBackdrop: function(closeByBackdrop) {
  368. this.options.closeByBackdrop = closeByBackdrop;
  369. return this;
  370. },
  371. canCloseByBackdrop: function() {
  372. return this.options.closeByBackdrop;
  373. },
  374. setCloseByKeyboard: function(closeByKeyboard) {
  375. this.options.closeByKeyboard = closeByKeyboard;
  376. return this;
  377. },
  378. canCloseByKeyboard: function() {
  379. return this.options.closeByKeyboard;
  380. },
  381. isAnimate: function() {
  382. return this.options.animate;
  383. },
  384. setAnimate: function(animate) {
  385. this.options.animate = animate;
  386. return this;
  387. },
  388. updateAnimate: function() {
  389. if (this.isRealized()) {
  390. this.getModal().toggleClass('fade', this.isAnimate());
  391. }
  392. return this;
  393. },
  394. getSpinicon: function() {
  395. return this.options.spinicon;
  396. },
  397. setSpinicon: function(spinicon) {
  398. this.options.spinicon = spinicon;
  399. return this;
  400. },
  401. addButton: function(button) {
  402. this.options.buttons.push(button);
  403. return this;
  404. },
  405. addButtons: function(buttons) {
  406. var that = this;
  407. $.each(buttons, function(index, button) {
  408. that.addButton(button);
  409. });
  410. return this;
  411. },
  412. getButtons: function() {
  413. return this.options.buttons;
  414. },
  415. setButtons: function(buttons) {
  416. this.options.buttons = buttons;
  417. this.updateButtons();
  418. return this;
  419. },
  420. /**
  421. * If there is id provided for a button option, it will be in dialog.indexedButtons list.
  422. *
  423. * In that case you can use dialog.getButton(id) to find the button.
  424. *
  425. * @param {type} id
  426. * @returns {undefined}
  427. */
  428. getButton: function(id) {
  429. if (typeof this.indexedButtons[id] !== 'undefined') {
  430. return this.indexedButtons[id];
  431. }
  432. return null;
  433. },
  434. getButtonSize: function() {
  435. if (typeof BootstrapDialog.BUTTON_SIZES[this.getSize()] !== 'undefined') {
  436. return BootstrapDialog.BUTTON_SIZES[this.getSize()];
  437. }
  438. return '';
  439. },
  440. updateButtons: function() {
  441. if (this.isRealized()) {
  442. if (this.getButtons().length === 0) {
  443. this.getModalFooter().hide();
  444. } else {
  445. this.getModalFooter().find('.' + this.getNamespace('footer')).html('').append(this.createFooterButtons());
  446. }
  447. }
  448. return this;
  449. },
  450. isAutodestroy: function() {
  451. return this.options.autodestroy;
  452. },
  453. setAutodestroy: function(autodestroy) {
  454. this.options.autodestroy = autodestroy;
  455. },
  456. getDescription: function() {
  457. return this.options.description;
  458. },
  459. setDescription: function(description) {
  460. this.options.description = description;
  461. return this;
  462. },
  463. getDefaultText: function() {
  464. return BootstrapDialog.DEFAULT_TEXTS[this.getType()];
  465. },
  466. getNamespace: function(name) {
  467. return BootstrapDialog.NAMESPACE + '-' + name;
  468. },
  469. createHeaderContent: function() {
  470. var $container = $('<div></div>');
  471. $container.addClass(this.getNamespace('header'));
  472. // title
  473. $container.append(this.createTitleContent());
  474. // Close button
  475. $container.prepend(this.createCloseButton());
  476. return $container;
  477. },
  478. createTitleContent: function() {
  479. var $title = $('<div></div>');
  480. $title.addClass(this.getNamespace('title'));
  481. return $title;
  482. },
  483. createCloseButton: function() {
  484. var $container = $('<div></div>');
  485. $container.addClass(this.getNamespace('close-button'));
  486. var $icon = $('<button class="close">&times;</button>');
  487. $container.append($icon);
  488. $container.on('click', {dialog: this}, function(event) {
  489. event.data.dialog.close();
  490. });
  491. return $container;
  492. },
  493. createBodyContent: function() {
  494. var $container = $('<div></div>');
  495. $container.addClass(this.getNamespace('body'));
  496. // Message
  497. $container.append(this.createMessageContent());
  498. return $container;
  499. },
  500. createMessageContent: function() {
  501. var $message = $('<div></div>');
  502. $message.addClass(this.getNamespace('message'));
  503. return $message;
  504. },
  505. createFooterContent: function() {
  506. var $container = $('<div></div>');
  507. $container.addClass(this.getNamespace('footer'));
  508. return $container;
  509. },
  510. createFooterButtons: function() {
  511. var that = this;
  512. var $container = $('<div></div>');
  513. $container.addClass(this.getNamespace('footer-buttons'));
  514. this.indexedButtons = {};
  515. $.each(this.options.buttons, function(index, button) {
  516. if (!button.id) {
  517. button.id = BootstrapDialog.newGuid();
  518. }
  519. var $button = that.createButton(button);
  520. that.indexedButtons[button.id] = $button;
  521. $container.append($button);
  522. });
  523. return $container;
  524. },
  525. createButton: function(button) {
  526. var $button = $('<button class="btn"></button>');
  527. $button.prop('id', button.id);
  528. // Icon
  529. if (typeof button.icon !== 'undefined' && $.trim(button.icon) !== '') {
  530. $button.append(this.createButtonIcon(button.icon));
  531. }
  532. // Label
  533. if (typeof button.label !== 'undefined') {
  534. $button.append(button.label);
  535. }
  536. // Css class
  537. if (typeof button.cssClass !== 'undefined' && $.trim(button.cssClass) !== '') {
  538. $button.addClass(button.cssClass);
  539. } else {
  540. $button.addClass('btn-default');
  541. }
  542. // Hotkey
  543. if (typeof button.hotkey !== 'undefined') {
  544. this.registeredButtonHotkeys[button.hotkey] = $button;
  545. }
  546. // Button on click
  547. $button.on('click', {dialog: this, $button: $button, button: button}, function(event) {
  548. var dialog = event.data.dialog;
  549. var $button = event.data.$button;
  550. var button = event.data.button;
  551. if (typeof button.action === 'function') {
  552. button.action.call($button, dialog);
  553. }
  554. if (button.autospin) {
  555. $button.toggleSpin(true);
  556. }
  557. });
  558. // Dynamically add extra functions to $button
  559. this.enhanceButton($button);
  560. return $button;
  561. },
  562. /**
  563. * Dynamically add extra functions to $button
  564. *
  565. * Using '$this' to reference 'this' is just for better readability.
  566. *
  567. * @param {type} $button
  568. * @returns {_L13.BootstrapDialog.prototype}
  569. */
  570. enhanceButton: function($button) {
  571. $button.dialog = this;
  572. // Enable / Disable
  573. $button.toggleEnable = function(enable) {
  574. var $this = this;
  575. if (typeof enable !== 'undefined') {
  576. $this.prop("disabled", !enable).toggleClass('disabled', !enable);
  577. } else {
  578. $this.prop("disabled", !$this.prop("disabled"));
  579. }
  580. return $this;
  581. };
  582. $button.enable = function() {
  583. var $this = this;
  584. $this.toggleEnable(true);
  585. return $this;
  586. };
  587. $button.disable = function() {
  588. var $this = this;
  589. $this.toggleEnable(false);
  590. return $this;
  591. };
  592. // Icon spinning, helpful for indicating ajax loading status.
  593. $button.toggleSpin = function(spin) {
  594. var $this = this;
  595. var dialog = $this.dialog;
  596. var $icon = $this.find('.' + dialog.getNamespace('button-icon'));
  597. if (typeof spin === 'undefined') {
  598. spin = !($button.find('.icon-spin').length > 0);
  599. }
  600. if (spin) {
  601. $icon.hide();
  602. $button.prepend(dialog.createButtonIcon(dialog.getSpinicon()).addClass('icon-spin'));
  603. } else {
  604. $icon.show();
  605. $button.find('.icon-spin').remove();
  606. }
  607. return $this;
  608. };
  609. $button.spin = function() {
  610. var $this = this;
  611. $this.toggleSpin(true);
  612. return $this;
  613. };
  614. $button.stopSpin = function() {
  615. var $this = this;
  616. $this.toggleSpin(false);
  617. return $this;
  618. };
  619. return this;
  620. },
  621. createButtonIcon: function(icon) {
  622. var $icon = $('<span></span>');
  623. $icon.addClass(this.getNamespace('button-icon')).addClass(icon);
  624. return $icon;
  625. },
  626. /**
  627. * Invoke this only after the dialog is realized.
  628. *
  629. * @param {type} enable
  630. * @returns {undefined}
  631. */
  632. enableButtons: function(enable) {
  633. $.each(this.indexedButtons, function(id, $button) {
  634. $button.toggleEnable(enable);
  635. });
  636. return this;
  637. },
  638. /**
  639. * Invoke this only after the dialog is realized.
  640. *
  641. * @returns {undefined}
  642. */
  643. updateClosable: function() {
  644. if (this.isRealized()) {
  645. // Close button
  646. this.getModalHeader().find('.' + this.getNamespace('close-button')).toggle(this.isClosable());
  647. }
  648. return this;
  649. },
  650. /**
  651. * Set handler for modal event 'show.bs.modal'.
  652. * This is a setter!
  653. */
  654. onShow: function(onshow) {
  655. this.options.onshow = onshow;
  656. return this;
  657. },
  658. /**
  659. * Set handler for modal event 'shown.bs.modal'.
  660. * This is a setter!
  661. */
  662. onShown: function(onshown) {
  663. this.options.onshown = onshown;
  664. return this;
  665. },
  666. /**
  667. * Set handler for modal event 'hide.bs.modal'.
  668. * This is a setter!
  669. */
  670. onHide: function(onhide) {
  671. this.options.onhide = onhide;
  672. return this;
  673. },
  674. /**
  675. * Set handler for modal event 'hidden.bs.modal'.
  676. * This is a setter!
  677. */
  678. onHidden: function(onhidden) {
  679. this.options.onhidden = onhidden;
  680. return this;
  681. },
  682. isRealized: function() {
  683. return this.realized;
  684. },
  685. setRealized: function(realized) {
  686. this.realized = realized;
  687. return this;
  688. },
  689. isOpened: function() {
  690. return this.opened;
  691. },
  692. setOpened: function(opened) {
  693. this.opened = opened;
  694. return this;
  695. },
  696. handleModalEvents: function() {
  697. this.getModal().on('show.bs.modal', {dialog: this}, function(event) {
  698. var dialog = event.data.dialog;
  699. if (dialog.isModalEvent(event) && typeof dialog.options.onshow === 'function') {
  700. return dialog.options.onshow(dialog);
  701. }
  702. });
  703. this.getModal().on('shown.bs.modal', {dialog: this}, function(event) {
  704. var dialog = event.data.dialog;
  705. dialog.isModalEvent(event) && typeof dialog.options.onshown === 'function' && dialog.options.onshown(dialog);
  706. });
  707. this.getModal().on('hide.bs.modal', {dialog: this}, function(event) {
  708. var dialog = event.data.dialog;
  709. if (dialog.isModalEvent(event) && typeof dialog.options.onhide === 'function') {
  710. return dialog.options.onhide(dialog);
  711. }
  712. });
  713. this.getModal().on('hidden.bs.modal', {dialog: this}, function(event) {
  714. var dialog = event.data.dialog;
  715. dialog.isModalEvent(event) && typeof dialog.options.onhidden === 'function' && dialog.options.onhidden(dialog);
  716. dialog.isAutodestroy() && $(this).remove();
  717. BootstrapDialog.moveFocus();
  718. });
  719. // Backdrop, I did't find a way to change bs3 backdrop option after the dialog is popped up, so here's a new wheel.
  720. this.getModal().on('click', {dialog: this}, function(event) {
  721. if (!BootstrapDialog.isModernModal()) {
  722. event.target === this && event.data.dialog.isClosable() && event.data.dialog.canCloseByBackdrop() && event.data.dialog.close();
  723. } else {
  724. $(event.target).hasClass('modal-backdrop') && event.data.dialog.isClosable() && event.data.dialog.canCloseByBackdrop() && event.data.dialog.close();
  725. }
  726. });
  727. // ESC key support
  728. this.getModal().on('keyup', {dialog: this}, function(event) {
  729. event.which === 27 && event.data.dialog.isClosable() && event.data.dialog.canCloseByKeyboard() && event.data.dialog.close();
  730. });
  731. // Button hotkey
  732. this.getModal().on('keyup', {dialog: this}, function(event) {
  733. var dialog = event.data.dialog;
  734. if (typeof dialog.registeredButtonHotkeys[event.which] !== 'undefined') {
  735. var $button = $(dialog.registeredButtonHotkeys[event.which]);
  736. !$button.prop('disabled') && $button.focus().trigger('click');
  737. }
  738. });
  739. return this;
  740. },
  741. isModalEvent: function(event) {
  742. return typeof event.namespace !== 'undefined' && event.namespace === 'bs.modal';
  743. },
  744. makeModalDraggable: function() {
  745. if (this.options.draggable) {
  746. this.getModalHeader().addClass(this.getNamespace('draggable')).on('mousedown', {dialog: this}, function(event) {
  747. var dialog = event.data.dialog;
  748. dialog.draggableData.isMouseDown = true;
  749. var dialogOffset = dialog.getModalDialog().offset();
  750. dialog.draggableData.mouseOffset = {
  751. top: event.clientY - dialogOffset.top,
  752. left: event.clientX - dialogOffset.left
  753. };
  754. });
  755. this.getModal().on('mouseup mouseleave', {dialog: this}, function(event) {
  756. event.data.dialog.draggableData.isMouseDown = false;
  757. });
  758. $('body').on('mousemove', {dialog: this}, function(event) {
  759. var dialog = event.data.dialog;
  760. if (!dialog.draggableData.isMouseDown) {
  761. return;
  762. }
  763. dialog.getModalDialog().offset({
  764. top: event.clientY - dialog.draggableData.mouseOffset.top,
  765. left: event.clientX - dialog.draggableData.mouseOffset.left
  766. });
  767. });
  768. }
  769. return this;
  770. },
  771. /**
  772. * To make multiple opened dialogs look better.
  773. *
  774. * Will be removed in later version, after Bootstrap Modal >= 3.3.0, updating z-index is unnecessary.
  775. */
  776. updateZIndex: function() {
  777. var dialogCount = 0;
  778. $.each(BootstrapDialog.dialogs, function(dialogId, dialogInstance) {
  779. dialogCount++;
  780. });
  781. var $modal = this.getModal();
  782. var $backdrop = $modal.data('bs.modal').$backdrop;
  783. $modal.css('z-index', BootstrapDialog.ZINDEX_MODAL + (dialogCount - 1) * 20);
  784. $backdrop.css('z-index', BootstrapDialog.ZINDEX_BACKDROP + (dialogCount - 1) * 20);
  785. return this;
  786. },
  787. realize: function() {
  788. this.initModalStuff();
  789. this.getModal().addClass(BootstrapDialog.NAMESPACE)
  790. .addClass(this.getCssClass());
  791. this.updateSize();
  792. if (this.getDescription()) {
  793. this.getModal().attr('aria-describedby', this.getDescription());
  794. }
  795. this.getModalFooter().append(this.createFooterContent());
  796. this.getModalHeader().append(this.createHeaderContent());
  797. this.getModalBody().append(this.createBodyContent());
  798. this.getModal().modal({
  799. backdrop: 'static',
  800. keyboard: false,
  801. show: false
  802. });
  803. this.makeModalDraggable();
  804. this.handleModalEvents();
  805. this.setRealized(true);
  806. this.updateButtons();
  807. this.updateType();
  808. this.updateTitle();
  809. this.updateMessage();
  810. this.updateClosable();
  811. this.updateAnimate();
  812. this.updateSize();
  813. return this;
  814. },
  815. open: function() {
  816. !this.isRealized() && this.realize();
  817. this.getModal().modal('show');
  818. !BootstrapDialog.isModernModal() && this.updateZIndex(); // Will be removed in later version.
  819. this.setOpened(true);
  820. return this;
  821. },
  822. close: function() {
  823. this.getModal().modal('hide');
  824. if (this.isAutodestroy()) {
  825. delete BootstrapDialog.dialogs[this.getId()];
  826. }
  827. this.setOpened(false);
  828. return this;
  829. }
  830. };
  831. /**
  832. * RFC4122 version 4 compliant unique id creator.
  833. *
  834. * Added by https://github.com/tufanbarisyildirim/
  835. *
  836. * @returns {String}
  837. */
  838. BootstrapDialog.newGuid = function() {
  839. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  840. var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
  841. return v.toString(16);
  842. });
  843. };
  844. /* ================================================
  845. * For lazy people
  846. * ================================================ */
  847. /**
  848. * Shortcut function: show
  849. *
  850. * @param {type} options
  851. * @returns the created dialog instance
  852. */
  853. BootstrapDialog.show = function(options) {
  854. return new BootstrapDialog(options).open();
  855. };
  856. /**
  857. * Alert window
  858. *
  859. * @returns the created dialog instance
  860. */
  861. BootstrapDialog.alert = function() {
  862. var options = {};
  863. var defaultOptions = {
  864. type: BootstrapDialog.TYPE_PRIMARY,
  865. title: null,
  866. message: null,
  867. closable: true,
  868. buttonLabel: BootstrapDialog.DEFAULT_TEXTS.OK,
  869. callback: null
  870. };
  871. if (typeof arguments[0] === 'object' && arguments[0].constructor === {}.constructor) {
  872. options = $.extend(true, defaultOptions, arguments[0]);
  873. } else {
  874. options = $.extend(true, defaultOptions, {
  875. message: arguments[0],
  876. closable: false,
  877. buttonLabel: BootstrapDialog.DEFAULT_TEXTS.OK,
  878. callback: typeof arguments[1] !== 'undefined' ? arguments[1] : null
  879. });
  880. }
  881. return new BootstrapDialog({
  882. type: options.type,
  883. title: options.title,
  884. message: options.message,
  885. closable: options.closable,
  886. data: {
  887. callback: options.callback
  888. },
  889. onhide: function(dialog) {
  890. !dialog.getData('btnClicked') && dialog.isClosable() && typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(false);
  891. },
  892. buttons: [{
  893. label: options.buttonLabel,
  894. action: function(dialog) {
  895. dialog.setData('btnClicked', true);
  896. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
  897. dialog.close();
  898. }
  899. }]
  900. }).open();
  901. };
  902. /**
  903. * Confirm window
  904. *
  905. * @param {type} message
  906. * @param {type} callback
  907. * @returns the created dialog instance
  908. */
  909. BootstrapDialog.confirm = function(message, callback) {
  910. return new BootstrapDialog({
  911. title: 'Confirmation',
  912. message: message,
  913. closable: false,
  914. data: {
  915. 'callback': callback
  916. },
  917. buttons: [{
  918. label: BootstrapDialog.DEFAULT_TEXTS.CANCEL,
  919. action: function(dialog) {
  920. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(false);
  921. dialog.close();
  922. }
  923. }, {
  924. label: BootstrapDialog.DEFAULT_TEXTS.OK,
  925. cssClass: 'btn-primary',
  926. action: function(dialog) {
  927. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
  928. dialog.close();
  929. }
  930. }]
  931. }).open();
  932. };
  933. /**
  934. * Warning window
  935. *
  936. * @param {type} message
  937. * @returns the created dialog instance
  938. */
  939. BootstrapDialog.warning = function(message, callback) {
  940. return new BootstrapDialog({
  941. type: BootstrapDialog.TYPE_WARNING,
  942. message: message
  943. }).open();
  944. };
  945. /**
  946. * Danger window
  947. *
  948. * @param {type} message
  949. * @returns the created dialog instance
  950. */
  951. BootstrapDialog.danger = function(message, callback) {
  952. return new BootstrapDialog({
  953. type: BootstrapDialog.TYPE_DANGER,
  954. message: message
  955. }).open();
  956. };
  957. /**
  958. * Success window
  959. *
  960. * @param {type} message
  961. * @returns the created dialog instance
  962. */
  963. BootstrapDialog.success = function(message, callback) {
  964. return new BootstrapDialog({
  965. type: BootstrapDialog.TYPE_SUCCESS,
  966. message: message
  967. }).open();
  968. };
  969. return BootstrapDialog;
  970. }));