search.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // var search_output = {};
  2. $(function() {
  3. $(".selectBox").on("click", function (e) {
  4. if(get_value_list_by_key(db_table["project"], "category_id", search_data["category"], "project_id").length) {
  5. if (checkboxes.style.display == "none") {
  6. checkboxes.style.display = "block";
  7. } else {
  8. checkboxes.style.display = "none";
  9. }
  10. }
  11. });
  12. $("body").on("click", function (event) {
  13. test = $(event.target);
  14. if (!$(event.target).hasClass("overSelect")) {
  15. if (($(event.target).closest("#checkboxes").length == 1)) {
  16. checkboxes.style.display = "block";
  17. } else {
  18. checkboxes.style.display = "none";
  19. }
  20. }
  21. });
  22. $("#category_button").on("change", function() {
  23. var category_id = [this.value.split(',')[0]];
  24. var list_id = [this.value.split(',')[1]];
  25. $("#p-select").prop("disabled", false);
  26. $('#type-button').prop("value", "any");
  27. $("#notes").slideUp(200);
  28. $('#notes-button').prop("value", "any");
  29. //set search_data
  30. if (this.value == "any") {
  31. search_data["category"] = get_value_list_by_key(db_table["category"], null, [], "category_id");
  32. search_data["project"] = get_value_list_by_key(db_table["project"], null, [], "project_id");
  33. search_data["type"] = get_value_list_by_key(db_table["type"], null, [], "type_id");
  34. } else {
  35. search_data["category"] = category_id;
  36. search_data["project"] = get_value_list_by_key(db_table["project"], "category_id", category_id, "project_id");
  37. target_type_id_list = get_value_list_by_key(db_table["category_type"], "list_id", list_id, "category_list");
  38. search_data["type"] = merge_string_to_list(target_type_id_list);
  39. }
  40. //show project option
  41. project_id_list = string_concat_list("project", search_data["project"]);
  42. show_multi_checkbox("project-button", project_id_list);
  43. $("#project-any").prop("checked", true);
  44. set_multi_checkbox_text("project-checkbox", "project")
  45. if (!search_data["project"].length) {
  46. $("#p-select").prop("disabled", true);
  47. $("#p-select option").text("無符合條件選項");
  48. }
  49. //show type option
  50. show_select_option("type-button", search_data["type"]);
  51. //init notes
  52. search_data["notes"] = notes_button_list;
  53. });
  54. $("#project-any").on("change", function() {
  55. if(this.checked) {
  56. search_data["project"] = get_value_list_by_key(db_table["project"], "category_id", search_data["category"], "project_id");
  57. project_id_list = string_concat_list("project", search_data["project"]);
  58. show_multi_checkbox("project-button", project_id_list);
  59. } else {
  60. search_data["project"] = [];
  61. $(".project-checkbox").prop("checked", false);
  62. }
  63. set_multi_checkbox_text("project-checkbox", "project");
  64. });
  65. $(".project-checkbox").on("change", function() {
  66. $('#type-button').prop("value", "any");
  67. $("#notes").slideUp(200);
  68. $('#notes-button').prop("value", "any");
  69. check_option = get_multi_checkbox_check_list("project-checkbox", "project");
  70. if(check_option.length < get_value_list_by_key(db_table["project"], "category_id", search_data["category"], "project_id").length) {
  71. $("#project-any").prop("checked", false);
  72. } else {
  73. $("#project-any").prop("checked", true);
  74. }
  75. search_data["project"] = check_option;
  76. search_data["notes"] = notes_button_list;
  77. set_multi_checkbox_text("project-checkbox", "project");
  78. });
  79. $("#type-button").on("change", function() {
  80. var type_id = this.value;
  81. var show_notes_list = [];
  82. if (type_id == "3") {
  83. project_id_list = get_multi_checkbox_check_list("project-checkbox", "project");
  84. project_id_list.forEach(element => {
  85. show_notes_list = show_notes_list.concat(db_table["notes"][element]);
  86. });
  87. show_notes_list = [...new Set(show_notes_list)];
  88. show_select_option("notes-button", show_notes_list);
  89. $("#notes").slideDown(200);
  90. } else {
  91. $("#notes").slideUp(200);
  92. $('#notes-button').prop("value", "any");
  93. }
  94. if(type_id == "any") {
  95. search_data["type"] = get_value_list_by_key(db_table["type"], null, [], "type_id");
  96. } else {
  97. search_data["type"] = [type_id];
  98. }
  99. search_data["notes"] = notes_button_list;
  100. });
  101. $("#notes-button").on("change", function() {
  102. var notes_name = this.value;
  103. if(notes_name == "any") {
  104. search_data["notes"] = notes_button_list;
  105. } else {
  106. search_data["notes"] = [notes_name];
  107. }
  108. });
  109. });
  110. //sql
  111. function get_value_list_by_key(obj_list, key_search, target_value, key_save) {
  112. output_list = [];
  113. obj_list.forEach(element => {
  114. if (target_value.length) {
  115. target_value.forEach(e => {
  116. if(e.toString() == element[key_search].toString()) {
  117. output_list.push(element[key_save]);
  118. }
  119. });
  120. } else {
  121. output_list.push(element[key_save]);
  122. }
  123. });
  124. output_list = [...new Set(output_list)];
  125. return output_list;
  126. }
  127. function merge_object_by_key(obj, key, value) {
  128. output_object = {};
  129. while (obj.length > 0) {
  130. pop_out = obj.pop();
  131. if (output_object[pop_out[key]] == undefined) {
  132. output_object[pop_out[key]] = [pop_out[value]];
  133. } else {
  134. output_object[pop_out[key]].push(pop_out[value]);
  135. }
  136. }
  137. return output_object
  138. }
  139. function merge_string_to_list(string_list) {
  140. output = [];
  141. string_list.forEach(element => {
  142. output = output.concat(element.split(","));
  143. });
  144. return output;
  145. }
  146. function string_concat_list(name, target_list) {
  147. output_list = [];
  148. target_list.forEach(element => {
  149. output_list.push(name + element);
  150. });
  151. return output_list;
  152. }
  153. function show_multi_checkbox(class_name, show_list) {
  154. $("."+class_name+" input").prop("checked", false);
  155. $("."+class_name).css("display", "none");
  156. show_list.forEach(element => {
  157. $("#"+element).prop("checked", true);
  158. $("#"+element).parent().css("display", "block");
  159. });
  160. }
  161. function show_select_option(select_id, show_list) {
  162. $("#" + select_id + " option").hide();
  163. $("#notes-button").prop("disabled", false);
  164. if(show_list.length == 1 & show_list[0] == undefined) {
  165. $('#notes-button').prop("value", "none");
  166. $("#notes-button").prop("disabled", true);
  167. } else {
  168. $("#" + select_id + " option[value=any]").show();
  169. show_list.forEach(element => {
  170. $("#" + select_id + " option[value=" + element + "]").show();
  171. });
  172. }
  173. }
  174. function set_multi_checkbox_text(checkbox_class, name_replace) {
  175. check_list = get_multi_checkbox_check_list(checkbox_class, name_replace);
  176. if(check_list.length > 3){
  177. $("#p-select option").text("已選擇 "+check_list.length+" 項");
  178. } else {
  179. if(check_list.length == 0) {
  180. $("#p-select option").text("未選擇任何專案");
  181. } else {
  182. $("#p-select option").text(check_list);
  183. }
  184. }
  185. }
  186. function get_multi_checkbox_check_list(checkbox_class, name_replace) {
  187. output = [];
  188. var $select_option = $("."+checkbox_class+":checked");
  189. $select_option.each(function(){
  190. output.push($(this).attr('id').replace(name_replace, ""));
  191. });
  192. return output;
  193. }