bootstrap-dialog.js 42 KB

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