widgets.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. var widgets = {
  2. loader:{
  3. //显示loading
  4. showLoader:null,
  5. //设置loading文本,默认文本为'loading'
  6. setLoaderText:null,
  7. //设置loader显示相关属性
  8. setAttributes:null,
  9. //移除掉loader
  10. removeLoader:null
  11. },
  12. alert:{
  13. //弹出自定义弹框
  14. showAlert: null,
  15. //clear alert
  16. clearAlert: null
  17. }
  18. };
  19. (function (widgets, $) {
  20. var alertDiv;
  21. //弹出自定义提示框
  22. // msg:提示语;
  23. // state:提示框颜色(true=success;false=danger);
  24. // width:提示框宽度;opacity:提示框透明度[0,1];
  25. // withBorder:提示框是否加边框;
  26. function showAlert(msg, state, width, withBorder, opacity) {
  27. //提示框颜色
  28. var className = "alert-", border, alpha;
  29. className += state ? "success" : "danger";
  30. //提示框宽度
  31. if (width === null || typeof (width) === 'undefined') {
  32. //默认值300
  33. width = 300;
  34. }
  35. //是否开启边框
  36. border = withBorder ? {"border": "1px solid"} : "none";
  37. //设置透明度[0,1]之间
  38. alpha = opacity;
  39. if (alertDiv) {
  40. $(alertDiv).remove();
  41. }
  42. if (!$('#msg_container')[0]) {
  43. alertDiv = $(`<div class='alert alert-dismissible' id='msg_container' role='alert'
  44. style='z-index:999999;position: absolute;top: 20px;left: 40%; display: none;text-align: center'><button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true' >&times;</span></button><strong><p id='msg' style='word-wrap: break-word'></p></strong></div>`);
  45. $('body').append(alertDiv)
  46. }
  47. $('#msg_container').css('width', width + 'px');
  48. $('#msg_container').css(border);
  49. $('#msg_container').css('opacity', alpha);
  50. $('#msg_container').addClass(className);
  51. $('#msg_container').slideDown(300);
  52. $('#msg').html(msg);
  53. }
  54. //清除提示框
  55. function clearAlert() {
  56. $('#msg_container').hide();
  57. }
  58. /*显示loading*/
  59. function showLoader(text, type, attributes) {
  60. if (!type) {
  61. type = "loader-default";
  62. attributes = attributes || {'data-half': true}
  63. }
  64. var $body = document.getElementsByTagName('body');
  65. $body = $body && $body[0];
  66. if ($body) {
  67. var $loader = document.getElementsByClassName('loader')[0];
  68. if (!$loader) {
  69. $loader = createLoader(type);
  70. }
  71. $loader.classList.add('is-active');
  72. setLoaderText(text);
  73. setAttributes(attributes);
  74. }
  75. }
  76. /*设置loading文本,默认文本为"loading"*/
  77. function setLoaderText(text, textAttributeField) {
  78. if (text != null) {
  79. var txtAttrField = textAttributeField != null ? textAttributeField : "data-text";
  80. var attributes = {};
  81. attributes[txtAttrField] = text;
  82. setAttributes(attributes);
  83. }
  84. }
  85. /*设置loader显示相关属性*/
  86. function setAttributes(attributes) {
  87. var $loader = document.getElementsByClassName('loader')[0];
  88. if ($loader && attributes) {
  89. for (var attr in attributes) {
  90. $loader.setAttribute(attr, attributes[attr]);
  91. }
  92. }
  93. }
  94. /*移除loader*/
  95. function removeLoader() {
  96. var $loader = document.getElementsByClassName('loader')[0];
  97. if ($loader) {
  98. $loader.parentNode.removeChild($loader);
  99. }
  100. }
  101. function createLoader(className) {
  102. var $loader, $body = document.getElementsByTagName('body');
  103. $body = $body && $body[0];
  104. if ($body) {
  105. $loader = document.createElement('div');
  106. $loader.className = "loader " + className;
  107. $body.insertBefore($loader, $body.children[0]);
  108. var style = document.createElement('style');
  109. style.type = 'text/css';
  110. style.innerHTML = ".loader.is-active{" +
  111. "background-color: rgba(0, 0, 0, 0.4) !important;" +
  112. "}";
  113. if (document.getElementsByTagName('head')) {
  114. document.getElementsByTagName('head')[0].appendChild(style);
  115. }
  116. }
  117. return $loader;
  118. }
  119. widgets.alert.showAlert = showAlert;
  120. widgets.alert.clearAlert = clearAlert;
  121. widgets.loader.showLoader = showLoader;
  122. widgets.loader.setLoaderText = setLoaderText;
  123. widgets.loader.setAttributes=setAttributes;
  124. widgets.loader.removeLoader = removeLoader;
  125. })(widgets, window.jQuery);