summaryrefslogtreecommitdiffstats
path: root/install/static/search.js
blob: 95ac482de92ac2ded9ab214e66afc1e2ead643b8 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//useSampleData is defined in index.xhtml.  Work around for development
var sampleData;


//Columns is an array of items in the form
// {title, column,  render}
//title: the the value that goes at the head of the column
//filed: the column in the response used for populating the value
//render: the function used to generate  cell.innerHtml
//       it is in the form:
//       render(current, cell)
//        current is the row in response
//        cell is the td in the table


//These are helper functions, either assigned to the rneder method
//Or called from a thin wrapper render method
function  renderSimpleColumn(current,cell){
    cell.innerHTML = current[this.column];
}


function  renderUnknownColumn(current,cell){
    cell.innerHTML = "Unknown";
}


function renderPkeyColumn(form,current,cell){
    $("<a/>",{
    href:"#tab="+form.obj+"&facet=details&pkey="+current[form.pkeyCol],
    html:  "" + current[form.pkeyCol],
    }).appendTo(cell);
}


function renderDetailColumn(current,cell,pkey,obj){
    $("<a/>",{
    href:"#tab="+obj+"&facet=details&pkey="+pkey,
    html:  ""+ current[this.column],
    }).appendTo(cell);
}



function SearchForm(obj, method, cols, searchSampleData){

    this.buildColumnHeaders =  function (){
    var columnHeaders  = document.createElement("tr");
    for (var i =0 ; i != this.columns.length ;i++){
        var th = document.createElement("th");
        th.innerHTML = this.columns[i].title;
        columnHeaders.appendChild(th);
    }
    $('#searchResultsTable thead:last').append(columnHeaders);
    }


    this.renderResultRow = function(current){
    var row = document.createElement("tr");
    var cell;
    var link;
    for(var index = 0 ; index < this.columns.length; index++){
        this.columns[index].render(current, row.insertCell(-1));
    }
    return row;
    }

    this.searchSuccess = function (json){
    if (json.result.truncated){
        $("#searchResultsTable tfoot").html("More than "+sizelimit+" results returned.  First "+ sizelimit+" results shown." );
    }else{
        $("#searchResultsTable tfoot").html(json.result.summary);
    }
    $("#searchResultsTable tbody").find("tr").remove();
    for (var index = 0; index !=  json.result.result.length; index++){
    var current = json.result.result[index];
        $('#searchResultsTable tbody:last').append(this.renderResultRow(current));
    }
    }

    this.searchWithFilter = function(queryFilter){
    var form = this;

    $('#searchResultsTable tbody').html("");
    $('#searchResultsTable tbody').html("");
    $('#searchResultsTable tfoot').html("");

    ipa_cmd(this.method,
        [queryFilter],
        {"all":"true"},
        function(json){
            form.searchSuccess(json);
        },
        function(json){
            alert("Search Failed");
        },form.obj, form.searchSampleData);

    }

    this.setup = function(){
    showSearch();

    $('#searchResultsTable thead').html("");
    $('#searchResultsTable tbody').html("");
    $('#searchResultsTable tfoot').html("");

    $("#new").click(function(){
        location.hash="tab="+obj+"&facet=add";
    });

    $("#query").click(executeSearch);

    this.buildColumnHeaders();

    var params = ipa_parse_qs();

    qs = location.hash.substring(1);
    //TODO fix this hack.  since parse returns an object, I don't know how to see if that object has a"critia" property if criteria is null.
    if (qs.indexOf("criteria") > 0)
    {
        this.searchWithFilter(params["criteria"]);
    }
    }

    this.obj = obj;
    this.method = method;
    this.columns = cols;
    this.searchSampleData = searchSampleData;

    this.setup();
}
executeSearch = function(){
    var queryFilter = $("#queryFilter").val();
    var qp = ipa_parse_qs();
    var tab = qp.tab;
    if (!tab){
        tab = 'user';
    }
    window.location.hash="#tab="
      +tab
    +"&facet=search&criteria="
    +queryFilter;
}