bootstrap-dialog.js 42 KB

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