ewin.confirm.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. (function ($) {
  2. window.Ewin = function () {
  3. var html = '<div id="[Id]" class="modal fade" role="dialog" style="display:flex" aria-labelledby="modalLabel">' +
  4. '<div class="modal-dialog modal-sm" style="margin:auto;">' +
  5. '<div class="modal-content">' +
  6. '<div class="modal-header">' +
  7. '<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>' +
  8. '<h4 class="modal-title" id="modalLabel">[Title]</h4>' +
  9. '</div>' +
  10. '<div class="modal-body">' +
  11. '<p>[Message]</p>' +
  12. '</div>' +
  13. '<div class="modal-footer">' +
  14. '<button type="button" class="btn btn-default cancel" data-dismiss="modal">[BtnCancel]</button>' +
  15. '<button type="button" class="btn btn-primary ok" data-dismiss="modal">[BtnOk]</button>' +
  16. '</div>' +
  17. '</div>' +
  18. '</div>' +
  19. '</div>';
  20. var dialogdHtml = '<div id="[Id]" class="modal fade" role="dialog" aria-labelledby="modalLabel">' +
  21. '<div class="modal-dialog">' +
  22. '<div class="modal-content">' +
  23. '<div class="modal-header">' +
  24. '<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>' +
  25. '<h4 class="modal-title" id="modalLabel">[Title]</h4>' +
  26. '</div>' +
  27. '<div class="modal-body">' +
  28. '</div>' +
  29. '</div>' +
  30. '</div>' +
  31. '</div>';
  32. var reg = new RegExp("\\[([^\\[\\]]*?)\\]", 'igm');
  33. var generateId = function () {
  34. var date = new Date();
  35. return 'mdl' + date.valueOf();
  36. }
  37. var init = function (options) {
  38. options = $.extend({}, {
  39. title: "操作提示",
  40. message: "提示内容",
  41. btnok: "确定",
  42. btncl: "取消",
  43. width: 200,
  44. auto: false
  45. }, options || {});
  46. var modalId = generateId();
  47. var content = html.replace(reg, function (node, key) {
  48. return {
  49. Id: modalId,
  50. Title: options.title,
  51. Message: options.message,
  52. BtnOk: options.btnok,
  53. BtnCancel: options.btncl
  54. }[key];
  55. });
  56. $('body').append(content);
  57. $('#' + modalId).modal({
  58. width: options.width,
  59. backdrop: 'static'
  60. });
  61. $('#' + modalId).on('hide.bs.modal', function (e) {
  62. $('body').find('#' + modalId).remove();
  63. });
  64. return modalId;
  65. }
  66. return {
  67. alert: function (options) {
  68. if (typeof options == 'string') {
  69. options = {
  70. message: options
  71. };
  72. }
  73. var id = init(options);
  74. var modal = $('#' + id);
  75. modal.find('.ok').removeClass('btn-success').addClass('btn-primary');
  76. modal.find('.cancel').hide();
  77. return {
  78. id: id,
  79. on: function (callback) {
  80. if (callback && callback instanceof Function) {
  81. modal.find('.ok').click(function () { callback(true); });
  82. }
  83. },
  84. hide: function (callback) {
  85. if (callback && callback instanceof Function) {
  86. modal.on('hide.bs.modal', function (e) {
  87. callback(e);
  88. });
  89. }
  90. }
  91. };
  92. },
  93. confirm: function (options) {
  94. var id = init(options);
  95. var modal = $('#' + id);
  96. modal.find('.ok').removeClass('btn-primary').addClass('btn-success');
  97. modal.find('.cancel').show();
  98. return {
  99. id: id,
  100. on: function (callback) {
  101. if (callback && callback instanceof Function) {
  102. modal.find('.ok').click(function () { callback(true); });
  103. modal.find('.cancel').click(function () { callback(false); });
  104. }
  105. },
  106. hide: function (callback) {
  107. if (callback && callback instanceof Function) {
  108. modal.on('hide.bs.modal', function (e) {
  109. callback(e);
  110. });
  111. }
  112. }
  113. };
  114. },
  115. dialog: function (options) {
  116. options = $.extend({}, {
  117. title: 'title',
  118. url: '',
  119. width: 800,
  120. height: 550,
  121. onReady: function () { },
  122. onShown: function (e) { }
  123. }, options || {});
  124. var modalId = generateId();
  125. var content = dialogdHtml.replace(reg, function (node, key) {
  126. return {
  127. Id: modalId,
  128. Title: options.title
  129. }[key];
  130. });
  131. $('body').append(content);
  132. var target = $('#' + modalId);
  133. target.find('.modal-body').load(options.url);
  134. if (options.onReady())
  135. options.onReady.call(target);
  136. target.modal();
  137. target.on('shown.bs.modal', function (e) {
  138. if (options.onReady(e))
  139. options.onReady.call(target, e);
  140. });
  141. target.on('hide.bs.modal', function (e) {
  142. $('body').find(target).remove();
  143. });
  144. }
  145. }
  146. }();
  147. })(jQuery);