lith.js 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  1. /*
  2. * lith 扩展
  3. * Date : 2019-08-22
  4. * Version: 1.6
  5. * author : Lith
  6. * email : serset@yeah.net
  7. <script type="text/javascript" src="lith.js"></script>
  8. */
  9. /** 扩展 String
  10. * 说明 : 对String类的prototype和类扩充函数: toJsonStr、toJsStr、toXmlStr、decodeXmlStr、html2Text、isNotStr、trim、lTrim、rTrim
  11. * Date : 2017-09-22
  12. * author: Lith
  13. */
  14. ; (function (String) {
  15. //String的去除空格函数
  16. String.prototype.trim = function () { return this.replace(/(^\s*)|(\s*$)/g, ""); };
  17. String.prototype.lTrim = function () { return this.replace(/(^\s*)/g, ""); };
  18. String.prototype.rTrim = function () { return this.replace(/(\s*$)/g, ""); };
  19. String.prototype.toJsonStr = function () {
  20. /// <summary> 向json键值对数据的字符串型值 转换。
  21. /// <para>例如 转换为javascript 代码 var oJson={"name":"ff"}; 中json对象的name属性所对应的值(以双引号包围)。</para>
  22. /// <para>转换 \b \t \n \f \r \" \\ 为 \\b \\t \\n \\f \\r \\" \\\\</para>
  23. /// </summary>
  24. /// <returns type="string">转换后的字符串</returns>
  25. return this.replace(/\\/g, "\\\\").replace(/\x08/g, "\\b").replace(/\t/g, "\\t").replace(/\n/g, "\\n").replace(/\f/g, "\\f").replace(/\r/g, "\\r").replace(/\"/g, "\\\"");
  26. };
  27. String.prototype.toJsStr = function () {
  28. /// <summary> 向javascript的字符串转换。
  29. /// <para>例如转换为javascript 代码 var str=""; 中str对象所赋的值(以引号包围)。 </para>
  30. /// <para>转换 \b \t \n \f \r \" \' \\ 为 \\b \\t \\n \\f \\r \\" \\' \\\\ </para>
  31. /// </summary>
  32. /// <returns type="string">转换后的字符串</returns>
  33. return this.replace(/\\/g, "\\\\").replace(/\x08/g, "\\b").replace(/\t/g, "\\t").replace(/\n/g, "\\n").replace(/\f/g, "\\f").replace(/\r/g, "\\r").replace(/\"/g, "\\\"").replace(/\'/g, "\\\'");
  34. };
  35. String.prototype.toXmlStr = function () {
  36. /// <summary> 向xml转换。
  37. /// <para>例如 转换 &lt;a title=&quot;&quot;&gt;ok&lt;/a&gt; 中a标签的内容体(innerHTML)或 转换 &lt;a title=&quot;&quot;&gt;ok&lt;/a&gt; 中title的值。</para>
  38. /// <para>转换 &amp; 双引号 &lt; &gt; 为 &amp;amp; &amp;quot; &amp;lt; &amp;gt;(注: 单引号 对应 &amp;apos; (&amp;#39;) ,但有些浏览器不支持,故此函数不转换。)</para>
  39. /// </summary>
  40. /// <returns type="string">转换后的字符串</returns>
  41. return this.replace(/\&/g, "&amp;").replace(/\"/g, "&quot;").replace(/\</g, "&lt;").replace(/\>/g, "&gt;");
  42. };
  43. String.prototype.decodeXmlStr = function () {
  44. /// <summary> xml属性字符串反向转换(与toXmlStr对应)。
  45. /// <para>例如 反向转换 &lt;a title=&quot;&quot;&gt;ok&lt;/a&gt; 中a标签的内容体(innerHTML)或 转换 &lt;a title=&quot;&quot;&gt;ok&lt;/a&gt; 中title的值。</para>
  46. /// <para>转换 &amp;amp; &amp;quot; &amp;lt; &amp;gt; 为 &quot; &amp; &lt; &gt; (注: 单引号 对应 &amp;apos; (&amp;#39;) ,但有些浏览器不支持,故此函数不转换。)</para>
  47. /// </summary>
  48. /// <returns type="string">转换后的字符串</returns>
  49. return this.replace(/\&amp\;/g, "&").replace(/\&quot\;/g, "\"").replace(/\&lt\;/g, "<").replace(/\&gt\;/g, ">");
  50. };
  51. String.prototype.html2Text = function () {
  52. /// <summary> 清除Html格式。例如 : 转换 "&lt;br/&gt;aa&lt;p&gt;ssfa&lt;/p&gt;" 为 "aassfa" <summary>
  53. /// <returns type="string">转换后的字符串</returns>
  54. return this.replace(/<[^>].*?>/g, "");
  55. };
  56. function isNotStr(str) {
  57. return null == str || undefined == str;
  58. }
  59. String.isNotStr = isNotStr;
  60. String.trim = function (str) { return isNotStr(str) ? '' : ('' + str).trim(); };
  61. String.lTrim = function (str) { return isNotStr(str) ? '' : ('' + str).lTrim(); };
  62. String.rTrim = function (str) { return isNotStr(str) ? '' : ('' + str).rTrim(); };
  63. String.toJsonStr = function (str) {
  64. /// <summary> 向json键值对数据的字符串型值 转换。
  65. /// <para>例如 转换为javascript 代码 var oJson={"name":"ff"}; 中json对象的name属性所对应的值(以双引号包围)。</para>
  66. /// <para>转换 \b \t \n \f \r \" \\ 为 \\b \\t \\n \\f \\r \\" \\\\</para>
  67. /// </summary>
  68. /// <returns type="string">转换后的字符串</returns>
  69. return isNotStr(str) ? '' : ('' + str).toJsonStr();
  70. };
  71. String.toJsStr = function (str) {
  72. /// <summary> 向javascript的字符串转换。
  73. /// <para>例如转换为javascript 代码 var str=""; 中str对象所赋的值(以引号包围)。 </para>
  74. /// <para>转换 \b \t \n \f \r \" \' \\ 为 \\b \\t \\n \\f \\r \\" \\' \\\\ </para>
  75. /// </summary>
  76. /// <returns type="string">转换后的字符串</returns>
  77. return isNotStr(str) ? '' : ('' + str).toJsStr();
  78. };
  79. String.toXmlStr = function (str) {
  80. /// <summary> 向xml转换。
  81. /// <para>例如 转换 &lt;a title=&quot;&quot;&gt;ok&lt;/a&gt; 中a标签的内容体(innerHTML)或 转换 &lt;a title=&quot;&quot;&gt;ok&lt;/a&gt; 中title的值。</para>
  82. /// <para>转换 &amp; 双引号 &lt; &gt; 为 &amp;amp; &amp;quot; &amp;lt; &amp;gt;(注: 单引号 对应 &amp;apos; (&amp;#39;) ,但有些浏览器不支持,故此函数不转换。)</para>
  83. /// </summary>
  84. /// <returns type="string">转换后的字符串</returns>
  85. return isNotStr(str) ? '' : ('' + str).toXmlStr();
  86. };
  87. String.decodeXmlStr = function (str) {
  88. /// <summary> xml属性字符串反向转换(与toXmlStr对应)。
  89. /// <para>例如 反向转换 &lt;a title=&quot;&quot;&gt;ok&lt;/a&gt; 中a标签的内容体(innerHTML)或 转换 &lt;a title=&quot;&quot;&gt;ok&lt;/a&gt; 中title的值。</para>
  90. /// <para>转换 &amp;amp; &amp;quot; &amp;lt; &amp;gt; 为 &quot; &amp; &lt; &gt; (注: 单引号 对应 &amp;apos; (&amp;#39;) ,但有些浏览器不支持,故此函数不转换。)</para>
  91. /// </summary>
  92. /// <returns type="string">转换后的字符串</returns>
  93. return isNotStr(str) ? '' : ('' + str).decodeXmlStr();
  94. };
  95. String.html2Text = function (str) {
  96. /// <summary> 清除Html格式。例如 : 转换 "&lt;br/&gt;aa&lt;p&gt;ssfa&lt;/p&gt;" 为 "aassfa" <summary>
  97. /// <returns type="string">转换后的字符串</returns>
  98. return isNotStr(str) ? '' : ('' + str).html2Text();
  99. };
  100. })(String);
  101. /** 对Date的扩展。扩展 Date.pattern
  102. * 说明 : 将 Date 转化为指定格式的String 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q)
  103. * demo : new Date().pattern("yyyy-MM-dd hh:mm:ss.S") //2006-07-02 08:09:04.423
  104. * Date : 2017-09-22
  105. * author: Lith
  106. */
  107. ; (function (Date) {
  108. /*** 对Date的扩展,将 Date 转化为指定格式的String * 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q)
  109. * 可以用 1-2 个占位符 * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
  110. * eg:
  111. * (newDate()).pattern("yyyy-MM-dd hh:mm:ss.S")==> 2006-07-02 08:09:04.423
  112. * (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04
  113. * (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04
  114. * (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04
  115. * (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
  116. */
  117. Date.prototype.pattern = function (fmt) {
  118. var o = {
  119. "M+": this.getMonth() + 1, //月份
  120. "d+": this.getDate(), //日
  121. "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时
  122. "H+": this.getHours(), //小时
  123. "m+": this.getMinutes(), //分
  124. "s+": this.getSeconds(), //秒
  125. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  126. "S": this.getMilliseconds() //毫秒
  127. };
  128. var week = {
  129. "0": "/u65e5",
  130. "1": "/u4e00",
  131. "2": "/u4e8c",
  132. "3": "/u4e09",
  133. "4": "/u56db",
  134. "5": "/u4e94",
  135. "6": "/u516d"
  136. };
  137. if (/(y+)/.test(fmt)) {
  138. fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  139. }
  140. if (/(E+)/.test(fmt)) {
  141. fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[this.getDay() + ""]);
  142. }
  143. for (var k in o) {
  144. if (new RegExp("(" + k + ")").test(fmt)) {
  145. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  146. }
  147. }
  148. return fmt;
  149. }
  150. Date.prototype.addDay = function (dayCount) {
  151. /// <summary><summary>
  152. /// <param name="dayCount" type="int">偏移的天数,可为负数</param>
  153. this.setTime(this.getTime() + dayCount * (1000 * 60 * 60 * 24));
  154. return this;
  155. };
  156. Date.prototype.addSecond = function (secondCount) {
  157. /// <summary><summary>
  158. /// <param name="secondCount" type="int">偏移的秒数,可为负数</param>
  159. this.setTime(this.getTime() + secondCount * 1000 );
  160. return this;
  161. };
  162. Date.addDay = function (date, dayCount) {
  163. if ("string" == typeof date) date = new Date(date);
  164. return date.addDay(dayCount);
  165. };
  166. Date.prototype.isWeekend = function () {
  167. /// <summary><summary>
  168. var week = this.getDay();
  169. if (week == 0 || week == 6) {
  170. return true;
  171. }
  172. return false;
  173. };
  174. Date.isWeekend = function (date) {
  175. if ("string" == typeof date) date = new Date(date);
  176. return date.isWeekend();
  177. };
  178. })(Date);
  179. /** Object.isNullOrEmpty
  180. * 说明 : 判定指定对象是否为null,或空对象(例如:{} )
  181. * demo : Object.isNullOrEmpty({}) //true
  182. * author: Lith
  183. */
  184. Object.isNullOrEmpty = function (obj) {
  185. /// <summary>判定指定对象是否为null,或空对象(例如:{} )</summary>
  186. /// <param name="obj" type="object"></param>
  187. /// <returns type="bool">返回指定对象是否为null,或空对象(例如:{} )</returns>
  188. if (!obj) return true;
  189. for (var n in obj) { return false; }
  190. return true;
  191. };
  192. /** 对lith的扩展。
  193. * Date : 2017-09-22
  194. * author: Lith
  195. */
  196. ; (function (scope) {
  197. /** 扩展 lith.stringify
  198. * 说明 : 把json(object 或 Array)转换为字符串。参照 /native/json2.js
  199. * Date : 2017-09-22
  200. * author: Lith
  201. */
  202. ; (function (scope) {
  203. var JSON = {};
  204. (function () {
  205. 'use strict';
  206. function f(n) {
  207. // Format integers to have at least two digits.
  208. return n < 10 ? '0' + n : n;
  209. }
  210. if (typeof Date.prototype.toJSON !== 'function') {
  211. Date.prototype.toJSON = function () {
  212. return isFinite(this.valueOf())
  213. ? this.getUTCFullYear() + '-' +
  214. f(this.getUTCMonth() + 1) + '-' +
  215. f(this.getUTCDate()) + 'T' +
  216. f(this.getUTCHours()) + ':' +
  217. f(this.getUTCMinutes()) + ':' +
  218. f(this.getUTCSeconds()) + 'Z'
  219. : null;
  220. };
  221. String.prototype.toJSON =
  222. Number.prototype.toJSON =
  223. Boolean.prototype.toJSON = function () {
  224. return this.valueOf();
  225. };
  226. }
  227. var cx,
  228. escapable,
  229. gap,
  230. indent,
  231. meta,
  232. rep;
  233. function quote(string) {
  234. // If the string contains no control characters, no quote characters, and no
  235. // backslash characters, then we can safely slap some quotes around it.
  236. // Otherwise we must also replace the offending characters with safe escape
  237. // sequences.
  238. escapable.lastIndex = 0;
  239. return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
  240. var c = meta[a];
  241. return typeof c === 'string'
  242. ? c
  243. : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
  244. }) + '"' : '"' + string + '"';
  245. }
  246. function str(key, holder) {
  247. // Produce a string from holder[key].
  248. var i, // The loop counter.
  249. k, // The member key.
  250. v, // The member value.
  251. length,
  252. mind = gap,
  253. partial,
  254. value = holder[key];
  255. // If the value has a toJSON method, call it to obtain a replacement value.
  256. if (value && typeof value === 'object' &&
  257. typeof value.toJSON === 'function') {
  258. value = value.toJSON(key);
  259. }
  260. // If we were called with a replacer function, then call the replacer to
  261. // obtain a replacement value.
  262. if (typeof rep === 'function') {
  263. value = rep.call(holder, key, value);
  264. }
  265. // What happens next depends on the value's type.
  266. switch (typeof value) {
  267. case 'string':
  268. return quote(value);
  269. case 'number':
  270. // JSON numbers must be finite. Encode non-finite numbers as null.
  271. return isFinite(value) ? String(value) : 'null';
  272. case 'boolean':
  273. case 'null':
  274. // If the value is a boolean or null, convert it to a string. Note:
  275. // typeof null does not produce 'null'. The case is included here in
  276. // the remote chance that this gets fixed someday.
  277. return String(value);
  278. // If the type is 'object', we might be dealing with an object or an array or
  279. // null.
  280. case 'object':
  281. // Due to a specification blunder in ECMAScript, typeof null is 'object',
  282. // so watch out for that case.
  283. if (!value) {
  284. return 'null';
  285. }
  286. // Make an array to hold the partial results of stringifying this object value.
  287. gap += indent;
  288. partial = [];
  289. // Is the value an array?
  290. if (Object.prototype.toString.apply(value) === '[object Array]') {
  291. // The value is an array. Stringify every element. Use null as a placeholder
  292. // for non-JSON values.
  293. length = value.length;
  294. for (i = 0; i < length; i += 1) {
  295. partial[i] = str(i, value) || 'null';
  296. }
  297. // Join all of the elements together, separated with commas, and wrap them in
  298. // brackets.
  299. v = partial.length === 0
  300. ? '[]'
  301. : gap
  302. ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'
  303. : '[' + partial.join(',') + ']';
  304. gap = mind;
  305. return v;
  306. }
  307. // If the replacer is an array, use it to select the members to be stringified.
  308. if (rep && typeof rep === 'object') {
  309. length = rep.length;
  310. for (i = 0; i < length; i += 1) {
  311. if (typeof rep[i] === 'string') {
  312. k = rep[i];
  313. v = str(k, value);
  314. if (v) {
  315. partial.push(quote(k) + (gap ? ': ' : ':') + v);
  316. }
  317. }
  318. }
  319. } else {
  320. // Otherwise, iterate through all of the keys in the object.
  321. for (k in value) {
  322. if (Object.prototype.hasOwnProperty.call(value, k)) {
  323. v = str(k, value);
  324. if (v) {
  325. partial.push(quote(k) + (gap ? ': ' : ':') + v);
  326. }
  327. }
  328. }
  329. }
  330. // Join all of the member texts together, separated with commas,
  331. // and wrap them in braces.
  332. v = partial.length === 0
  333. ? '{}'
  334. : gap
  335. ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'
  336. : '{' + partial.join(',') + '}';
  337. gap = mind;
  338. return v;
  339. }
  340. }
  341. // If the JSON object does not yet have a stringify method, give it one.
  342. if (typeof JSON.stringify !== 'function') {
  343. escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
  344. meta = { // table of character substitutions
  345. '\b': '\\b',
  346. '\t': '\\t',
  347. '\n': '\\n',
  348. '\f': '\\f',
  349. '\r': '\\r',
  350. '"': '\\"',
  351. '\\': '\\\\'
  352. };
  353. JSON.stringify = function (value) {
  354. // The stringify method takes a value and an optional replacer, and an optional
  355. // space parameter, and returns a JSON text. The replacer can be a function
  356. // that can replace values, or an array of strings that will select the keys.
  357. // A default replacer method can be provided. Use of the space parameter can
  358. // produce text that is more easily readable.
  359. var replacer = arguments[1], space = arguments[2];
  360. var i;
  361. gap = '';
  362. indent = '';
  363. // If the space parameter is a number, make an indent string containing that
  364. // many spaces.
  365. if (typeof space === 'number') {
  366. for (i = 0; i < space; i += 1) {
  367. indent += ' ';
  368. }
  369. // If the space parameter is a string, it will be used as the indent string.
  370. } else if (typeof space === 'string') {
  371. indent = space;
  372. }
  373. // If there is a replacer, it must be a function or an array.
  374. // Otherwise, throw an error.
  375. rep = replacer;
  376. if (replacer && typeof replacer !== 'function' &&
  377. (typeof replacer !== 'object' ||
  378. typeof replacer.length !== 'number')) {
  379. throw new Error('JSON.stringify');
  380. }
  381. // Make a fake root object containing our value under the key of ''.
  382. // Return the result of stringifying the value.
  383. return str('', { '': value });
  384. };
  385. }
  386. // If the JSON object does not yet have a parse method, give it one.
  387. if (typeof JSON.parse !== 'function') {
  388. cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
  389. JSON.parse = function (text, reviver) {
  390. // The parse method takes a text and an optional reviver function, and returns
  391. // a JavaScript value if the text is a valid JSON text.
  392. var j;
  393. function walk(holder, key) {
  394. // The walk method is used to recursively walk the resulting structure so
  395. // that modifications can be made.
  396. var k, v, value = holder[key];
  397. if (value && typeof value === 'object') {
  398. for (k in value) {
  399. if (Object.prototype.hasOwnProperty.call(value, k)) {
  400. v = walk(value, k);
  401. if (v !== undefined) {
  402. value[k] = v;
  403. } else {
  404. delete value[k];
  405. }
  406. }
  407. }
  408. }
  409. return reviver.call(holder, key, value);
  410. }
  411. // Parsing happens in four stages. In the first stage, we replace certain
  412. // Unicode characters with escape sequences. JavaScript handles many characters
  413. // incorrectly, either silently deleting them, or treating them as line endings.
  414. text = String(text);
  415. cx.lastIndex = 0;
  416. if (cx.test(text)) {
  417. text = text.replace(cx, function (a) {
  418. return '\\u' +
  419. ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
  420. });
  421. }
  422. // In the second stage, we run the text against regular expressions that look
  423. // for non-JSON patterns. We are especially concerned with '()' and 'new'
  424. // because they can cause invocation, and '=' because it can cause mutation.
  425. // But just to be safe, we want to reject all unexpected forms.
  426. // We split the second stage into 4 regexp operations in order to work around
  427. // crippling inefficiencies in IE's and Safari's regexp engines. First we
  428. // replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
  429. // replace all simple value tokens with ']' characters. Third, we delete all
  430. // open brackets that follow a colon or comma or that begin the text. Finally,
  431. // we look to see that the remaining characters are only whitespace or ']' or
  432. // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
  433. if (/^[\],:{}\s]*$/
  434. .test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
  435. .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
  436. .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
  437. // In the third stage we use the eval function to compile the text into a
  438. // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
  439. // in JavaScript: it can begin a block or an object literal. We wrap the text
  440. // in parens to eliminate the ambiguity.
  441. j = eval('(' + text + ')');
  442. // In the optional fourth stage, we recursively walk the new structure, passing
  443. // each name/value pair to a reviver function for possible transformation.
  444. return typeof reviver === 'function'
  445. ? walk({ '': j }, '')
  446. : j;
  447. }
  448. // If the text is not JSON parseable, then a SyntaxError is thrown.
  449. throw new SyntaxError('JSON.parse');
  450. };
  451. }
  452. }());
  453. /// <field name='stringify' type='fucntion'>把json转换为字符串</field>
  454. scope.stringify = JSON.stringify;
  455. })(scope);
  456. /** 扩展 lith.stringNson
  457. * 说明 : 把nson转换为字符串(目前相对json扩充了function类型。值可为function类型)
  458. * Date : 2018-08-02
  459. * author: Lith
  460. */
  461. ; (function (scope) {
  462. // var toJsonStr = String.toJsonStr;
  463. // function dateToStr(v) { return v.pattern('"yyyy-MM-dd hh:mm:ss"'); }
  464. function toJsonStr(v) {
  465. /// <summary> 向json键值对数据的字符串型值 转换。
  466. /// <para>例如 转换为javascript 代码 var oJson={"name":"ff"}; 中json对象的name属性所对应的值(以双引号包围)。</para>
  467. /// <para>转换 \b \t \n \f \r \" \\ 为 \\b \\t \\n \\f \\r \\" \\\\</para>
  468. /// </summary>
  469. /// <returns type="string">转换后的字符串</returns>
  470. return v.replace(/\\/g, "\\\\").replace(/\x08/g, "\\b").replace(/\t/g, "\\t").replace(/\n/g, "\\n").replace(/\f/g, "\\f").replace(/\r/g, "\\r").replace(/\"/g, "\\\"");
  471. }
  472. function dateToStr(d) {
  473. function f(n) {
  474. return n < 10 ? '0' + n : n;
  475. }
  476. return d.getUTCFullYear() + '-' +
  477. f(d.getUTCMonth() + 1) + '-' +
  478. f(d.getUTCDate()) + ' ' +
  479. f(d.getUTCHours()) + ':' +
  480. f(d.getUTCMinutes()) + ':' +
  481. f(d.getUTCSeconds());
  482. }
  483. function arrayToStr(array) {
  484. var str = '[';
  485. for (var p in array) str += stringify(array[p]) + ',';
  486. if (1 == str.length) return '[]';
  487. return str.slice(0, -1) + ']';
  488. }
  489. function objectToStr(object) {
  490. if (!object) return 'null';
  491. var str = '{';
  492. for (var p in object) str += '"' + toJsonStr(p) + '":' + stringify(object[p]) + ',';
  493. if (1 == str.length) return '{}';
  494. return str.slice(0, -1) + '}';
  495. }
  496. function stringify(v) {
  497. if ('string' == typeof (v)) return '"' + toJsonStr(v) + '"';
  498. if ('object' == typeof (v))
  499. if (Object.prototype.toString.apply(v) === '[object Array]') return arrayToStr(v);
  500. else if (v instanceof Date) return dateToStr(v);
  501. else return objectToStr(v);
  502. return '' + v;
  503. }
  504. scope.stringNson = stringify;
  505. })(scope);
  506. /** 扩展 lith.math (lith.math.mod lith.math.guid)
  507. * Date : 2017-01-04
  508. * author:Lith
  509. */
  510. ; (function (scope) {
  511. var objName = 'math';
  512. var obj = (scope[objName] || (scope[objName] = {}));
  513. obj.mod = function (a, b) {
  514. /// <summary> a对b取模(余数),结果始终为非负数。 mod(-1,10)结果为9 。(b符号无意义,若为0则始终返回0)<summary>
  515. /// <param name="a" type="int">sss</param>
  516. /// <param name="b" type="int">s</param>
  517. /// <returns type="int"></returns>
  518. return b == 0 ? 0 : (a < 0 ? (b - ((-a) % b)) : a % b);
  519. };
  520. obj.guid = function (len, radix) {
  521. /// <summary>
  522. /// <para> 获取guid。(若不传参数,则为 rfc4122, version 4 form) </para>
  523. /// <para> lith.math.guid() // "3D6F0925-9535-4A30-9F70-EE57B77095B1" </para>
  524. /// <para> lith.math.guid(8, 2) // "11000101" </para>
  525. /// </summary>
  526. /// <param name="len" type="int">可不指定</param>
  527. /// <param name="radix" type="int">可不指定,默认16</param>
  528. /// <returns type="int"></returns>
  529. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  530. var uuid = [], i;
  531. radix = radix || chars.length;
  532. if (len) {
  533. // Compact form
  534. for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  535. } else {
  536. // rfc4122, version 4 form
  537. var r;
  538. // rfc4122 requires these characters
  539. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  540. uuid[14] = '4';
  541. // Fill in random data. At i==19 set the high bits of clock sequence as
  542. // per rfc4122, sec. 4.1.5
  543. for (i = 0; i < 36; i++) {
  544. if (!uuid[i]) {
  545. r = 0 | Math.random() * 16;
  546. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  547. }
  548. }
  549. }
  550. return uuid.join('');
  551. };
  552. })(scope);
  553. /** 扩展 EventForHit
  554. * Date : 2017-09-22
  555. * author:Lith
  556. */
  557. scope.EventForHit = function () {
  558. /// <summary>demo:
  559. /// <para> var event=new lith.EventForHit();</para>
  560. /// <para> event.doAfterHit(func1);</para>
  561. /// <para> event.onHit(); //会调用 func1 </para>
  562. /// <para> event.doAfterHit(func2); //会调用 func2 </para>
  563. /// <para> event.hited=false;</para>
  564. /// <para> event.doAfterHit(func3);</para>
  565. /// <para> event.doAfterHit(func4);</para>
  566. /// <para> event.onHit(); //会调用 func3,func4 </para>
  567. /// <para> </para>
  568. /// <para> var event=new lith.EventForHit();</para>
  569. /// <para> event.doAfterHit(func1);</para>
  570. /// <para> event.addHit('a');event.addHit('b'); </para>
  571. /// <para> event.onHit('a'); </para>
  572. /// <para> event.onHit('b'); //会调用 func1 </para>
  573. /// <para> </para>
  574. /// <para> event.doAfterHit(func1);</para>
  575. /// <para> event.clearCallback(); // 会清除func1
  576. /// <para> event.doAfterHit(func2);</para>
  577. /// <para> event.onHit(); //会调用 func2 </para>
  578. /// </summary>
  579. var cbList;
  580. this.hited = false;
  581. this.doAfterHit = function (callback) {
  582. if (this.hited) {
  583. callback();
  584. } else {
  585. if (cbList) cbList.push(callback);
  586. else cbList = [callback];
  587. }
  588. };
  589. var hitNames;
  590. this.addHit = function (hitName) {
  591. if (!hitNames) hitNames = {};
  592. hitNames[hitName] = true;
  593. };
  594. function Object_isNullOrEmpty(obj) {
  595. /// <summary>判定指定对象是否为null,或空对象(例如:{} )</summary>
  596. /// <param name="obj" type="object"></param>
  597. /// <returns type="bool">返回指定对象是否为null,或空对象(例如:{} )</returns>
  598. if (!obj) return true;
  599. for (var n in obj) { return false; }
  600. return true;
  601. }
  602. this.onHit = function (hitName) {
  603. if (hitName) {
  604. if (hitNames) {
  605. delete hitNames[hitName];
  606. }
  607. }
  608. if (!Object_isNullOrEmpty(hitNames)) {
  609. return;
  610. }
  611. this.hited = true;
  612. if (cbList) {
  613. for (var t in cbList) {
  614. cbList[t]();
  615. }
  616. cbList = null;
  617. }
  618. };
  619. this.clearCallback = function () {
  620. cbList = null;
  621. };
  622. };
  623. /** 扩展 override、virtual
  624. * Date : 2017-09-22
  625. * author:Lith
  626. */
  627. ; (function (scope) {
  628. scope.override = function (object, funcName, func) {
  629. /// <signature>
  630. /// <summary>
  631. /// 用this函数(func)重写对象(object)的base函数(funcName),base函数作为this函数的参数传入
  632. /// <para>注:调用base函数的建议方式为 base.apply(this,arguments) 或 base.call(this,p1,p2...)</para>
  633. /// </summary>
  634. /// <param name="object" type="object">对象,若不指定(例如null)则参数funcName必须为函数</param>
  635. /// <param name="funcName" type="string or function">函数 或 在对象(object)中的函数名称</param>
  636. /// <param name="func" type="function">重写的函数</param>
  637. /// <param name="baseAsFirstParam" type="bool">true:旧函数作为新函数的第一个参数传入; 其他:旧函数会作为新函数的最后一个参数传入</param>
  638. /// <param name="times" type="int">调用的次数(若为正数则在调用times次后 再次调用时就调用base函数)(若为数字,会先取整)</param>
  639. /// <param name="forceBaseToFunc" type="bool">是否强制base参数为函数,若funcName对应的值不为函数,则把base转换为返回funcName对应的值的无参函数</param>
  640. /// </signature>
  641. /// <signature>
  642. /// <summary>
  643. /// 用this函数(func)重写对象(object)的base函数(funcName),base函数作为this函数的参数传入
  644. /// <para>注:调用base函数的建议方式为 base.apply(this,arguments) 或 base.call(this,p1,p2...)</para>
  645. /// </summary>
  646. /// <param name="object" type="object">对象,若不指定(例如null)则参数funcName必须为函数</param>
  647. /// <param name="funcName" type="string or function">函数 或 在对象(object)中的函数名称</param>
  648. /// <param name="func" type="function">重写的函数</param>
  649. /// <param name="param" type="object">
  650. /// param.baseAsFirstParam[bool]: true:旧函数作为新函数的第一个参数传入; 其他:旧函数会作为新函数的最后一个参数传入
  651. /// param.times[int]: 调用的次数(若为正数则在调用times次后 再次调用时就调用base函数)(若为数字,会先取整)
  652. /// param.forceBaseToFunc[bool]: 是否强制base参数为函数,若funcName对应的值不为函数,则把base转换为返回funcName对应的值的无参函数
  653. /// Demo: {},{baseAsFirstParam:true,times:0,forceBaseToFunc:true},{times:1,forceBaseToFunc:true}
  654. /// </param>
  655. /// </signature>
  656. var param = arguments[3];
  657. if (!param || 'object' != typeof (param)) {
  658. param = { baseAsFirstParam: param, times: arguments[4], forceBaseToFunc: arguments[5] };
  659. }
  660. var baseValue, base, funcCallBase;
  661. if ('function' == typeof (funcName)) {
  662. funcCallBase = base = baseValue = funcName;
  663. } else if (object && 'object' == typeof (object)) {
  664. if ('function' == typeof (baseValue = object[funcName])) {
  665. funcCallBase = base = baseValue;
  666. } else {
  667. base = param.forceBaseToFunc ? (funcCallBase = function () { return baseValue; }) : baseValue;
  668. }
  669. } else {
  670. throw new Error('override参数不合法,没有正确指定base函数');
  671. }
  672. if (isNaN(param.times)) param.times = -1;
  673. else param.times = parseInt(param.times);
  674. var funcCur = function () {
  675. var args = Array.prototype.slice.call(arguments);
  676. if (true == param.baseAsFirstParam) {
  677. args.splice(0, 0, base);
  678. } else {
  679. args.push(base);
  680. }
  681. var ret = func.apply(this, args);
  682. if (param.times > 0) {
  683. param.times--;
  684. if (0 >= param.times) {
  685. funcCur = funcCallBase;
  686. }
  687. }
  688. return ret;
  689. }
  690. var funcRet = function () { return funcCur.apply(this, arguments); };
  691. if (object && 'object' == typeof (object)) object[funcName] = funcRet;
  692. return funcRet;
  693. };
  694. scope.overrideEasyBase = function (object, funcName, func) {
  695. /// <signature>
  696. /// <summary>
  697. /// 用this函数(func)重写对象(object)的base函数(funcName),base函数作为this函数的参数传入
  698. /// <para>若base对应的值不为函数,则把base转换为返回对应的值的无参函数</para>
  699. /// <para>调用base函数时,函数所在的this对象始终为this函数所在的对象(其参数根据ignoreBaseArgs决定使用this函数的参数还是调用时传入的参数)。</para>
  700. /// </summary>
  701. /// <param name="object" type="object">对象,若不指定(例如null)则参数funcName必须为函数</param>
  702. /// <param name="funcName" type="string or function">函数 或 在对象(object)中的函数名称</param>
  703. /// <param name="func" type="function">重写的函数</param>
  704. /// <param name="baseAsFirstParam" type="bool">true:旧函数作为新函数的第一个参数传入; 其他:旧函数会作为新函数的最后一个参数传入</param>
  705. /// <param name="times" type="int">调用的次数(若为正数则在调用times次后 再次调用时就调用base函数)(若为数字,会先取整)</param>
  706. /// <param name="ignoreBaseArgs" type="bool">true:忽略base函数调用时传入的参数(无论调用base函数时是否指定参数)而使用this函数的原始参数 其他(false 或 undifined等):使用base函数调用时传入的参数 </param>
  707. /// </signature>
  708. /// <signature>
  709. /// <summary>
  710. /// 用this函数(func)重写对象(object)的base函数(funcName),base函数作为this函数的参数传入
  711. /// <para>若base对应的值不为函数,则把base转换为返回对应的值的无参函数</para>
  712. /// <para>调用base函数时,函数所在的this对象始终为this函数所在的对象(其参数根据ignoreBaseArgs决定使用this函数的参数还是调用时传入的参数)。</para>
  713. /// </summary>
  714. /// <param name="object" type="object">对象,若不指定(例如null)则参数funcName必须为函数</param>
  715. /// <param name="funcName" type="string or function">函数 或 在对象(object)中的函数名称</param>
  716. /// <param name="func" type="function">重写的函数</param>
  717. /// <param name="param" type="object">
  718. /// param.baseAsFirstParam[bool]: true:旧函数作为新函数的第一个参数传入; 其他:旧函数会作为新函数的最后一个参数传入
  719. /// param.times[int]: 调用的次数(若为正数则在调用times次后 再次调用时就调用base函数)(若为数字,会先取整)
  720. /// param.ignoreBaseArgs[bool] true:忽略base函数调用时传入的参数(无论调用base函数时是否指定参数)而使用this函数的原始参数 其他(false 或 undifined等):使用base函数调用时传入的参数
  721. /// Demo: {},{baseAsFirstParam:true,times:0,ignoreBaseArgs:false},{times:1}
  722. /// </param>
  723. /// </signature>
  724. var param = arguments[3];
  725. if (!param || 'object' != typeof (param)) {
  726. param = { baseAsFirstParam: param, times: arguments[4], ignoreBaseArgs: arguments[5] };
  727. }
  728. var baseValue, funcCallBase;
  729. if ('function' == typeof (funcName)) {
  730. funcCallBase = baseValue = funcName;
  731. } else if (object && 'object' == typeof (object)) {
  732. if ('function' == typeof (baseValue = object[funcName])) {
  733. funcCallBase = baseValue;
  734. } else {
  735. funcCallBase = function () { return baseValue; }
  736. }
  737. } else {
  738. throw new Error('overrideEasyBase参数不合法,没有正确指定base函数');
  739. }
  740. if (isNaN(param.times)) param.times = -1;
  741. else param.times = parseInt(param.times);
  742. var funcCur = function () {
  743. var args = Array.prototype.slice.call(arguments);
  744. var self = this, self_args = arguments, base = function () {
  745. return funcCallBase.apply(self, (true == param.ignoreBaseArgs ? self_args : arguments));
  746. }
  747. if (true == param.baseAsFirstParam) {
  748. args.splice(0, 0, base);
  749. } else {
  750. args.push(base);
  751. }
  752. var ret = func.apply(this, args);
  753. if (param.times > 0) {
  754. param.times--;
  755. if (0 >= param.times) {
  756. funcCur = funcCallBase;
  757. }
  758. }
  759. return ret;
  760. }
  761. var funcRet = function () { return funcCur.apply(this, arguments); };
  762. if (object && 'object' == typeof (object)) object[funcName] = funcRet;
  763. return funcRet;
  764. };
  765. scope.virtual = function (object, funcName, func) {
  766. /// <signature>
  767. /// <summary>继承函数,在调用新函数时会调用旧函数</summary>
  768. /// <param name="object" type="object">对象,若不指定(例如null)则参数funcName必须为函数</param>
  769. /// <param name="funcName" type="string or function">函数 或 在对象(object)中的函数名称</param>
  770. /// <param name="func" type="function">重写的函数</param>
  771. /// <param name="callThisFirst" type="bool">是否先调用重写的函数. true:先调用重写的函数,后调用base函数; 其他(例如false): 先调用base函数,后调用重写的函数。 </param>
  772. /// <param name="retBase" type="bool">是否用base函数的返回值作为返回值。 true:base函数的返回值作为返回值; 其他(例如false): 重写函数的返回值作为返回值。 </param>
  773. /// <param name="times" type="int">调用的次数(若为正数则在调用times次后 再次调用时就调用base函数)(若为数字,会先取整)</param>
  774. /// </signature>
  775. /// <signature>
  776. /// <summary>继承函数,在调用新函数时会调用旧函数</summary>
  777. /// <param name="object" type="object">对象,若不指定(例如null)则参数funcName必须为函数</param>
  778. /// <param name="funcName" type="string or function">函数 或 在对象(object)中的函数名称</param>
  779. /// <param name="func" type="function">重写的函数</param>
  780. /// <param name="param" type="object">
  781. /// param.callThisFirst: 是否先调用重写的函数. true:先调用重写的函数,后调用base函数; 其他(例如false): 先调用base函数,后调用重写的函数。
  782. /// param.retBase: 是否用base函数的返回值作为返回值。 true:base函数的返回值作为返回值; 其他(例如false): 重写函数的返回值作为返回值。
  783. /// param.times:调用的次数(若为正数则在调用times次后 再次调用时就调用base函数)(若为数字,会先取整)
  784. /// Demo: {},{callThisFirst:false,retBase:true,times:0},{retBase:true,times:1}
  785. /// </param>
  786. /// </signature>
  787. var param = arguments[3];
  788. if (!param || 'object' != typeof (param)) {
  789. param = { callThisFirst: param, retBase: arguments[4], times: arguments[5] };
  790. }
  791. var baseValue, funcCallBase;
  792. if ('function' == typeof (funcName)) {
  793. funcCallBase = baseValue = funcName;
  794. } else if (object && 'object' == typeof (object)) {
  795. if ('function' == typeof (baseValue = object[funcName])) {
  796. funcCallBase = baseValue;
  797. } else {
  798. funcCallBase = function () { return baseValue; };
  799. }
  800. } else {
  801. throw new Error('virtual参数不合法,没有正确指定base函数');
  802. }
  803. if (isNaN(param.times))
  804. param.times = -1;
  805. else
  806. param.times = parseInt(param.times);
  807. var funcCur = function () {
  808. var thisRet, baseRet;
  809. if (true == param.callThisFirst)
  810. thisRet = func.apply(this, arguments);
  811. baseRet = funcCallBase.apply(this, arguments);
  812. if (true != param.callThisFirst)
  813. thisRet = func.apply(this, arguments);
  814. if (param.times > 0) {
  815. param.times--;
  816. if (0 >= param.times) {
  817. funcCur = funcCallBase;
  818. }
  819. }
  820. return true == param.retBase ? baseRet : thisRet;
  821. };
  822. var funcRet = function () { return funcCur.apply(this, arguments); };
  823. if (object && 'object' == typeof (object)) object[funcName] = funcRet;
  824. return funcRet;
  825. };
  826. scope.virtualBaseFirstRetBase = function (object, funcName, func) {
  827. /// <summary>用新函数func重写对象object的函数funcName,先调用base函数,再调用this函数,返回base函数的返回值</summary>
  828. /// <param name="object" type="object">对象,若不指定(例如null)则参数funcName必须为函数</param>
  829. /// <param name="funcName" type="string or function">函数 或 在对象(object)中的函数名称</param>
  830. /// <param name="func" type="function">重写的函数</param>
  831. return scope.virtual(object, funcName, func, { callThisFirst: false, retBase: true });
  832. };
  833. scope.virtualBaseFirstRetThis = function (object, funcName, func) {
  834. /// <summary>用新函数func重写对象object的函数funcName,先调用base函数,再调用this函数,返回this函数的返回值</summary>
  835. /// <param name="object" type="object">对象,若不指定(例如null)则参数funcName必须为函数</param>
  836. /// <param name="funcName" type="string or function">函数 或 在对象(object)中的函数名称</param>
  837. /// <param name="func" type="function">重写的函数</param>
  838. return scope.virtual(object, funcName, func, { callThisFirst: false, retBase: false });
  839. };
  840. scope.virtualThisFirstRetBase = function (object, funcName, func) {
  841. /// <summary>用新函数func重写对象object的函数funcName,先调用this函数,再调用base函数,返回base函数的返回值</summary>
  842. /// <param name="object" type="object">对象,若不指定(例如null)则参数funcName必须为函数</param>
  843. /// <param name="funcName" type="string or function">函数 或 在对象(object)中的函数名称</param>
  844. /// <param name="func" type="function">重写的函数</param>
  845. return scope.virtual(object, funcName, func, { callThisFirst: true, retBase: true });
  846. };
  847. scope.virtualThisFirstRetThis = function (object, funcName, func) {
  848. /// <summary>用新函数func重写对象object的函数funcName,先调用this函数,再调用base函数,返回this函数的返回值</summary>
  849. /// <param name="object" type="object">对象,若不指定(例如null)则参数funcName必须为函数</param>
  850. /// <param name="funcName" type="string or function">函数 或 在对象(object)中的函数名称</param>
  851. /// <param name="func" type="function">重写的函数</param>
  852. return scope.virtual(object, funcName, func, { callThisFirst: true, retBase: false });
  853. };
  854. })(scope['function'] || (scope['function'] = {}));
  855. /** 扩展 lith.localStorage 客户端存储
  856. * Date : 2019-04-17
  857. * author:Lith
  858. */
  859. ; (function (scope) {
  860. var objName = 'localStorage';
  861. var obj = scope[objName] || (scope[objName] = {});
  862. obj.set = function (key, value, expireSeconds) {
  863. /// <summary>存储值到对应的key中</summary>
  864. /// <param name="name" type="string">索引码或者名称,需要唯一.</param>
  865. /// <param name="value" type="string">具体的内容值</param>
  866. /// <param name="expireSeconds" type="int">过期秒数,不传则永不失效</param>
  867. var data = { value: value };
  868. if (expireSeconds && expireSeconds > 0) {
  869. data.expireTime = new Date().addSecond(expireSeconds).getTime();
  870. }
  871. try {
  872. localStorage.setItem(key, JSON.stringify(data));
  873. } catch (e) {
  874. }
  875. }
  876. obj.get = function (key) {
  877. /// <summary>根据对应的key返回对应值,找不到返回null</summary>
  878. /// <param name="key" type="string">索引码或者名称,需要唯一.</param>
  879. try {
  880. var data = localStorage.getItem(key);
  881. if (!data) return null;
  882. data = JSON.parse(data);
  883. if (data.expireTime) {
  884. if (data.expireTime < new Date().getTime()) {
  885. localStorage.removeItem(key);
  886. return null;
  887. }
  888. }
  889. return data.value;
  890. } catch (e) {
  891. return null;
  892. }
  893. }
  894. obj.delete = function (key) {
  895. /// <summary>根据对应的key删除值</summary>
  896. /// <param name="name" type="string">索引码或者名称,需要唯一.</param>
  897. localStorage.removeItem(key);
  898. }
  899. })(scope);
  900. /** 扩展 lith.cookie 客户端缓存
  901. * Date : 2017-09-22
  902. * author:Lith
  903. */
  904. ; (function (scope) {
  905. var objName = 'cookie';
  906. var obj = scope[objName] || (scope[objName] = {});
  907. obj.set = function (name, value, expires) {
  908. /// <summary>存储值到对应的key中</summary>
  909. /// <param name="name" type="string">索引码或者名称,需要唯一.</param>
  910. /// <param name="value" type="string">具体的内容值</param>
  911. /// <param name="expires" type="int">有效期到期时间,不传则默认有效期为1天.例如:new Date().getTime(),秒数值:1496305713020</param>
  912. if (!expires) {
  913. var Days = 1;
  914. var exp = new Date();
  915. expires = exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  916. expires = new Date(expires);
  917. }
  918. document.cookie = name + "=" + escape(value) + ";path=/;expires=" + expires.toGMTString();
  919. }
  920. obj.get = function (name) {
  921. /// <summary>根据对应的key返回对应存放在cookie中的值,找不到返回null</summary>
  922. /// <param name="name" type="string">索引码或者名称,需要唯一.</param>
  923. var arr;
  924. var reg = new RegExp("(^|)" + name + "=([^;]*)(;|$)")
  925. if (arr = document.cookie.match(reg))
  926. return unescape(arr[2]);
  927. else
  928. return null;
  929. }
  930. obj.delete = function (name) {
  931. /// <summary>根据对应的key删除存放在cookie中的值</summary>
  932. /// <param name="name" type="string">索引码或者名称,需要唯一.</param>
  933. var exp = new Date();
  934. exp.setTime(exp.getTime() - 1);
  935. exp = new Date(exp);
  936. var cval = this.get(name);
  937. if (cval)
  938. document.cookie = name + "=" + escape(cval) + ";expires=" + exp.toGMTString();
  939. }
  940. })(scope);
  941. /** 扩展 scope.browse 浏览器类别
  942. * Date : 2017-09-22
  943. * author:Lith
  944. */
  945. ; (function (scope) {
  946. scope.browser = function () {
  947. try {
  948. var userAgent = navigator.userAgent.toLowerCase();
  949. // Figure out what browser is being used
  950. var browser = {
  951. version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
  952. safari: /webkit/.test(userAgent),
  953. opera: /opera/.test(userAgent),
  954. msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
  955. mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent)
  956. };
  957. return browser;
  958. } catch (e) {
  959. return {};
  960. }
  961. }
  962. })(scope);
  963. /** 扩展 lith.document(动态加载 js 和 css,打开新窗口,获取url参数等)
  964. * Date : 2018-08-02
  965. * author:Lith
  966. */
  967. ; (function (scope) {
  968. var objName = 'document';
  969. var obj = scope[objName] || (scope[objName] = {});
  970. var toXmlStr = function toXmlStr(str) {
  971. /// <summary> 向xml转换。
  972. /// <para>例如 转换 &lt;a title=&quot;&quot;&gt;ok&lt;/a&gt; 中a标签的内容体(innerHTML)或 转换 &lt;a title=&quot;&quot;&gt;ok&lt;/a&gt; 中title的值。</para>
  973. /// <para>转换 &amp; 双引号 &lt; &gt; 为 &amp;amp; &amp;quot; &amp;lt; &amp;gt;(注: 单引号 对应 &amp;apos; (&amp;#39;) ,但有些浏览器不支持,故此函数不转换。)</para>
  974. /// </summary>
  975. /// <returns type="string">转换后的字符串</returns>
  976. return str.replace(/\&/g, "&amp;").replace(/\"/g, "&quot;").replace(/\</g, "&lt;").replace(/\>/g, "&gt;");
  977. };
  978. obj.url_GetArg = function (src, key) {
  979. /// <summary>获取当前src中的参数
  980. /// <para>demo: var jsName=lith.document.url_GetArg("aaa.html?a=1&amp;b=2",'name');</para>
  981. /// </summary>
  982. /// <param name="src" type="string">例如:"?a=1&amp;b=2"</param>
  983. /// <param name="key" type="string">若不为字符串,则返回把所有参数做为键值对的对象。若为字符串,且获取不到,则返回 null</param>
  984. /// <returns type="string or object"></returns>
  985. if (arguments.length == 1) {
  986. key = src;
  987. src = location.search;
  988. }
  989. if ('string' == typeof key) {
  990. var v = (src.match(new RegExp("(?:\\?|&)" + key + "=(.*?)(?=&|$)")) || ['', null])[1];
  991. return v && decodeURIComponent(v);
  992. } else {
  993. var reg = /(?:\?|&)(.*?)=(.*?)(?=&|$)/g, temp, res = {};
  994. while ((temp = reg.exec(src)) != null)
  995. res[temp[1]] = decodeURIComponent(temp[2]);
  996. return res;
  997. }
  998. //var src = location.search;
  999. //if (src.length < 2 || src.charAt(0) != '?') {
  1000. // return null;
  1001. //}
  1002. //var params = src.substring(1).split('&');
  1003. //var ps = null;
  1004. //for (var i in params) {
  1005. // ps = params[i].split('=');
  1006. // if (decodeURIComponent(ps[0]) == name) {
  1007. // return decodeURIComponent(ps[1]);
  1008. // }
  1009. //}
  1010. //return null;
  1011. };
  1012. obj.script_getArg = function (key) {
  1013. /// <summary>返回所在脚本src参数。
  1014. /// <para>demo: var jsName=lith.document.script_getArg('name');</para>
  1015. /// <para>不要在方法中调用此方法,否则可能始终获取的是最后一个js的文件的参数</para>
  1016. /// </summary>
  1017. /// <param name="key" type="string">若不为字符串,则返回把所有参数做为键值对的对象。若为字符串,且获取不到,则返回 null</param>
  1018. ////假如上面的js是在这个js1.js的脚本中<script type="text/javascript" src="js1.js?a=abc&b=汉字&c=123"></script>
  1019. var scripts = document.getElementsByTagName("script"),
  1020. //因为当前dom加载时后面的script标签还未加载,所以最后一个就是当前的script
  1021. script = scripts[scripts.length - 1],
  1022. src = script.src;
  1023. return obj.url_GetArg(src, key);
  1024. };
  1025. obj.openWin = function (html) {
  1026. /// <summary>在新页面中显示html</summary>
  1027. /// <param name="html" type="String">html 代码</param>
  1028. /// <returns type="Window"></returns>
  1029. var oWin = window.open('');
  1030. oWin.document.write(html);
  1031. return oWin;
  1032. };
  1033. obj.openForm = function (param) {
  1034. /// <summary>在新页面中新建Form,发送请求。
  1035. /// <para> demo:lith.document.openForm({url:'http://www.a.com',reqParam:{a:3},type:'post'}); </para>
  1036. /// </summary>
  1037. /// <param name="param" type="object">
  1038. /// <para> demo:{url:'http://www.a.com',reqParam:{a:3},type:'post'} </para>
  1039. /// <para> url[string]:要打开的链接地址。</para>
  1040. /// <para> reqParam[object]:请求参数。</para>
  1041. /// <para> type[string]:请求方式。可为'get'、'post'、'put'等,不指定则为get。</para>
  1042. /// </param>
  1043. /// <param name="url" type="string"></param>
  1044. /// <param name="postParam" type="object">。</param>
  1045. /// <param name="type" type="string"></param>
  1046. /// <returns type="window"></returns>
  1047. var html = '<!DOC' + 'TYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head ><meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /><tit';
  1048. html += 'le>请稍等</title> </head><body>';
  1049. html += '<h3>请稍等 ...</h3>';
  1050. html += '<form accept-charset="UTF-8" name="tempForm" action="' + toXmlStr(param.url) + '" method="' + (param.type || "get") + '" style="display:none">';
  1051. for (var name in param.reqParam) {
  1052. html += '<input type="hidden" name="' + toXmlStr(name) + '" value="' + toXmlStr(param.reqParam[name]) + '"/>';
  1053. }
  1054. html += '</form>';
  1055. html += '<script type="text/javascript">document.tempForm.submit();</sc' + 'ript>';
  1056. html += '</body></html>';
  1057. return obj.openWin(html);
  1058. };
  1059. obj.iframeSetOnload = function (iframe, event) {
  1060. //if...else...是一种兼容ie的写法
  1061. if (iframe.attachEvent) {
  1062. iframe.attachEvent("onload", event);
  1063. } else {
  1064. iframe.onload = event;
  1065. }
  1066. };
  1067. obj.loadJs = function (jsSrc) {
  1068. /// <summary>载入js文件。在文档加载过程中或已经加载完成后载入js文件。</summary>
  1069. /// <param name="jsSrc" type="string">例如:"/Scripts/jquery-easyui/jquery.easyui.min.js"</param>
  1070. if (document.readyState == "loading") {
  1071. loadJs_BeforeDocumentLoaded(jsSrc);
  1072. } else {
  1073. loadJs_AfterDocumentLoaded(jsSrc);
  1074. }
  1075. function loadJs_BeforeDocumentLoaded(jsSrc) {
  1076. /// <summary>载入js文件。在文档加载过程中载入js文件。</summary>
  1077. /// <param name="jsSrc" type="string">例如:"/Scripts/jquery-easyui/jquery.easyui.min.js"</param>
  1078. // <script type="text/javascript" src="/Scripts/jquery-easyui/jquery.easyui.min.js"></script>
  1079. document.write('<script type="text/javascript" src="' + toXmlStr('' + jsSrc) + '"></script>');
  1080. }
  1081. function loadJs_AfterDocumentLoaded(jsSrc) {
  1082. /// <summary>载入js文件。在文档已经加载完成后载入js文件。</summary>
  1083. /// <param name="jsSrc" type="string">例如:"/Scripts/jquery-easyui/jquery.easyui.min.js"</param>
  1084. var eJs = document.createElement('script');
  1085. eJs.type = 'text/javascript';
  1086. eJs.language = 'javascript';
  1087. eJs.src = jsSrc;
  1088. document.getElementsByTagName("head")[0].appendChild(eJs);
  1089. }
  1090. };
  1091. obj.loadCss = function (cssSrc) {
  1092. /// <summary>载入css文件。在文档加载过程中或已经加载完成后载入css文件。</summary>
  1093. /// <param name="cssSrc" type="string">例如:"/Scripts/jquery-easyui/themes/icon.css"</param>
  1094. if (document.readyState == "loading") {
  1095. loadCss_BeforeDocumentLoaded(cssSrc);
  1096. } else {
  1097. loadCss_AfterDocumentLoaded(cssSrc);
  1098. }
  1099. function loadCss_BeforeDocumentLoaded(cssSrc) {
  1100. /// <summary>载入css文件。在文档加载过程中载入css文件。</summary>
  1101. /// <param name="cssSrc" type="string">例如:"/Scripts/jquery-easyui/themes/icon.css"</param>
  1102. // <link rel="stylesheet" type="text/css" href="/Scripts/jquery-easyui/themes/icon.css" />
  1103. document.write('<link rel="stylesheet" type="text/css" href="' + toXmlStr('' + cssSrc) + '" />');
  1104. }
  1105. function loadCss_BeforeDocumentLoaded(cssSrc) {
  1106. /// <summary>载入css文件。在文档已经加载完成后载入css文件。</summary>
  1107. /// <param name="cssSrc" type="string">例如:"/Scripts/jquery-easyui/themes/icon.css"</param>
  1108. var eCss = document.createElement('link');
  1109. eCss.rel = 'Stylesheet';
  1110. eCss.type = 'text/css';
  1111. eCss.href = cssSrc;
  1112. document.getElementsByTagName("head")[0].appendChild(eCss);
  1113. }
  1114. };
  1115. obj.addCss = function (cssText) {
  1116. /// <summary>添加新的CSS样式节点。demo: lith.document.addCss('.header{ background-color:#8f8;}');</summary>
  1117. /// <param name="cssText" type="String"></param>
  1118. var style = document.createElement('style'); //创建一个style元素
  1119. style.type = 'text/css'; //这里必须显示设置style元素的type属性为text/css,否则在ie中不起作用
  1120. var head = document.head || document.getElementsByTagName('head')[0]; //获取head元素
  1121. head.appendChild(style); //把创建的style元素插入到head中
  1122. if (style.styleSheet) { //IE
  1123. var func = function () {
  1124. try {
  1125. //防止IE中stylesheet数量超过限制而发生错误
  1126. style.styleSheet.cssText = cssText;
  1127. } catch (e) { }
  1128. }
  1129. //如果当前styleSheet还不能用,则放到异步中则行
  1130. if (style.styleSheet.disabled) {
  1131. setTimeout(func, 10);
  1132. } else {
  1133. func();
  1134. }
  1135. } else { //w3c
  1136. //w3c浏览器中只要创建文本节点插入到style元素中就行了
  1137. var textNode = document.createTextNode(cssText);
  1138. style.appendChild(textNode);
  1139. }
  1140. };
  1141. })(scope);
  1142. })('undefined' === typeof (lith) ? lith = {} : lith);