You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.0 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. {% extends "base.html" %}
  2. {% block content %}
  3. <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.min.js"></script>
  4. <script type="module">
  5. //import { createApp } from 'https://unpkg.com/petite-vue?module'
  6. //import { createApp } from 'https://unpkg.com/vue@2.7.14/dist/vue.esm-browser.prod.js'
  7. function valid(str) {
  8. return !isNaN(str) && !isNaN(parseFloat(str))
  9. }
  10. function debounce(func, timeout = 300){
  11. let timer;
  12. return (...args) => {
  13. clearTimeout(timer);
  14. timer = setTimeout(() => { func.apply(this, args); }, timeout);
  15. };
  16. }
  17. const app = new Vue({
  18. el: "#search",
  19. data() {
  20. return {
  21. mz_min: 0,
  22. mz_max: 0,
  23. rt_min: 0,
  24. rt_max: 0,
  25. results: [],
  26. error: null,
  27. }
  28. },
  29. methods: {
  30. // methods
  31. async fetch_data() {
  32. if (!(valid(this.mz_min) && valid(this.mz_max) && valid(this.rt_max) && valid(this.rt_min))) {
  33. return
  34. }
  35. const url = `/chemical/search?mz_min=${this.mz_min}&mz_max=${this.mz_max}&rt_min=${this.rt_min}&rt_max=${this.rt_max}`
  36. fetch(url).then(res => {
  37. if (res.status !== 200) {
  38. console.log(`Error Status, ${res.status}`)
  39. }
  40. return res.json()
  41. }).then(json => {
  42. this.results = json;
  43. this.error = null;
  44. }).catch(e => {
  45. console.error(e);
  46. this.error = true;
  47. })
  48. }
  49. },
  50. created() {
  51. this.fetch_data = debounce(this.fetch_data, 300)
  52. }
  53. });
  54. </script>
  55. <h1>Search Box</h1>
  56. <div id="search">
  57. <main>
  58. <table>
  59. <tr>
  60. <td>
  61. <label for="mz_min">Minimum M/Z Ratio</label>
  62. </td>
  63. <td>
  64. <input id="mz_min" type="number" name="mz_min" v-model="mz_min" @input="fetch_data()" value="0">
  65. </td>
  66. </tr>
  67. <tr>
  68. <td>
  69. <label for="mz_min">Maximum M/Z Ratio</label>
  70. </td>
  71. <td>
  72. <input id="mz_max" type="number" name="mz_max" v-model="mz_max" @input="fetch_data()" value="0">
  73. </td>
  74. </tr>
  75. <tr>
  76. <td>
  77. <label for="mz_min">Minimum Retention Time</label>
  78. </td>
  79. <td>
  80. <input id="rt_min" type="number" name="rt_min" v-model="rt_min" @input="fetch_data()" value="0">
  81. </td>
  82. </tr>
  83. <tr>
  84. <td>
  85. <label for="mz_min">Maximum Retention Time</label>
  86. </td>
  87. <td>
  88. <input id="rt_max" type="number" name="rt_max" v-model="rt_max" @input="fetch_data()" value="0">
  89. </td>
  90. </tr>
  91. </table>
  92. <!--
  93. <br>
  94. <button @click="fetch_data()">Search</button>
  95. -->
  96. </main>
  97. <hr>
  98. {% raw %}
  99. <div id="search">
  100. <div v-if="error" style="color: red;">
  101. Uh Oh! There is an Error!
  102. </div>
  103. <div v-for="result in results">
  104. <a :href="result.url">
  105. <h3>{{result.name}}</h3>
  106. </a>
  107. <table>
  108. <tr>
  109. <td>Retention Time</td>
  110. <td>{{result.rt}}</td>
  111. </tr>
  112. <tr>
  113. <td>M/Z Ratio</td>
  114. <td>{{result.mz}}</td>
  115. </tr>
  116. </table>
  117. <hr>
  118. </div>
  119. </div>
  120. {% endraw %}
  121. </div>
  122. {% endblock %}