Browse Source

update chem

root 2 years ago
parent
commit
ca8f987d90
2 changed files with 89 additions and 29 deletions
  1. 25 8
      chem/index.html
  2. 64 21
      js/chem.js

+ 25 - 8
chem/index.html

@@ -65,10 +65,27 @@
             display: inline-block;
             transform: translateY(-2px);
         }
+
+        .MathJax {
+            color: midnightblue;
+        }
+
+        .result {
+            color: slategrey;
+            border-bottom: 1px solid;
+            border-image: linear-gradient(to right, transparent, rgba(143, 65, 233, 0.5), #578aef, transparent) 1;
+        }
+
+        .result:hover {
+            background-color: rgba(159, 186, 200, 0.1);
+        }
+
+        .glyphicon-share-alt {
+            transform: rotateX(180deg);
+        }
     </style>
     <script id="MathJax-script" src="https://cdn.bootcss.com/mathjax/3.2.2/es5/tex-mml-chtml.js"></script>
-
-    <script src="/js/chem.min.js"></script>
+    <script src="/js/chem.js"></script>
 
 </head>
 
@@ -76,14 +93,16 @@
     <div class="row">
         <div class="col-lg-2 col-md-2 col-xs-12">
             <a class="btn btn-info" href="/"><i class="glyphicon glyphicon-home" aria-hidden="true"></i>&nbsp;返回</a>
-
             <hr>
+            <button type="button" class="btn btn-default active" data-toggle="button" onclick="preview=!preview" id="preview" title="性能降低时关闭">实时预览</button>
+            <hr>
+            <p>小提示:<br><code>&lt;e+&gt;</code>、<code>&lt;2e-&gt;</code> 用于标注电荷,<code>.</code> 用于代替水合物等中间的点。输入 ! 快速导入查询结果。</p>
         </div>
         <div class="col-lg-8 col-md-10 col-xs-12">
             <div class="input-group">
-                <span class="input-group-btn">
+                <!-- <span class="input-group-btn">
                     <button type="button" class="btn btn-default" onclick="balUp()" id="balShare" title="导入配平结果"> <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span></button>
-                </span>
+                </span> -->
                 <input id="balInput" type="text" class="form-control" oninput="input()" autocomplete="off">
                 <span class="input-group-btn">
                     <button type="button" class="btn btn-default" onclick="balance()" id="balBtn">配平</button>
@@ -108,7 +127,7 @@
             <hr>
             <div class="input-group">
                 <span class="input-group-btn">
-                    <button type="button" class="btn btn-default" onclick="qryUp()" id="qryShare" title="导入配平结果"> <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span></button>
+                    <button type="button" class="btn btn-default"> <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span></button>
                     <button type="button" class="btn btn-default" data-toggle="button" onclick="qryToggleMatch()" id="qryMatch">匹配分子</button>
                 </span>
                 <input id="qryInput" type="text" class="form-control qryInput" autocomplete="off" oninput="input2()">
@@ -162,8 +181,6 @@
         <p>上下两个面板都可以用文字直接输入化学式,上面那个负责运算,下面那个负责数据库的交互。</p>
         <p>具体怎么操作相信大家都能看懂,用最右边的箭头可以切换功能,然后输入化学式,按下按钮,或者回车也行。至于相对质量,由于不需要多少算力,它是实时更新的。</p>
         <p>我运用了聪明绝顶的正则表达式(也就是那一串火星文),保证了查询方程式时,只要所有分子完全匹配,无论顺序都能查得到。你也可以改为只查包含某种元素的化学式,按左边的按钮就行。</p>
-        <p>左边的箭头按钮考虑的是在上下之间快速复制粘贴方程式,不过这个功能还没完全实现。以及根据描述查询方程式的功能也还没实现。</p>
         <p>我目前只录入了高一上学期讲的一些式子,你要是吃饱了闲着没事干也可以帮我录入。查到的方程式带着的数字就是它的 id,修改的时候用得到。</p>
-        <p>小提示:<code>&lt;e+&gt;</code>、<code>&lt;2e-&gt;</code> 用于标注电荷,<code>.</code> 用于代替水合物等中间的点,假如配平结果有一堆变量说明你的方程有多解。</p>
     </div>
 </body>

+ 64 - 21
js/chem.js

@@ -26,7 +26,7 @@ var weighList2 = {
     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
 }
 
-var bracket = {}
+var bracket = {}, preview = 1
 
 function getco(str) {
     var n = str.length, ret = 0
@@ -148,7 +148,12 @@ function parseEquation(str) {
     return ans
 }
 
-function renderEquation(str) {
+function getCondition(e) {
+    if (e == '加热') return '\\Delta'
+    return e
+}
+
+function renderEquation(str, condition = '') {
     str = str.replace(/[\[{]/g, "(").replace(/[\]}]/g, ")");
     str = str.replace(/([\+\=\.;])\1+/g, '$1')
     str = str.replace(/[^\dA-Za-z<>\(\)\+\-=\.;]/g, "");
@@ -157,11 +162,34 @@ function renderEquation(str) {
     str = str.replace(/<(\d*)\\text\{e\}([\+\-])>/g, "^{$1$2}");
     str = str.replace(/([\}\)])(\d+)/g, "$1_{$2}");
     str = str.replace(/\./g, "\\cdot");
-    str = "<a href='#'>\\(" + str.replace(/([\+\-=\.;])([^\}])/g, "\\)</a> \\($1\\) <a href='#'>\\($2") + '\\)</a>';
+    if (condition) str = str.replace(/=/g, `\\stackrel{${getCondition(condition)}}{=}`)
+    str = "\\(" + str + "\\)"
+    // str = "<a href='#'>\\(" + str.replace(/([\+\-=\.;])([^\}])/g, "\\)</a> \\($1\\) <a href='#'>\\($2") + '\\)</a>';
     // console.log(str)
     return str
 }
 
+// function renderEquation(str, condition = '') {
+//     str = str.replace(/[\[{]/g, "(").replace(/[\]}]/g, ")");
+//     str = str.replace(/([\+\=\.;])\1+/g, '$1')
+//     console.log(str)
+//     str = str.replace(/([^e]|^)([\+\=\.;])/g, '$1 $2 ')
+//     console.log(str)
+//     str = str.replace(/[^\dA-Za-z<>\(\)\+\-=\.; ]/g, "");
+//     console.log(str)
+//     // console.log('Rendering equation', str)
+//     // str = str.replace(/([A-Za-z]+)/g, "\\text{$1}");
+//     console.log(str)
+//     str = str.replace(/<(\d*)\{e\}([\+\-])>/g, "^{$1$2}");
+//     console.log(str)
+//     str = str.replace(/([\}\)])(\d+)/g, "$1_{$2}");
+//     str = str.replace(/\./g, "\\cdot");
+//     // str = "<a href='#'>\\(" + str.replace(/([\+\-=\.;])([^\}])/g, "\\)</a> \\($1\\) <a href='#'>\\($2") + '\\)</a>';
+//     str = `\\(\\ce{${str}}\\)`
+//     console.log(str)
+//     return str
+// }
+
 //PROCESS-------------------------------
 
 var mode = 'bal', balInput, balText = '', inputText, case1 = 'CrI3+Cl2+KOH=K2CrO4+KIO4+KCl+H2O', case2 = 'CH3CHO+2Ag(NH3)2OH=CH3COONH4+2Ag+3NH3+H2O'
@@ -179,6 +207,7 @@ $().ready(function () {
             $("#qryBtn")[0].click();
         }
     });
+    $("#preview").tooltip()
     $(function () { $("[data-toggle='tooltip']").tooltip(); });
 })
 function setBal() {
@@ -214,16 +243,17 @@ function input() {
     inputText = balInput.value
     if (mode == 'bal') {
         $('.frame')[0].innerHTML = renderEquation((inputText == '') ? case1 : inputText) + '<br>'
-            + '<span class="glyphicon glyphicon-chevron-down" aria-hidden="true"></span><br>' + ((balText != '') ? (renderEquation(balText)) : ('...'));
-        MathJax.typeset()
+        + '<span class="glyphicon glyphicon-chevron-down" aria-hidden="true"></span><br>' + ((balText != '') ? (renderEquation(balText)) : ('...'));
     } else if (mode == 'weigh') {
         $('.frame')[0].innerHTML = renderEquation((inputText == '') ? case2 : inputText) + '<br>'
-            + weighEquation((inputText == '') ? case2 : inputText);
-        MathJax.typeset()
+        + weighEquation((inputText == '') ? case2 : inputText);
     } else if (mode == 'weigh2') {
         $('.frame')[0].innerHTML = renderEquation((inputText == '') ? case2 : inputText) + '<br>'
-            + weighEquation((inputText == '') ? case2 : inputText, 1);
-        MathJax.typeset()
+        + weighEquation((inputText == '') ? case2 : inputText, 1);
+    }
+    if (preview) MathJax.typeset()
+    if(inputText.match('!')){
+        balUp()
     }
 }
 var running;
@@ -245,6 +275,7 @@ function balance() {
         running = 0;
     })
 }
+
 function balUp() {
     if (mode != 'bal') return;
     $('#balInput').val(balText);
@@ -268,7 +299,7 @@ function toggl(str, e = 0, f = 0) {
 function setQryEq() {
     toggl('查询方程式')
     modeq = 'query', nameq = 'eq', strict = false
-    $('#qryInput').attr('placeholder', 'O2=H2O' + '(输入化学式查询数据库,支持模糊搜索)')
+    $('#qryInput').attr('placeholder', 'O2=H2O' + '(输入化学式查询数据库,也可输入 id)')
     input2();
 }
 function setQryEq2() {
@@ -328,6 +359,7 @@ function getRegex() {
         ret += '.*'
     } else {
         ret = $('#qryInput').val()
+        if (ret && !ret.match(/[^\d]/g)) return ret
         if (!ret) ret = $('#qryInput').attr('placeholder').split('(')[0]
         ret = replaceRegex(ret)
         var scont = ret.split('=')
@@ -350,7 +382,7 @@ function getRegex() {
     return ret.replace(/%/g, 'e+')
 }
 
-function doQuery(bd, isId = '') {
+function doQuery(bd, isId = '', replace = 1) {
     fetch('/chem/query/' + nameq + isId, {
         method: 'POST',
         headers: {
@@ -363,26 +395,32 @@ function doQuery(bd, isId = '') {
         if (e[0] == '!') {
             $('.frame')[1].innerHTML = '<pre class="text-danger bg-danger">' + e + '</pre>';
         } else {
+            let qin = $('#qryInput').val()
             e = JSON.parse(e)
-            if (!isId) {
-                $('.frame')[1].innerHTML = '<span id="qryInputRender">' + (strict ? renderEquation($('#qryInput').val() + '=' + $('#qryInput2').val()) : renderEquation($('#qryInput').val())) + ' - 匹配到 ' + e.length + ' 个</span><br><span class="glyphicon glyphicon-chevron-down" aria-hidden="true"></span><br>';
+            if (!isId || !replace) {
+                $('.frame')[1].innerHTML = '<span id="qryInputRender">' + (strict ? renderEquation(qin + '=' + $('#qryInput2').val()) : renderEquation(qin)) + ' - 匹配到 ' + e.length + ' 个</span><br><span class="glyphicon glyphicon-chevron-down" aria-hidden="true"></span><br>';
             } else $('.frame')[1].innerHTML = ''
+            var str = ''
             for (let i = 0; i < e.length; i++) {
-                $('.frame')[1].innerHTML += renderEquation(e[i].content) + '<br><span class="label label-default">' + e[i].id + '</span> ';
-                if (e[i].conditions) $('.frame')[1].innerHTML += '(' + e[i].conditions + ')';
-                $('.frame')[1].innerHTML += e[i].descriptions + '<br>';
+                str += '<div class="result">' + renderEquation(e[i].content, e[i].conditions) + '<br><span class="label label-default">' + e[i].id + '</span> ';
+                if (e[i].conditions) str += '(' + e[i].conditions + ')';
+                str += e[i].descriptions + '<br>';
                 if (e[i].rel > 0) {
-                    $('.frame')[1].innerHTML += 'rel: <span class="label label-success">' + e[i].rel + '</span><br>';
+                    str += '<span class="glyphicon glyphicon-share-alt"></span> <span class="label label-success">' + e[i].rel + '</span><br>';
                 }
                 if (e[i].rel < 0) {
-                    $('.frame')[1].innerHTML += 'rel: <span class="label label-warning">' + (-e[i].rel) + '</span><br>';
+                    str += '<span class="glyphicon glyphicon-share-alt"></span> <span class="label label-warning">' + (-e[i].rel) + '</span><br>';
                 }
                 if (isId) {
-                    $('#qryInput').val(e[0].content)
+                    if (replace) $('#qryInput').val(e[0].content)
                     $('#addCondition').val(e[0].conditions)
                     $('#addDescription').val(e[0].descriptions)
                 }
+                console.log(qin)
+                if (qin.match('!') || replace == 2) $('#qryInput').val(e[0].content)
+                str += '</div>'
             }
+            $('.frame')[1].innerHTML += str
             MathJax.typeset()
         }
     });
@@ -466,8 +504,14 @@ function input2() {
     if (modeq == 'query') {
         $('.ok').text(getRegex())
     }
+    if ($('#qryInput').val() && $('#qryInput').val().match(/^\d+!?$/g)) {
+        doQuery(JSON.stringify({ content: $('#qryInput').val().split('!')[0] }), 'id', 0)
+    } else if ($('#qryInput').val().match('!')) {
+        $('#qryInput').val($('#qryInput').val().split('!')[0])
+        doQuery(JSON.stringify({ content: getRegex() }), '', 2)
+    }
     if ($('#qryInputRender')[0]) $('#qryInputRender')[0].innerHTML = (strict ? renderEquation($('#qryInput').val() + '=' + $('#qryInput2').val()) : renderEquation($('#qryInput').val()))
-    MathJax.typeset()
+    if (preview) MathJax.typeset()
 }
 
 function inputId() {
@@ -478,7 +522,6 @@ function inputId() {
         $('.frame')[1].innerHTML = '';
         return;
     }
-    console.log(bd)
     doQuery(bd, 'id')
 }