base.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * 字符串全部替换
  3. * @param s1
  4. * @param s2
  5. * @returns
  6. */
  7. String.prototype.replaceAll = function(s1,s2){
  8. return this.replace(new RegExp(s1,"gm"),s2);
  9. }
  10. /**
  11. * 判断是否以特定字符串结尾
  12. * @param suffix
  13. * @returns {Boolean}
  14. */
  15. String.prototype.endsWith = function(suffix) {
  16. return this.indexOf(suffix, this.length - suffix.length) !== -1;
  17. };
  18. //ie支持Array.prototype.includes()
  19. if (!Array.prototype.includes) {
  20. Object.defineProperty(Array.prototype, 'includes', {
  21. value: function(searchElement, fromIndex) {
  22. // 1. Let O be ? ToObject(this value).
  23. if (this == null) {
  24. throw new TypeError('"this" is null or not defined');
  25. }
  26. var o = Object(this);
  27. // 2. Let len be ? ToLength(? Get(O, "length")).
  28. var len = o.length >>> 0;
  29. // 3. If len is 0, return false.
  30. if (len === 0) {
  31. return false;
  32. }
  33. // 4. Let n be ? ToInteger(fromIndex).
  34. // (If fromIndex is undefined, this step produces the value 0.)
  35. var n = fromIndex | 0;
  36. // 5. If n ≥ 0, then
  37. // a. Let k be n.
  38. // 6. Else n < 0,
  39. // a. Let k be len + n.
  40. // b. If k < 0, let k be 0.
  41. var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
  42. // 7. Repeat, while k < len
  43. while (k < len) {
  44. // a. Let elementK be the result of ? Get(O, ! ToString(k)).
  45. // b. If SameValueZero(searchElement, elementK) is true, return true.
  46. // c. Increase k by 1.
  47. // NOTE: === provides the correct "SameValueZero" comparison needed here.
  48. if (o[k] === searchElement) {
  49. return true;
  50. }
  51. k++;
  52. }
  53. // 8. Return false
  54. return false;
  55. }
  56. });
  57. }
  58. /**
  59. * 获取上下文路径
  60. * @returns
  61. */
  62. function getContextPath() {
  63. var pathName = document.location.pathname;
  64. var index = pathName.substr(1).indexOf("/");
  65. var result = pathName.substr(0,index+1);
  66. result=result=="/myconsole"?"":result;
  67. return result;
  68. }
  69. /**
  70. * 隐藏jgrid的编辑对话框中的行并清空数据
  71. *
  72. * @param inputid
  73. */
  74. function hide(inputid) {
  75. $("#tr_" + inputid).hide();
  76. $("#" + inputid).val("");
  77. }
  78. /**
  79. * 显示jgrid的编辑对话框中的行
  80. *
  81. * @param inputid
  82. */
  83. function show(inputid) {
  84. $("#tr_" + inputid).show();
  85. }
  86. /**
  87. * 行内显示图片,需要在colModel中设置formatter: alarmFormatter
  88. *
  89. * @param cellvalue
  90. * @param options
  91. * @param rowdata
  92. * @returns {String}
  93. */
  94. function alarmFormatter(cellvalue, options, rowdata) {
  95. return ' <img src="data:image/png;base64,' + cellvalue + '" class="bigimg" onclick=show("' + cellvalue + '") id="img'
  96. + rowdata.Id + '" style="height:35px;"/>';
  97. }
  98. /**
  99. * 修改JSON中key的方法
  100. */
  101. function modifyJosnKey(json, oddkey, newkey) {
  102. var val = json[oddkey];
  103. delete json[oddkey];
  104. json[newkey] = val;
  105. }
  106. /**
  107. * 给jgrid添加Jfinal中getModel方法中必需的model
  108. *
  109. * @param postdata
  110. * @param modelName
  111. * @returns
  112. */
  113. function addModel(modelName, postdata) {
  114. $.each(postdata, function(index, value) {
  115. // console.log(index);
  116. if (index != "jqGrid_id" && index != "oper" && index != "id") {
  117. modifyJosnKey(postdata, index, modelName + "." + index);
  118. }
  119. if (index == "id") {
  120. postdata[modelName + "." + index] = value
  121. }
  122. // console.log(index);
  123. })
  124. return postdata;
  125. }
  126. /*--------------以下为表单中的tree展示-------------------*/
  127. function makeTree(inputid,actionURL,type){
  128. var menuContentID=inputid+"_menuContent";
  129. var treeDemoID=inputid+"_tree";
  130. //myconsole.log(type);
  131. var check={enable: true,chkStyle: "radio",radioType: "all"};
  132. if(type=="checkbox"){
  133. check={enable: true,chkboxType: {"Y":"", "N":""}};
  134. }
  135. var hideMenu=function () {
  136. $("#"+menuContentID).fadeOut("fast");
  137. $("body").unbind("mousedown", onBodyDown);
  138. }
  139. var onBodyDown=function (event) {
  140. if (!(event.target.id == "menuBtn" || event.target.id == inputid
  141. || event.target.id == menuContentID || $(event.target).parents(
  142. "#"+menuContentID).length > 0)) {
  143. hideMenu();
  144. }
  145. }
  146. var onClick=function (e, treeID, treeNode) {
  147. var zTree = $.fn.zTree.getZTreeObj(treeID);
  148. zTree.checkNode(treeNode, !treeNode.checked, null, true);
  149. return false;
  150. }
  151. var onCheck=function (e, treeID, treeNode) {
  152. var zTree = $.fn.zTree.getZTreeObj(treeID),
  153. nodes = zTree.getCheckedNodes(true),
  154. v = "",// 显示值
  155. rv = "";// 真正值
  156. // console.log(nodes);
  157. for (var i = 0, l = nodes.length; i < l; i++) {
  158. rv += nodes[i].id + ",";
  159. v += nodes[i].name + ",";
  160. }
  161. if (v.length > 0)
  162. v = v.substring(0, v.length - 1);
  163. if (rv.length > 0)
  164. rv = rv.substring(0, rv.length - 1);
  165. //myconsole.log(inputid+"---"+rv+"----"+v);
  166. //myconsole.log(inputid+"---"+rv+"----"+v);
  167. $("#" + inputid).val(rv);
  168. $("#" + inputid).attr("offval",rv);
  169. // $("#jqGrid").jqGrid("setRowData", inputid, rv+"");
  170. $("#_" + inputid).val(v);
  171. }
  172. $.get(getContextPath()+actionURL,{"parentid":$("#"+inputid).val()},function(data){
  173. $("#_"+inputid).val(data);
  174. })
  175. var value=$("#"+inputid).val();
  176. var upbtn=$('<input class="FormElement ui-widget-content ui-corner-all" role="textbox" type="text" id="_'+inputid +'" value="'+value+'"/>');
  177. //myconsole.log($("#"+inputid));
  178. $("#"+inputid).parent().append(upbtn);
  179. $("#"+inputid).hide();
  180. var setting = {
  181. check: check,
  182. view: {
  183. dblClickExpand: false
  184. },
  185. data: {
  186. simpleData: {
  187. enable: true,
  188. idKey: "id",
  189. pIdKey: "parentid",
  190. rootPId: "0"
  191. }
  192. },
  193. callback: {
  194. onClick: onClick,
  195. onCheck: onCheck
  196. }
  197. };
  198. $("#_"+inputid).click(function(){
  199. var id = $("#"+inputid).val();
  200. $.get(getContextPath()+actionURL,{id:$("#"+inputid).attr("rowid")},function(data){
  201. zNodes=data;
  202. $("#"+menuContentID).remove();
  203. var menu='<div id="'+menuContentID+'" class="menuContent" style="display:none; position: absolute;"><ul id="'+treeDemoID+'" class="ztree" style="margin-top:0; width:250px; height: 180px;"></ul></div>';
  204. $("#"+inputid).parent().append(menu);
  205. var tree=$.fn.zTree.init($("#"+treeDemoID), setting, zNodes);
  206. // tree.expandAll(false);
  207. var cityObj = $("#_"+inputid);
  208. var cityOffset = $("#_"+inputid).position();
  209. $("#"+menuContentID).css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
  210. $("body").bind("mousedown", onBodyDown);
  211. var nodes = tree.getNodesByParam("id", id, null);
  212. tree.checkNode(nodes[0], true, true);
  213. });
  214. })
  215. }
  216. /*--------------以上为表单中的tree展示-------------------*/
  217. /**
  218. * iframe高度自适应
  219. */
  220. function dyniframesize(down) {
  221. var pTar = null;
  222. if (document.getElementById) {
  223. pTar = document.getElementById(down);
  224. } else {
  225. eval('pTar = ' + down + ';');
  226. }
  227. if (pTar && !window.opera) {
  228. // begin resizing iframe
  229. pTar.style.display = "block"
  230. if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight) {
  231. // ns6 syntax
  232. pTar.height = pTar.contentDocument.body.offsetHeight + 20;
  233. pTar.width = pTar.contentDocument.body.scrollWidth + 20;
  234. } else if (pTar.Document && pTar.Document.body.scrollHeight) {
  235. // ie5+ syntax
  236. pTar.height = pTar.Document.body.scrollHeight;
  237. pTar.width = pTar.Document.body.scrollWidth;
  238. }
  239. }
  240. }
  241. function design(templateFile){
  242. window.open(templateFile);
  243. }
  244. function fullOpen(winurl) {
  245. objWin__ = window.open( winurl,"","top=0,left=0,scrollbars=yes,width="+(screen.availWidth-200)+",height="+(screen.availHeight-30));
  246. objWin__ .focus();
  247. }
  248. /*$(function(){
  249. jQuery.fn.center = function () {
  250. this.css("position","absolute");
  251. var amend=80;
  252. if(this.height()>$(window).height()){
  253. amend=300
  254. }
  255. //console.log($(window).height()+"_"+this.height()+"_"+$(window).scrollTop());
  256. this.css("top", ( $(window).height() - this.height()+amend) / 2+$(window).scrollTop() + "px");
  257. this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
  258. return this;
  259. }
  260. $('body').on('DOMNodeInserted', '.ui-jqdialog', function () {
  261. $(this).center();
  262. });
  263. })*/