search.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. $(function() {
  2. $(".selectBox").on("click", function (e) {
  3. if($(this).siblings(".checkboxes").find("label").css("display") == "block") {
  4. if($(this).siblings(".checkboxes").css("display") == "none") {
  5. $(".checkboxes").css("display", "none");
  6. $(this).siblings(".checkboxes").css("display", "block");
  7. } else {
  8. $(".checkboxes").css("display", "none");
  9. }
  10. }
  11. });
  12. $("body").on("click", function (event) {
  13. if (!$(event.target).hasClass("overSelect")) {
  14. if (($(event.target).closest(".checkboxes").length == 0)) {
  15. $(".checkboxes").css("display", "none");
  16. }
  17. }
  18. });
  19. $(".multiselect #category-any").on("change", function() {
  20. $("#notes").slideUp(200);
  21. if(this.checked) {
  22. search_data["category"] = get_value_list_by_key(db_table["category"], null, -1, "category_id");
  23. search_data["project"] = get_value_list_by_key(db_table["project"], null, -1, "project_id");
  24. search_data["type"] = get_value_list_by_key(db_table["type"], null, -1, "type_id");
  25. $(".category-option").prop("checked", true);
  26. show_select_option("project", search_data["project"], "p", true);
  27. $("#project-any").prop("checked", true);
  28. show_select_option("type", search_data["type"], "t", true);
  29. $("#type-any").prop("checked", true);
  30. } else {
  31. search_data["category"] = [];
  32. search_data["project"] = [];
  33. search_data["type"] = [];
  34. $(".category-option").prop("checked", false);
  35. show_select_option("project", search_data["project"], "p", true);
  36. show_select_option("type", search_data["type"], "t", true);
  37. }
  38. search_data["notes"] = [];
  39. search_data["project"].forEach(element => {
  40. search_data["notes"] = search_data["notes"].concat(db_table["notes"][element]);
  41. });
  42. notes_show_id_list = [];
  43. search_data["notes"].forEach(element => {
  44. for(i=0; i<notes_button_list.length; i++) {
  45. if(notes_button_list[i] == element) {
  46. notes_show_id_list.push(i);
  47. }
  48. }
  49. });
  50. show_select_option("notes", notes_show_id_list, "n", true);
  51. });
  52. $(".multiselect .category-option").on("change", function() {
  53. $("#notes").slideUp(200);
  54. category_id_list = [];
  55. list_id_list = [];
  56. //set search data
  57. if (this.value == "any") {
  58. search_data["category"] = get_value_list_by_key(db_table["category"], null, -1, "category_id");
  59. search_data["project"] = get_value_list_by_key(db_table["project"], null, -1, "project_id");
  60. search_data["type"] = get_value_list_by_key(db_table["type"], null, -1, "type_id");
  61. } else {
  62. check_list = get_multi_checkbox_check_list("category-option");
  63. check_list.forEach(element => {
  64. if(element != "any") {
  65. category_id_list.push(element.split("-")[0].replace("c", ""));
  66. list_id_list.push(element.split("-")[1].replace("l", ""));
  67. }
  68. });
  69. search_data["category"] = category_id_list;
  70. search_data["project"] = get_value_list_by_key(db_table["project"], "category_id", category_id_list, "project_id");
  71. target_type_id_list = get_value_list_by_key(db_table["category_type"], "list_id", list_id_list, "category_list");
  72. search_data["type"] = merge_string_to_list(target_type_id_list);
  73. }
  74. //set view
  75. show_select_option("project", search_data["project"], "p", true);
  76. $("#project-any").prop("checked", true);
  77. show_select_option("type", search_data["type"], "t", true);
  78. $("#type-any").prop("checked", true);
  79. if(category_id_list.length < db_table["category"].length) {
  80. $("#category-any").prop("checked", false);
  81. } else {
  82. $("#category-any").prop("checked", true);
  83. }
  84. search_data["notes"] = [];
  85. search_data["project"].forEach(element => {
  86. search_data["notes"] = search_data["notes"].concat(db_table["notes"][element]);
  87. });
  88. notes_show_id_list = [];
  89. search_data["notes"].forEach(element => {
  90. for(i=0; i<notes_button_list.length; i++) {
  91. if(notes_button_list[i] == element) {
  92. notes_show_id_list.push(i);
  93. }
  94. }
  95. });
  96. show_select_option("notes", notes_show_id_list, "n", true);
  97. });
  98. $(".multiselect #project-any").on("change", function() {
  99. $("#notes").slideUp(200);
  100. if(this.checked) {
  101. search_data["project"] = [];
  102. $(".project-button:visible").prop("checked", true);
  103. $(".project-button:visible").each(function(){
  104. search_data["project"].push($(this).find("input").attr("id").replace("p", ""));
  105. $(this).find("input").prop("checked", true);
  106. });
  107. } else {
  108. search_data["project"] = [];
  109. $(".project-option").prop("checked", false);
  110. }
  111. set_select_text("project", false);
  112. });
  113. $(".multiselect .project-option").on("change", function() {
  114. $("#notes").slideUp(200);
  115. search_data["project"] = get_multi_checkbox_check_list("project-option", "p");
  116. set_select_text("project", false);
  117. search_data["notes"] = [];
  118. search_data["project"].forEach(element => {
  119. search_data["notes"] = search_data["notes"].concat(db_table["notes"][element]);
  120. });
  121. notes_show_id_list = [];
  122. search_data["notes"].forEach(element => {
  123. for(i=0; i<notes_button_list.length; i++) {
  124. if(notes_button_list[i] == element) {
  125. notes_show_id_list.push(i);
  126. }
  127. }
  128. });
  129. show_select_option("notes", notes_show_id_list, "n", true);
  130. if($(".project-option:checked").length < $(".project-button:visible").length) {
  131. $("#project-any").prop("checked", false);
  132. } else {
  133. $("#project-any").prop("checked", true);
  134. }
  135. reset_checkbox("type", "t", true);
  136. });
  137. $(".multiselect #type-any").on("change", function() {
  138. $("#notes").slideUp(200);
  139. if(this.checked) {
  140. search_data["type"] = [];
  141. $(".type-button:visible").prop("checked", true);
  142. $(".type-button:visible").each(function(){
  143. search_data["type"].push($(this).find("input").attr("id").replace("t", ""));
  144. $(this).find("input").prop("checked", true);
  145. });
  146. } else {
  147. search_data["type"] = [];
  148. $(".type-option").prop("checked", false);
  149. }
  150. set_select_text("type", false);
  151. reset_checkbox("notes", "n", true);
  152. });
  153. $(".multiselect .type-option").on("change", function() {
  154. search_data["type"] = get_multi_checkbox_check_list("type-option", "t");
  155. set_select_text("type", false);
  156. if($(".type-option:checked").length < $(".type-button:visible").length) {
  157. $("#type-any").prop("checked", false);
  158. } else {
  159. $("#type-any").prop("checked", true);
  160. }
  161. if($("#t3").prop("checked")) {
  162. $("#notes").slideDown(200);
  163. reset_checkbox("notes", "n", true);
  164. } else {
  165. $("#notes").slideUp(200);
  166. }
  167. });
  168. $(".multiselect #notes-any").on("change", function() {
  169. if(this.checked) {
  170. search_data["notes"] = [];
  171. $(".notes-button:visible").prop("checked", true);
  172. $(".notes-button:visible").each(function(){
  173. search_data["notes"].push(notes_button_list[parseInt($(this).find("input").attr("id").replace("n", ""))]);
  174. $(this).find("input").prop("checked", true);
  175. });
  176. } else {
  177. search_data["notes"] = [];
  178. $(".notes-option").prop("checked", false);
  179. }
  180. set_select_text("notes", false);
  181. });
  182. $(".multiselect .notes-option").on("change", function() {
  183. search_data["notes"] = get_multi_checkbox_check_list("notes-option", "n");
  184. set_select_text("notes", false);
  185. if($(".notes-option:checked").length < $(".notes-button:visible").length) {
  186. $("#notes-any").prop("checked", false);
  187. } else {
  188. $("#notes-any").prop("checked", true);
  189. }
  190. });
  191. });
  192. function merge_string_to_list(string_list) {
  193. output = [];
  194. string_list.forEach(element => {
  195. output = output.concat(element.split(","));
  196. });
  197. return output;
  198. }
  199. function string_concat_list(name, target_list) {
  200. output_list = [];
  201. target_list.forEach(element => {
  202. output_list.push(name + element);
  203. });
  204. return output_list;
  205. }
  206. function get_multi_checkbox_check_list(checkbox_class, name_replace) {
  207. output = [];
  208. var $select_option = $("."+checkbox_class+":checked");
  209. $select_option.each(function(){
  210. if(checkbox_class == "notes-option") {
  211. output.push($(this).attr('value'));
  212. } else {
  213. output.push($(this).attr('id').replace(name_replace, ""));
  214. }
  215. });
  216. return output;
  217. }
  218. function set_select_text(address, lock) {
  219. //unlock select bar
  220. $("#"+address+" select").prop("disabled", false);
  221. //get check number
  222. check_length = $("."+address+"-option:checked").length;
  223. //get option name that show on select bar
  224. show_list = [];
  225. $("."+address+"-option:checked").each(function() {
  226. show_list = show_list.concat($(this).attr("value"));
  227. });
  228. //set select bar text
  229. if(check_length == 0){
  230. if(lock) {
  231. $("#"+address+" select option").text("無符合條件選項");
  232. $("#"+address+" select").prop("disabled", true);
  233. } else {
  234. $("#"+address+" select option").text("未選擇任何專案");
  235. }
  236. } else {
  237. if(check_length > 2) {
  238. $("#"+address+" select option").text("已選擇 "+check_length+" 項");
  239. } else {
  240. $("#"+address+" select option").text(show_list);
  241. }
  242. }
  243. }
  244. function show_select_option(address, show_list, id_name, lock) {
  245. //init option
  246. $("#" + address + " .checkboxes input").prop("checked", false);
  247. $("#" + address + " .checkboxes label").css("display", "none");
  248. //show option
  249. show_list.forEach(element => {
  250. $("#"+id_name+element).prop("checked", true);
  251. $("#"+id_name+element).parent().css("display", "block");
  252. });
  253. if(show_list.length) {
  254. $("#" + address + " #" + address + "-any").prop("checked", true);
  255. $("#" + address + " #" + address + "-any").parent().css("display", "block");
  256. }
  257. set_select_text(address, lock);
  258. }
  259. function reset_checkbox(checkbox_class, id_name, lock) {
  260. search_data[checkbox_class] = [];
  261. $("#"+checkbox_class+" ." + checkbox_class + "-button").each(function() {
  262. if($(this).css("display") == "block") {
  263. $(this).find("input").prop("checked", true);
  264. if(checkbox_class == "notes") {
  265. search_data[checkbox_class] = search_data[checkbox_class].concat($(this).find("input").attr("value"));
  266. } else {
  267. search_data[checkbox_class] = search_data[checkbox_class].concat($(this).find("input").attr("id").replace(id_name, ""));
  268. }
  269. }
  270. });
  271. $("#"+checkbox_class+" #" + checkbox_class + "-any").prop("checked", true);
  272. set_select_text(checkbox_class, lock)
  273. }