ks-core.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. $.extend({
  2. round : function(n, digit) {
  3. if (isNaN(n) || isNaN(digit))
  4. return 0;
  5. return Math.round(n * Math.pow(10, digit)) / Math.pow(10, digit);
  6. },
  7. toFloat : function(str) {
  8. var r = parseFloat(str.replace(/,/g, ''));
  9. if (isNaN(r))
  10. return 0;
  11. return r;
  12. },
  13. formatCurrency : function(d, digit) {
  14. var s = this.round(d, digit).toString();
  15. var hasSymbol = s.charAt(0) == '-' || s.charAt(0) == '+';
  16. var integerStr = hasSymbol ? s.substr(1) : s;
  17. var digitPos = integerStr.indexOf(".");
  18. var result = "";
  19. var digitStr = "";
  20. if (digitPos != -1) {
  21. integerStr = s.substr(hasSymbol ? 1 : 0, digitPos);
  22. digitStr = s.substr(digitPos + 1 + (hasSymbol ? 1 : 0));
  23. }
  24. for (var i = integerStr.length; i >= 0; i = i - 3) {
  25. result = integerStr
  26. .substr(i - 3 < 0 ? 0 : i - 3, i - 3 < 0 ? i : 3)
  27. + result;
  28. if (i - 3 > 0)
  29. result = "," + result;
  30. }
  31. if (digit > 0) {
  32. result += "." + digitStr;
  33. for (var i = digitStr.length; i < digit; i++) {
  34. result += '0';
  35. }
  36. }
  37. if (hasSymbol)
  38. result = s.charAt(0) + result;
  39. return result;
  40. },
  41. opennewwin : function(url, name, height, width, options) {
  42. if (height < 1) {
  43. height = Math.round(screen.availHeight * height);
  44. }
  45. if (width < 1) {
  46. width = Math.round(screen.availWidth * width);
  47. }
  48. var top = (screen.availHeight - height) / 2;
  49. var left = (screen.availWidth - width) / 2;
  50. if (!$.browser.msie) {
  51. var features = "resizable=1,status=1,scrollbars=1,height=" + height
  52. + ",width=" + width + ",top=" + top + ",left=" + left;
  53. for (key in options) {
  54. features += "," + key + "=" + options[key];
  55. }
  56. return window.open(url, name, features);
  57. } else {
  58. var features = "resizable:1;status:1;scroll:1;center:1;dialogHeight:"
  59. + height
  60. + "px;dialogWidth:"
  61. + width
  62. + "px;dialogTop:"
  63. + top + "px;dialogLeft:" + left + "px";
  64. if (options && options.modal) {
  65. return window.showModalDialog(url, window, features);
  66. }
  67. return window.showModelessDialog(url, window, features);
  68. }
  69. },
  70. getFormParams : function(form) {
  71. var r = {};
  72. $(":text, :hidden, :password, select", form).each(function() {
  73. if (typeof(this.name) != "undefined" && this.name.length > 0
  74. && $(this).visible()) {
  75. r[this.name] = this.value;
  76. }
  77. });
  78. $(":radio:checked, :checkbox:checked", form).each(function() {
  79. r[this.name] = true;
  80. });
  81. return r;
  82. },
  83. querystring : function(qs) {
  84. var params = {};
  85. if (qs == null)
  86. qs = location.search.substring(1, location.search.length);
  87. if (qs.length > 0) {
  88. qs = qs.replace(/\+/g, ' ');
  89. var args = qs.split('&'); // parse out name/value pairs separated
  90. // via &
  91. for (var i = 0; i < args.length; i++) {
  92. var pair = args[i].split('=');
  93. var name = decodeURIComponent(pair[0]);
  94. var value = (pair.length == 2)
  95. ? decodeURIComponent(pair[1])
  96. : name;
  97. params[name] = value;
  98. }
  99. }
  100. var parray = {
  101. get : function(name) {
  102. return params[name];
  103. },
  104. set : function(name, v) {
  105. if (typeof(v) == 'undefined' || v.length == 0)
  106. params[v] = null;
  107. else
  108. params[name] = v;
  109. return this;
  110. },
  111. toString : function() {
  112. var u = "";
  113. for (var name in params) {
  114. if (params[name] != null)
  115. u += name + "=" + encodeURIComponent(params[name])
  116. + "&";
  117. }
  118. return u.substring(0, u.length - 1);
  119. }
  120. }
  121. return parray;
  122. },
  123. getCookie : function(sName) {
  124. // cookies are separated by semicolons
  125. var aCookie = document.cookie.split("; ");
  126. for (var i = 0; i < aCookie.length; i++) {
  127. // a name/value pair (a crumb) is separated by an equal sign
  128. var aCrumb = aCookie[i].split("=");
  129. if (sName == aCrumb[0])
  130. return unescape(aCrumb[1]);
  131. }
  132. return null;
  133. },
  134. setCookie : function(sName, sValue, date) {
  135. document.cookie = sName + "=" + escape(sValue) + "; expires="
  136. + date.toGMTString();
  137. },
  138. loadXML : function(xmlFile, async) {
  139. var xmlDoc;
  140. if (window.ActiveXObject) {// for ie
  141. xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
  142. } else if (document.implementation
  143. && document.implementation.createDocument) {// for moz
  144. xmlDoc = document.implementation.createDocument('', '', null);
  145. } else {
  146. alert('xml read ERROR!');
  147. return null;
  148. }
  149. xmlDoc.async = async;
  150. xmlDoc.preserveWhiteSpace = false;
  151. xmlDoc.load(xmlFile);
  152. return xmlDoc;
  153. }
  154. });
  155. $.fn.extend({
  156. blockUI : function(opts) {
  157. var topDoc = window.document;
  158. var tw = topDoc.documentElement.clientWidth;
  159. var th = topDoc.documentElement.clientHeight;
  160. var el = $(this);
  161. var st = document.documentElement.scrollTop;
  162. var sl = document.documentElement.scrollLeft;
  163. var ww = document.documentElement.scrollWidth;
  164. var wh = document.documentElement.scrollHeight;
  165. if (ww < tw)
  166. ww = tw;
  167. if (wh < th)
  168. wh = th;
  169. var lay = $("#hcks_overlay");
  170. if (lay.length == 0) {
  171. var overlay = topDoc.createElement('DIV');
  172. overlay.id = 'hcks_overlay';
  173. overlay.style.position = 'absolute';
  174. overlay.style.left = '0px';
  175. overlay.style.top = '0px';
  176. overlay.style.width = (ww + sl) + 'px';
  177. overlay.style.height = (wh + st) + 'px';
  178. overlay.style.zIndex = 100;
  179. overlay.style.backgroundColor = '#000';
  180. overlay.className = 'overlay';
  181. // fix ie 6-- bug
  182. if ($.browser.msie && $.browser.version < 7) {
  183. var overlayFrame = topDoc.createElement("IFRAME");
  184. overlayFrame.id = 'hcks_overlayFrame';
  185. overlayFrame.style.position = 'absolute';
  186. overlayFrame.style.left = '0px';
  187. overlayFrame.style.top = '0px';
  188. overlayFrame.style.width = (ww + sl) + 'px';
  189. overlayFrame.style.height = (wh + st) + 'px';
  190. overlayFrame.style.zIndex = 99;
  191. overlayFrame.allowTransparency = true;
  192. overlayFrame.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
  193. topDoc.body.appendChild(overlayFrame);
  194. }
  195. topDoc.body.appendChild(overlay);
  196. } else {
  197. if (lay.is(":hidden")) {
  198. $("#hcks_overlayFrame").css({
  199. width : (ww + sl),
  200. height : (wh + st)
  201. }).show();
  202. lay.css({
  203. width : (ww + sl),
  204. height : (wh + st)
  205. }).show();
  206. }
  207. }
  208. var t = (th - el.height()) / 2 + st;
  209. var l = (tw - el.width()) / 2 + sl;
  210. if (t < 0)
  211. t = 10;
  212. if (l < 0)
  213. l = 10;
  214. el.revertValidation();
  215. el.css("position", "absolute").css('zIndex', 101).css("left", l + "px")
  216. .css("top", t + "px").show();
  217. return el;
  218. },
  219. unblockUI : function() {
  220. var overlay = $("#hcks_overlay");
  221. overlay.hide();
  222. $("#hcks_overlayFrame").hide();
  223. $(this).hide();
  224. },
  225. visible : function() {
  226. var v = ((this.attr("tagName") == 'INPUT' && this.attr("type") == 'hidden') || $(this)
  227. .is(":visible"))
  228. && $(this).parents(":hidden").length == 0;
  229. return v;
  230. }
  231. });
  232. Number.prototype.add = function(arg) {
  233. var r1, r2, m;
  234. try {
  235. r1 = this.toString().split(".")[1].length
  236. } catch (e) {
  237. r1 = 0
  238. }
  239. try {
  240. r2 = arg.toString().split(".")[1].length
  241. } catch (e) {
  242. r2 = 0
  243. }
  244. m = Math.pow(10, Math.max(r1, r2));
  245. return (this * m + arg * m) / m;
  246. }
  247. Number.prototype.sub = function(arg) {
  248. return this.add(-arg);
  249. }
  250. Number.prototype.mul = function(arg) {
  251. var m = 0, s1 = this.toString(), s2 = arg.toString();
  252. try {
  253. m += s1.split(".")[1].length
  254. } catch (e) {
  255. }
  256. try {
  257. m += s2.split(".")[1].length
  258. } catch (e) {
  259. }
  260. return Number(s1.replace(".", "")) * Number(s2.replace(".", ""))
  261. / Math.pow(10, m);
  262. }
  263. Number.prototype.div = function(arg) {
  264. var t1 = 0, t2 = 0, r1, r2;
  265. try {
  266. t1 = this.toString().split(".")[1].length
  267. } catch (e) {
  268. }
  269. try {
  270. t2 = arg.toString().split(".")[1].length
  271. } catch (e) {
  272. }
  273. with (Math) {
  274. r1 = Number(this.toString().replace(".", ""))
  275. r2 = Number(arg.toString().replace(".", ""))
  276. return (r1 / r2) * pow(10, t2 - t1);
  277. }
  278. }
  279. Number.prototype.round = function(arg) {
  280. return $.round(this, arg);
  281. }
  282. Date.prototype.format = function(fm) {
  283. if (!fm)
  284. fm = "yyyy-MM-dd";
  285. return fm.replace(/yyyy/g, this.getFullYear().toString()).replace(
  286. /MM/g,
  287. this.getMonth() < 9 ? "0" + (this.getMonth() + 1) : this.getMonth()
  288. .toString()).replace(
  289. /dd/g,
  290. this.getDate() < 10 ? "0" + this.getDate() : this.getDate()
  291. .toString()).replace(/yy/g,
  292. this.getFullYear().toString().substr(2)).replace(/M/g,
  293. this.getMonth() + 1).replace(/d/g, this.getDate().toString());
  294. }
  295. /**
  296. * 判断是否出现纵向滚动条
  297. * @return {}
  298. */
  299. function getIsScroll() {
  300. var xScroll, yScroll;
  301. if (window.innerHeight && window.scrollMaxY) {
  302. xScroll = document.body.scrollWidth;
  303. yScroll = window.innerHeight + window.scrollMaxY;
  304. } else if (document.body.scrollHeight > document.body.offsetHeight) { // all
  305. // but
  306. // Explorer
  307. // Mac
  308. xScroll = document.body.scrollWidth;
  309. yScroll = document.body.scrollHeight;
  310. } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla
  311. // and Safari
  312. xScroll = document.body.offsetWidth;
  313. yScroll = document.body.offsetHeight;
  314. }
  315. var windowWidth, windowHeight;
  316. if (self.innerHeight) { // all except Explorer
  317. windowWidth = self.innerWidth;
  318. windowHeight = self.innerHeight;
  319. } else if (document.documentElement
  320. && document.documentElement.clientHeight) { // Explorer 6 Strict
  321. // Mode
  322. windowWidth = document.documentElement.clientWidth;
  323. windowHeight = document.documentElement.clientHeight;
  324. } else if (document.body) { // other Explorers
  325. windowWidth = document.body.clientWidth;
  326. windowHeight = document.body.clientHeight;
  327. }
  328. // for small pages with total height less then height of the viewport
  329. if (yScroll < windowHeight) {
  330. pageHeight = windowHeight;
  331. } else {
  332. pageHeight = yScroll;
  333. }
  334. if (xScroll < windowWidth) {
  335. pageWidth = windowWidth;
  336. } else {
  337. pageWidth = xScroll;
  338. }
  339. // alert(pageWidth);
  340. // alert(pageHeight);
  341. // alert(windowWidth);
  342. // alert(windowHeight);
  343. return pageHeight - windowHeight > 0;
  344. }