home.html.erb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <div id="queries">
  2. <div id="header" style="margin-bottom: 20px;">
  3. <div class="pull-right">
  4. <% if blazer_user %>
  5. <%= link_to "All", root_path, class: !params[:filter] ? "active" : nil, style: "margin-right: 40px;" %>
  6. <% if Blazer.audit %>
  7. <%= link_to "Viewed", root_path(filter: "viewed"), class: params[:filter] == "viewed" ? "active" : nil, style: "margin-right: 40px;" %>
  8. <% end %>
  9. <%= link_to "Mine", root_path(filter: "mine"), class: params[:filter] == "mine" ? "active" : nil, style: "margin-right: 40px;" %>
  10. <% end %>
  11. <div class="btn-group">
  12. <%= link_to "New Query", new_query_path, class: "btn btn-info" %>
  13. <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  14. <span class="caret"></span>
  15. <span class="sr-only">Toggle Dropdown</span>
  16. </button>
  17. <ul class="dropdown-menu">
  18. <li><%= link_to "Dashboards", dashboards_path %></li>
  19. <li><%= link_to "Checks", checks_path %></li>
  20. <li role="separator" class="divider"></li>
  21. <li><%= link_to "New Dashboard", new_dashboard_path %></li>
  22. <li><%= link_to "New Check", new_check_path %></li>
  23. </ul>
  24. </div>
  25. </div>
  26. <input type="text" v-model="searchTerm" placeholder="Start typing a query or person" style="width: 300px; display: inline-block;" autofocus=true class="search form-control" />
  27. </div>
  28. <table class="table">
  29. <thead>
  30. <tr>
  31. <th>Name</th>
  32. <th style="width: 20%; text-align: right;">Mastermind</th>
  33. </tr>
  34. </thead>
  35. <tbody class="list" v-cloak>
  36. <tr v-for="query in visibleItems">
  37. <td>
  38. <a :href="itemPath(query)" :class="{dashboard: query.dashboard}">{{ query.name }}</a>
  39. <span class="vars">{{ query.vars }}</span>
  40. </td>
  41. <td class="creator">{{ query.creator }}</td>
  42. </tr>
  43. </tbody>
  44. </table>
  45. <p v-if="more" class="text-muted">Loading...</p>
  46. </div>
  47. <script>
  48. var prepareSearch = function (list) {
  49. var i, q
  50. for (i = 0; i < list.length; i++) {
  51. q = list[i]
  52. q.searchStr = prepareQuery(q.name + q.creator)
  53. }
  54. }
  55. var prepareQuery = function (str) {
  56. return str.toLowerCase().replace(/\W+/g, "")
  57. }
  58. var app = new Vue({
  59. el: "#queries",
  60. data: {
  61. searchTerm: "",
  62. more: gon.more,
  63. updateCounter: 0
  64. },
  65. created: function() {
  66. this.listItems = gon.dashboards.concat(gon.queries)
  67. prepareSearch(this.listItems)
  68. this.queryIds = {}
  69. for (i = 0; i < gon.queries.length; i++) {
  70. this.queryIds[gon.queries[i].id] = true
  71. }
  72. if (this.more) {
  73. var _this = this
  74. $.getJSON(Routes.blazer_queries_path(), function (data) {
  75. var i, j, newValues, val, size = 500;
  76. var newValues = []
  77. for (j = 0; j < data.length; j++) {
  78. val = data[j]
  79. if (val && !_this.queryIds[val.id]) {
  80. newValues.push(val)
  81. }
  82. }
  83. prepareSearch(newValues)
  84. _this.listItems = _this.listItems.concat(newValues)
  85. _this.more = false
  86. // hack to get to update
  87. _this.updateCounter++
  88. })
  89. }
  90. },
  91. computed: {
  92. visibleItems: function () {
  93. // hack to get to update
  94. this.updateCounter
  95. var pageSize = 200
  96. var q, i
  97. if (this.searchTerm.length > 0) {
  98. var term = prepareQuery(this.searchTerm)
  99. var items = []
  100. var fuzzyItems = []
  101. for (i = 0; i < this.listItems.length; i++) {
  102. q = this.listItems[i]
  103. if (q.searchStr.indexOf(term) !== -1) {
  104. items.push(q)
  105. if (items.length == pageSize) {
  106. break
  107. }
  108. } else if (fuzzysearch(term, q.searchStr)) {
  109. fuzzyItems.push(q)
  110. }
  111. }
  112. return items.concat(fuzzyItems).slice(0, pageSize)
  113. } else {
  114. return this.listItems.slice(0, pageSize)
  115. }
  116. }
  117. },
  118. methods: {
  119. itemPath: function (item) {
  120. if (item.dashboard) {
  121. return Routes.blazer_dashboard_path(item.to_param)
  122. } else {
  123. return Routes.blazer_query_path(item.to_param)
  124. }
  125. }
  126. }
  127. })
  128. </script>