aboutsummaryrefslogtreecommitdiff
blob: 17cb798db52caf0de0f5feb66c733e202544f7a3 (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
function updateDropdown(self) {
    getThirdParent(self).querySelector('button > span:first-child').innerHTML = self.innerHTML;
}

function buildAdvancedQuery(){
    var query = ""
    document.querySelectorAll('#search-container > .row').forEach(function(element) {
        var term = element.querySelector('.form-control').value;

        if(!term.replace(/\s/g, '').length){
            return;
        }else{
            term = parseSearchTerm(term);
        }

        var operator = parseOperator(element.querySelector('.pgo-query-operator > span:first-child').innerHTML);
        var field = element.querySelector('.pgo-query-field > span:first-child').innerHTML;

        query += operator + field + ":" + term + " ";
    });
    document.getElementById('q').value = query;
}

function parseOperator(operator){
    switch(operator) {
        case "should match":
            return "";
        case "must match":
            return "+";
        case "must not match":
            return "-";
        default:
            return "";
    }
}

function parseSearchTerm(term){
    if (/\s/.test(term) && !/^\".*\"$/.test(term)) {
        return "\"" + term + "\""
    }else{
        return term
    }
}

function addInput(self){
    var new_input = document.querySelector('#search-container > .row').cloneNode(true);
    resetInput(new_input);
    document.querySelector('#search-container').append(new_input);
    checkDeleteButtons();
    checkAddButtons();
}

function resetInput(input) {
    input.querySelector('.form-control').value = '';
    input.querySelector('.pgo-query-operator > span:first-child').innerHTML = 'should match';
    input.querySelector('.pgo-query-field > span:first-child').innerHTML = 'name';
}

function deleteInput(self){
    getThirdParent(self).removeChild(getSecondParent(self));
    checkDeleteButtons();
    checkAddButtons();
}

function checkDeleteButtons(){
    if(document.querySelectorAll('#search-container > .row').length == 1){
        document.querySelectorAll('.pgo-query-delete-btn').forEach(function(element) {
            element.style.display = 'none';
        });
    }else{
        document.querySelectorAll('.pgo-query-delete-btn').forEach(function(element) {
            element.style.display = 'block';
        });
    }
}

function checkAddButtons(){
    document.querySelectorAll('.pgo-query-add-btn').forEach(function(element) {
        element.style.display = 'none';
    });

    document.querySelectorAll('.pgo-query-add-btn')[document.querySelectorAll('.pgo-query-add-btn').length - 1].style.display = 'block';
}

function getThirdParent(self) {
    return self.parentElement.parentElement.parentElement;
}

function getSecondParent(self) {
    return self.parentElement.parentElement;
}

checkDeleteButtons();