common.js 4.5 KB

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