bootstrap-dialog.js 45 KB

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