common.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // 获取项目地址
  2. var $location = (window.location + '').split('/');
  3. var $path = document.location.pathname;
  4. var $basePath = $location[0] + '//' + $location[2] + $path.substr(0, $path.substr(1).indexOf("/")+1);
  5. // 全局内容区域
  6. var $initContainer = $("#initContainer");
  7. $initContainer.css("min-height", $(window).height() - 125);
  8. // 监控定时器
  9. var timer;
  10. // ******************* 插件封装 ***************************
  11. // 全局提示框
  12. function bootGrowl(data, type) {
  13. $.bootstrapGrowl(data, { // data为提示信息
  14. type: type == undefined ? 'success' : type,// type指提示类型
  15. delay: 3000,// 提示框显示时间
  16. allow_dismiss: true // 显示取消提示框
  17. });
  18. }
  19. // 跳转主页
  20. function backIndexPage() {
  21. // 加载页面
  22. doLoader("/index?refresh=" + new Date().getTime());
  23. }
  24. // 美化SQL
  25. function beautifySql(){
  26. var $sql = $("#sql");
  27. var $tmp = $sql.attr('tmp');
  28. if(null == $tmp){
  29. $sql.attr('tmp', $sql.val());
  30. $sql.val(sqlFormatter.format($sql.val()));
  31. return;
  32. }
  33. $sql.val($tmp);
  34. $sql.removeAttr('tmp');
  35. }
  36. // 初始化select组件,默认选中
  37. function initSelectIndex($select, $selectedIndex){
  38. initSelect($select);
  39. if($selectedIndex < 0){
  40. return;
  41. }
  42. $.each($select, function () {
  43. var v = $(this).selectpicker('val');
  44. if (undefined == v || '' == v) {
  45. var $option = $(this).find("option")[$selectedIndex];
  46. if(undefined != $option){
  47. $(this).selectpicker('val', $option.value);
  48. }
  49. }
  50. });
  51. }
  52. function initSelect($select){
  53. $select.selectpicker({
  54. "style":'dbsyncer_btn-info',
  55. "title":"请选择",
  56. "actionsBox":true,
  57. "liveSearch":true,
  58. "selectAllText":"全选",
  59. "deselectAllText":"取消全选",
  60. "noneResultsText":"没有找到 {0}",
  61. "selectedTextFormat":"count > 10"
  62. });
  63. }
  64. // ******************* 扩展JS表单方法 ***************************
  65. $.fn.serializeJson = function () {
  66. var o = {};
  67. var a = this.serializeArray();
  68. $.each(a, function () {
  69. if (o[this.name] !== undefined) {
  70. if (!o[this.name].push) {
  71. o[this.name] = [o[this.name]];
  72. }
  73. o[this.name].push(this.value || '');
  74. } else {
  75. o[this.name] = this.value || '';
  76. }
  77. });
  78. return o;
  79. };
  80. // 全局加载页面
  81. function doLoader(url){
  82. // 加载页面
  83. $initContainer.load($basePath + url, function (response, status, xhr) {
  84. if (status != 'success') {
  85. bootGrowl(response);
  86. }
  87. $.loadingT(false);
  88. });
  89. }
  90. // 异常请求
  91. function doRequest(action, data){
  92. $.loadingT(false);
  93. // 异常请求:302
  94. if (!(data instanceof Object)) {
  95. bootGrowl("会话过期, 3秒后将访问登录主页...", "danger");
  96. setTimeout(function () {
  97. location.href = $basePath;
  98. }, 3000);
  99. } else {
  100. action(data);
  101. }
  102. }
  103. // 异常响应
  104. function doErrorResponse(xhr, status, info) {
  105. $.loadingT(false);
  106. bootGrowl("访问异常,请刷新或重试.", "danger");
  107. }
  108. // 全局Ajax post
  109. function doPoster(url, params, action) {
  110. $.loadingT(true);
  111. $.post($basePath + url, params, function (data) {
  112. doRequest(action, data);
  113. }).error(function (xhr, status, info) {
  114. doErrorResponse(xhr, status, info);
  115. });
  116. }
  117. // 全局Ajax get
  118. function doGetter(url, params, action, loading) {
  119. if(loading == undefined || loading == true){
  120. $.loadingT(true);
  121. }
  122. $.get($basePath + url, params, function (data) {
  123. doRequest(action, data);
  124. }).error(function (xhr, status, info) {
  125. doErrorResponse(xhr, status, info);
  126. });
  127. }
  128. // 全局Ajax get, 不显示加载动画
  129. function doGetWithoutLoading(url, params, action) {
  130. doGetter(url, params, action, false);
  131. }
  132. /**
  133. * 判断字符串是否为空串
  134. * @eg undefined true
  135. * @eg null true
  136. * @eg '' true
  137. * @eg ' ' true
  138. * @eg '1' false
  139. * @return Boolean
  140. */
  141. function isBlank(str) {
  142. return str === undefined || str === null || str === false || str.length === 0;
  143. }
  144. /**
  145. * 按照指定分隔符切分字符串
  146. *
  147. * @param str 带切分字符
  148. * @param delimiter 分隔符
  149. * @return Array
  150. */
  151. function splitStrByDelimiter(str, delimiter) {
  152. return isBlank(str) ? [] : str.split(delimiter);
  153. }