Webadmin - searchable application

Make Application selection searchable

当前为 2016-05-26 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Webadmin - searchable application
  3. // @namespace com.aforms2web.ds.ujs
  4. // @description Make Application selection searchable
  5. // @author dietmar.stoiber@aforms2web.com
  6. // @include *webadmin*
  7. // @version 0.8
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. init();
  12.  
  13. function init(){
  14. var select = $("#afs_application");
  15. if(select.size() > 0){
  16. select.attr("size", 10);
  17. select.attr("style", "min-height: 3em");
  18. var tr = document.createElement("tr");
  19. var td = document.createElement("td");
  20. td.innerHTML = "Filter";
  21. tr.appendChild(td);
  22. td = document.createElement("td");
  23.  
  24. var input = document.createElement("input");
  25. input.addEventListener ('keydown', stopMovingCarretONUpDown, true);
  26. input.addEventListener ('keyup', filter, true);
  27. input.setAttribute("id", "afs_application_filter");
  28. input.setAttribute("type", "text");
  29. input.setAttribute("autocomplete", "off");
  30. input.setAttribute("class", "input_field_size300");
  31. input.setAttribute("style", "border-top-right-radius: 5em 2em;");
  32. td.appendChild(input);
  33.  
  34. tr.appendChild(td);
  35. select.closest("tbody").prepend(tr);
  36.  
  37. input.focus();
  38.  
  39. }
  40. }
  41.  
  42. var appSelect_currentSelected = null;
  43.  
  44. function stopMovingCarretONUpDown(event){
  45. if(event.keyCode == 38 || event.keyCode == 40){
  46. event.preventDefault();
  47. }
  48. }
  49.  
  50. function filter(event){
  51. var filter = $("#afs_application_filter").val();
  52. var appSelect = $("#afs_application");
  53. var selected = null;
  54. var first = null;
  55. if(appSelect_currentSelected != null){
  56. if(event.keyCode == 38){ // up
  57. appSelect_currentSelected.prevAll(':not(:disabled)').first().attr("selected", true);
  58. appSelect_currentSelected = appSelect.find("option:selected");
  59. return;
  60. }else if(event.keyCode == 40){ // down
  61. appSelect_currentSelected.nextAll(':not(:disabled)').first().attr("selected", true);
  62. appSelect_currentSelected = appSelect.find("option:selected");
  63. return;
  64. }
  65. }
  66. appSelect_currentSelected = null;
  67. $("#afs_application > option").each(function() {
  68. if(this.text.indexOf(filter) >= 0 || this.text.match(new RegExp(filter, 'gi'))){
  69. $(this).attr("style", "");
  70. $(this).attr("disabled", false);
  71. if(first == null){
  72. first = $(this);
  73. }
  74. if($(this).attr("selected") && selected == null){
  75. appSelect_currentSelected = $(this);
  76. }
  77. }else{
  78. $(this).attr("style", "display: none; visibility: hidden;");
  79. $(this).attr("disabled", true);
  80. $(this).attr("selected", false);
  81. }
  82. });
  83. if(appSelect_currentSelected == null){
  84. first.attr("selected", true);
  85. appSelect_currentSelected = appSelect.find("option:selected");
  86. }
  87. if(appSelect_currentSelected != null){
  88. appSelect.scrollTop(appSelect_currentSelected.position().top);
  89. }
  90. }