aboutsummaryrefslogtreecommitdiff
path: root/gn2/wqflask/static/new/javascript/auth/search_mrna.js
blob: 76b2dc6bbbddae1b3b5fc0b5cce9c0c789efd182 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
function toggle_link_button() {
    num_groups = $("#frm-link select option").length - 1;
    num_selected = JSON.parse(
	$("#tbl-link").attr("data-datasets")).length;
    if(num_groups > 0 && num_selected > 0) {
	$("#frm-link input[type='submit']").prop("disabled", false);
    } else {
	$("#frm-link input[type='submit']").prop("disabled", true);
    }
}

function search_mrna() {
    form = document.getElementById("txt-query").form;
    query = document.getElementById("txt-query").value;
    selected = JSON.parse(document.getElementById(
	"tbl-link").getAttribute("data-datasets"));
    species_name = document.getElementById("txt-species-name").value
    search_endpoint = "/auth/data/mrna/search"
    search_table = new TableDataSource(
	"#tbl-search", "data-datasets", search_checkbox);
    $.ajax(
	form.action,
	{
	    "method": "POST",
	    "contentType": "application/json; charset=utf-8",
	    "dataType": "json",
	    "data": JSON.stringify({
		"query": query,
		"selected": selected,
		"dataset_type": "mrna",
		"species_name": species_name}),
	    "error": function(jqXHR, textStatus, errorThrown) {
		error_data = jqXHR.responseJSON;
		console.debug("ERROR_DATA:", error_data);
		elt = document.getElementById("search-error").setAttribute(
		    "style", "display: block;");
		document.getElementById("search-error-text").innerHTML = (
		    error_data.error + " (" + error_data.status_code + "): " +
			error_data.error_description);
		document.getElementById("tbl-search").setAttribute(
		    "data-datasets", JSON.stringify([]));
		render_table(search_table);
	    },
	    "success": function(data, textStatus, jqXHR) {
		document.getElementById("search-error").setAttribute(
		    "style", "display: none;");
		document.getElementById("tbl-search").setAttribute(
		    "data-datasets", JSON.stringify(data));
		render_table(search_table);
	    }
	});
}

/**
 * Make function to check whether `dataset` is in array of `datasets`.
 * @param {mRNADataset} A mrna dataset.
 * @param {Array} An array of mrna datasets.
 */
function make_filter(dataset) {
    return (dst) => {
	return (dst.SpeciesId == dataset.SpeciesId &&
		dst.InbredSetId == dataset.InbredSetId &&
		dst.ProbeFreezeId == dataset.ProbeFreezeId &&
		dst.ProbeSetFreezeId == dataset.ProbeSetFreezeId);
    };
}

$(document).ready(function() {
    let search_table = new TableDataSource(
	"#tbl-search", "data-datasets", search_checkbox);
    let link_table = new TableDataSource(
	"#tbl-link", "data-datasets", link_checkbox);

    $("#frm-search").submit(function(event) {
	event.preventDefault();
	return false;
    });

    $("#txt-query").keyup(debounce(search_mrna));

    $("#tbl-search").on("change", ".checkbox-search", function(event) {
        if(this.checked) {
	    dataset = JSON.parse(this.value);
	    select_deselect(
		dataset, search_table, link_table, make_filter(dataset));
	    toggle_link_button();
        }
    });

    $("#tbl-link").on("change", ".checkbox-selected", function(event) {
	if(!this.checked) {
	    dataset = JSON.parse(this.value);
	    select_deselect(
		dataset, link_table, search_table, make_filter(dataset));
	    toggle_link_button();
	}
    });
});