chem.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. var list = ['',
  2. 'H', 'He',
  3. 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne',
  4. 'Na', 'Mg', 'Al', 'Si', 'P', 'S', 'Cl', 'Ar',
  5. 'K', 'Ca', 'Sc', 'Ti', 'V', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr',
  6. 'Rb', 'Sr', 'Y', 'Zr', 'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd', 'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te', 'I', 'Xe',
  7. 'Cs', 'Ba', 'La', 'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er', 'Tm', 'Yb', 'Lu', 'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt', 'Au', 'Hg', 'Tl', 'Pb', 'Bi', 'Po', 'At', 'Rn',
  8. 'Fr', 'Ra', 'Ac', 'Th', 'Pa', 'U', 'Np', 'Pu', 'Am', 'Cm', 'Bk', 'Cf', 'Es', 'Fm', 'Md', 'No', 'Lr', 'Rf', 'Db', 'Sg', 'Bh', 'Hs', 'Mt', 'Ds', 'Rg', 'Cn', 'Nh', 'Fl', 'Mc', 'Lv', 'Ts', 'Og'
  9. ]
  10. var weighList = {
  11. 'H': 1, 'He': 4,
  12. 'Li': 7, 'Be': 9, 'B': 11, 'C': 12, 'N': 14, 'O': 16, 'F': 19, 'Ne': 20,
  13. 'Na': 23, 'Mg': 24, 'Al': 27, 'Si': 28, 'P': 31, 'S': 32, 'Cl': 35.5, 'Ar': 40,
  14. 'K': 39, 'Ca': 40, 'Sc': 45, 'Ti': 48, 'V': 51, 'Cr': 52, 'Mn': 55, 'Fe': 56, 'Co': 59, 'Ni': 59, 'Cu': 64, 'Zn': 65.5, 'Ga': 70, 'Ge': 72.5, 'As': 75, 'Se': 79, 'Br': 80, 'Kr': 84,
  15. 'Rb': 85.5, 'Sr': 87.5, 'Y': 89, 'Zr': 91, 'Nb': 93, 'Mo': 96, 'Tc': 99, 'Ru': 101, 'Rh': 103, 'Pd': 106.5, 'Ag': 108, 'Cd': 112.5, 'In': 115, 'Sn': 118.5, 'Sb': 122, 'Te': 127.5, 'I': 127, 'Xe': 131,
  16. 'Cs': 133, 'Ba': 137, 'La': 139, 'Ce': 140, 'Pr': 141, 'Nd': 144, 'Pm': 147, 'Sm': 150.5, 'Eu': 152, 'Gd': 157, 'Tb': 159, 'Dy': 162.5, 'Ho': 165, 'Er': 167, 'Tm': 169, 'Yb': 173, 'Lu': 175, 'Hf': 178.5, 'Ta': 181, 'W': 184, 'Re': 186, 'Os': 190, 'Ir': 192, 'Pt': 195, 'Au': 197, 'Hg': 200.5, 'Tl': 204.5, 'Pb': 207, 'Bi': 209, 'Po': 209, 'At': 210, 'Rn': 222,
  17. 'Fr': 223, 'Ra': 226, 'Ac': 227, 'Th': 232, 'Pa': 231, 'U': 238, 'Np': 237, 'Pu': 244, 'Am': 243, 'Cm': 247, 'Bk': 247, 'Cf': 251, 'Es': 252, 'Fm': 257, 'Md': 258, 'No': 259, 'Lr': 260, 'Rf': 261, 'Db': 262, 'Sg': 263, 'Bh': 262, 'Hs': 265, 'Mt': 266, 'Ds': 269, 'Rg': 272, 'Cn': 285, 'Nh': 284, 'Fl': 289, 'Mc': 288, 'Lv': 293, 'Ts': 291, 'Og': 294
  18. }
  19. var weighList2 = {
  20. 'H': 1.008, 'He': 4.003,
  21. 'Li': 6.941, 'Be': 9.012, 'B': 10.81, 'C': 12.01, 'N': 14.01, 'O': 16.00, 'F': 19.00, 'Ne': 20.18,
  22. 'Na': 22.99, 'Mg': 24.31, 'Al': 26.98, 'Si': 28.09, 'P': 30.97, 'S': 32.07, 'Cl': 35.45, 'Ar': 49.95,
  23. 'K': 39.10, 'Ca': 40.08, 'Sc': 44.96, 'Ti': 47.87, 'V': 50.94, 'Cr': 52.00, 'Mn': 54.94, 'Fe': 55.85, 'Co': 58.93, 'Ni': 58.69, 'Cu': 63.55, 'Zn': 65.39, 'Ga': 79.72, 'Ge': 72.61, 'As': 74.92, 'Se': 78.96, 'Br': 79.90, 'Kr': 83.80,
  24. 'Rb': 85.47, 'Sr': 87.62, 'Y': 88.91, 'Zr': 91.22, 'Nb': 92.91, 'Mo': 95.94, 'Tc': 99, 'Ru': 101.1, 'Rh': 102.9, 'Pd': 106.4, 'Ag': 107.9, 'Cd': 112.4, 'In': 114.8, 'Sn': 118.7, 'Sb': 121.8, 'Te': 127.6, 'I': 126.9, 'Xe': 131.3,
  25. 'Cs': 132.9, 'Ba': 137.3, 'La': 138.9, 'Ce': 140.1, 'Pr': 140.9, 'Nd': 144.2, 'Pm': 147, 'Sm': 150.4, 'Eu': 152.0, 'Gd': 157.3, 'Tb': 158.9, 'Dy': 162.5, 'Ho': 164.9, 'Er': 167.3, 'Tm': 168.9, 'Yb': 173.0, 'Lu': 175.0, 'Hf': 178.5, 'Ta': 180.9, 'W': 183.8, 'Re': 186.2, 'Os': 190.2, 'Ir': 192.2, 'Pt': 195.1, 'Au': 197.0, 'Hg': 200.6, 'Tl': 204.4, 'Pb': 207.2, 'Bi': 209.0, 'Po': 209, 'At': 210, 'Rn': 222,
  26. 'Fr': 223, 'Ra': 226.0, 'Ac': 227.0, 'Th': 232.0, 'Pa': 231.0, 'U': 238.0, 'Np': 237.0, 'Pu': 244, 'Am': 243, 'Cm': 247, 'Bk': 247, 'Cf': 251, 'Es': 252, 'Fm': 257, 'Md': 258, 'No': 259, 'Lr': 260, 'Rf': 261, 'Db': 262, 'Sg': 263, 'Bh': 262, 'Hs': 265, 'Mt': 266, 'Ds': 269, 'Rg': 272, 'Cn': 285, 'Nh': 284, 'Fl': 289, 'Mc': 288, 'Lv': 293, 'Ts': 291, 'Og': 294
  27. }
  28. var bracket = {}
  29. function getco(str) {
  30. var n = str.length, ret = 0
  31. for (var i = 0; i < n; i++) {
  32. var ch = str.charAt(i)
  33. if (isNaN(ch)) break;
  34. ret *= 10;
  35. ret += ch - 0;
  36. }
  37. str = str.slice(i, n)
  38. return [ret, str, i]
  39. }
  40. function workMolecule(str, st) {
  41. // console.log(str, st)
  42. var n = str.length, co = 1, ans = {};
  43. for (let i = 0; i < n; i++) {
  44. var ch = str.charAt(i)
  45. var f = {}
  46. if (ch == '(') {
  47. f = workMolecule(str.slice(i + 1, bracket[i + st] - st), st + i + 1)
  48. i = bracket[i + st] - st;
  49. } else if ('A' <= ch && ch <= 'Z') {
  50. if (i + 1 < n) {
  51. let nch = str.charAt(i + 1)
  52. if ('a' <= nch && nch <= 'z') i++, ch += nch
  53. }
  54. if (f[ch]) f[ch]++;
  55. else f[ch] = 1;
  56. } else return {}
  57. let g = getco(str.slice(i + 1, n))
  58. if (g[0]) {
  59. for (key in f) {
  60. f[key] *= g[0]
  61. }
  62. }
  63. for (key in f) {
  64. if (ans[key]) ans[key] = ans[key] + f[key]
  65. else ans[key] = f[key]
  66. }
  67. i += g[2]
  68. }
  69. // console.log('ans', ans)
  70. return ans;
  71. }
  72. function parseMolecule(str) {
  73. str = str.replace(/[\[{]/g, "(").replace(/[\]}]/g, ")")
  74. str = str.replace(/<\d*e[\+\-]>*/g, "");
  75. // console.log('Parsing molecule', str)
  76. var n = str.length, co = 1, stack = [], top = 0;
  77. for (let i = 0; i < n; i++) {
  78. var ch = str.charAt(i);
  79. if (ch == '(') stack[top] = i, top++;
  80. else if (ch == ')') bracket[stack[top - 1]] = i, bracket[i] = stack[top - 1], top--;
  81. if (top < 0) return {}
  82. }
  83. if (top != 0) return {};
  84. var g = getco(str)
  85. if (g[0]) co = g[0]
  86. var f = workMolecule(g[1], g[2])
  87. for (key in f) {
  88. f[key] *= co;
  89. }
  90. return f;
  91. }
  92. function eps(a, mode = 0) {
  93. if (mode) {
  94. var s = a.toString().split('e')
  95. console.log(a)
  96. console.log(s[0])
  97. if (s.length == 1) return parseFloat(parseFloat(a).toFixed(4))
  98. return Math.round(parseFloat(s[0]) * 10000) / 10000 + 'e' + s[1];
  99. } else return a
  100. }
  101. function weigh(hash, mode = 0) {
  102. var ans = 0
  103. if (mode) {
  104. for (key in hash) {
  105. if (weighList2[key]) ans += hash[key] * weighList2[key]
  106. }
  107. } else {
  108. for (key in hash) {
  109. if (weighList[key]) ans += hash[key] * weighList[key]
  110. }
  111. }
  112. return eps(ans, mode);
  113. }
  114. function weighEquation(str, mode = 0) {
  115. str = str.replace(/<\d*e[\+\-]>*/g, "").replace(/[^\dA-Za-z<>\(\)\+\-=\.;]/g, "");
  116. var q = str.split(/[=\+\-\.;]/g)
  117. var p = str.replace(/[^=\+\-\.;]/g, "").replace(/\./g, '*')
  118. var ans = ""
  119. if (str == "") return ""
  120. var n = p.length, sum = 0, gsum = "";
  121. for (let i = 0; i <= n; i++) {
  122. var k = weigh(parseMolecule(q[i]), mode)
  123. sum += k
  124. if (p.charAt(i) == '=') gsum += eps(sum, mode) + '=', sum = 0;
  125. ans = ans + k + p.charAt(i)
  126. }
  127. if (n) ans += '<br>' + gsum + eps(sum, mode)
  128. return ans.replace(/\*/g, "\\cdot");
  129. }
  130. function parseEquation(str) {
  131. str = str.replace(/ /g, '')
  132. var q = str.split(/[=\+\-\.]/g)
  133. var p = str.replace(/[^=\+\-\.]/g, "")
  134. var ans = {}
  135. var n = p.length;
  136. for (let i = 0; i <= n; i++) {
  137. var f = parseMolecule(q[i])
  138. for (key in f) {
  139. if (ans[key]) ans[key] = ans[key] + f[key]
  140. else ans[key] = f[key]
  141. }
  142. }
  143. return ans
  144. }
  145. function renderEquation(str) {
  146. str = str.replace(/[\[{]/g, "(").replace(/[\]}]/g, ")");
  147. str = str.replace(/([\+\=\.;])\1+/g, '$1')
  148. str = str.replace(/[^\dA-Za-z<>\(\)\+\-=\.;]/g, "");
  149. // console.log('Rendering equation', str)
  150. str = str.replace(/([A-Za-z]+)/g, "\\text{$1}");
  151. str = str.replace(/<(\d*)\\text\{e\}([\+\-])>/g, "^{$1$2}");
  152. str = str.replace(/([\}\)])(\d+)/g, "$1_{$2}");
  153. str = str.replace(/\./g, "\\cdot");
  154. str = "<a href='#'>\\(" + str.replace(/([\+\-=\.;])([^\}])/g, "\\)</a> \\($1\\) <a href='#'>\\($2") + '\\)</a>';
  155. // console.log(str)
  156. return str
  157. }
  158. //PROCESS-------------------------------
  159. var mode = 'bal', balInput, balText = '', inputText
  160. $().ready(function () {
  161. balInput = $("#balInput")[0]
  162. setBal();
  163. setQryEq();
  164. $("#balInput").keydown(function (e) {
  165. if (e.keyCode == 13 && mode == 'bal') {
  166. $("#balBtn")[0].click();
  167. }
  168. });
  169. $(function () { $("[data-toggle='tooltip']").tooltip(); });
  170. })
  171. function setBal() {
  172. $('.frame')[0].innerHTML = renderEquation(balText);
  173. MathJax.typeset()
  174. $('#balBtn').text('配平')
  175. $('#balShare').removeClass('disabled')
  176. $('#balInput').attr('placeholder', 'CrI3+Cl2+KOH=K2CrO4+KIO4+KCl+H2O')
  177. $('#balBtn').attr('href', '/chem?CrI3+Cl2+KOH=K2CrO4+KIO4+KCl+H2O')
  178. $('#balBtn').removeClass('disabled')
  179. mode = 'bal'
  180. input()
  181. }
  182. function setWeigh() {
  183. $('#balBtn').text('相对质量')
  184. $('#balShare').addClass('disabled')
  185. $('#balInput').attr('placeholder', 'CH3CHO+2Ag(NH3)2OH=CH3COONH4+2Ag+3NH3+H2O')
  186. $('#balBtn').attr('href', '')
  187. $('#balBtn').addClass('disabled')
  188. mode = 'weigh'
  189. input()
  190. }
  191. function setWeigh2() {
  192. $('#balBtn').text('相对质量*')
  193. $('#balShare').addClass('disabled')
  194. $('#balInput').attr('placeholder', 'CH3CHO+2Ag(NH3)2OH=CH3COONH4+2Ag+3NH3+H2O')
  195. $('#balBtn').attr('href', '')
  196. $('#balBtn').addClass('disabled')
  197. mode = 'weigh2'
  198. input()
  199. }
  200. function input() {
  201. inputText = balInput.value
  202. if (mode == 'bal') {
  203. $('.frame')[0].innerHTML = renderEquation((inputText == '') ? 'CrI3+Cl2+KOH=K2CrO4+KIO4+KCl+H2O' : inputText) + '<br>'
  204. + '<span class="glyphicon glyphicon-chevron-down" aria-hidden="true"></span><br>' + ((balText != '') ? (renderEquation(balText)) : ('...'));
  205. MathJax.typeset()
  206. } else if (mode == 'weigh') {
  207. $('.frame')[0].innerHTML = renderEquation((inputText == '') ? 'CH3CHO+2Ag(NH3)2OH=CH3COONH4+2Ag+3NH3+H2O' : inputText) + '<br>'
  208. + weighEquation((inputText == '') ? 'CH3CHO+2Ag(NH3)2OH=CH3COONH4+2Ag+3NH3+H2O' : inputText);
  209. MathJax.typeset()
  210. } else if (mode == 'weigh2') {
  211. $('.frame')[0].innerHTML = renderEquation((inputText == '') ? 'CH3CHO+2Ag(NH3)2OH=CH3COONH4+2Ag+3NH3+H2O' : inputText) + '<br>'
  212. + weighEquation((inputText == '') ? 'CH3CHO+2Ag(NH3)2OH=CH3COONH4+2Ag+3NH3+H2O' : inputText, 1);
  213. MathJax.typeset()
  214. }
  215. }
  216. var running;
  217. function balance() {
  218. input()
  219. if (running || mode != 'bal') return;
  220. $('#balBtn').text('配平...')
  221. $('#balBtn').addClass('disabled')
  222. $('.frame').addClass('text-muted')
  223. running = 1;
  224. return $.get('/chem?' + ((inputText == '') ? 'CrI3+Cl2+KOH=K2CrO4+KIO4+KCl+H2O' : inputText), function (e) {
  225. $('.frame')[0].innerHTML = (e.charAt(0) == '!') ? ('<pre class="text-danger bg-danger">' + e.slice(1, e.length) + '</pre>') : (renderEquation((inputText == '') ? 'CrI3+Cl2+KOH=K2CrO4+KIO4+KCl+H2O' : inputText)
  226. + '<br><span class="glyphicon glyphicon-chevron-down" aria-hidden="true"></span><br>' + renderEquation(e));
  227. if (e.charAt(0) != '!') balText = e
  228. MathJax.typeset()
  229. $('#balBtn').text('配平')
  230. $('#balBtn').removeClass('disabled')
  231. $('.frame').removeClass('text-muted')
  232. running = 0;
  233. })
  234. }
  235. function balUp() {
  236. if (mode != 'bal') return;
  237. $('#balInput').val(balText);
  238. input()
  239. }
  240. //QUERY-------------------------------
  241. var modeq = 'query', nameq = 'eq', strict = false, matchMode = 'mole'
  242. function setQryEq() {
  243. $('#qryMatch').show()
  244. $('.addInput').hide()
  245. $('.ok').show()
  246. modeq = 'query', nameq = 'eq', strict = false
  247. $('.qryInputHidable').hide()
  248. $('#qryBtn').text('查询方程式')
  249. $('#qryInput').attr('placeholder', 'O2=H2O')
  250. input2();
  251. }
  252. function setQryEq2() {
  253. $('#qryMatch').show()
  254. $('.addInput').hide()
  255. $('.ok').show()
  256. modeq = 'query', nameq = 'eq', strict = true
  257. $('.qryInputHidable').show()
  258. $('#qryBtn').text('查询方程式*')
  259. $('#qryInput').attr('placeholder', 'H2O')
  260. input2();
  261. }
  262. function setQryMo() {
  263. $('#qryMatch').show()
  264. $('.addInput').hide()
  265. $('.ok').show()
  266. modeq = 'query', nameq = 'mo'
  267. $('.qryInputHidable').hide()
  268. $('#qryBtn').text('查询分子')
  269. input2();
  270. }
  271. function setAddEq() {
  272. $('#qryMatch').hide()
  273. $('.addInput').show()
  274. $('#addId').hide()
  275. $('.ok').hide()
  276. modeq = 'add', nameq = 'eq'
  277. $('.qryInputHidable').hide()
  278. $('#qryBtn').text('上传方程式')
  279. $('#qryInput').attr('placeholder', 'H2+O2=H2O')
  280. }
  281. function setAddMo() {
  282. $('#qryMatch').hide()
  283. $('.addInput').show()
  284. $('#addId').hide()
  285. $('.ok').hide()
  286. modeq = 'add', nameq = 'mo'
  287. $('.qryInputHidable').hide()
  288. $('#qryBtn').text('上传分子')
  289. }
  290. function setUpdEq() {
  291. $('#qryMatch').hide()
  292. $('.addInput').show()
  293. $('#addId').show()
  294. $('.ok').hide()
  295. modeq = 'upd', nameq = 'eq'
  296. $('.qryInputHidable').hide()
  297. $('#qryBtn').text('修改方程式')
  298. $('#qryInput').attr('placeholder', '')
  299. }
  300. function setUpdMo() {
  301. $('#qryMatch').hide()
  302. $('.addInput').show()
  303. $('#addId').show()
  304. $('.ok').hide()
  305. modeq = 'upd', nameq = 'mo'
  306. $('.qryInputHidable').hide()
  307. $('#qryBtn').text('修改分子')
  308. }
  309. function getRegex() {
  310. var ret = ''
  311. if (strict) {
  312. var acont = $('#qryInput').val(), bcont = $('#qryInput2').val();
  313. if (!acont && !bcont) acont = $('#qryInput').attr('placeholder')
  314. acont = acont.replace(/([\+\=\.;])+/g, '$1')
  315. bcont = bcont.replace(/([\+\=\.;])+/g, '$1')
  316. ret = '^'
  317. if (acont) {
  318. var as = acont.split('+')
  319. for (let i = 0; i < as.length; i++) {
  320. if (matchMode == 'mole') ret += '(?=([^=]*\\+|)[0-9]?' + as[i] + '([+=]|$))'
  321. else ret += '(?=[^=]*' + as[i] + '([^a-z]|$))'
  322. }
  323. }
  324. if (bcont) {
  325. var bs = bcont.split('+');
  326. for (let i = 0; i < bs.length; i++) {
  327. if (matchMode == 'mole') ret += '(?=.*=(.*\\+|)[0-9]?' + bs[i] + '([+=]|$))'
  328. else ret += '(?=.*=.*' + bs[i] + '([^a-z]|$))'
  329. }
  330. }
  331. ret += '.*'
  332. } else {
  333. ret = $('#qryInput').val()
  334. if (!ret) ret = $('#qryInput').attr('placeholder')
  335. ret = ret.replace(/([\+\=\.;])+/g, '$1')
  336. var scont = ret.split('=')
  337. if (scont.length > 2) return;
  338. var as = scont[0].split('+');
  339. ret = matchMode == 'mole' ? '^' : ''
  340. for (let i = 0; i < as.length; i++) {
  341. if (matchMode == 'mole') ret += "(?=(.*[+=]|)[0-9]?" + as[i] + "([+=]|$))"
  342. else ret += '(?=.*' + as[i] + '([^a-z]|$))'
  343. }
  344. if (scont.length > 1) {
  345. var bs = scont[1].split('+')
  346. for (let i = 0; i < bs.length; i++) {
  347. if (matchMode == 'mole') ret += "(?=(.*[+=]|)[0-9]?" + bs[i] + "([+=]|$))"
  348. else ret += '(?=.*' + bs[i] + '([^a-z]|$))'
  349. }
  350. }
  351. ret += '.*'
  352. }
  353. return ret
  354. }
  355. function query() {
  356. if (modeq == 'query' || modeq == 'add') {
  357. var cont = getRegex();
  358. $('.ok')[0].innerHTML = cont;
  359. var bd = JSON.stringify({
  360. content: cont,
  361. })
  362. console.log(bd)
  363. fetch('/chem/query/' + nameq, {
  364. method: 'POST',
  365. headers: {
  366. 'Content-type': 'application/json',
  367. },
  368. body: bd
  369. }).then(res => {
  370. res.text().then(resj => {
  371. if (resj[0] == '!') {
  372. $('.frame')[1].innerHTML = '<pre class="text-danger bg-danger">' + resj + '</pre>';
  373. } else {
  374. resj = JSON.parse(resj)
  375. $('.frame')[1].innerHTML = '';
  376. for (let i = 0; i < resj.length; i++) {
  377. console.log(resj[i])
  378. $('.frame')[1].innerHTML += renderEquation(resj[i].content) + '<br>';
  379. $('.frame')[1].innerHTML += '<span class="label label-default">' + resj[i].id + '</span>';
  380. if (resj[i].conditions) $('.frame')[1].innerHTML += '(' + resj[i].conditions + ')';
  381. $('.frame')[1].innerHTML += resj[i].descriptions + '<br>';
  382. if (resj[i].rel) {
  383. $('.frame')[1].innerHTML += 'rel: <span class="label label-default">' + resj[i].rel + '</span><br>';
  384. }
  385. }
  386. MathJax.typeset()
  387. }
  388. });
  389. })
  390. }
  391. if (modeq == 'add') {
  392. $('#balInput').val($('#qryInput').val() ? $('#qryInput').val() : $('#qryInput').attr('placeholder'))
  393. setBal();
  394. balance().then(e => {
  395. console.log(e)
  396. if (e[0] == '!') {
  397. return;
  398. }
  399. input();
  400. if (!$('#addDescription').val()) {
  401. alert('无描述')
  402. return;
  403. }
  404. var resp = confirm(e + '请确认')
  405. if (!resp) return;
  406. $('#qryInput').val(e)
  407. var bd = JSON.stringify({
  408. content: $('#qryInput').val(),
  409. conditions: $('#addCondition').val(),
  410. descriptions: $('#addDescription').val(),
  411. })
  412. console.log(bd)
  413. fetch('/chem/add/' + nameq, {
  414. method: 'POST',
  415. headers: {
  416. 'Content-type': 'application/json',
  417. },
  418. body: bd
  419. }).then(re => {
  420. fetch('/chem/query/' + nameq, {
  421. method: 'POST',
  422. headers: {
  423. 'Content-type': 'application/json',
  424. },
  425. body: JSON.stringify({
  426. content: $('#qryInput').val(),
  427. })
  428. }).then(res => {
  429. res.text().then(resj => {
  430. console.log(resj)
  431. if (resj[0] == '!') {
  432. $('.frame')[1].innerHTML = '<pre class="text-danger bg-danger">' + resj + '</pre>';
  433. } else {
  434. resj = JSON.parse(resj)
  435. $('.frame')[1].innerHTML = '';
  436. for (let i = 0; i < resj.length; i++) {
  437. console.log(resj[i])
  438. $('.frame')[1].innerHTML += renderEquation(resj[i].content) + '<br>';
  439. $('.frame')[1].innerHTML += '<span class="label label-default">' + resj[i].id + '</span>';
  440. if (resj[i].conditions) $('.frame')[1].innerHTML += '(' + resj[i].conditions + ')';
  441. $('.frame')[1].innerHTML += resj[i].descriptions + '<br>';
  442. if (resj[i].rel) {
  443. $('.frame')[1].innerHTML += 'rel: <span class="label label-default">' + resj[i].rel + '</span><br>';
  444. }
  445. }
  446. MathJax.typeset()
  447. }
  448. });
  449. })
  450. })
  451. })
  452. }
  453. if (modeq == 'upd') {
  454. $('#balInput').val($('#qryInput').val() ? $('#qryInput').val() : $('#qryInput').attr('placeholder'))
  455. setBal();
  456. balance().then(e => {
  457. console.log(e)
  458. if (e[0] == '!') {
  459. return;
  460. }
  461. input();
  462. if (!$('#addDescription').val()) {
  463. alert('无描述')
  464. return;
  465. }
  466. var resp = confirm(e + '请确认')
  467. if (!resp) return;
  468. $('#qryInput').val(e)
  469. var bd = JSON.stringify({
  470. content: $('#qryInput').val(),
  471. id: $('#addIdText').val(),
  472. conditions: $('#addCondition').val(),
  473. descriptions: $('#addDescription').val(),
  474. })
  475. console.log(bd)
  476. fetch('/chem/upd/' + nameq, {
  477. method: 'POST',
  478. headers: {
  479. 'Content-type': 'application/json',
  480. },
  481. body: bd
  482. })
  483. inputId();
  484. })
  485. }
  486. }
  487. function input2() {
  488. if (modeq == 'query') {
  489. $('.ok')[0].innerHTML = getRegex()
  490. }
  491. }
  492. function inputId() {
  493. var bd = JSON.stringify({
  494. content: $('#addIdText').val(),
  495. })
  496. if (!$('#addIdText').val()) {
  497. $('.frame')[1].innerHTML = '';
  498. return;
  499. }
  500. console.log(bd)
  501. fetch('/chem/query/' + nameq + 'id', {
  502. method: 'POST',
  503. headers: {
  504. 'Content-type': 'application/json',
  505. },
  506. body: bd
  507. }).then(res => {
  508. res.text().then(resj => {
  509. if (resj[0] == '!') {
  510. $('.frame')[1].innerHTML = '<pre class="text-danger bg-danger">' + resj + '</pre>';
  511. } else {
  512. resj = JSON.parse(resj)[0]
  513. if (resj) {
  514. $('.frame')[1].innerHTML = '';
  515. console.log(resj)
  516. $('.frame')[1].innerHTML += renderEquation(resj.content) + '<br>';
  517. $('.frame')[1].innerHTML += '<span class="label label-default">' + resj.id + '</span>';
  518. if (resj.conditions) $('.frame')[1].innerHTML += '(' + resj.conditions + ')';
  519. $('.frame')[1].innerHTML += resj.descriptions + '<br>';
  520. if (resj.rel) {
  521. $('.frame')[1].innerHTML += 'rel: <span class="label label-default">' + resj.rel + '</span><br>';
  522. }
  523. $('#qryInput').val(resj.content)
  524. $('#addCondition').val(resj.conditions)
  525. $('#addDescription').val(resj.descriptions)
  526. MathJax.typeset()
  527. } else $('.frame')[1].innerHTML = '';
  528. }
  529. });
  530. })
  531. }
  532. function qryToggleMatch() {
  533. if (matchMode == 'mole') {
  534. $('#qryMatch')[0].innerHTML = '匹配元素'
  535. matchMode = 'elem'
  536. } else {
  537. $('#qryMatch')[0].innerHTML = '匹配分子'
  538. matchMode = 'mole'
  539. }
  540. }
  541. function qryUp() {
  542. var sp = $('#balInput').val().split('=')
  543. if (sp.length > 1) {
  544. if (modeq == 'query') {
  545. $('#qryInput').val(sp[0])
  546. $('#qryInput2').val(sp[1])
  547. setQryEq2();
  548. } else $('#qryInput').val($('#balInput').val());
  549. } else $('#qryInput').val($('#balInput').val());
  550. }