comm.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. var VisibleMenu = ""; // 記錄目前顯示的子選單的 ID
  2. // 顯示或隱藏子選單
  3. function switchMenu(theMainMenu, theSubMenu, theEvent) {
  4. var SubMenu = document.getElementById(theSubMenu);
  5. if (SubMenu.style.display == "none") {
  6. // 顯示子選單
  7. SubMenu.style.display = "block";
  8. hideMenu(); // 隱藏子選單
  9. VisibleMenu = theSubMenu;
  10. } else {
  11. // 隱藏子選單
  12. if (theEvent != "MouseOver" || VisibleMenu != theSubMenu) {
  13. SubMenu.style.display = "none";
  14. VisibleMenu = "";
  15. }
  16. }
  17. }
  18. // 隱藏子選單
  19. function hideMenu() {
  20. if (VisibleMenu != "") {
  21. document.getElementById(VisibleMenu).style.display = "none";
  22. }
  23. VisibleMenu = "";
  24. }
  25. function com_show_sub_tree(obj) {
  26. eParent = obj.parentNode;
  27. var x = eParent.getElementsByTagName("ul");
  28. if (x[0].style.display == "none") {
  29. x[0].style.display = "block";
  30. obj.getElementsByTagName("span")[0].innerHTML = "-";
  31. } else {
  32. x[0].style.display = "none";
  33. obj.getElementsByTagName("span")[0].innerHTML = "+";
  34. }
  35. }
  36. //check if the next sibling node is an element node
  37. function com_get_nextsibling(n) {
  38. let x = n.nextSibling;
  39. if (x != null) {
  40. while (x.nodeType != 1) {
  41. x = x.nextSibling;
  42. if (x == null) {
  43. return null;
  44. }
  45. }
  46. }
  47. return x;
  48. }
  49. function com_guid(trim = true, hyphen = false) {
  50. //guid生成器
  51. if (trim) {
  52. if (hyphen) {
  53. var tmp = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
  54. } else {
  55. var tmp = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  56. }
  57. } else {
  58. if (hyphen) {
  59. var tmp = "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}";
  60. } else {
  61. var tmp = "{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}";
  62. }
  63. }
  64. var guid = tmp.replace(/[xy]/g, function (c) {
  65. var r = (Math.random() * 16) | 0,
  66. v = c == "x" ? r : (r & 0x3) | 0x8;
  67. return v.toString(16);
  68. });
  69. return guid.toUpperCase();
  70. }
  71. function com_uuid() {
  72. //guid生成器
  73. let tmp = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
  74. let uuid = tmp.replace(/[xy]/g, function (c) {
  75. var r = (Math.random() * 16) | 0,
  76. v = c == "x" ? r : (r & 0x3) | 0x8;
  77. return v.toString(16);
  78. });
  79. return uuid.toLowerCase();
  80. }
  81. function com_xmlToString(elem) {
  82. var serialized;
  83. try {
  84. serializer = new XMLSerializer();
  85. serialized = serializer.serializeToString(elem);
  86. } catch (e) {
  87. serialized = elem.xml;
  88. }
  89. return serialized;
  90. }
  91. function com_getPaliReal(inStr) {
  92. if (typeof inStr == "undefined") {
  93. return "";
  94. }
  95. var paliletter = "abcdefghijklmnoprstuvyāīūṅñṭḍṇḷṃ";
  96. var output = "";
  97. inStr = inStr.toLowerCase();
  98. inStr = inStr.replace(/ṁ/g, "ṃ");
  99. inStr = inStr.replace(/ŋ/g, "ṃ");
  100. for (const iterator of inStr) {
  101. if (paliletter.indexOf(iterator) != -1) {
  102. output += iterator;
  103. }
  104. }
  105. return output;
  106. }
  107. function com_getPaliEn(inStr) {
  108. if (typeof inStr == "undefined") {
  109. return "";
  110. }
  111. inStr = inStr.toLowerCase();
  112. inStr = inStr.replace(/ā/g, "a");
  113. inStr = inStr.replace(/ī/g, "i");
  114. inStr = inStr.replace(/ū/g, "u");
  115. inStr = inStr.replace(/ṅ/g, "n");
  116. inStr = inStr.replace(/ñ/g, "n");
  117. inStr = inStr.replace(/ṇ/g, "n");
  118. inStr = inStr.replace(/ṭ/g, "t");
  119. inStr = inStr.replace(/ḍ/g, "d");
  120. inStr = inStr.replace(/ḷ/g, "l");
  121. inStr = inStr.replace(/ṃ/g, "m");
  122. return inStr;
  123. }
  124. function getCookie(c_name) {
  125. if (document.cookie.length > 0) {
  126. c_start = document.cookie.indexOf(c_name + "=");
  127. if (c_start != -1) {
  128. c_start = c_start + c_name.length + 1;
  129. c_end = document.cookie.indexOf(";", c_start);
  130. if (c_end == -1) c_end = document.cookie.length;
  131. return unescape(document.cookie.substring(c_start, c_end));
  132. } else {
  133. return "";
  134. }
  135. } else {
  136. return "";
  137. }
  138. }
  139. function setTimeZone() {
  140. const date = new Date();
  141. const timezone = date.getTimezoneOffset();
  142. setCookie("timezone", timezone, 10);
  143. }
  144. function setCookie(c_name, value, expiredays) {
  145. var exdate = new Date();
  146. exdate.setDate(exdate.getDate() + expiredays);
  147. document.cookie =
  148. c_name + "=" + escape(value) + (expiredays == null ? "" : "; expires=" + exdate.toGMTString() + ";path=/");
  149. }
  150. function copy_to_clipboard(strInput) {
  151. const input = document.createElement("input");
  152. input.setAttribute("readonly", "readonly");
  153. input.setAttribute("value", strInput);
  154. document.body.appendChild(input);
  155. // input.setSelectionRange(0, strInput.length);
  156. // input.focus();
  157. input.select();
  158. if (document.execCommand("copy")) {
  159. document.execCommand("copy");
  160. console.log("复制成功");
  161. ntf_show("“" + strInput + "”" + gLocal.gui.copied_to_clipboard);
  162. }
  163. document.body.removeChild(input);
  164. }
  165. function getPassDataTime(time) {
  166. let currDate = new Date();
  167. let pass = currDate.getTime() - time;
  168. let strPassTime = "";
  169. if (pass < 120 * 1000) {
  170. //二分钟内
  171. strPassTime = Math.floor(pass / 1000) + gLocal.gui.secs_ago;
  172. } else if (pass < 7200 * 1000) {
  173. //二小时内
  174. strPassTime = Math.floor(pass / 1000 / 60) + gLocal.gui.mins_ago;
  175. } else if (pass < 3600 * 48 * 1000) {
  176. //二天内
  177. strPassTime = Math.floor(pass / 1000 / 3600) + gLocal.gui.hs_ago;
  178. } else if (pass < 3600 * 24 * 14 * 1000) {
  179. //二周内
  180. strPassTime = Math.floor(pass / 1000 / 3600 / 24) + gLocal.gui.days_ago;
  181. } else if (pass < 3600 * 24 * 60 * 1000) {
  182. //二个月内
  183. strPassTime = Math.floor(pass / 1000 / 3600 / 24 / 7) + gLocal.gui.weeks_ago;
  184. } else if (pass < 3600 * 24 * 365 * 1000) {
  185. //一年内
  186. strPassTime = Math.floor(pass / 1000 / 3600 / 24 / 30) + gLocal.gui.months_ago;
  187. } else if (pass < 3600 * 24 * 730 * 1000) {
  188. //超过1年小于2年
  189. strPassTime = Math.floor(pass / 1000 / 3600 / 24 / 365) + gLocal.gui.year_ago;
  190. } else {
  191. strPassTime = Math.floor(pass / 1000 / 3600 / 24 / 365) + gLocal.gui.years_ago;
  192. }
  193. return strPassTime;
  194. }
  195. function getFullDataTime(time) {
  196. let inputDate = new Date();
  197. inputDate.setTime(time);
  198. return inputDate.toLocaleString();
  199. }
  200. function getDataTime(time) {
  201. let today = new Date();
  202. let inputDate = new Date();
  203. inputDate.setTime(time);
  204. let day = inputDate.getDate();
  205. let month = inputDate.getMonth() + 1;
  206. let year = inputDate.getFullYear();
  207. let hours = inputDate.getHours();
  208. let minutes = inputDate.getMinutes();
  209. let seconds = inputDate.getSeconds();
  210. let today_day = today.getDate();
  211. let today_month = today.getMonth() + 1;
  212. let today_year = today.getFullYear();
  213. let today_hours = today.getHours();
  214. let today_minutes = today.getMinutes();
  215. let today_seconds = today.getSeconds();
  216. let output = "";
  217. if (today_day == day && today_month == month && today_year == year) {
  218. //当天
  219. output = hours + ":" + minutes;
  220. } else if (today_year != year) {
  221. //不同年
  222. output = year;
  223. } else {
  224. //同一年
  225. output = month + "/" + day;
  226. }
  227. return output;
  228. }
  229. function str_diff(str1, str2) {
  230. let output = "";
  231. const diff = Diff.diffChars(str1, str2);
  232. diff.forEach((part) => {
  233. // green for additions, red for deletions
  234. // grey for common parts
  235. if (part.added) {
  236. output += "<ins>" + part.value + "</ins>";
  237. } else if (part.removed) {
  238. output += "<del>" + part.value + "</del>";
  239. } else {
  240. output += part.value;
  241. }
  242. });
  243. return output;
  244. }
  245. function testCJK(string){
  246. /*
  247. \u4e00-\u9fa5 (中文)
  248. U+4E00至U+9FFF[1]
  249. U+3400至U+4DBF[2](扩展A)
  250. U+20000至U+2A6DF[3](扩展B)
  251. U+2A700至U+2B73F[4](扩展C)
  252. U+2B740至U+2B81F[5](扩展D)
  253. U+2B820至U+2CEAF[6](扩展E)
  254. U+F900至U+FAFF[7](兼容)
  255. U+2F800至U+2FA1F[8](兼容补充)
  256. U+2F00至U+2FDF[9](康熙部首)
  257. U+2E80至U+2EFF[10](部首补充)
  258. U+31C0至U+31EF[11](笔画)
  259. \x3130-\x318F (韩文
  260. \xAC00-\xD7A3 (韩文)
  261. Unicode范围 U+AC00-U+D7A3,
  262. U+1100-U+11FF,
  263. U+3131-U+318E,
  264. U+FFA1-U+FFDC
  265. \u3040-\u309f (日文)ひらがな平仮名
  266. U+4E00–U+9FBF 汉字; U+3040–U+309F 平假名; U+30A0–U+30FF 片假名
  267. */
  268. reg = /[\u4e00-\u9fa5]+/;//cn
  269. return reg.test(string);
  270. }
  271. //所有页面都需要在加载的的时候设置浏览器时区
  272. setTimeZone();