No Description

common.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. if(typeof jQuery === 'undefined') {
  2. throw new Error('this JavaScript requires jQuery')
  3. } +
  4. !(function($, win, doc) {
  5. var mds = {};
  6. mds["jQuery"] = $;
  7. mds["win"] = win;
  8. mds["doc"] = doc;
  9. mds["body"] = $("body");
  10. $.define = function(deps, id, factory) {
  11. var dm = [];
  12. for(var i = 0; i < deps.length; ++i) {
  13. var dep = mds[deps[i]];
  14. if(dep) {
  15. dm.push(dep);
  16. } else {
  17. throw new Error("no found model [" + deps[i] + "]");
  18. }
  19. }
  20. try {
  21. var md = factory.apply(win, dm);
  22. mds[id] = md;
  23. return md;
  24. } catch(err) {
  25. throw new Error("build model[" + id + "] error:"+err);
  26. }
  27. };
  28. $.use = function(deps, handle) {
  29. var dm = [];
  30. for(var i = 0; i < deps.length; ++i) {
  31. var dep = mds[deps[i]];
  32. if(dep) {
  33. dm.push(dep);
  34. } else {
  35. throw new Error("no found model [" + deps[i] + "]");
  36. }
  37. }
  38. return handle.apply(win, dm);
  39. };
  40. $.define(["jQuery", "body", "win", "doc"], "util", function($,body, win, doc) {
  41. var rclass = /[\t\r\n\f]/g,
  42. noop = function() {},
  43. valCache ={},
  44. infoDiv = $("#g_info"),
  45. errDiv = $("#g_err"),
  46. warnDiv = $("#g_warn"),
  47. msgDiv = $("#g_msg"),
  48. err_msg = {},
  49. layer_css=function(){
  50. this.shade.css("display", "block");
  51. },
  52. layer_curr = {
  53. index: 5000000,
  54. remove: noop,
  55. css: noop
  56. },
  57. layer_remove = function() {
  58. this.shade.remove();
  59. this.ctn.empty();
  60. this.ctn.remove();
  61. this.prev.css("display", "block");
  62. layer_curr = this.prev;
  63. },
  64. hideMsg = function(e) {
  65. $(this).parent().hide();
  66. },
  67. bindHide = function($p) {
  68. $p.find(".click-hide-parent").on("click", hideMsg);
  69. },
  70. fillContent = function($c /* child node [String|Function|jQObj] */ , $p) {
  71. if($c) {
  72. if(typeof $c === "string") {
  73. $p.html($c);
  74. } else if(typeof $c == "function") {
  75. $c.call($p);
  76. } else if($c.jquery) {
  77. $p.append($c);
  78. }
  79. }
  80. },
  81. modal = function(ctn) {
  82. var inx = layer_curr.index + 2,
  83. ly = {
  84. index: inx,
  85. remove: layer_remove,
  86. prev: layer_curr,
  87. css:layer_css
  88. };
  89. ly.shade = $(
  90. "<div class='layer-shade layer-" + inx + "' style='z-index:" +
  91. inx + ";'></div>").appendTo(body);
  92. ++inx;
  93. ly.ctn = $(
  94. "<div class='layer-ctn layer-" + inx + "' style='z-index:" +
  95. inx + ";'></div>").appendTo(body);
  96. ly.prev = layer_curr;
  97. layer_curr = ly;
  98. fillContent(ctn, ly.ctn);
  99. return ly;
  100. },
  101. rmModal = function() {
  102. layer_curr.remove();
  103. },
  104. // function error message
  105. errMsg = function($c, $t) {
  106. var $e = $(
  107. "<div class='err-ctn'><i class='icon click-hide-parent'></i></div>")
  108. .appendTo(errDiv);
  109. $("<span></span>").appendTo($e).html($c);
  110. bindHide($e);
  111. setTimeout(function() {
  112. $e.remove()
  113. }, $t ? $t : 10000);
  114. },
  115. // function warn message
  116. warnMsg = function($c, $t) {
  117. var $w = $(
  118. "<div class='warn-ctn'><i class='icon click-hide-parent'></i></div>")
  119. .appendTo(warnDiv);
  120. $("<span></span>").appendTo($w).html($c);
  121. bindHide($w);
  122. setTimeout(function() {
  123. $w.remove()
  124. }, $t ? $t : 3000);
  125. },
  126. msg = function($c, $t) {
  127. var $m = $(
  128. "<div class='warn-ctn'><i class='icon click-hide-parent'></i></div>")
  129. .appendTo(warnDiv);
  130. $("<span></span>").appendTo($w).html($c);
  131. bindHide($m);
  132. setTimeout(function() {
  133. $w.remove()
  134. }, $t ? $t : 3000);
  135. },
  136. loadref = 0,
  137. loadingDiv = $("#g_loading"),
  138. loading = function() {
  139. ++loadref;
  140. if(loadref === 1) {
  141. loadingDiv.show()
  142. }
  143. },
  144. unLoading = function() {
  145. --loadref;
  146. if(loadref === 0) {
  147. loadingDiv.hide()
  148. }
  149. },
  150. alertMsg = function(title, content, hand) {
  151. if(content) {
  152. if($.isFunction(content)) {
  153. hand = content;
  154. content = title;
  155. title = "提示";
  156. }
  157. } else {
  158. content = title;
  159. title = "提示";
  160. }
  161. hand = hand || noop;
  162. var ly = modal('<div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h4 class="modal-title"></h4></div><div class="modal-body"><p></p></div><div class="modal-footer"><button type="button" class="btn">关闭</button></div></div></div>');
  163. ly.ctn.find(".modal-title").text(title);
  164. $c = ly.ctn.find("p").text(content);
  165. ly.ctn.find("button").on("click", function() {
  166. rmModal();
  167. hand();
  168. });
  169. },
  170. bm_addBtn = function($p, $cp, $h) {
  171. var $b = $('<button type="button" class="btn"></button>').appendTo($p);
  172. $b.text($cp).on("click", function() {
  173. rmModal();
  174. $h();
  175. });
  176. },
  177. boxMsg = function(obj) {
  178. var ly = modal('<div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h4 class="modal-title"></h4></div><div class="modal-body"><p></p></div><div class="modal-footer"></div></div></div>');
  179. ly.ctn.find(".modal-title").text(obj.title || "确认");
  180. ly.ctn.find("p").text(obj.content);
  181. var m_footer = ly.ctn.find(".modal-footer");
  182. for(var i = 0; i < obj.btns.length; ++i) {
  183. bm_addBtn(m_footer, obj.btns[i].caption || "no name", obj.btns[i].hand ||
  184. noop);
  185. }
  186. },
  187. serInArray = function(val, key, ret) {
  188. var t = $.type(val);
  189. if("boolean" === t) {
  190. ret.push(key + "=" + (val ? "1" : "0"));
  191. } else if("string" === t) {
  192. if(val) {
  193. ret.push(key + "=" + encodeURIComponent(val));
  194. }
  195. } else if("number" === t) {
  196. ret.push(key + "=" + val);
  197. } else {
  198. ret.push(key + "=" + encodeURIComponent(JSON.stringify(val)));
  199. }
  200. },
  201. serialize = function(obj) {
  202. var ret = [];
  203. if(obj) {
  204. for(key in obj) {
  205. var val = obj[key];
  206. var t = $.type(val);
  207. if("boolean" === t) {
  208. ret.push(key + "=" + (val ? "1" : "0"));
  209. } else if("string" === t) {
  210. if(val) {
  211. ret.push(key + "=" + encodeURIComponent(val));
  212. }
  213. } else if("number" === t) {
  214. ret.push(key + "=" + val);
  215. } else if("array" === t) {
  216. val.forEach(function(item) {
  217. serInArray(item, key, ret);
  218. });
  219. } else if("object" === t) {
  220. ret.push(key + "=" + encodeURIComponent(JSON.stringify(val)));
  221. }
  222. }
  223. }
  224. return ret.join("&");
  225. },
  226. g_def_err_hand = function(ep) {
  227. errMsg("http access error:\r\n" + JSON.stirngify(ep));
  228. },
  229. g_err = {
  230. "defErrHand": function(ep) {
  231. errMsg(JSON.stringify(ep));
  232. },
  233. "0": "未定义的错误",
  234. "1": function() {
  235. window.location.href = "login.html#" + encodeURIComponent(window.location.href);
  236. },
  237. },
  238. /**
  239. * ep={code:"",msg:"",detailMsg:"",url:""} pa=function(code,msg,detailMsg)
  240. * pa=boolean
  241. */
  242. defErrHand = function(errparam, errHand) {
  243. if(errHand) {
  244. var pt = $.type(errHand);
  245. if(pt == "booean") {
  246. g_def_err_hand(errparam);
  247. } else if(pt == "function") {
  248. errHand(errparam);
  249. } else {
  250. var ph = errHand[errparam.code] || g_err[errparam.code] || errHand["defErrHand"] ||
  251. g_err["defErrHand"];
  252. if(typeof ph === "string") {
  253. errMsg(ph);
  254. } else {
  255. ph(errparam);
  256. }
  257. }
  258. } else {
  259. var ph = g_err[errparam.code] || g_err["defErrHand"];
  260. if(typeof ph === "string") {
  261. errMsg(ph);
  262. } else {
  263. ph(errparam);
  264. }
  265. }
  266. },
  267. ajaxAccess = function(method, pUrl, pData, sh, eh, config) {
  268. config = config || {};
  269. if(false !== config.mask) {
  270. loading();
  271. }
  272. config.traditional = true;
  273. config.type = method;
  274. config.url = pUrl, config.data = pData;
  275. config.contentType = method === "put" ? "application/json" : "application/x-www-form-urlencoded";
  276. config.success = function(rd) {
  277. if(rd.success) {
  278. sh(rd.data);
  279. } else {
  280. rd.code = "" + rd.code;
  281. rd.url = pUrl;
  282. defErrHand(rd, eh);
  283. }
  284. };
  285. config.error = function(jqXHR, textStatus, errorThrown) {
  286. defErrHand({
  287. code: "err_" + (textStatus || ""),
  288. msg: textStatus,
  289. detailMsg: textStatus,
  290. xhr: jqXHR,
  291. eObj: errorThrown,
  292. url: pUrl
  293. }, eh);
  294. };
  295. $.ajax(config).always(false !== config.mask ? unLoading : noop);
  296. },
  297. buildElement = function(pe, obj) {
  298. if($.isArray(obj)) {
  299. obj.forEach(function(item) {
  300. buildElement(pe, item);
  301. });
  302. } else {
  303. var e;
  304. if(typeof obj === 'string') {
  305. e = doc.createTextNode(obj);
  306. pe.appendChild(e);
  307. } else {
  308. e = doc.createElement(obj.tn);
  309. pe.appendChild(e);
  310. if(obj.attrs && obj.attrs.length) {
  311. obj.attrs.forEach(function(attr) {
  312. e.setAttribute(attr.an, attr.av);
  313. });
  314. }
  315. if(obj.chs && obj.chs.length) {
  316. obj.chs.forEach(function(ch) {
  317. buildElement(e, ch);
  318. });
  319. }
  320. }
  321. }
  322. },
  323. ret= {
  324. classCheck: function(ele, clses) {
  325. var ret = {},
  326. cs = " " + (ele.getAttribute("class") || "") + " ",
  327. cls;
  328. for(var i = 0; i < clses.length; ++i) {
  329. var cls = clses[i];
  330. ret[cls] = cs.indexOf(" " + cls + " ") > -1;
  331. }
  332. return ret;
  333. },
  334. showModal: modal,
  335. closeModal: rmModal,
  336. showLoading: loading,
  337. hideLoading: unLoading,
  338. listModalIndex: function() {
  339. return layer_curr.index + 1;
  340. },
  341. error: errMsg,
  342. warn: warnMsg,
  343. msg: msg,
  344. alert: alertMsg,
  345. boxMsg: boxMsg,
  346. confirm: function(msg, yes, no) {
  347. bm({
  348. content: msg,
  349. btns: [{
  350. caption: "取消",
  351. hand: no || noop
  352. }, {
  353. caption: "确认",
  354. hand: yes
  355. }]
  356. });
  357. },
  358. get: function(url, data, sh, eh, config) {
  359. ajaxAccess("GET", url, data, sh, eh, config);
  360. },
  361. post: function(url, data, sh, eh, config) {
  362. ajaxAccess("POST", url, data, sh, eh, config);
  363. },
  364. put: function(url, data, sh, eh, config) {
  365. ajaxAccess("PUT", url, data ? JSON.stringify(data) : null, sh, eh,
  366. config);
  367. },
  368. del: function(url, sh, eh, pObj) {
  369. ajaxAccess("DELETE", url, null, sh, eh,pObj);
  370. },
  371. noop: noop,
  372. nochange: function(d) { return d },
  373. returnTrue: function() {
  374. return true;
  375. },
  376. returnFalse: function() {
  377. return false;
  378. },
  379. addRules: function(target, rule) {
  380. var te = $.type(rule);
  381. if("function" === te) {
  382. target.push(rule);
  383. } else if("array" === te) {
  384. rule.forEach(function(item){target.push(item)});
  385. }
  386. },
  387. validate: function(rules, obj) {
  388. for(var i = 0; i < rules.length; ++i) {
  389. var ret = rules[i](obj);
  390. if(ret) {
  391. if(obj.invalid) {
  392. obj.invalid(ret);
  393. }
  394. return false;
  395. }
  396. }
  397. if(obj.valid) {
  398. obj.valid();
  399. }
  400. return true;
  401. },
  402. valid: function($e) {
  403. $e.removeClass("invalid");
  404. },
  405. invalid: function($e) {
  406. $e.addClass("invalid");
  407. },
  408. appendChild: function(e, obj) {
  409. var docf = doc.createDocumentFragment();
  410. buildElement(docf, obj);
  411. e.appendChild(docf);
  412. },
  413. serialize: serialize,
  414. data:function(k,v){
  415. if(arguments.length>1){
  416. valCache[k]=v;
  417. }else if(k){
  418. return valCache[k];
  419. }
  420. },
  421. raise:function(err){defErrHand(err)}
  422. };
  423. $.util=ret;
  424. return ret;
  425. });
  426. })(jQuery, window, document);