oransheep 3 лет назад
Родитель
Сommit
80d9376a33
3 измененных файлов с 241 добавлено и 382 удалено
  1. 7 167
      index.php
  2. 219 0
      script/js/search.js
  3. 15 215
      script/js/vue-search-block.js

+ 7 - 167
index.php

@@ -62,6 +62,7 @@ if (!isset($_SESSION['loggedin'])) {
 
 	<!-- vue3 -->
 	<script src="https://unpkg.com/vue@next"></script>
+	<script src='./script/js/search.js'></script>
 	<script src="./script/js/vue-search-block.js"></script>
 
 	<style>
@@ -251,30 +252,26 @@ if (!isset($_SESSION['loggedin'])) {
 		}
 
 		#checkboxes {
-			padding: 10;
 			display: none;
 			border: 1px #dadada solid;
 		}
 
 		#checkboxes label {
 			display: block;
-		}
-
-		#checkboxes label:hover {
-			background-color: #1e90ff;
-		}
-
-		.project-button {
 			padding-left: 10px;
 			padding-right: 10px;
 			padding-top: 5px;
 			padding-bottom: 5px;
 		}
 
+		#checkboxes label:hover {
+			background-color: #1e90ff;
+			color: #ffffff;
+		}
+
 		.multiselect {
 			display: inline-block;
 		}
-
 	</style>
 
 	<script>
@@ -794,166 +791,9 @@ if (!isset($_SESSION['loggedin'])) {
 		</div>
 	</section>
 	</div>
-
+	
 	<script>vm.mount('.body');</script>
 
-	<script>
-		var search_info = {
-			"search_category_list": [],
-			"search_project_list": [],
-			"search_type_list": [],
-			"search_notes_list": []
-		}
-		var search_project = [];
-		var search_type = [];
-		var search_notes = [];
-		var output;
-
-		update_serch_list("category_button", search_info["search_category_list"]);
-		update_serch_list("project_button", search_info["search_project_list"]);
-		update_serch_list("type_button", search_info["search_type_list"]);
-		update_serch_list("notes_button", search_info["search_notes_list"]);
-
-		function update_serch_list(button_group, list_to_update){
-			$("." + button_group + " .button").click(function() {
-				if($(this).text() == "any"){
-					list_to_update.splice(0,list_to_update.length)
-				} else {
-					data_filter = this.getAttribute("data-filter");
-
-					if(!$(this).closest('li').hasClass("active")){
-						list_to_update.push(data_filter);
-					} else {
-						list_to_update.splice(list_to_update.indexOf(data_filter), 1);
-					}
-				}
-				hide_button(search_info);
-			});
-		}
-
-		function hide_button(obj) {
-			if(search_info["search_category_list"].length <= 0){
-				$(".category_button").find("li").removeClass("active");
-				$(".category_button .any").addClass("active");
-
-				$(".project_button .button").show();
-			} else {
-				$(".category_button").find("li").removeClass("active");
-				$(".project_button .button").hide();
-				$(".project_button .any").find(".button").show();
-				search_info["search_category_list"].forEach(function(c) {
-					$("[data-filter=" + c + "]").closest("li").addClass("active");
-					$(".project_button ." + c).show();
-				});
-			}
-
-			if(search_info["search_project_list"].length <= 0){
-				$(".project_button").find("li").removeClass("active");
-				$(".project_button .any").addClass("active");
-				
-				$(".notes_button .button").show();
-			} else {
-				$(".project_button").find("li").removeClass("active");
-				$(".notes_button .button").hide();
-				$(".notes_button .any").find(".button").show();
-				search_info["search_project_list"].forEach(function(p) {
-					$("[data-filter=" + p + "]").closest("li").addClass("active");
-					if(notes_list[p] != undefined) {
-						notes_list[p].forEach(function(n) {
-							$(".notes_button ." + n).show();
-						});
-					}
-				});
-			}
-
-			if(search_info["search_type_list"].length <= 0){
-				$(".type_button").find("li").removeClass("active");
-				$(".type_button .any").addClass("active");
-				$(".notes_button").hide();
-				search_info["search_notes_list"].splice(0,search_info["search_notes_list"].length);
-			} else {
-				$(".type_button").find("li").removeClass("active");
-
-				if(search_info["search_type_list"].includes("設計圖")) {
-					$(".notes_button").show();
-				} else {
-					$(".notes_button").hide();
-				}
-				search_info["search_type_list"].forEach(function(t) {
-					$("[data-filter=" + t + "]").closest("li").addClass("active");
-				});
-			}
-
-			if(search_info["search_notes_list"].length <= 0){
-				$(".notes_button").find("li").removeClass("active");
-				$(".notes_button .any").addClass("active");
-			} else {
-				$(".notes_button").find("li").removeClass("active");
-				search_info["search_notes_list"].forEach(function(n) {
-					$("[data-filter=" + n + "]").closest("li").addClass("active");
-				});
-			}
-		}
-
-		$(".search-by-filter").click(function() {
-			// search_project = [];
-			// search_type = [];
-			// search_notes = [];
-
-			// if(search_info["search_project_list"].length <= 0) {
-			// 	if(search_info["search_category_list"].length <= 0) {
-			// 		search_project = get_object_value_to_list(project_list, "project_id");
-			// 	} else {
-			// 		search_project = get_list_by_given_value(search_info["search_category_list"], project_list, "category_name", "project_id");
-			// 	}
-			// } else {
-			// 	search_project = get_list_by_given_value(search_info["search_category_list"], project_list, "category_name", "project_id").filter(function(v){ return search_info["search_project_list"].indexOf(v) > -1 });
-			// }
-
-			// if(search_info["search_type_list"].length <=0) {
-			// 	search_type = get_object_value_to_list(type_list, "type_name");
-			// 	search_type = get_list_by_given_value(search_type, type_list, "type_name", "type_id");
-			// } else {
-			// 	search_type = search_info["search_type_list"];
-			// }
-
-			// if(search_info["search_notes_list"].length <=0) {
-			// 	search_project.forEach(function(p) {
-			// 		if(notes_list[p] != undefined) {
-			// 			notes_list[p].forEach(function(n) {
-			// 				search_notes.push(n)
-			// 			});
-			// 		}
-			// 	});
-			// 	search_notes = [...new Set(search_notes)];
-			// } else {
-			// 	search_notes = search_info["search_notes_list"];
-			// }
-
-			
-			
-			// $.ajax({
-			// 	url: "./script/php/get_search_list.php",
-			// 	data: {
-			// 		project_id: search_project,
-			// 		type_id: search_type,
-			// 		notes_name: search_notes
-			// 	},
-			// 	type: "GET",
-			// 	async: false,
-			// 	contentType: "application/json",
-			// 	dataType: "json"
-			// }).done(function(data) {
-			// 	output = data;
-			// }).error(function(error) {
-			// 	console.log(error);
-			// });
-
-			get_multiselect_value("project-button");
-
-		});
-	</script>
-
 </body>
 
 </html>

+ 219 - 0
script/js/search.js

@@ -0,0 +1,219 @@
+
+
+$(function() {
+    $(".search-by-filter").on("click", function (e) {
+        console.log($("#category_button").val());
+    });
+
+    $(".selectBox").on("click", function (e) {
+        if(get_value_list_by_key(db_table["project"], "category_id", search_data["category"], "project_id").length) {
+            if (checkboxes.style.display == "none") {
+                checkboxes.style.display = "block";
+            } else {
+                checkboxes.style.display = "none";
+            }
+        }
+    });
+
+    $("body").on("click", function (event) {
+        test = $(event.target);
+        if (!$(event.target).hasClass("overSelect")) {
+            if (($(event.target).closest("#checkboxes").length == 1)) {
+                checkboxes.style.display = "block";
+            } else {
+                checkboxes.style.display = "none";
+            }
+        }
+    });
+
+    $("#category_button").on("change", function() {
+        var category_id = [this.value.split(',')[0]];
+        var list_id = [this.value.split(',')[1]];
+        $("#p-select").prop("disabled", false);
+        $('#type-button').prop("value", "any");
+        $("#notes").slideUp(200);
+        $('#notes-button').prop("value", "any");
+        //set search_data
+        if (this.value == "any") {
+            search_data["category"] = get_value_list_by_key(db_table["category"], null, [], "category_id");
+            search_data["project"] = get_value_list_by_key(db_table["project"], null, [], "project_id");
+            search_data["type"] = get_value_list_by_key(db_table["type"], null, [], "type_id");
+        } else {
+            search_data["category"] = category_id;
+            search_data["project"] = get_value_list_by_key(db_table["project"], "category_id", category_id, "project_id");
+            target_type_id_list = get_value_list_by_key(db_table["category_type"], "list_id", list_id, "category_list");
+            search_data["type"] = merge_string_to_list(target_type_id_list);
+        }
+
+        //show project option
+        project_id_list = string_concat_list("project", search_data["project"]);
+        show_multi_checkbox("project-button", project_id_list);
+        $("#project-any").prop("checked", true);
+        set_multi_checkbox_text("project-checkbox", "project")
+        if (!search_data["project"].length) {
+            $("#p-select").prop("disabled", true);
+            $("#p-select option").text("無符合條件選項");
+        }
+
+        //show type option
+        show_select_option("type-button", search_data["type"]);
+
+        //init notes
+        search_data["notes"] = notes_button_list;
+    });
+
+    $("#project-any").on("change", function() {
+        if(this.checked) {
+            search_data["project"] = get_value_list_by_key(db_table["project"], "category_id", search_data["category"], "project_id");
+            project_id_list = string_concat_list("project", search_data["project"]);
+            show_multi_checkbox("project-button", project_id_list);
+        } else {
+            search_data["project"] = [];
+            $(".project-checkbox").prop("checked", false);
+        }
+        set_multi_checkbox_text("project-checkbox", "project");
+    });
+
+    $(".project-checkbox").on("change", function() {
+        $('#type-button').prop("value", "any");
+        $("#notes").slideUp(200);
+        $('#notes-button').prop("value", "any");        
+
+        check_option = get_multi_checkbox_check_list("project-checkbox", "project");
+        if(check_option.length < get_value_list_by_key(db_table["project"], "category_id", search_data["category"], "project_id").length) {
+            $("#project-any").prop("checked", false);
+        } else {
+            $("#project-any").prop("checked", true);
+        }
+
+        search_data["project"] = check_option;
+        search_data["notes"] = notes_button_list;
+        set_multi_checkbox_text("project-checkbox", "project");
+    });
+
+    $("#type-button").on("change", function() {
+        var type_id = this.value;
+        var show_notes_list = [];
+
+        if (type_id == "3") {
+            project_id_list = get_multi_checkbox_check_list("project-checkbox", "project");
+            project_id_list.forEach(element => {
+                show_notes_list = show_notes_list.concat(db_table["notes"][element]);
+            });
+            show_notes_list = [...new Set(show_notes_list)];
+            show_select_option("notes-button", show_notes_list);
+            $("#notes").slideDown(200);
+        } else {
+            $("#notes").slideUp(200);
+            $('#notes-button').prop("value", "any");
+        }
+
+        if(type_id == "any") {
+            search_data["type"] = db_table["type"];
+        } else {
+            search_data["type"] = [type_id];
+        }
+        search_data["notes"] = notes_button_list;
+    });
+
+    $("#notes-button").on("change", function() {
+        var notes_name = this.value;
+        if(notes_name == "any") {
+            search_data["notes"] = notes_button_list;
+        } else {
+            search_data["notes"] = [notes_name];
+        }
+        });
+
+});
+
+//sql
+function get_value_list_by_key(obj_list, key_search, target_value, key_save) {
+    output_list = [];
+    obj_list.forEach(element => {
+        if (target_value.length) {
+            target_value.forEach(e => {
+                if(e.toString() == element[key_search].toString()) {
+                    output_list.push(element[key_save]);
+                }
+            });
+        } else {
+            output_list.push(element[key_save]);
+        }
+    });
+    output_list = [...new Set(output_list)];
+    return output_list;
+}
+
+function merge_object_by_key(obj, key, value) {
+    output_object = {};
+    while (obj.length > 0) {
+        pop_out = obj.pop();
+        if (output_object[pop_out[key]] == undefined) {
+            output_object[pop_out[key]] = [pop_out[value]];
+        } else {
+            output_object[pop_out[key]].push(pop_out[value]);
+        }
+    }
+    return output_object
+}
+
+function merge_string_to_list(string_list) {
+    output = [];
+    string_list.forEach(element => {
+        output = output.concat(element.split(","));
+    });
+    return output;
+}
+
+function string_concat_list(name, target_list) {
+    output_list = [];
+    target_list.forEach(element => {
+        output_list.push(name + element);
+    });
+    return output_list;
+}
+
+function show_multi_checkbox(class_name, show_list) {
+    $("."+class_name+" input").prop("checked", false);
+    $("."+class_name).css("display", "none");
+    
+    show_list.forEach(element => {
+        $("#"+element).prop("checked", true);
+        $("#"+element).parent().css("display", "block");
+    });
+}
+
+function show_select_option(select_id, show_list) {
+    $("#" + select_id + " option").hide();
+
+    $("#" + select_id + " option[value=any]").show();
+    show_list.forEach(element => {
+        $("#" + select_id + " option[value=" + element + "]").show();
+    });
+}
+
+//set multi_checkbox text
+function set_multi_checkbox_text(checkbox_class, name_replace) {
+    check_list = get_multi_checkbox_check_list(checkbox_class, name_replace);
+
+    if(check_list.length > 3){
+        $("#p-select option").text("已選擇 "+check_list.length+" 項");
+    } else {
+        if(check_list.length == 0) {
+            $("#p-select option").text("未選擇任何專案");
+        } else {
+            $("#p-select option").text(check_list);
+        }
+    }
+}
+
+//get check_list
+function get_multi_checkbox_check_list(checkbox_class, name_replace) {
+    output = [];
+    var $select_option = $("."+checkbox_class+":checked");
+    $select_option.each(function(){
+        output.push($(this).attr('id').replace(name_replace, ""));
+    });
+    return output;
+}

+ 15 - 215
script/js/vue-search-block.js

@@ -16,12 +16,11 @@ db_table["notes"] = merge_object_by_key(db_table["notes"], "project_id", "notes"
 var notes_button_list = [...new Set([].concat(...Object.values(db_table["notes"])))];
 
 var search_data = {
-    "category":  db_table["category"],
-    "project": db_table["project"],
-    "type": db_table["type"],
+    "category": get_value_list_by_key(db_table["category"], null, [], "category_id"),
+    "project": get_value_list_by_key(db_table["project"], null, [], "project_id"),
+    "type": get_value_list_by_key(db_table["type"], null, [], "type_id"),
     "notes": notes_button_list
 };
-var project_check = [];
 
 const vm = Vue.createApp({});
 
@@ -40,8 +39,8 @@ vm.component('search-block', {
                     <li style="padding: 10px 15px;">
                         <label>工程類別:</label>
                     </li>
-                    <select id="category_button" onchange="category_onchange(this)">
-                        <option>any</option>
+                    <select id="category_button">
+                        <option value="any">any</option>
                         <category-button v-for="item in category_list" v-bind:object="item"></category-button>
                     </select>
                 </ul>
@@ -51,24 +50,25 @@ vm.component('search-block', {
                         <label >專案類別:</label>
                     </li>
                     <div class="multiselect">
-                        <div class="selectBox" onclick="showCheckboxes()">
+                        <div class="selectBox">
                             <select id="p-select">
                                 <option>選擇專案</option>
                             </select>
                             <div class="overSelect"></div>
                         </div>
-                        <div id="checkboxes" class="form-inline">                                
+                        <div id="checkboxes" class="form-inline" style="display: none;">
+                            <label for="project-any"><input type="checkbox" id="project-any" value="any" checked/> 全選</label>
                             <project-button v-for="item in project_list" :object="item"></project-button>
                         </div>
                     </div>
                 </ul>
                 
-                <div id="type" hidden>
+                <div id="type">
                     <ul class="button-group nav nav-pills nav-pills-primary">
                         <li style="padding: 10px 15px;">
                             <label>文件類別:</label>
                         </li>
-                        <select id="type_button" onchange="type_onchange(this)">
+                        <select id="type-button">
                             <option value="any">any</option>
                             <type-button v-for="item in type_list" v-bind:object="item"></type-button>
                         </select>
@@ -80,7 +80,7 @@ vm.component('search-block', {
                         <li style="padding: 10px 15px;">
                             <label>設計圖類別:</label>
                         </li>
-                        <select id="notes-button" onchange="notes_onchange(this)">
+                        <select id="notes-button">
                             <option value="any">any</option>
                             <notes-button v-for="item in notes_button_list" v-bind:object="item"></notes-button>
                         </select>
@@ -112,45 +112,18 @@ vm.component('category-button', {
 });
 
 vm.component('project-button', {
-    data() {
-        return {
-            project_check: project_check
-        }
-    },
-    template: `<label class="project-button" v-bind:for="concat">
-        <input type="checkbox" class="checkbox" :value=object.project_id 
-        :id="concat" @change="changeText"/>{{object.project_name}}
-        </label>`,
+    template: `<label class="project-button" v-bind:for="id_concat"><input type="checkbox" class="project-checkbox" :value=object.project_id :id="id_concat" checked/>{{" [" + this.object.project_id + "] " + this.object.project_name}}</label>`,
     props: {
         object: {
             project_id: String,
             project_name: String,
             category_id: String
         },
-    },    
-    methods: {
-        changeText(event) {
-            if(event.target.checked) {
-                project_check.push(this.object.project_id);
-                $("#p-select option").text(project_check);
-            } else {
-                project_check.splice(project_check.indexOf(this.object.project_id), 1);
-                $("#p-select option").text(project_check);
-            }
-
-            if(!project_check.length){
-                $("#p-select option").text("未選擇任何專案");
-            }
-
-            $("#notes").slideUp(200);
-            document.getElementById('notes-button').value="any";
-            document.getElementById('type_button').value="any";
-        }
     },
     computed: {
-        concat(){
-            return 'project'+ this.object.project_id;
-        }        
+        id_concat(){
+            return "project" + this.object.project_id;
+        }  
     }
 
 });
@@ -166,7 +139,6 @@ vm.component('type-button', {
     }
 });
 
-
 vm.component('notes-button', {
     template: `<option v-bind:value=object>{{object}}</option>`,
     props: {
@@ -174,176 +146,4 @@ vm.component('notes-button', {
     }
 });
 
-function category_onchange(obj) {
-    var category_id = obj.value.split(',')[0];
-    var list_id = obj.value.split(',')[1];
-
-    if(category_id == "any") {
-        search_data["project"] = db_table["project"];
-        search_data["type"] = db_table["type"];
-
-        $("#type").slideUp(200);
-    } else {
-        search_data["project"] = get_list_by_key(db_table["project"], "category_id", [category_id]);
-        if(search_data["project"].length) {
-            $("#p-select option").text("選擇專案");
-            project_id_list = string_concat_list("project", get_object_value_to_list(search_data["project"], "project_id"));
-            show_multi_checkbox("project-button", project_id_list);
-            $("#type").slideDown(200);
-        } else {
-            $("#p-select option").text("無符合條件選項");
-            $("#p-select option").disabled = false;
-        }
-
-        target_type_id_list = get_list_by_key(db_table["category_type"], "list_id", [list_id]);
-        target_type_id = merge_obj_list_value(target_type_id_list, "category_list").split(",");
-        search_data["type"] = get_list_by_key(db_table["type"], "type_id", target_type_id)
-    }
-    $("#notes").slideUp(200);
-    document.getElementById('notes-button').value="any";
-
-    type_id_list = get_object_value_to_list(search_data["type"], "type_id");
-    show_select_option("type_button", type_id_list);
-    document.getElementById('type_button').value="any";
-
-    
-}
-
-function type_onchange(obj) {
-    var type_id = obj.value;
-    var show_notes_list = [];
-    if(type_id == "3") {
-        project_id_list = get_multiselect_value("checkbox");
-        project_id_list.forEach(element => {
-            show_notes_list = show_notes_list.concat(db_table["notes"][element.split("project")[1]]);
-        });
-        show_notes_list = [...new Set(show_notes_list)];
-        show_select_option("notes-button", show_notes_list);
-        $("#notes").slideDown(200);
-    } else {
-        $("#notes").slideUp(200);
-        document.getElementById('notes-button').value="any";
-    }
-}
-
-function notes_onchange(obj) {
-    var notes_name = obj.value;
-
-}
-
-function get_value_list_by_key(obj_list, key, target_value) {
-    output_list = [];
-    obj_list.forEach(element => {
-        if(target_value.length) {
-            if(target_value.includes(element[key].toString())) {
-                output_list.push(element);
-            }
-        } else {
-            output_list.push(element);
-        }
-    });
-    output_list = [...new Set(output_list)];
-    return output_list;
-}
-
-function get_list_by_key(obj, key_name, value_list) {
-    output_list = [];
-    obj.forEach(element => {
-        if(value_list.includes(element[key_name].toString())) {
-            output_list.push(element)
-        }
-    });
-    output_list = [...new Set(output_list)];
-    return output_list;
-}
-
-function get_object_value_to_list(obj, key) {
-    value_list = [];
-    for(i=0; i<obj.length; i++){
-        value_list.push(obj[i][key]);
-    }
-    return value_list
-}
-
-function merge_object_by_key(obj, key, value) {
-    output_object = {};
-    while(obj.length > 0){
-        pop_out = obj.pop();
-        if(output_object[pop_out[key]] == undefined){
-            output_object[pop_out[key]] = [pop_out[value]];
-        } else {
-            output_object[pop_out[key]].push(pop_out[value]);
-        }
-    }
-    return output_object
-}
-
-function merge_obj_list_value(obj_list, key) {
-    output = "";
-    obj_list.forEach(element => {
-        output = output.concat(element[key]);
-    });
-    return output;
-}
-
-function string_concat_list(name, target_list) {
-    output_list = [];
-    target_list.forEach(element => {
-        output_list.push(name + element);
-    });
-    return output_list;
-}
-
-var expanded = false;
-function showCheckboxes() {
-  var checkboxes = document.getElementById("checkboxes");
-  if (!expanded) {
-    checkboxes.style.display = "block";
-    expanded = true;
-  } else {
-    checkboxes.style.display = "none";
-    expanded = false;
-  }
-}
 
-function show_multi_checkbox(class_name, show_list) {
-    //initial checkbox to uncheck
-    var init_checkboxes = document.getElementsByClassName("checkbox");
-    for (var x = 0; x < init_checkboxes.length; x++) {
-            
-        init_checkboxes[x].checked = false;
-    }
-
-    //hide all option
-    var checkboxes = document.getElementsByClassName(class_name);
-    if (checkboxes) {
-        for (var x = 0; x < checkboxes.length; x++) {
-            checkboxes[x].style.display = 'none';
-        }
-    }
-
-    //show target option
-    show_list.forEach(element => {
-        document.getElementById(element).parentElement.style.display = 'block';
-    });
-}
-
-function show_select_option(select_id, show_list) {
-    $("#"+select_id+" option").hide();
-
-    $("#"+select_id+" option[value=any]").show();
-    show_list.forEach(element => {
-        $("#"+select_id+" option[value=" + element + "]").show();
-    });
-}
-
-function get_multiselect_value(checkbox_class){
-    output = [];
-    var checkboxes = document.getElementsByClassName(checkbox_class);
-    for (var x = 0; x < checkboxes.length; x++) {
-        if(checkboxes[x].checked) {
-            output.push(checkboxes[x].id);
-        }
-    }
-    return output;
-}