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.

104 lines
3.4 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
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. //debounced_fetch: debounce(this.fetch_data.bind(this), 300)
  50. },
  51. created() {
  52. this.fetch_data = debounce(this.fetch_data, 300)
  53. }
  54. });
  55. </script>
  56. <h1>Search Box</h1>
  57. <div id="search">
  58. <main>
  59. <label for="mz_min">Mz Min</label>
  60. <input id="mz_min" type="number" name="mz_min" v-model="mz_min" @input="fetch_data()" value="0">
  61. <label for="mz_min">Mz Max</label>
  62. <input id="mz_max" type="number" name="mz_max" v-model="mz_max" @input="fetch_data()" value="0">
  63. <label for="mz_min">RT Min</label>
  64. <input id="rt_min" type="number" name="rt_min" v-model="rt_min" @input="fetch_data()" value="0">
  65. <label for="mz_min">RT Max</label>
  66. <input id="rt_max" type="number" name="rt_max" v-model="rt_max" @input="fetch_data()" value="0">
  67. <!--
  68. <br>
  69. <button @click="fetch_data()">Search</button>
  70. -->
  71. </main>
  72. <hr>
  73. {% raw %}
  74. <div id="search">
  75. <div v-if="error" style="color: red;">
  76. Uh Oh! There is an Error!
  77. </div>
  78. <div v-for="result in results">
  79. <a :href="result.url">
  80. <h3>{{result.name}}</h3>
  81. </a>
  82. <table>
  83. <tr>
  84. <td>Rt</td>
  85. <td>{{result.rt}}</td>
  86. </tr>
  87. <tr>
  88. <td>Mz</td>
  89. <td>{{result.mz}}</td>
  90. </tr>
  91. </table>
  92. <hr>
  93. </div>
  94. </div>
  95. {% endraw %}
  96. </div>
  97. {% endblock %}