common.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /**
  2. * 包含通用公共方法的脚本,c为common的缩写
  3. */
  4. if (!window.wd)
  5. window.wd = {};
  6. wd.c = {};
  7. /*
  8. * wd.c.oriClose = window.close;//原版的window.close方法
  9. *
  10. * wd.c.closeWindow = function() {//在IE7中,无提示关闭窗口的方法 top.oriOpener =
  11. * top.opener;//保存原来的window.opener top.opener = null; top.open('', '_self');
  12. * wd.c.oriClose(); }
  13. *
  14. * window.close=wd.c.closeWindow;//覆盖原版的window.close方法
  15. */
  16. /** 通过NAME或ID获得页面元素的通用方法 * */
  17. wd.c.g = function (nameOrId, // 要获得的DOM元素的name或ID
  18. windowObj) { // 获得的DOM元素所属的window对象(可选参数,不填则默认为window)
  19. if (!windowObj)
  20. windowObj = window;
  21. var rs = windowObj.document.getElementById(nameOrId);
  22. if (!rs) // 如果上面获取不到对象,则采用下面的方法取得对象实例
  23. rs = windowObj.document.getElementsByName(nameOrId)[0];
  24. if (!rs) // 如果上面获取不到对象,则采用下面的方法取得对象实例
  25. rs = windowObj.frames[nameOrId];
  26. return rs;
  27. }
  28. /** 打印调信息的方法 * */
  29. wd.c.debug = function (s) {
  30. // 如果显示debug的窗口不存在,则初始化一个
  31. if (!window.debugWindow || window.debugWindow.closed) {
  32. window.debugWindow = window.open();
  33. // 如果本窗口关闭则debug窗口也关闭
  34. window.onbeforeunload = function () {
  35. window.debugWindow.close();
  36. };
  37. }
  38. window.debugWindow.document.write(s);
  39. window.debugWindow.document.write('<br/>');
  40. }
  41. /**
  42. * 为DOM元素targetElem的evenName事件追加事件触发时回调函数activeFun
  43. * 通过此函数为DOM元素绑定监听事件的好处在于:可以把新添加的监听函数与DOM元素
  44. * 已绑定的旧的监听函数合并成为新的监听函数,而不会覆盖DOM元素旧的同名监听函数。
  45. */
  46. wd.c.addEventListener = function (targetElem, // 需绑定监听事件的DOM元素
  47. eventName, // 事件名称,如:onclick
  48. activeFun) { // 事件触发的回调函数
  49. // DOM元素原来的监听事件
  50. var oriListener = targetElem[eventName];
  51. if (oriListener) { // 如果原来已经存在同名的监听事件了
  52. targetElem[eventName] = function () {
  53. oriListener.call(targetElem); // 调用原来已经存在监听方法
  54. return activeFun.call(targetElem); // 调用新添加的监听方法
  55. }
  56. } else {
  57. targetElem[eventName] = function () {
  58. return activeFun.call(targetElem); // 调用新添加的监听方法
  59. }
  60. }
  61. }
  62. // 把json转化为字符串格式
  63. wd.c.O2String = function (O) {
  64. // return JSON.stringify(jsonobj);
  65. var S = [];
  66. var J = "";
  67. if (Object.prototype.toString.apply(O) === "[object Array]") {
  68. for (var i = 0; i < O.length; i++) {
  69. S.push(O2String(O[i]));
  70. }
  71. J = "[" + S.join(",") + "]";
  72. } else {
  73. if (Object.prototype.toString.apply(O) === "[object Date]") {
  74. J = "new Date(" + O.getTime() + ")";
  75. } else {
  76. if (Object.prototype.toString.apply(O) === "[object RegExp]" || Object.prototype.toString.apply(O) === "[object Function]") {
  77. J = O.toString();
  78. } else {
  79. if (Object.prototype.toString.apply(O) === "[object Object]") {
  80. for (var i in O) {
  81. O[i] = typeof (O[i]) == "string" ? "\"" + O[i] + "\"" : (typeof (O[i]) === "object" ? O2String(O[i]) : O[i]);
  82. S.push(i + ":" + O[i]);
  83. }
  84. J = "{" + S.join(",") + "}";
  85. }
  86. }
  87. }
  88. }
  89. return J;
  90. };
  91. // 获得传入的对象的e的坐标、宽高,保存到一个对象中并返回
  92. wd.c.getAbsPoint = function (e) {
  93. try {
  94. var x = e.offsetLeft;
  95. var y = e.offsetTop;
  96. var w = e.offsetWidth;
  97. var h = e.offsetHeight;
  98. /**
  99. * OffsetParent为从中计算偏移量的元素。 如果某个元素的父级或此元素层次结构中的其他元素使用相对或绝对定位, 则
  100. * OffsetParent 将成为当前元素嵌套到的第一个相对或绝对定位元素。如果当前元素以上的任何元素都不是绝对 或相对定位的,则
  101. * OffsetParent 将是文档的 BODY 标记。
  102. *
  103. * obj.offsetTop 指 obj 距离上方或上层控件的位置,整型,单位像素。 obj.offsetLeft 指 obj
  104. * 距离左方或上层控件的位置,整型,单位像素。
  105. *
  106. * offsetTop与offsetLeft均为相对偏移量,但是下面不断地拿offsetParent的偏移量来累加,
  107. * 直至拿到offsetParent为body之后,就没有offsetParent了,此时循环才终止。
  108. * 最终计算出来的x,y为e相对body的绝对坐标。
  109. */
  110. while (e = e.offsetParent) { // 只要对象的offsetParent存在,则继续累加XY,
  111. x += e.offsetLeft;
  112. y += e.offsetTop;
  113. }
  114. return {
  115. "x": x,
  116. "y": y,
  117. "w": w,
  118. "h": h
  119. };
  120. } catch (e) {
  121. if (this.debug)
  122. alert('方法getAbsPoint执行异常:' + e);
  123. }
  124. };
  125. // var jb51 =new function() {
  126. // dom = [];
  127. // dom.isReady = false;
  128. // dom.isFunction = function(obj) {
  129. // return Object.prototype.toString.call(obj) === "[object Function]";
  130. // }
  131. // dom.Ready = function(fn) {
  132. // dom.initReady();
  133. // //如果没有建成DOM树,则走第二步,存储起来一起杀
  134. // if (dom.isFunction(fn)) {
  135. // if (dom.isReady) {
  136. // fn();
  137. // //如果已经建成DOM,则来一个杀一个
  138. // } else {
  139. // dom.push(fn);
  140. // //存储加载事件
  141. // }
  142. // }
  143. // }
  144. // dom.fireReady = function() {
  145. // if (dom.isReady) return;
  146. // dom.isReady = true;
  147. // for (var i = 0, n = dom.length; i < n; i++) {
  148. // var fn = dom[i];
  149. // fn();
  150. // }
  151. // dom.length = 0;
  152. // //清空事件
  153. // }
  154. // dom.initReady = function() {
  155. // if (document.addEventListener) {
  156. // document.addEventListener("DOMContentLoaded", function() {
  157. // document.removeEventListener("DOMContentLoaded", arguments.callee, false);
  158. // //清除加载函数
  159. // dom.fireReady();
  160. // },
  161. // false);
  162. // } else {
  163. // if (document.getElementById) {
  164. // document.write("<script id=\"ie-domReady\"
  165. // defer='defer'src=\"//:\"><\/script>");
  166. // document.getElementById("ie-domReady").onreadystatechange = function() {
  167. // if (this.readyState === "complete") {
  168. // dom.fireReady();
  169. // this.onreadystatechange = null;
  170. // this.parentNode.removeChild(this)
  171. // }
  172. // };
  173. // }
  174. // }
  175. // }
  176. // }
  177. Date.prototype.Format = Date.prototype.format = function (fmt) {
  178. var o = {
  179. "M+": this.getMonth() + 1, // 月份
  180. "d+": this.getDate(), // 日
  181. "h+": this.getHours(), // 小时
  182. "m+": this.getMinutes(), // 分
  183. "s+": this.getSeconds(), // 秒
  184. "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
  185. "S": this.getMilliseconds(), // 毫秒
  186. "E": "星期" + "日一二三四五六".charAt(new Date().getDay())
  187. };
  188. if (/(y+)/.test(fmt))
  189. fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
  190. .substr(4 - RegExp.$1.length));
  191. for (var k in o)
  192. if (new RegExp("(" + k + ")").test(fmt))
  193. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  194. return fmt;
  195. };
  196. /**
  197. * 获取ueditor对应路径的html
  198. * @param {Object} fpath
  199. * @param {Object} options
  200. */
  201. wd.c.getUeditorData = function (fpath, options) {
  202. var ret = {};
  203. var opt = {};
  204. for (var key in options) {
  205. if (typeof options[key] == 'string') {
  206. opt[key] = options[key];
  207. }
  208. }
  209. if (fpath) fpath = fpath.replace(/(^\s*)|(\s*$)/g, "")
  210. opt.path = fpath;
  211. /* 再去掉,去掉 ?wdApplication= -- 不支持多个应用,ssServ= 里可以写 ss.xxx。Lin
  212. if (!opt.wdApp)
  213. console.error("没获取到当前应用名 参数 wdApp");
  214. */
  215. opt.extra = (opt && opt.isextra) || opt.extra || undefined;
  216. if (!opt.extra) {
  217. delete opt["extra"];
  218. }
  219. for(var key in opt){
  220. if(opt[key]==null){
  221. delete opt[key];
  222. }
  223. }
  224. wd.c.wdAjax({
  225. type: "post",
  226. /* 再改,规范命名。Lin
  227. * 去掉 ?wdApplication=,不支持多个应用 -- 服务名可以写 ss.xxx
  228. * &wdService= 改为 ssServ
  229. url: "/service?wdApplication=wd&wdService=loadEditorBody",
  230. */ url: "/service?ssServ=loadEditorBody",
  231. dataType: "json",
  232. data: opt,
  233. async: false,
  234. success: function (data) {
  235. if (data.msg_0) console.error("**" + data.msg_0 + "**");
  236. ret=data;
  237. },
  238. });
  239. return ret;
  240. };
  241. /**
  242. *
  243. * @param {Object} options
  244. */
  245. wd.c.wdAjax = function (options) {
  246. // function ga_ajax(options) {
  247. var xhr = createXHR();
  248. options.url = options.url; // 清除缓存
  249. options.data = params(options.data); // 转义字符串
  250. if (options.type === "get") { // 判断使用的是否是get方式发送
  251. options.url += options.url.indexOf("?") == "-1" ? "?" + options.data : "&" + options.data;
  252. }
  253. // 异步
  254. if (options.async === true) {
  255. // 异步的时候需要触发onreadystatechange事件
  256. xhr.onreadystatechange = function () {
  257. // 执行完成
  258. if (xhr.readyState == 4) {
  259. callBack();
  260. }
  261. }
  262. }
  263. xhr.open(options.type, options.url, options.async); // false是同步 true是异步 // "demo.php?rand="+Math.random()+"&name=ga&ga",
  264. if (options.type === "post") {
  265. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  266. xhr.send(options.data);
  267. } else {
  268. xhr.send(null);
  269. }
  270. // xhr.abort(); // 取消异步请求
  271. // 同步
  272. if (options.async === false) {
  273. callBack();
  274. }
  275. // 返回数据
  276. function callBack() {
  277. // 判断是否返回正确
  278. if (xhr.status == 200) {
  279. if (options.dataType == "json") {
  280. options.success(JSON.parse(xhr.responseText));
  281. } else {
  282. options.success(xhr.responseText);
  283. }
  284. } else {
  285. if (options.error)
  286. options.error("获取数据失败,错误代号为:" + xhr.status + "错误信息为:" + xhr.statusText);
  287. }
  288. }
  289. // }
  290. function createXHR() {
  291. if (typeof XMLHttpRequest != "undefined") { // 非IE6浏览器
  292. return new XMLHttpRequest();
  293. } else if (typeof ActiveXObject != "undefined") { // IE6浏览器
  294. var version = [
  295. "MSXML2.XMLHttp.6.0",
  296. "MSXML2.XMLHttp.3.0",
  297. "MSXML2.XMLHttp",
  298. ];
  299. for (var i = 0; i < version.length; i++) {
  300. try {
  301. return new ActiveXObject(version[i]);
  302. } catch (e) {
  303. //跳过
  304. }
  305. }
  306. } else {
  307. throw new Error("您的系统或浏览器不支持XHR对象!");
  308. }
  309. }
  310. // 转义字符
  311. function params(data) {
  312. var arr = [];
  313. for (var i in data) {
  314. arr.push(encodeURIComponent(i) + "=" + encodeURIComponent(data[i]));
  315. }
  316. return arr.join("&");
  317. }
  318. }
  319. wd.c.date2min = function (dateStr) {
  320. console.log(dateStr);
  321. var d = new Date(dateStr);
  322. return (d.getTime() / 60 / 1000);
  323. };
  324. wd.c.min2date = function (minStr) {
  325. if (!isNaN(minStr)) {
  326. var d = new Date(parseInt(minStr) * 60 * 1000);
  327. return d.Format("yyyy-MM-dd hh:mm:ss");
  328. }
  329. };
  330. wd.c.loadScriptIns = function (src, id, callback) {
  331. if (document.getElementById("#jquery_js")) {
  332. wd.c.loadSctipt("/wd/js/jquery/jquery.js", 'jquwey_js', function () {
  333. wd.c.loadScriptIns(src, callback)
  334. });
  335. return;
  336. } else {
  337. if ($("[src$='" + (src.substring(src.lastIndexOf("/"), src.length)) + "']").length > 0) {
  338. //已存在,直接callback
  339. callback();
  340. } else {
  341. wd.c.loadSctipt(src, id, callback);
  342. }
  343. }
  344. };
  345. wd.c.loadSctipt = function (src, id, callback) {
  346. if (src) {
  347. var head = document.getElementsByTagName('head')[0];
  348. var script = document.createElement('script');
  349. if (!id || id == "") {
  350. id = src.substring(src.lastIndexOf("/"), src.length);
  351. }
  352. script.id = id;
  353. script.type = 'text/javascript';
  354. script.onload = script.onreadystatechange = function () {
  355. if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
  356. callback();
  357. // Handle memory leak in IE
  358. script.onload = script.onreadystatechange = null;
  359. }
  360. };
  361. script.src = src;
  362. head.appendChild(script);
  363. }
  364. };
  365. (function () {
  366. var ie = !!(window.attachEvent && !window.opera);
  367. var wk = /webkit\/(\d+)/i.test(navigator.userAgent) && (RegExp.$1 < 525);
  368. var fn = [];
  369. var run = function () {
  370. for (var i = 0; i < fn.length; i++)
  371. fn[i]();
  372. };
  373. var d = document;
  374. d.ready = function (f) {
  375. if (!ie && !wk && d.addEventListener)
  376. return d.addEventListener('DOMContentLoaded', f, false);
  377. if (fn.push(f) > 1)
  378. return;
  379. if (ie)
  380. (function () {
  381. try {
  382. d.documentElement.doScroll('left');
  383. run();
  384. } catch (err) {
  385. setTimeout(arguments.callee, 0);
  386. }
  387. })();
  388. else if (wk)
  389. var t = setInterval(function () {
  390. if (/^(loaded|complete)$/.test(d.readyState))
  391. clearInterval(t), run();
  392. }, 0);
  393. };
  394. })();
  395. // document.ready(function(){ window.top.clearToken();});
  396. if(!wd.common)
  397. wd.common={}
  398. wd.common.loadFile=function(path){
  399. var appName="";
  400. $.ajax({url:"/wd/page/LoginAppName.jsp",
  401. async:false,
  402. success:function(res){
  403. res=$.trim(res)
  404. appName=res;
  405. }
  406. })
  407. return "/"+appName+"/"+path;
  408. }
  409. wd.common.loadSkinFile=function(path){
  410. var result="";
  411. $.ajax({url:"/skin/skinDir.jsp", // :"/ss/page/skinDir.jsp", < :"/wd/page/skinPath.jsp",。Lin
  412. async:false,
  413. success:function(res){
  414. res=$.trim(res)
  415. result=res;
  416. }
  417. })
  418. return result+path;
  419. }
  420. wd.common.wdPostAjax=function(id,url,callback,extradata){
  421. var opt={};
  422. if(extradata!=undefined){
  423. opt=extradata;
  424. }
  425. if(id!=null){
  426. if($('#'+id)[0].tagName=="FROM"){
  427. var t = $('#'+id).serializeArray();
  428. $.each(t, function() {
  429. if(this.value!=undefined&&this.value!=null&&this.value!=''){
  430. opt[this.name] = this.value;
  431. }
  432. });
  433. }else{
  434. var t = $('#'+id).find("[name='*']");
  435. $.each(t, function() {
  436. if(this.value!=undefined&&this.value!=null&&this.value!=''){
  437. opt[this.name] = this.value;
  438. }
  439. });
  440. }
  441. }else{
  442. if($("from")){
  443. var t = $("from").serializeArray();
  444. $.each(t, function() {
  445. if(this.value!=undefined&&this.value!=null&&this.value!=''){
  446. opt[this.name] = this.value;
  447. }
  448. });
  449. }
  450. }
  451. alert(JSON.stringify(d));
  452. wd.c.wdAjax({
  453. type: "post",
  454. url: url,
  455. dataType: "json",
  456. data: opt,
  457. async: false,
  458. success: function (data) {
  459. eval(callback)(data);
  460. },
  461. });
  462. }