msg_box.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. (function($){
  10. // setting = {
  11. // id: 'calc-type',
  12. // title: '提示',
  13. // message: '切换计量模式,已计量数据会被清空。',
  14. // ok: {
  15. // caption: '确定',
  16. // //callback
  17. // },
  18. // cancel: {
  19. // caption: '取消',
  20. // //callback
  21. // }
  22. // };
  23. $.msgBox = function(setting) {
  24. const html = [];
  25. html.push('<div class="modal fade" data-backdrop="static" id="' + setting.id + '">');
  26. html.push('<div class="modal-dialog " role="document" >');
  27. html.push('<div class="modal-content">');
  28. html.push('<div class="modal-header">', '<h5 class="modal-title">', setting.title, '</h5>','</div>');
  29. html.push('<div class="modal-body">', '<h5>', setting.message, '</h5>', '</div>');
  30. html.push('<div class="modal-footer">');
  31. if (setting.cancel) {
  32. html.push('<button type="button" class="btn btn-secondary btn-sm" id="' + setting.id + '-cancel">', setting.cancel.caption, '</button>');
  33. }
  34. html.push('<button type="button" class="btn btn-primary btn-sm" id="' + setting.id + '-ok">', setting.ok.caption, '</button>');
  35. html.push('</div>');
  36. html.push('</div>');
  37. html.push('</div>');
  38. html.push('</div>');
  39. $('body').append(html.join(''));
  40. const obj = $('#' + setting.id);
  41. if (!obj) return;
  42. const btnOk = $('#' + setting.id + '-ok'), btnCancel = $('#' + setting.id + '-cancel');
  43. if (btnOk) {
  44. btnOk.on('click', function () {
  45. if (setting.ok.callback) {
  46. setting.ok.callback();
  47. }
  48. obj.modal('hide');
  49. });
  50. }
  51. if (btnCancel) {
  52. btnCancel.on('click', function () {
  53. if (setting.cancel.callback) {
  54. setting.cancel.callback();
  55. }
  56. obj.modal('hide');
  57. });
  58. }
  59. obj.on('hidden.bs.modal', function () {
  60. setTimeout(() => { obj.remove(); }, 1000);
  61. });
  62. obj.modal('show');
  63. }
  64. })(jQuery);