bootstrap-dialog.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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.SIZE_NORMAL = 'size-normal';
  69. BootstrapDialog.SIZE_LARGE = 'size-large';
  70. BootstrapDialog.BUTTON_SIZES = {};
  71. BootstrapDialog.BUTTON_SIZES[BootstrapDialog.SIZE_NORMAL] = '';
  72. BootstrapDialog.BUTTON_SIZES[BootstrapDialog.SIZE_LARGE] = 'btn-lg';
  73. BootstrapDialog.ICON_SPINNER = 'glyphicon glyphicon-asterisk';
  74. BootstrapDialog.ZINDEX_BACKDROP = 1040;
  75. BootstrapDialog.ZINDEX_MODAL = 1050;
  76. /**
  77. * Default options.
  78. */
  79. BootstrapDialog.defaultOptions = {
  80. type: BootstrapDialog.TYPE_PRIMARY,
  81. size: BootstrapDialog.SIZE_NORMAL,
  82. cssClass: '',
  83. title: null,
  84. message: null,
  85. nl2br: true,
  86. closable: true,
  87. closeByBackdrop: true,
  88. closeByKeyboard: true,
  89. spinicon: BootstrapDialog.ICON_SPINNER,
  90. autodestroy: true,
  91. draggable: false
  92. };
  93. /**
  94. * Config default options.
  95. */
  96. BootstrapDialog.configDefaultOptions = function(options) {
  97. BootstrapDialog.defaultOptions = $.extend(true, BootstrapDialog.defaultOptions, options);
  98. };
  99. /**
  100. * Open / Close all created dialogs all at once.
  101. */
  102. BootstrapDialog.dialogs = {};
  103. BootstrapDialog.openAll = function() {
  104. $.each(BootstrapDialog.dialogs, function(id, dialogInstance) {
  105. dialogInstance.open();
  106. });
  107. };
  108. BootstrapDialog.closeAll = function() {
  109. $.each(BootstrapDialog.dialogs, function(id, dialogInstance) {
  110. dialogInstance.close();
  111. });
  112. };
  113. BootstrapDialog.prototype = {
  114. constructor: BootstrapDialog,
  115. initOptions: function(options) {
  116. this.options = $.extend(true, this.defaultOptions, options);
  117. return this;
  118. },
  119. holdThisInstance: function() {
  120. BootstrapDialog.dialogs[this.getId()] = this;
  121. return this;
  122. },
  123. initModalStuff: function() {
  124. this.setModal(this.createModal())
  125. .setModalDialog(this.createModalDialog())
  126. .setModalContent(this.createModalContent())
  127. .setModalHeader(this.createModalHeader())
  128. .setModalBody(this.createModalBody())
  129. .setModalFooter(this.createModalFooter());
  130. this.getModal().append(this.getModalDialog());
  131. this.getModalDialog().append(this.getModalContent());
  132. this.getModalContent()
  133. .append(this.getModalHeader())
  134. .append(this.getModalBody())
  135. .append(this.getModalFooter());
  136. return this;
  137. },
  138. createModal: function() {
  139. var $modal = $('<div class="modal fade" tabindex="-1"></div>');
  140. $modal.prop('id', this.getId());
  141. return $modal;
  142. },
  143. getModal: function() {
  144. return this.$modal;
  145. },
  146. setModal: function($modal) {
  147. this.$modal = $modal;
  148. return this;
  149. },
  150. createModalDialog: function() {
  151. return $('<div class="modal-dialog"></div>');
  152. },
  153. getModalDialog: function() {
  154. return this.$modalDialog;
  155. },
  156. setModalDialog: function($modalDialog) {
  157. this.$modalDialog = $modalDialog;
  158. return this;
  159. },
  160. createModalContent: function() {
  161. return $('<div class="modal-content"></div>');
  162. },
  163. getModalContent: function() {
  164. return this.$modalContent;
  165. },
  166. setModalContent: function($modalContent) {
  167. this.$modalContent = $modalContent;
  168. return this;
  169. },
  170. createModalHeader: function() {
  171. return $('<div class="modal-header"></div>');
  172. },
  173. getModalHeader: function() {
  174. return this.$modalHeader;
  175. },
  176. setModalHeader: function($modalHeader) {
  177. this.$modalHeader = $modalHeader;
  178. return this;
  179. },
  180. createModalBody: function() {
  181. return $('<div class="modal-body"></div>');
  182. },
  183. getModalBody: function() {
  184. return this.$modalBody;
  185. },
  186. setModalBody: function($modalBody) {
  187. this.$modalBody = $modalBody;
  188. return this;
  189. },
  190. createModalFooter: function() {
  191. return $('<div class="modal-footer"></div>');
  192. },
  193. getModalFooter: function() {
  194. return this.$modalFooter;
  195. },
  196. setModalFooter: function($modalFooter) {
  197. this.$modalFooter = $modalFooter;
  198. return this;
  199. },
  200. createDynamicContent: function(rawContent) {
  201. var content = null;
  202. if (typeof rawContent === 'function') {
  203. content = rawContent.call(rawContent, this);
  204. } else {
  205. content = rawContent;
  206. }
  207. if (typeof content === 'string') {
  208. content = this.formatStringContent(content);
  209. }
  210. return content;
  211. },
  212. formatStringContent: function(content) {
  213. if (this.options.nl2br) {
  214. return content.replace(/\r\n/g, '<br />').replace(/[\r\n]/g, '<br />');
  215. }
  216. return content;
  217. },
  218. setData: function(key, value) {
  219. this.options.data[key] = value;
  220. return this;
  221. },
  222. getData: function(key) {
  223. return this.options.data[key];
  224. },
  225. setId: function(id) {
  226. this.options.id = id;
  227. return this;
  228. },
  229. getId: function() {
  230. return this.options.id;
  231. },
  232. getType: function() {
  233. return this.options.type;
  234. },
  235. setType: function(type) {
  236. this.options.type = type;
  237. return this;
  238. },
  239. getSize: function() {
  240. return this.options.size;
  241. },
  242. setSize: function(size) {
  243. this.options.size = size;
  244. return this;
  245. },
  246. getCssClass: function() {
  247. return this.options.cssClass;
  248. },
  249. setCssClass: function(cssClass) {
  250. this.options.cssClass = cssClass;
  251. return this;
  252. },
  253. getTitle: function() {
  254. return this.options.title;
  255. },
  256. setTitle: function(title) {
  257. this.options.title = title;
  258. this.updateTitle();
  259. return this;
  260. },
  261. updateTitle: function() {
  262. if (this.isRealized()) {
  263. var title = this.getTitle() !== null ? this.createDynamicContent(this.getTitle()) : this.getDefaultText();
  264. this.getModalHeader().find('.' + this.getNamespace('title')).html('').append(title);
  265. }
  266. return this;
  267. },
  268. getMessage: function() {
  269. return this.options.message;
  270. },
  271. setMessage: function(message) {
  272. this.options.message = message;
  273. this.updateMessage();
  274. return this;
  275. },
  276. updateMessage: function() {
  277. if (this.isRealized()) {
  278. var message = this.createDynamicContent(this.getMessage());
  279. this.getModalBody().find('.' + this.getNamespace('message')).html('').append(message);
  280. }
  281. return this;
  282. },
  283. isClosable: function() {
  284. return this.options.closable;
  285. },
  286. setClosable: function(closable) {
  287. this.options.closable = closable;
  288. this.updateClosable();
  289. return this;
  290. },
  291. setCloseByBackdrop: function(closeByBackdrop) {
  292. this.options.closeByBackdrop = closeByBackdrop;
  293. return this;
  294. },
  295. canCloseByBackdrop: function() {
  296. return this.options.closeByBackdrop;
  297. },
  298. setCloseByKeyboard: function(closeByKeyboard) {
  299. this.options.closeByKeyboard = closeByKeyboard;
  300. return this;
  301. },
  302. canCloseByKeyboard: function() {
  303. return this.options.closeByKeyboard;
  304. },
  305. getSpinicon: function() {
  306. return this.options.spinicon;
  307. },
  308. setSpinicon: function(spinicon) {
  309. this.options.spinicon = spinicon;
  310. return this;
  311. },
  312. addButton: function(button) {
  313. this.options.buttons.push(button);
  314. return this;
  315. },
  316. addButtons: function(buttons) {
  317. var that = this;
  318. $.each(buttons, function(index, button) {
  319. that.addButton(button);
  320. });
  321. return this;
  322. },
  323. getButtons: function() {
  324. return this.options.buttons;
  325. },
  326. setButtons: function(buttons) {
  327. this.options.buttons = buttons;
  328. this.updateButtons();
  329. return this;
  330. },
  331. /**
  332. * If there is id provided for a button option, it will be in dialog.indexedButtons list.
  333. *
  334. * In that case you can use dialog.getButton(id) to find the button.
  335. *
  336. * @param {type} id
  337. * @returns {undefined}
  338. */
  339. getButton: function(id) {
  340. if (typeof this.indexedButtons[id] !== 'undefined') {
  341. return this.indexedButtons[id];
  342. }
  343. return null;
  344. },
  345. getButtonSize: function() {
  346. if (typeof BootstrapDialog.BUTTON_SIZES[this.getSize()] !== 'undefined') {
  347. return BootstrapDialog.BUTTON_SIZES[this.getSize()];
  348. }
  349. return '';
  350. },
  351. updateButtons: function() {
  352. if (this.isRealized()) {
  353. if (this.getButtons().length === 0) {
  354. this.getModalFooter().hide();
  355. } else {
  356. this.getModalFooter().find('.' + this.getNamespace('footer')).html('').append(this.createFooterButtons());
  357. }
  358. }
  359. return this;
  360. },
  361. isAutodestroy: function() {
  362. return this.options.autodestroy;
  363. },
  364. setAutodestroy: function(autodestroy) {
  365. this.options.autodestroy = autodestroy;
  366. },
  367. getDefaultText: function() {
  368. return BootstrapDialog.DEFAULT_TEXTS[this.getType()];
  369. },
  370. getNamespace: function(name) {
  371. return BootstrapDialog.NAMESPACE + '-' + name;
  372. },
  373. createHeaderContent: function() {
  374. var $container = $('<div></div>');
  375. $container.addClass(this.getNamespace('header'));
  376. // title
  377. $container.append(this.createTitleContent());
  378. // Close button
  379. $container.prepend(this.createCloseButton());
  380. return $container;
  381. },
  382. createTitleContent: function() {
  383. var $title = $('<div></div>');
  384. $title.addClass(this.getNamespace('title'));
  385. return $title;
  386. },
  387. createCloseButton: function() {
  388. var $container = $('<div></div>');
  389. $container.addClass(this.getNamespace('close-button'));
  390. var $icon = $('<button class="close">&times;</button>');
  391. $container.append($icon);
  392. $container.on('click', {dialog: this}, function(event) {
  393. event.data.dialog.close();
  394. });
  395. return $container;
  396. },
  397. createBodyContent: function() {
  398. var $container = $('<div></div>');
  399. $container.addClass(this.getNamespace('body'));
  400. // Message
  401. $container.append(this.createMessageContent());
  402. return $container;
  403. },
  404. createMessageContent: function() {
  405. var $message = $('<div></div>');
  406. $message.addClass(this.getNamespace('message'));
  407. return $message;
  408. },
  409. createFooterContent: function() {
  410. var $container = $('<div></div>');
  411. $container.addClass(this.getNamespace('footer'));
  412. return $container;
  413. },
  414. createFooterButtons: function() {
  415. var that = this;
  416. var $container = $('<div></div>');
  417. $container.addClass(this.getNamespace('footer-buttons'));
  418. this.indexedButtons = {};
  419. $.each(this.options.buttons, function(index, button) {
  420. if (!button.id) {
  421. button.id = BootstrapDialog.newGuid();
  422. }
  423. var $button = that.createButton(button);
  424. that.indexedButtons[button.id] = $button;
  425. $container.append($button);
  426. });
  427. return $container;
  428. },
  429. createButton: function(button) {
  430. var $button = $('<button class="btn"></button>');
  431. $button.addClass(this.getButtonSize());
  432. $button.prop('id', button.id);
  433. // Icon
  434. if (typeof button.icon !== 'undefined' && $.trim(button.icon) !== '') {
  435. $button.append(this.createButtonIcon(button.icon));
  436. }
  437. // Label
  438. if (typeof button.label !== 'undefined') {
  439. $button.append(button.label);
  440. }
  441. // Css class
  442. if (typeof button.cssClass !== 'undefined' && $.trim(button.cssClass) !== '') {
  443. $button.addClass(button.cssClass);
  444. } else {
  445. $button.addClass('btn-default');
  446. }
  447. // Hotkey
  448. if (typeof button.hotkey !== 'undefined') {
  449. this.registeredButtonHotkeys[button.hotkey] = $button;
  450. }
  451. // Button on click
  452. $button.on('click', {dialog: this, $button: $button, button: button}, function(event) {
  453. var dialog = event.data.dialog;
  454. var $button = event.data.$button;
  455. var button = event.data.button;
  456. if (typeof button.action === 'function') {
  457. button.action.call($button, dialog);
  458. }
  459. if (button.autospin) {
  460. $button.toggleSpin(true);
  461. }
  462. });
  463. // Dynamically add extra functions to $button
  464. this.enhanceButton($button);
  465. return $button;
  466. },
  467. /**
  468. * Dynamically add extra functions to $button
  469. *
  470. * Using '$this' to reference 'this' is just for better readability.
  471. *
  472. * @param {type} $button
  473. * @returns {_L13.BootstrapDialog.prototype}
  474. */
  475. enhanceButton: function($button) {
  476. $button.dialog = this;
  477. // Enable / Disable
  478. $button.toggleEnable = function(enable) {
  479. var $this = this;
  480. $this.prop("disabled", !enable).toggleClass('disabled', !enable);
  481. return $this;
  482. };
  483. $button.enable = function() {
  484. var $this = this;
  485. $this.toggleEnable(true);
  486. return $this;
  487. };
  488. $button.disable = function() {
  489. var $this = this;
  490. $this.toggleEnable(false);
  491. return $this;
  492. };
  493. // Icon spinning, helpful for indicating ajax loading status.
  494. $button.toggleSpin = function(spin) {
  495. var $this = this;
  496. var dialog = $this.dialog;
  497. var $icon = $this.find('.' + dialog.getNamespace('button-icon'));
  498. if (spin) {
  499. $icon.hide();
  500. $button.prepend(dialog.createButtonIcon(dialog.getSpinicon()).addClass('icon-spin'));
  501. } else {
  502. $icon.show();
  503. $button.find('.icon-spin').remove();
  504. }
  505. return $this;
  506. };
  507. $button.spin = function() {
  508. var $this = this;
  509. $this.toggleSpin(true);
  510. return $this;
  511. };
  512. $button.stopSpin = function() {
  513. var $this = this;
  514. $this.toggleSpin(false);
  515. return $this;
  516. };
  517. return this;
  518. },
  519. createButtonIcon: function(icon) {
  520. var $icon = $('<span></span>');
  521. $icon.addClass(this.getNamespace('button-icon')).addClass(icon);
  522. return $icon;
  523. },
  524. /**
  525. * Invoke this only after the dialog is realized.
  526. *
  527. * @param {type} enable
  528. * @returns {undefined}
  529. */
  530. enableButtons: function(enable) {
  531. $.each(this.indexedButtons, function(id, $button) {
  532. $button.toggleEnable(enable);
  533. });
  534. return this;
  535. },
  536. /**
  537. * Invoke this only after the dialog is realized.
  538. *
  539. * @returns {undefined}
  540. */
  541. updateClosable: function() {
  542. if (this.isRealized()) {
  543. // Close button
  544. this.getModalHeader().find('.' + this.getNamespace('close-button')).toggle(this.isClosable());
  545. }
  546. return this;
  547. },
  548. /**
  549. * Set handler for modal event 'show.bs.modal'.
  550. * This is a setter!
  551. */
  552. onShow: function(onshow) {
  553. this.options.onshow = onshow;
  554. return this;
  555. },
  556. /**
  557. * Set handler for modal event 'shown.bs.modal'.
  558. * This is a setter!
  559. */
  560. onShown: function(onshown) {
  561. this.options.onshown = onshown;
  562. return this;
  563. },
  564. /**
  565. * Set handler for modal event 'hide.bs.modal'.
  566. * This is a setter!
  567. */
  568. onHide: function(onhide) {
  569. this.options.onhide = onhide;
  570. return this;
  571. },
  572. /**
  573. * Set handler for modal event 'hidden.bs.modal'.
  574. * This is a setter!
  575. */
  576. onHidden: function(onhidden) {
  577. this.options.onhidden = onhidden;
  578. return this;
  579. },
  580. isRealized: function() {
  581. return this.realized;
  582. },
  583. setRealized: function(realized) {
  584. this.realized = realized;
  585. return this;
  586. },
  587. isOpened: function() {
  588. return this.opened;
  589. },
  590. setOpened: function(opened) {
  591. this.opened = opened;
  592. return this;
  593. },
  594. handleModalEvents: function() {
  595. this.getModal().on('show.bs.modal', {dialog: this}, function(event) {
  596. var dialog = event.data.dialog;
  597. dialog.showPageScrollBar(true);
  598. if (typeof dialog.options.onshow === 'function') {
  599. return dialog.options.onshow(dialog);
  600. }
  601. });
  602. this.getModal().on('shown.bs.modal', {dialog: this}, function(event) {
  603. var dialog = event.data.dialog;
  604. typeof dialog.options.onshown === 'function' && dialog.options.onshown(dialog);
  605. dialog.showPageScrollBar(true);
  606. });
  607. this.getModal().on('hide.bs.modal', {dialog: this}, function(event) {
  608. var dialog = event.data.dialog;
  609. if (typeof dialog.options.onhide === 'function') {
  610. return dialog.options.onhide(dialog);
  611. }
  612. });
  613. this.getModal().on('hidden.bs.modal', {dialog: this}, function(event) {
  614. var dialog = event.data.dialog;
  615. typeof dialog.options.onhidden === 'function' && dialog.options.onhidden(dialog);
  616. dialog.isAutodestroy() && $(this).remove();
  617. dialog.showPageScrollBar(false);
  618. });
  619. // Backdrop, I did't find a way to change bs3 backdrop option after the dialog is popped up, so here's a new wheel.
  620. this.getModal().on('click', {dialog: this}, function(event) {
  621. event.target === this && event.data.dialog.isClosable() && event.data.dialog.canCloseByBackdrop() && event.data.dialog.close();
  622. });
  623. // ESC key support
  624. this.getModal().on('keyup', {dialog: this}, function(event) {
  625. event.which === 27 && event.data.dialog.isClosable() && event.data.dialog.canCloseByKeyboard() && event.data.dialog.close();
  626. });
  627. // Button hotkey
  628. this.getModal().on('keyup', {dialog: this}, function(event) {
  629. var dialog = event.data.dialog;
  630. if (typeof dialog.registeredButtonHotkeys[event.which] !== 'undefined') {
  631. var $button = $(dialog.registeredButtonHotkeys[event.which]);
  632. !$button.prop('disabled') && $button.focus().trigger('click');
  633. }
  634. });
  635. return this;
  636. },
  637. makeModalDraggable: function() {
  638. if (this.options.draggable) {
  639. this.getModalHeader().addClass(this.getNamespace('draggable')).on('mousedown', {dialog: this}, function(event) {
  640. var dialog = event.data.dialog;
  641. dialog.draggableData.isMouseDown = true;
  642. var dialogOffset = dialog.getModalContent().offset();
  643. dialog.draggableData.mouseOffset = {
  644. top: event.clientY - dialogOffset.top,
  645. left: event.clientX - dialogOffset.left
  646. };
  647. });
  648. this.getModal().on('mouseup mouseleave', {dialog: this}, function(event) {
  649. event.data.dialog.draggableData.isMouseDown = false;
  650. });
  651. $('body').on('mousemove', {dialog: this}, function(event) {
  652. var dialog = event.data.dialog;
  653. if (!dialog.draggableData.isMouseDown) {
  654. return;
  655. }
  656. dialog.getModalContent().offset({
  657. top: event.clientY - dialog.draggableData.mouseOffset.top,
  658. left: event.clientX - dialog.draggableData.mouseOffset.left
  659. });
  660. });
  661. }
  662. return this;
  663. },
  664. showPageScrollBar: function(show) {
  665. $(document.body).toggleClass('modal-open', show);
  666. },
  667. /**
  668. * To make multiple opened dialogs look better.
  669. */
  670. updateZIndex: function() {
  671. var dialogCount = Object.keys(BootstrapDialog.dialogs).length;
  672. if (dialogCount > 1) {
  673. var $modal = this.getModal();
  674. var $backdrop = $modal.data('bs.modal').$backdrop;
  675. $modal.css('z-index', BootstrapDialog.ZINDEX_MODAL + (dialogCount - 1) * 20);
  676. $backdrop.css('z-index', BootstrapDialog.ZINDEX_BACKDROP + (dialogCount - 1) * 20);
  677. }
  678. return this;
  679. },
  680. realize: function() {
  681. this.initModalStuff();
  682. this.getModal().addClass(BootstrapDialog.NAMESPACE)
  683. .addClass(this.getType())
  684. .addClass(this.getSize())
  685. .addClass(this.getCssClass());
  686. this.getModalFooter().append(this.createFooterContent());
  687. this.getModalHeader().append(this.createHeaderContent());
  688. this.getModalBody().append(this.createBodyContent());
  689. this.getModal().modal({
  690. backdrop: 'static',
  691. keyboard: false,
  692. show: false
  693. });
  694. this.makeModalDraggable();
  695. this.handleModalEvents();
  696. this.setRealized(true);
  697. this.updateButtons();
  698. this.updateTitle();
  699. this.updateMessage();
  700. this.updateClosable();
  701. return this;
  702. },
  703. open: function() {
  704. !this.isRealized() && this.realize();
  705. this.getModal().modal('show');
  706. this.updateZIndex();
  707. this.setOpened(true);
  708. return this;
  709. },
  710. close: function() {
  711. this.getModal().modal('hide');
  712. if (this.isAutodestroy()) {
  713. delete BootstrapDialog.dialogs[this.getId()];
  714. }
  715. this.setOpened(false);
  716. return this;
  717. }
  718. };
  719. /**
  720. * RFC4122 version 4 compliant unique id creator.
  721. *
  722. * Added by https://github.com/tufanbarisyildirim/
  723. *
  724. * @returns {String}
  725. */
  726. BootstrapDialog.newGuid = function() {
  727. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  728. var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
  729. return v.toString(16);
  730. });
  731. };
  732. /* ================================================
  733. * For lazy people
  734. * ================================================ */
  735. /**
  736. * Shortcut function: show
  737. *
  738. * @param {type} options
  739. * @returns the created dialog instance
  740. */
  741. BootstrapDialog.show = function(options) {
  742. return new BootstrapDialog(options).open();
  743. };
  744. /**
  745. * Alert window
  746. *
  747. * @returns the created dialog instance
  748. */
  749. BootstrapDialog.alert = function() {
  750. var options = {};
  751. var defaultOptions = {
  752. type: BootstrapDialog.TYPE_PRIMARY,
  753. title: null,
  754. message: null,
  755. closable: true,
  756. buttonLabel: 'OK',
  757. callback: null
  758. };
  759. if (typeof arguments[0] === 'object' && arguments[0].constructor === {}.constructor) {
  760. options = $.extend(true, defaultOptions, arguments[0]);
  761. } else {
  762. options = $.extend(true, defaultOptions, {
  763. message: arguments[0],
  764. closable: false,
  765. buttonLabel: 'OK',
  766. callback: typeof arguments[1] !== 'undefined' ? arguments[1] : null
  767. });
  768. }
  769. return new BootstrapDialog({
  770. type: options.type,
  771. title: options.title,
  772. message: options.message,
  773. closable: options.closable,
  774. data: {
  775. callback: options.callback
  776. },
  777. onhide: function(dialog) {
  778. !dialog.getData('btnClicked') && dialog.isClosable() && typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(false);
  779. },
  780. buttons: [{
  781. label: options.buttonLabel,
  782. action: function(dialog) {
  783. dialog.setData('btnClicked', true);
  784. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
  785. dialog.close();
  786. }
  787. }]
  788. }).open();
  789. };
  790. /**
  791. * Confirm window
  792. *
  793. * @param {type} message
  794. * @param {type} callback
  795. * @returns the created dialog instance
  796. */
  797. BootstrapDialog.confirm = function(message, callback) {
  798. return new BootstrapDialog({
  799. title: 'Confirmation',
  800. message: message,
  801. closable: false,
  802. data: {
  803. 'callback': callback
  804. },
  805. buttons: [{
  806. label: 'Cancel',
  807. action: function(dialog) {
  808. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(false);
  809. dialog.close();
  810. }
  811. }, {
  812. label: 'OK',
  813. cssClass: 'btn-primary',
  814. action: function(dialog) {
  815. typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
  816. dialog.close();
  817. }
  818. }]
  819. }).open();
  820. };
  821. return BootstrapDialog;
  822. }));